黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.DevicePanel
{
    /// <summary>
    /// 按键面板的颜色选择界面
    /// </summary>
    public class PanelColorSelectForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 颜色选择结束的事件(R,G,B)
        /// </summary>
        public Action<int, int, int> FinishSelectColorEvent;
        /// <summary>
        /// 选择的RGB
        /// </summary>
        private List<int> listRGB = null;
        /// <summary>
        /// 当前选择的控件(外框)
        /// </summary>
        private FrameLayoutStatuControl nowSelectIcon1 = null;
        /// <summary>
        /// 圆盘选择的颜色显示控件(外框)
        /// </summary>
        private FrameLayoutStatuControl wheelSelectIcon1 = null;
        /// <summary>
        /// 圆盘选择的颜色显示控件(内框)
        /// </summary>
        private IconViewControl wheelSelectIcon2 = null;
        /// <summary>
        /// 是否是颜色选择
        /// </summary>
        private bool isColorSelect = false;
        /// <summary>
        /// 颜色值输入控件
        /// </summary>
        private TextInputControl txtColorInput = null;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_R">R</param>
        /// <param name="i_G">G</param>
        /// <param name="i_B">B</param>
        public void ShowForm(int i_R, int i_G, int i_B)
        {
            this.ScrollEnabled = false;
 
            //设置头部信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uSelectColor));
 
            //初始化中部信息
            this.InitMiddleFrame(i_R, i_G, i_B);
 
            //确定
            var btnfinish = new BottomClickButton();
            btnfinish.TextID = R.MyInternationalizationString.Confrim;
            bodyFrameLayout.AddChidren(btnfinish);
            btnfinish.MouseUpEventHandler += (sender, e) =>
            {
                if (listRGB == null || FinishSelectColorEvent == null)
                {
                    this.CloseForm();
                    return;
                }
                //回调函数
                this.FinishSelectColorEvent(listRGB[0], listRGB[1], listRGB[2]);
                this.CloseForm();
            };
 
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        /// <param name="i_R">R</param>
        /// <param name="i_G">G</param>
        /// <param name="i_B">B</param>
        private void InitMiddleFrame(int i_R, int i_G, int i_B)
        {
            bodyFrameLayout.BackgroundColor = UserCenterColor.Current.White;
            //清空bodyFrame
            this.ClearBodyFrame();
            //初始化颜色圆盘控件
            this.InitColorPictrueControl();
            //初始化备用颜色的控件
            this.InitSpareColorControl(i_R, i_G, i_B);
        }
 
        #endregion
 
        #region ■ 初始化颜色圆盘_____________________
 
        /// <summary>
        /// 初始化颜色圆盘控件
        /// </summary>
        private void InitColorPictrueControl()
        {
            //初始化颜色输入控件
            this.txtColorInput = this.InitColorInputControl();
 
            var framePic = new FrameLayout();
            framePic.Width = this.GetPictrueRealSize(588);
            framePic.Height = this.GetPictrueRealSize(588);
            framePic.Gravity = Gravity.CenterHorizontal;
            framePic.Y = Application.GetRealHeight(192);
            bodyFrameLayout.AddChidren(framePic);
 
            //圆盘控件
            var colorPic = new ColorPicker();
            colorPic.ColorImagePath = "Item/PickerColorWheel.png";
            framePic.AddChidren(colorPic);
 
            //白点控件
            var btnWhiteView = new IconViewControl(30);
            btnWhiteView.Visible = false;
            btnWhiteView.BackgroundColor = UserCenterColor.Current.White;
            btnWhiteView.Radius = (uint)this.GetPictrueRealSize(30) / 2;
            framePic.AddChidren(btnWhiteView);
 
            bool pointIsRight = false;
            //圆的半径
            int circleR = framePic.Width / 2 - this.GetPictrueRealSize(35);
            colorPic.ColorChaged += (sender, eColor) =>
            {
                if (pointIsRight == false)
                {
                    //点的区域不是圆盘内
                    return;
                }
                listRGB = new List<int>();
                listRGB.Add(Convert.ToInt32(Convert.ToString(eColor[0])));
                listRGB.Add(Convert.ToInt32(Convert.ToString(eColor[1])));
                listRGB.Add(Convert.ToInt32(Convert.ToString(eColor[2])));
 
                //显示选择颜色的控件
                this.ShowNowSelectColorControl(eColor);
 
                //获取颜色的翻译文本
                isColorSelect = true;
                this.txtColorInput.Text = this.GetColorTextTranslate(listRGB[0], listRGB[1], listRGB[2]);
            };
            colorPic.MouseDownEventHandler += (sender, e) =>
            {
                pointIsRight = this.CheckPoint(circleR, framePic.Width / 2, framePic.Height / 2, (int)e.X, (int)e.Y);
                if (pointIsRight == false)
                {
                    //点的区域不是圆盘内
                    return;
                }
                //显示白点
                btnWhiteView.X = (int)e.X - btnWhiteView.Width / 2;
                btnWhiteView.Y = (int)e.Y - btnWhiteView.Height / 2;
                if (btnWhiteView.Visible == false)
                {
                    btnWhiteView.Visible = true;
                }
            };
            colorPic.MouseMoveEventHandler += (sender, e) =>
            {
                //当鼠标点下事件处理
                colorPic.MouseDownEventHandler(sender, e);
            };
 
            //手动输入值改变事件
            this.txtColorInput.TextChangeEventHandler += (sender, value) =>
            {
                if (isColorSelect == true)
                {
                    //这里是由点击色盘进行赋值触发
                    isColorSelect = false;
                    return;
                }
                //检测输入的颜色值
                var byteColor = this.CheckInputColorValue(value);
                if (byteColor == null)
                {
                    return;
                }
                if (btnWhiteView.Visible == true)
                {
                    //白点隐藏
                    btnWhiteView.Visible = false;
                }
 
                //记录缓存
                listRGB = new List<int>();
                listRGB.Add(Convert.ToInt32(Convert.ToString(byteColor[0])));
                listRGB.Add(Convert.ToInt32(Convert.ToString(byteColor[1])));
                listRGB.Add(Convert.ToInt32(Convert.ToString(byteColor[2])));
 
                //显示选择颜色的控件
                this.ShowNowSelectColorControl(byteColor);
            };
        }
 
        /// <summary>
        /// 初始化颜色输入控件
        /// </summary>
        /// <returns></returns>
        private TextInputControl InitColorInputControl()
        {
            //自定义色值的外框
            var frameInput = new NormalFrameLayout();
            frameInput.Width = Application.GetRealWidth(458);
            frameInput.Height = Application.GetRealHeight(104);
            frameInput.Y = Application.GetRealHeight(1103);
            frameInput.Gravity = Gravity.CenterHorizontal;
            frameInput.BorderColor = 0xffcccccc;
            frameInput.Radius = (uint)Application.GetRealWidth(17);
            frameInput.BorderWidth = Application.GetRealWidth(3) == 0 ? 1 : (uint)Application.GetRealWidth(3);
            bodyFrameLayout.AddChidren(frameInput);
            var btnMark = new NormalViewControl(100, 58, true);
            btnMark.X = Application.GetRealWidth(35);
            btnMark.Gravity = Gravity.CenterVertical;
            btnMark.IsBold = true;
            btnMark.Text = "# ";
            btnMark.Width = btnMark.GetRealWidthByText();
            btnMark.TextColor = UserCenterColor.Current.TextGrayColor2;
            frameInput.AddChidren(btnMark);
            //自定义色值输入框
            var txtInput = new TextInputControl(400, 58, true);
            txtInput.X = btnMark.Right;
            txtInput.Gravity = Gravity.CenterVertical;
            txtInput.TextSize = 16;
            txtInput.FrameBorder = frameInput;
            frameInput.AddChidren(txtInput);
            //色值号为6位(由任意数字+字母组合)
            var btnTipView = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(55), false);
            btnTipView.Y = frameInput.Bottom + Application.GetRealHeight(20);
            btnTipView.IsBold = true;
            btnTipView.TextAlignment = TextAlignment.Center;
            btnTipView.TextColor = UserCenterColor.Current.TextGrayColor2;
            btnTipView.TextSize = 12;
            btnTipView.TextID = R.MyInternationalizationString.uColorLengthMsg;
            bodyFrameLayout.AddChidren(btnTipView);
 
            return txtInput;
        }
 
        /// <summary>
        /// 显示选择颜色的控件
        /// </summary>
        /// <param name="eColor"></param>
        private void ShowNowSelectColorControl(byte[] eColor)
        {
            //显示选择颜色的控件
            if (nowSelectIcon1 != null)
            {
                nowSelectIcon1.BorderColor = UserCenterColor.Current.White;
            }
            var uintColor = this.GetColorByRGB(eColor[0], eColor[1], eColor[2]);
            wheelSelectIcon2.BackgroundColor = uintColor;
            wheelSelectIcon1.BorderColor = 0xffcccccc;
            wheelSelectIcon1.Name = listRGB[0] + "-" + listRGB[1] + "-" + listRGB[2];
            nowSelectIcon1 = wheelSelectIcon1;
        }
 
        #endregion
 
        #region ■ 初始化预选颜色_____________________
 
        /// <summary>
        /// 初始化备用颜色的控件
        /// </summary>
        /// <param name="i_R">R</param>
        /// <param name="i_G">G</param>
        /// <param name="i_B">B</param>
        private void InitSpareColorControl(int i_R, int i_G, int i_B)
        {
            //控件数
            var listColor = this.InitSpareDefultColor();
            //X轴初始坐标
            int Xdefult = (bodyFrameLayout.Width - this.GetPictrueRealSize(80 + 24) * (listColor.Count - 1) - this.GetPictrueRealSize(80)) / 2;
            for (int i = 0; i < listColor.Count; i++)
            {
                var dataColor = listColor[i];
 
                var btnFrame = new FrameLayoutStatuControl();
                btnFrame.Height = this.GetPictrueRealSize(80);
                btnFrame.Width = this.GetPictrueRealSize(80);
                btnFrame.Radius = (uint)this.GetPictrueRealSize(80) / 2;
                btnFrame.X = Xdefult + i * this.GetPictrueRealSize(80 + 24);
                btnFrame.Y = Application.GetRealHeight(880);
                btnFrame.BackgroundColor = UserCenterColor.Current.White;
                btnFrame.BorderWidth = 1;
                btnFrame.BorderColor = UserCenterColor.Current.White;
                bodyFrameLayout.AddChidren(btnFrame);
 
                //分两步计算,不然有可能它得出的结果有误差导致不居中
                int iconWidth = btnFrame.Height - this.GetPictrueRealSize(10) - this.GetPictrueRealSize(10);
                var btnIcon = new IconViewControl(iconWidth, false);
                btnIcon.Gravity = Gravity.Center;
                btnIcon.Radius = (uint)btnIcon.IconSize / 2;
                btnFrame.AddChidren(btnIcon, ChidrenBindMode.BindEvent);
 
                if (dataColor != null)
                {
                    //预设置的颜色
                    btnFrame.Name = dataColor[0] + "-" + dataColor[1] + "-" + dataColor[2];
                    btnIcon.BackgroundColor = this.GetColorByRGB((byte)dataColor[0], (byte)dataColor[1], (byte)dataColor[2]);
                }
                else
                {
                    //原来的颜色
                    btnFrame.BorderColor = 0xffcccccc;
                    btnIcon.BackgroundColor = this.GetColorByRGB((byte)i_R, (byte)i_G, (byte)i_B);
                    wheelSelectIcon1 = btnFrame;
                    wheelSelectIcon2 = btnIcon;
                    nowSelectIcon1 = btnFrame;
 
                    //获取颜色的翻译文本
                    isColorSelect = true;
                    this.txtColorInput.Text = this.GetColorTextTranslate(i_R, i_G, i_B);
                }
 
                btnFrame.ButtonClickEvent += (sender, e) =>
                {
                    //显示选择颜色的控件
                    if (nowSelectIcon1 != null)
                    {
                        nowSelectIcon1.BorderColor = UserCenterColor.Current.White;
                    }
                    btnFrame.BorderColor = 0xffcccccc;
                    nowSelectIcon1 = btnFrame;
 
                    if (btnFrame.Name != null)
                    {
                        listRGB = new List<int>();
                        string[] Arry = btnFrame.Name.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
                        listRGB.Add(Convert.ToInt32(Convert.ToString(Arry[0])));
                        listRGB.Add(Convert.ToInt32(Convert.ToString(Arry[1])));
                        listRGB.Add(Convert.ToInt32(Convert.ToString(Arry[2])));
                    }
                    //获取颜色的翻译文本
                    isColorSelect = true;
                    this.txtColorInput.Text = this.GetColorTextTranslate(listRGB[0], listRGB[1], listRGB[2]);
                };
            }
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 检测点击点
        /// </summary>
        /// <param name="circleR">圆的半径</param>
        /// <param name="circleX">圆心X轴</param>
        /// <param name="circleY">圆心Y轴</param>
        /// <param name="pointX">点击点的X轴</param>
        /// <param name="pointY">点击点的Y轴</param>
        /// <returns></returns>
        private bool CheckPoint(int circleR, int circleX, int circleY, int pointX, int pointY)
        {
            int dwidth = circleX - pointX;
            if (dwidth < 0) { dwidth *= -1; }
 
            int dHeight = circleY - pointY;
            if (dHeight < 0) { dHeight *= -1; }
 
            //根据三角函数,求三角形的斜边长
            int dlength = dwidth * dwidth + dHeight * dHeight;
            //半径长度(不开方,所以是按平方算)
            circleR *= circleR;
            if (dlength < circleR)
            {
                //如果组成的三角形并没有长过半径,则代表还在圆内(不允许点边界)
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 初始化备用颜色的默认值
        /// </summary>
        /// <returns></returns>
        private List<List<int>> InitSpareDefultColor()
        {
            var list = new List<List<int>>();
 
            list.Add(new List<int>() { 253, 226, 39 });
            list.Add(new List<int>() { 121, 255, 197 });
            list.Add(null);
            list.Add(new List<int>() { 198, 85, 250 });
            list.Add(new List<int>() { 255, 117, 67 });
 
            return list;
        }
 
        /// <summary>
        /// 检测输入的颜色值
        /// </summary>
        /// <param name="colorText"></param>
        /// <returns></returns>
        private byte[] CheckInputColorValue(string colorText)
        {
            //只有长度为6才处理
            if (colorText.Length != 6) { return null; }
 
            colorText = colorText.ToUpper();
            foreach (var c in colorText)
            {
                if (char.IsNumber(c) == true)
                {
                    //数字的话,OK
                    continue;
                }
                if ('A' <= c && c <= 'F')
                {
                    //A-F的话,OK
                    continue;
                }
                return null;
            }
            int R = Convert.ToInt32(colorText.Substring(0, 2), 16);
            int G = Convert.ToInt32(colorText.Substring(2, 2), 16);
            int B = Convert.ToInt32(colorText.Substring(4, 2), 16);
            return new byte[] { (byte)R, (byte)G, (byte)B };
        }
 
        /// <summary>
        /// 获取颜色的翻译文本
        /// </summary>
        /// <param name="i_R"></param>
        /// <param name="i_G"></param>
        /// <param name="i_B"></param>
        /// <returns></returns>
        private string GetColorTextTranslate(int i_R, int i_G, int i_B)
        {
            return Convert.ToString(i_R, 16).ToUpper().PadLeft(2, '0')
                + Convert.ToString(i_G, 16).ToUpper().PadLeft(2, '0')
                + Convert.ToString(i_B, 16).ToUpper().PadLeft(2, '0');
        }
 
        /// <summary>
        /// 将RGB转换为颜色
        /// </summary>
        /// <param name="R"></param>
        /// <param name="G"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        private uint GetColorByRGB(byte R, byte G, byte B)
        {
            return (uint)(0xFF000000 + R * 256 * 256 + G * 256 + B);
        }
 
        #endregion
 
        #region ■ 界面关闭___________________________
 
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            this.FinishSelectColorEvent = null;
 
            base.CloseFormBefore();
        }
 
        #endregion
    }
}