Subversion Repositories AndroidProjects

Rev

Rev 829 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
823 chris 1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Data;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Windows.Forms;
10
 
11
namespace BauzoidNET.controls
12
{
13
    public partial class ColorPalette : UserControl
14
    {
824 chris 15
        public class SwatchEntry
16
        {
17
            private Color mColor = Color.FromArgb(255, 255, 255);
18
            private bool mIsSet = false;
19
 
20
            public SwatchEntry()
21
            {
22
            }
23
 
24
            public Color SwatchColor
25
            {
26
                get { return Color.FromArgb(mColor.ToArgb()); }
27
                set { mColor = Color.FromArgb(value.ToArgb()); mIsSet = true; }
28
            }
29
 
30
            public bool isSet()
31
            {
32
                return mIsSet;
33
            }
34
 
35
            public void unSet()
36
            {
37
                mIsSet = false;
38
            }
39
        }
40
 
41
 
823 chris 42
        private int mNumRows = 8;
43
        private int mNumCols = 4;
44
 
45
        private int mCellSize = 16;
46
 
824 chris 47
        private int mCurrentCell = -1;
48
 
49
 
50
        private SwatchEntry[] mColors = null;
51
 
52
        private Color mCurrentColor = Color.FromArgb(255, 255, 255);
53
 
823 chris 54
        public ColorPalette()
55
        {
56
            InitializeComponent();
824 chris 57
 
58
            updateSizes();
823 chris 59
        }
60
 
61
        public void updateSizes()
62
        {
824 chris 63
            mColors = new SwatchEntry[getNumColors()];
64
 
65
            for (int i = 0; i < getNumColors(); i++)
66
            {
67
                mColors[i] = new SwatchEntry();
68
            }
69
 
823 chris 70
            swatchesBox.Width = mCellSize * mNumCols;
71
            swatchesBox.Height = mCellSize * mNumRows;
72
            swatchesBox.Invalidate();
73
        }
74
 
824 chris 75
        public int getNumColors()
76
        {
77
            return mNumCols * mNumRows;
78
        }
79
 
80
        public int getColorIndex(int x, int y)
81
        {
82
            return (y * mNumCols + x);
83
        }
84
 
85
        public SwatchEntry getSwatchEntry(int index)
86
        {
87
            return mColors[index];
88
        }
89
 
90
        public Color getColor(int index)
91
        {
92
            return mColors[index].SwatchColor;
93
        }
94
 
95
        public void setColor(int index, Color color)
96
        {
97
            mColors[index].SwatchColor = color;
98
        }
99
 
100
        public void clearColor(int index)
101
        {
102
            mColors[index].unSet();
103
        }
104
 
105
        [Description("Current color"), Category("Behavior")]
106
        public Color CurrentColor
107
        {
108
            get { return mCurrentColor; }
109
            set { mCurrentColor = value; }
110
        }
111
 
823 chris 112
        [Description("Number of Rows"), Category("Appearance")]
113
        public int NumRows
114
        {
115
            get { return mNumRows; }
116
            set { mNumRows = value; updateSizes(); }
117
        }
118
 
119
        [Description("Number of Columns"), Category("Appearance")]
120
        public int NumCols
121
        {
122
            get { return mNumCols; }
123
            set { mNumCols = value; updateSizes(); }
124
        }
125
 
126
        [Description("Size of a color cell"), Category("Appearance")]
127
        public int CellSIze
128
        {
129
            get { return mCellSize; }
130
            set { mCellSize = value; updateSizes();  }
131
        }
132
 
133
        private void swatchesBox_Paint(object sender, PaintEventArgs e)
134
        {
135
            Graphics g = e.Graphics;
136
 
137
            for (int y = 0; y < mNumRows; y++)
138
            {
139
                for (int x = 0; x < mNumCols; x++)
140
                {
141
                    int size = mCellSize - 2;
142
                    int posX = mCellSize * x + 1;
143
                    int posY = mCellSize * y + 1;
144
 
145
                    //g.DrawRectangle(new Pen(Color.Black, 1), new Rectangle(posX+1, posY+1, mCellSize-2, mCellSize-2));
824 chris 146
                    int index = getColorIndex(x, y);
823 chris 147
 
824 chris 148
                    SwatchEntry entry = getSwatchEntry(index);
149
 
150
                    if (entry.isSet())
151
                    {
152
                        g.FillRectangle(new SolidBrush(entry.SwatchColor), new Rectangle(posX, posY, size, size));
153
                    }
154
                    else
155
                    {
156
                        g.FillRectangle(new SolidBrush(Color.White), new Rectangle(posX, posY, size, size));
157
                        g.FillRectangle(new SolidBrush(Color.LightGray), new Rectangle(posX + size / 2, posY, size / 2, size / 2));
158
                        g.FillRectangle(new SolidBrush(Color.LightGray), new Rectangle(posX, posY + size / 2, size / 2, size / 2));
159
                    }
160
 
161
                    if (index == mCurrentCell)
162
                    {
163
                        g.DrawRectangle(new Pen(SystemColors.Highlight, 1), new Rectangle(posX, posY, size, size));
164
                        //g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(posX, posY, size, size));
165
                    }
823 chris 166
                }
167
            }
168
        }
824 chris 169
 
170
        private void swatchesBox_MouseMove(object sender, MouseEventArgs e)
171
        {
172
            int x = e.X / mCellSize;
173
            int y = e.Y / mCellSize;
174
 
175
            mCurrentCell = getColorIndex(x, y);
176
 
177
            //System.Diagnostics.Debug.WriteLine("X: " + x + " Y: " + y + " I: " + mCurrentCell);
178
            swatchesBox.Invalidate();
179
        }
180
 
181
        private void swatchesBox_MouseDown(object sender, MouseEventArgs e)
182
        {
183
            int x = e.X / mCellSize;
184
            int y = e.Y / mCellSize;
185
 
186
            int index = getColorIndex(x, y);
187
            SwatchEntry entry = getSwatchEntry(index);
825 chris 188
 
189
            mCurrentCell = index;
824 chris 190
            if (entry.isSet())
191
            {
192
                CurrentColor = getColor(mCurrentCell);
193
                OnColorPicked(EventArgs.Empty);
194
            }
829 chris 195
 
196
            swatchesBox.Focus();
824 chris 197
        }
198
 
199
        private void swatchesContextMenu_Opening(object sender, CancelEventArgs e)
200
        {
201
            if (mCurrentCell == -1)
202
            {
203
                e.Cancel = true;
204
            }
205
            else
206
            {
207
                SwatchEntry entry = getSwatchEntry(mCurrentCell);
208
                contextMenuClear.Enabled = entry.isSet();
209
            }
210
        }
211
 
212
        private void contextMenuSet_Click(object sender, EventArgs e)
213
        {
214
            setColor(mCurrentCell, CurrentColor);
215
        }
216
 
217
        private void contextMenuClear_Click(object sender, EventArgs e)
218
        {
219
            clearColor(mCurrentCell);
220
        }
221
 
222
 
223
        [Category("Action")]
224
        [Description("Fires when a color is picked")]
225
        public event EventHandler ColorPicked;
226
 
227
        protected virtual void OnColorPicked(EventArgs e)
228
        {
229
            EventHandler handler = this.ColorPicked;
230
            if (handler != null)
231
            {
232
                handler(this, e);
233
            }
234
        }
235
 
829 chris 236
        private void swatchesBox_MouseHover(object sender, EventArgs e)
237
        {
238
            swatchesBox.Focus();
239
        }
240
 
831 chris 241
 
823 chris 242
    }
243
}