ColorChip.cs 6.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
//  
// Copyright (c) Jesse Freeman. All rights reserved.  
// 
// Licensed under the Microsoft Public License (MS-PL) License. 
// See LICENSE file in the project root for full license information. 
// 
// Contributors
// --------------------------------------------------------
// This is the official list of Pixel Vision 8 contributors:
//  
// Jesse Freeman - @JesseFreeman
// Christer Kaitila - @McFunkypants
// Pedro Medeiros - @saint11
// Shawn Rakowski - @shwany
// 

using System;
using PixelVisionSDK.Utils;

namespace PixelVisionSDK.Chips
{

    /// <summary>
    ///     The <see cref="ColorChip" /> represents the system colors of the engine.
    ///     It allows the engine to work in color indexes that the display can map
    ///     to actual colors in Unity via the class's set of APIs.
    /// </summary>
    public class ColorChip : AbstractChip, IColorChip
    {

        protected string[] _colors =
        {
            "#000000",
            "#FFFFFF",
            "#9D9D9D",
            "#BE2633",
            "#E06F8B",
            "#493C2B",
            "#A46422",
            "#EB8931",
            "#F7E26B",
            "#2F484E",
            "#44891A",
            "#A3CE27",
            "#1B2632",
            "#005784",
            "#31A2F2",
            "#B2DCEF"
        };

        protected int _colorsPerPage = 64;
        protected int _pages = 4;
        protected string _transparent = "#FF00FF";
        protected ColorData[] colorCache;
        private bool invalid;
        protected Vector pageSize = new Vector(8, 8);

        public string[] hexColors
        {
            get
            {
                var colors = new string[total];

                Array.Copy(_colors, colors, total);

                return colors;
            }
        }


        /// <summary>
        ///     Defines the total number of colors per virtual page.
        /// </summary>
        /// <value>Int</value>
        public int colorsPerPage
        {
            get { return _colorsPerPage; }
        }

        /// <summary>
        ///     Returns the total virtual pages of colors.
        /// </summary>
        /// <value>Int</value>
        public int pages
        {
            get { return _pages; }
            set
            {
                if (_pages == value)
                    return;

                _pages = value.Clamp(1, 4);

                var oldTotal = _colors.Length;

                Array.Resize(ref _colors, total);

                if (oldTotal < total)
                {
                    for (var i = oldTotal; i < total; i++)
                    {
                        _colors[i] = transparent;
                    }
                }
            }
        }

        /// <summary>
        ///     The default <see cref="transparent" /> color to be used in the
        ///     engine.
        /// </summary>
        /// <value>String</value>
        public string transparent
        {
            get { return _transparent; }

            set { _transparent = value; }
        }

        /// <summary>
        ///     Get and Set the <see cref="supportedColors" /> number of <see cref="colors" />
        ///     in the palette. Changing the <see cref="supportedColors" /> will clear the
        ///     palette when it resizes.
        /// </summary>
        /// <value>Int</value>
        public int supportedColors
        {
            get
            {
                var count = 0;
                var total = _colors.Length;
                for (var i = 0; i < total; i++)
                {
                    if (_colors[i] != transparent)
                        count ++;
                }

                return count;
            }
        }

        /// <summary>
        ///     The <see cref="supportedColors" /> number of <see cref="colors" /> the chip can
        ///     support. This lock makes the sure that the <see cref="colors" />
        ///     array will never be larger than this value.
        /// </summary>
        /// <value>Int</value>
        public int total
        {
            get { return pages * colorsPerPage; }
        }

        /// <summary>
        ///     Returns a list of color data to be used for rendering.
        /// </summary>
        /// <value>ColorData[]</value>
        public ColorData[] colors
        {
            get
            {
                if (invalid)
                {
                    var t = total;
                    colorCache = new ColorData[t];

                    for (var i = 0; i < t; i++)
                    {
                        colorCache[i] = new ColorData(_colors[i]);

                        //color.HexToColor(_colors[i]);
                        //colorCache[i] = color;
                    }

                    invalid = false;
                }

                return colorCache;
            }
        }

        public string ReadColorAt(int index)
        {
            return index < 0 || index > _colors.Length - 1 ? transparent : _colors[index];
        }

        public int FindColorID(string color)
        {
            return Array.IndexOf(_colors, color);
        }

        public void Clear()
        {
            var t = _colors.Length;
            for (var i = 0; i < t; i++)
            {
                UpdateColorAt(i, transparent);
            }
        }

        public void UpdateColorAt(int index, string color)
        {
            if (index > _colors.Length || index < 0)
                return;

            if (ColorData.ValidateColor(color))
            {
                _colors[index] = color;
            }

            invalid = true;
        }


        /// <summary>
        ///     This method configures the chip. It registers itself with the
        ///     engine as the default ColorChip, it sets the supported
        ///     <see cref="colors" /> to the maximum value of 256 and calls
        ///     <see cref="RevertColors" /> to add the default <see cref="colors" />
        ///     to the <see cref="colors" /> array.
        /// </summary>
        public override void Configure()
        {
            engine.colorChip = this;
            RebuildColorPages(16);
        }

        public override void Deactivate()
        {
            base.Deactivate();
            engine.colorChip = null;
        }

        /// <summary>
        ///     Recalculates the total number of pages based on the new total
        ///     number of colors.
        /// </summary>
        /// <param name="total"></param>
        public void RebuildColorPages(int total)
        {
            pages = MathUtil.CeilToInt(total / colorsPerPage);
        }

        

    }

247
}