xm
2019-07-16 b910cb79c9b5bcc204022a3cf9e6950f0a64dfbd
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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.DevicePanel
{
    /// <summary>
    /// 按键面板的指示灯配置的菜单界面★
    /// </summary>
    public class PanelPilolightSettionForm : UserCenterCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 按键面板的回路设备
        /// </summary>
        private Panel panel = null;
        /// <summary>
        /// 指示灯状态区分 1:正常状态  2:点击后的状态
        /// </summary>
        private StatuMode PilolightStatu = StatuMode.Normal;
        /// <summary>
        /// 图标控件
        /// </summary>
        private Dictionary<string, IconViewControl> dicIconContr = new Dictionary<string, IconViewControl>();
        /// <summary>
        /// 选择的RGB
        /// </summary>
        private List<int> listRGB = null;
        /// <summary>
        /// 按键原来的颜色数据
        /// </summary>
        private Panel.KeyColorData keyColorData = null;
        /// <summary>
        /// 颜色选择的打勾控件
        /// </summary>
        private IconViewControl btnCheckContr = null;
        /// <summary>
        /// 备选颜色的FrameLayout
        /// </summary>
        private FrameLayout defultColorFrame = null;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_panel">按键面板的回路设备</param>
        /// <param name="i_statu">指示灯状态区分 1:正常状态  2:点击后的状态</param>
        public void ShowForm(Panel i_panel, StatuMode i_statu)
        {
            this.panel = i_panel;
            this.PilolightStatu = i_statu;
            this.dicIconContr.Clear();
 
            if (i_statu == StatuMode.Normal)
            {
                //设置头部信息:正常状态
                base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uNormalStatu));
            }
            else if (i_statu == StatuMode.ClickStatu)
            {
                //设置头部信息:点击后的状态
                base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAfterClickStatu));
            }
 
            //完成
            var btnfinish = new TopLayoutFinshView();
            topFrameLayout.AddChidren(btnfinish);
            btnfinish.MouseUpEventHandler += (sender, e) =>
            {
                if (listRGB == null)
                {
                    this.CloseForm();
                    return;
                }
                //将颜色设置到面板
                this.SetSelectColorToPanel();
            };
 
            //初始化中部信息
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //初始化颜色圆盘控件
            var frame = this.InitColorPictrueControl();
            //初始化备用颜色的控件
            this.InitSpareColorControl(frame);
            //从网关获取颜色信息然后设置到界面
            this.GetColorAndSetToForm();
        }
 
        #endregion
 
        #region ■ 初始化颜色圆盘_____________________
 
        /// <summary>
        /// 初始化颜色圆盘控件
        /// </summary>
        private FrameLayout InitColorPictrueControl()
        {
            //颜色调节
            var btnMsg = new ViewNormalControl(800, true);
            btnMsg.X = ControlCommonResourse.XXLeft;
            btnMsg.Y = Application.GetRealHeight(40);
            btnMsg.TextID = R.MyInternationalizationString.uColorRegulation;
            bodyFrameLayout.AddChidren(btnMsg);
 
            //圆盘控件的FrameLayout
            var frame = new FrameLayout();
            frame.Y = btnMsg.Bottom + Application.GetRealHeight(50);
            frame.Width = Application.GetMinRealAverage(720);
            frame.Height = Application.GetRealHeight(720);
            frame.Gravity = Gravity.CenterHorizontal;
            bodyFrameLayout.AddChidren(frame);
 
            //圆盘控件
            var colorPic = new ColorPicker();
            colorPic.ColorImagePath = "Item/PickerColorWheel.png";
            frame.AddChidren(colorPic);
 
            //白点控件
            var btnWhiteView = new IconViewControl(20);
            btnWhiteView.Visible = false;
            btnWhiteView.BackgroundColor = UserCenterColor.Current.White;
            btnWhiteView.Radius = (uint)Application.GetMinRealAverage(20) / 2;
            frame.AddChidren(btnWhiteView);
 
            colorPic.ColorChaged += (sender, eColor) =>
            {
                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.dicIconContr["Defult"].BackgroundColor = this.GetColorByRGB(eColor[0], eColor[1], eColor[2]);
                this.dicIconContr["Defult"].Tag = listRGB;
 
                //显示打勾选择颜色的控件
                this.ShowSelectColorControl(this.dicIconContr["Defult"]);
            };
            colorPic.MouseDownEventHandler += (sender, e) =>
            {
                //显示白点
                btnWhiteView.X = (int)e.X;
                btnWhiteView.Y = (int)e.Y;
                btnWhiteView.Visible = true;
            };
 
            return frame;
        }
 
        #endregion
 
        #region ■ 将颜色设置到面板___________________
 
        /// <summary>
        /// 将选择的颜色设置到面板
        /// </summary>
        private async void SetSelectColorToPanel()
        {
            if (PilolightStatu == StatuMode.Normal)
            {
                this.keyColorData.CloseColorR = Convert.ToString(listRGB[0], 16);
                this.keyColorData.CloseColorG = Convert.ToString(listRGB[1], 16);
                this.keyColorData.CloseColorB = Convert.ToString(listRGB[2], 16);
            }
            else
            {
                this.keyColorData.OpenColorR = Convert.ToString(listRGB[0], 16);
                this.keyColorData.OpenColorG = Convert.ToString(listRGB[1], 16);
                this.keyColorData.OpenColorB = Convert.ToString(listRGB[2], 16);
            }
            //设置按键面板指定端点的【指示灯开关颜色】的信息
            var result = await Common.LocalDevice.Current.SetPanelEpointColorInfo(this.panel, this.keyColorData);
            if (result == false)
            {
                return;
            }
            Application.RunOnMainThread(() =>
            {
                this.CloseForm();
            });
        }
 
        #endregion
 
        #region ■ 从网关获取颜色信息_________________
 
        /// <summary>
        /// 从网关获取颜色信息然后设置到界面
        /// </summary>
        private async void GetColorAndSetToForm()
        {
            //开启进度条
            this.ShowProgressBar();
            this.keyColorData = await Common.LocalDevice.Current.GetPanelEpointColorInfo(this.panel);
            if (this.keyColorData == null)
            {
                //关闭进度条
                this.CloseProgressBar(ShowReLoadMode.YES);
                return;
            }
            //关闭进度条
            this.CloseProgressBar();
 
            int R = PilolightStatu == StatuMode.Normal ? Convert.ToInt32(this.keyColorData.CloseColorR, 16) : Convert.ToInt32(this.keyColorData.OpenColorR, 16);
            int G = PilolightStatu == StatuMode.Normal ? Convert.ToInt32(this.keyColorData.CloseColorG, 16) : Convert.ToInt32(this.keyColorData.OpenColorG, 16);
            int B = PilolightStatu == StatuMode.Normal ? Convert.ToInt32(this.keyColorData.CloseColorB, 16) : Convert.ToInt32(this.keyColorData.OpenColorB, 16);
 
            //设置背景色
            string keys = this.GetColorKeys(R, G, B);
            if (this.dicIconContr.ContainsKey(keys) == false)
            {
                keys = "Defult";
            }
            uint backColor = this.GetColorByRGB((byte)R, (byte)G, (byte)B);
            Application.RunOnMainThread(() =>
            {
                this.dicIconContr[keys].BackgroundColor = backColor;
                //显示打勾选择颜色的控件
                this.ShowSelectColorControl(this.dicIconContr[keys]);
            });
        }
 
        #endregion
 
        #region ■ 初始化预选颜色_____________________
 
        /// <summary>
        /// 初始化备用颜色的控件
        /// </summary>
        /// <param name="frame">颜色圆盘控件的FrameLayout</param>
        private void InitSpareColorControl(FrameLayout frame)
        {
            //控件大小
            int size = Application.GetMinRealAverage(100);
            //控件数
            var dicColor = this.InitSpareDefultColor();
            int controlCount = dicColor.Count;
            //控件间的间隔
            int space = (bodyFrameLayout.Width - controlCount * size) / (controlCount + 1);
 
            //比颜色控件高一点
            this.defultColorFrame = new FrameLayout();
            this.defultColorFrame.Height = Application.GetRealHeight(120);
            this.defultColorFrame.Y = frame.Bottom + Application.GetRealHeight(120);
            bodyFrameLayout.AddChidren(this.defultColorFrame);
 
            foreach (var keys in dicColor.Keys)
            {
                //添加图标
                var btnIcon = this.AddColorIconControl(this.defultColorFrame, dicColor[keys], size, space);
                this.dicIconContr[keys] = btnIcon;
            }
        }
 
        /// <summary>
        /// 添加一个显示颜色的控件
        /// </summary>
        /// <param name="frame">父容器</param>
        /// <param name="litColor">RGB</param>
        /// <param name="size">图标大小</param>
        /// <param name="Space">控件间隔</param>
        private IconViewControl AddColorIconControl(FrameLayout frame, List<int> litColor, int size, int Space)
        {
            byte R = (byte)litColor[0];
            byte G = (byte)litColor[1];
            byte B = (byte)litColor[2];
 
            var btnColor = new IconViewControl(size, false);
            btnColor.Tag = litColor;
            btnColor.Radius = 5;
            btnColor.BorderWidth = 1;
            btnColor.Gravity = Gravity.CenterVertical;
            btnColor.BorderColor = UserCenterColor.Current.TextFrameColor;
            btnColor.BackgroundColor = this.GetColorByRGB(R, G, B);
 
            //计算X轴坐标
            int XX = Space;
            var myView = frame.GetChildren(frame.ChildrenCount - 1);
            if (myView != null)
            {
                XX = myView.Right + Space;
            }
            btnColor.X = XX;
            frame.AddChidren(btnColor);
 
            //单击颜色
            btnColor.MouseUpEventHandler += (sender, e) =>
            {
                this.listRGB = (List<int>)btnColor.Tag;
                //显示打勾选择颜色的控件
                this.ShowSelectColorControl(btnColor);
            };
 
            return btnColor;
        }
 
        /// <summary>
        /// 显示打勾选择颜色的控件
        /// </summary>
        /// <param name="control"></param>
        private void ShowSelectColorControl(IconViewControl control)
        {
            if (this.btnCheckContr == null)
            {
                this.btnCheckContr = new IconViewControl();
                this.btnCheckContr.InitAvgSize(72, 61);
                this.btnCheckContr.UnSelectedImagePath = "Item/Check.png";
                this.defultColorFrame.AddChidren(this.btnCheckContr);
            }
            int XX = control.X + (control.Width - this.btnCheckContr.Width) / 2;
            this.btnCheckContr.X = XX;
            this.btnCheckContr.Gravity = Gravity.CenterVertical;
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 初始化备用颜色的默认值
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, List<int>> InitSpareDefultColor()
        {
            var dic = new Dictionary<string, List<int>>();
 
            dic["Defult"] = new List<int>() { 208, 199, 186 };
            dic["114-182-216"] = new List<int>() { 114, 182, 216 };
            dic["224-144-218"] = new List<int>() { 224, 144, 218 };
            dic["232-235-195"] = new List<int>() { 232, 235, 195 };
            dic["212-156-197"] = new List<int>() { 212, 156, 197 };
            dic["178-229-236"] = new List<int>() { 178, 229, 236 };
 
            return dic;
        }
 
        /// <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);
        }
 
        /// <summary>
        /// 获取字典颜色的主键
        /// </summary>
        /// <param name="listRGB"></param>
        /// <returns></returns>
        private string GetColorKeys(byte[] listRGB)
        {
            int R = Convert.ToInt32(Convert.ToString(listRGB[0]));
            int G = Convert.ToInt32(Convert.ToString(listRGB[1]));
            int B = Convert.ToInt32(Convert.ToString(listRGB[2]));
 
            return this.GetColorKeys(R, G, B);
        }
 
        /// <summary>
        /// 获取字典颜色的主键
        /// </summary>
        /// <param name="R"></param>
        /// <param name="G"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        private string GetColorKeys(int R, int G, int B)
        {
            return R + "-" + G + "-" + B;
        }
 
        #endregion
    }
}