Subversion Repositories AndroidProjects

Rev

Rev 824 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BauzoidNET.controls
{
    public partial class ColorPalette : UserControl
    {
        public class SwatchEntry
        {
            private Color mColor = Color.FromArgb(255, 255, 255);
            private bool mIsSet = false;

            public SwatchEntry()
            {
            }

            public Color SwatchColor
            {
                get { return Color.FromArgb(mColor.ToArgb()); }
                set { mColor = Color.FromArgb(value.ToArgb()); mIsSet = true; }
            }

            public bool isSet()
            {
                return mIsSet;
            }

            public void unSet()
            {
                mIsSet = false;
            }
        }


        private int mNumRows = 8;
        private int mNumCols = 4;

        private int mCellSize = 16;

        private int mCurrentCell = -1;
       

        private SwatchEntry[] mColors = null;

        private Color mCurrentColor = Color.FromArgb(255, 255, 255);

        public ColorPalette()
        {
            InitializeComponent();

            updateSizes();
        }

        public void updateSizes()
        {
            mColors = new SwatchEntry[getNumColors()];

            for (int i = 0; i < getNumColors(); i++)
            {
                mColors[i] = new SwatchEntry();
            }

            swatchesBox.Width = mCellSize * mNumCols;
            swatchesBox.Height = mCellSize * mNumRows;
            swatchesBox.Invalidate();
        }

        public int getNumColors()
        {
            return mNumCols * mNumRows;
        }

        public int getColorIndex(int x, int y)
        {
            return (y * mNumCols + x);
        }

        public SwatchEntry getSwatchEntry(int index)
        {
            return mColors[index];
        }

        public Color getColor(int index)
        {
            return mColors[index].SwatchColor;
        }

        public void setColor(int index, Color color)
        {
            mColors[index].SwatchColor = color;
        }

        public void clearColor(int index)
        {
            mColors[index].unSet();
        }

        [Description("Current color"), Category("Behavior")]
        public Color CurrentColor
        {
            get { return mCurrentColor; }
            set { mCurrentColor = value; }
        }

        [Description("Number of Rows"), Category("Appearance")]
        public int NumRows
        {
            get { return mNumRows; }
            set { mNumRows = value; updateSizes(); }
        }

        [Description("Number of Columns"), Category("Appearance")]
        public int NumCols
        {
            get { return mNumCols; }
            set { mNumCols = value; updateSizes(); }
        }

        [Description("Size of a color cell"), Category("Appearance")]
        public int CellSIze
        {
            get { return mCellSize; }
            set { mCellSize = value; updateSizes();  }
        }

        private void swatchesBox_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            for (int y = 0; y < mNumRows; y++)
            {
                for (int x = 0; x < mNumCols; x++)
                {
                    int size = mCellSize - 2;
                    int posX = mCellSize * x + 1;
                    int posY = mCellSize * y + 1;

                    //g.DrawRectangle(new Pen(Color.Black, 1), new Rectangle(posX+1, posY+1, mCellSize-2, mCellSize-2));
                    int index = getColorIndex(x, y);

                    SwatchEntry entry = getSwatchEntry(index);

                    if (entry.isSet())
                    {
                        g.FillRectangle(new SolidBrush(entry.SwatchColor), new Rectangle(posX, posY, size, size));
                    }
                    else
                    {
                        g.FillRectangle(new SolidBrush(Color.White), new Rectangle(posX, posY, size, size));
                        g.FillRectangle(new SolidBrush(Color.LightGray), new Rectangle(posX + size / 2, posY, size / 2, size / 2));
                        g.FillRectangle(new SolidBrush(Color.LightGray), new Rectangle(posX, posY + size / 2, size / 2, size / 2));
                    }

                    if (index == mCurrentCell)
                    {
                        g.DrawRectangle(new Pen(SystemColors.Highlight, 1), new Rectangle(posX, posY, size, size));
                        //g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(posX, posY, size, size));
                    }
                }
            }
        }

        private void swatchesBox_MouseMove(object sender, MouseEventArgs e)
        {
            int x = e.X / mCellSize;
            int y = e.Y / mCellSize;

            mCurrentCell = getColorIndex(x, y);

            //System.Diagnostics.Debug.WriteLine("X: " + x + " Y: " + y + " I: " + mCurrentCell);
            swatchesBox.Invalidate();
        }

        private void swatchesBox_MouseDown(object sender, MouseEventArgs e)
        {
            int x = e.X / mCellSize;
            int y = e.Y / mCellSize;

            int index = getColorIndex(x, y);
            SwatchEntry entry = getSwatchEntry(index);

            mCurrentCell = index;
            if (entry.isSet())
            {
                CurrentColor = getColor(mCurrentCell);
                OnColorPicked(EventArgs.Empty);
            }
        }

        private void swatchesContextMenu_Opening(object sender, CancelEventArgs e)
        {
            if (mCurrentCell == -1)
            {
                e.Cancel = true;
            }
            else
            {
                SwatchEntry entry = getSwatchEntry(mCurrentCell);
                contextMenuClear.Enabled = entry.isSet();
            }
        }

        private void contextMenuSet_Click(object sender, EventArgs e)
        {
            setColor(mCurrentCell, CurrentColor);
        }

        private void contextMenuClear_Click(object sender, EventArgs e)
        {
            clearColor(mCurrentCell);
        }


        [Category("Action")]
        [Description("Fires when a color is picked")]
        public event EventHandler ColorPicked;

        protected virtual void OnColorPicked(EventArgs e)
        {
            EventHandler handler = this.ColorPicked;
            if (handler != null)
            {
                handler(this, e);
            }
        }

    }
}