黄学彪
2020-09-22 ade5917841b0fdcb1df7353ef7c56b1a1bdc9282
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
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone.MainPage.Controls
{
    /// <summary>
    /// 场景卡片控件
    /// </summary>
    public class SceneCardControl : FrameLayoutStatuControl
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 卡片需要被移除的事件
        /// </summary>
        public Action CardNeedRemoveEvent = null;
        /// <summary>
        /// 场景图片控件
        /// </summary>
        private ImageView btnScenePic = null;
        /// <summary>
        /// 场景背景图控件
        /// </summary>
        private PicViewControl btnSceneBackGroud = null;
        /// <summary>
        /// 时间图标控件
        /// </summary>
        private IconViewControl btnTimeIcon = null;
        /// <summary>
        /// 延时时间显示控件
        /// </summary>
        private NormalViewControl btnTimeView = null;
        /// <summary>
        /// 场景名字控件
        /// </summary>
        private NormalViewControl btnSceneName = null;
        /// <summary>
        /// 场景ID
        /// </summary>
        private int SceneId = 0;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 场景卡片控件(宽度:458 + 14 * 2 高度:305 + 43)
        /// </summary>
        public SceneCardControl()
        {
            //图片真实宽度+图片自身左右空白
            this.Width = HdlControlLogic.Current.GetPictrueRealSize(458 + 14 * 2);
            //图片高度+阴影
            this.Height = HdlControlLogic.Current.GetPictrueRealSize(305 + 43);
            //取消点击特效
            this.UseClickStatu = false;
 
            this.ButtonClickEvent += (sender, e) =>
            {
                //调用场景
                this.SetSceneAction();
            };
        }
 
        /// <summary>
        /// 初始化控件
        /// </summary>
        /// <param name="i_scene">场景对象</param>
        public void InitControl(Common.SceneUI i_scene)
        {
            this.SceneId = i_scene.Id;
            string hourText = Language.StringByID(R.MyInternationalizationString.Hour);
            string minuText = Language.StringByID(R.MyInternationalizationString.Minute);
            string secondText = Language.StringByID(R.MyInternationalizationString.Second);
 
            //场景图片控件
            this.btnScenePic = new ImageView();
            btnScenePic.X = HdlControlLogic.Current.GetPictrueRealSize(14);
            btnScenePic.Width = HdlControlLogic.Current.GetPictrueRealSize(458);
            btnScenePic.Height = HdlControlLogic.Current.GetPictrueRealSize(305);
            btnScenePic.Radius = (uint)Application.GetRealHeight(17);
            this.AddChidren(btnScenePic, ChidrenBindMode.NotBind);
 
            //场景背景图控件
            this.btnSceneBackGroud = new PicViewControl(this.Width, this.Height, false);
            btnSceneBackGroud.UnSelectedImagePath = "Scene/Background.png";
            this.AddChidren(btnSceneBackGroud, ChidrenBindMode.BindEvent);
 
            //延时时间显示控件
            this.btnTimeView = new NormalViewControl(HdlControlLogic.Current.GetPictrueRealSize(280), HdlControlLogic.Current.GetPictrueRealSize(63), false);
            btnTimeView.X = HdlControlLogic.Current.GetPictrueRealSize(37);
            btnTimeView.Y = HdlControlLogic.Current.GetPictrueRealSize(10);
            btnTimeView.TextColor = 0xffffb400;
            btnTimeView.IsBold = true;
            if (i_scene.SceneDelayTime > 0)
            {
                btnTimeView.Text= this.GetTimeString(i_scene.SceneDelayTime, hourText, minuText, secondText);
            }
            this.AddChidren(btnTimeView, ChidrenBindMode.BindEvent);
 
            //时间图标控件
            this.btnTimeIcon = new IconViewControl(63);
            btnTimeIcon.X = HdlControlLogic.Current.GetPictrueRealSize(37);
            btnTimeIcon.Y = HdlControlLogic.Current.GetPictrueRealSize(20);
            btnTimeIcon.UnSelectedImagePath = "Item/Time.png";
            this.AddChidren(btnTimeIcon, ChidrenBindMode.NotBind);
            if (i_scene.SceneDelayTime > 0)
            {
                btnTimeIcon.Visible = false;
            }
            btnTimeIcon.ButtonClickEvent += (sender, e) =>
            {
                if (i_scene.RemainTime > 0)
                {
                    //该场景正在延时,请稍后
                    var msgContr = new ShowMsgControl(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.TheSceneIsDelaying));
                    msgContr.Show();
                    return;
                }
                var timeSelect = new Device.CommonForm.SelectTime();
                Common.CommonPage.Instance.AddChidren(timeSelect);
                timeSelect.TempTime = i_scene.SceneDelayTime;
                timeSelect.Init();
                timeSelect.TimeAction = (time) =>
                {
                    i_scene.SceneDelayTime = time;
 
                   //时间图标不显示
                   this.btnTimeIcon.Visible = false;
                   //显示剩余的时间
                   this.btnTimeView.Text = this.GetTimeString(time, hourText, minuText, secondText);
                };
            };
 
            //收藏控件
            var btnCollect = new IconViewControl(107);
            btnCollect.X = HdlControlLogic.Current.GetPictrueRealSize(350);
            btnCollect.Y = HdlControlLogic.Current.GetPictrueRealSize(12);
            btnCollect.UnSelectedImagePath = "Item/Collection1.png";
            btnCollect.SelectedImagePath = "Item/CollectionSelected1.png";
            this.AddChidren(btnCollect, ChidrenBindMode.NotBind);
            btnCollect.IsSelected = HdlRoomLogic.Current.IsCollectInRoom(i_scene);
            btnCollect.ButtonClickEvent += (sender, e) =>
            {
                //状态取反
                btnCollect.IsSelected = !btnCollect.IsSelected;
                if (btnCollect.IsSelected == false)
                {
                    //取消收藏
                    HdlSceneLogic.Current.DeleteLoveScene(i_scene);
                    if (HdlRoomLogic.Current.NowMainPageRoom.IsLove == true)
                    {
                        //如果当前房间是我的喜爱的话,回调卡片被删除的事件
                        this.CardNeedRemoveEvent?.Invoke();
                    }
                }
                else
                {
                    //添加收藏
                    HdlSceneLogic.Current.AddLoveScene(i_scene);
                }
            };
 
            //场景名称控件
            this.btnSceneName = new NormalViewControl(HdlControlLogic.Current.GetPictrueRealSize(280), HdlControlLogic.Current.GetPictrueRealSize(63), false);
            btnSceneName.X = HdlControlLogic.Current.GetPictrueRealSize(37);
            btnSceneName.Y = HdlControlLogic.Current.GetPictrueRealSize(236);
            btnSceneName.IsBold = true;
            btnSceneName.TextColor = UserCenterColor.Current.White;
            this.AddChidren(btnSceneName, ChidrenBindMode.BindEvent);
 
            //刷新控件状态
            this.RefreshControlInfo(i_scene);
        }
 
        #endregion
 
        #region ■ 调用场景___________________________
 
        /// <summary>
        /// 调用场景
        /// </summary>
        private async void SetSceneAction()
        {
            //这个场景不见的情况应该不可能
            var scene = HdlSceneLogic.Current.GetSceneUIBySceneId(this.SceneId);
            if (scene.SceneDelayTime <= 0 && scene.RemainTime <= 0)
            {
                //如果没有延迟的话,直接开启Loading特效
                this.StartLoadingApreal();
            }
            //执行调用场景
            var result = await HdlSceneLogic.Current.ControlScene(scene);
            if (result == false)
            {
                return;
            }
            //修改时间
            scene.RemainTime = scene.SceneDelayTime;
            scene.SceneDelayTime = 0;
            //开启延时倒计时特效
            this.StartRemainTimeApreal(scene);
        }
 
        #endregion
 
        #region ■ 刷新控件状态_______________________
 
        /// <summary>
        /// 刷新控件状态
        /// </summary>
        /// <param name="i_scene">场景对象</param>
        public void RefreshControlInfo(Common.SceneUI i_scene)
        {
            //场景名称必须刷新
            this.btnSceneName.Text = i_scene.Name;
            //刷新图片
            if (i_scene.IconPathType == 0)
            {
                //主页的场景卡片用的是另外的图
                string newPath = i_scene.IconPath.Replace("SceneIcon", "SceneCardIcon");
                if (this.btnScenePic.ImagePath != newPath)
                {
                    this.btnScenePic.ImagePath = newPath;
                }
            }
            else
            {
                string fullPath = System.IO.Path.Combine(Common.Config.Instance.FullPath, i_scene.IconPath);
                if (this.btnScenePic.ImagePath != fullPath)
                {
                    this.btnScenePic.ImagePath = fullPath;
                }
            }
 
            //开启延时倒计时特效
            this.StartRemainTimeApreal(i_scene);
        }
 
        #endregion
 
        #region ■ 延时倒计时特效_____________________
 
        /// <summary>
        /// 开启延时倒计时特效
        /// </summary>
        /// <param name="i_scene"></param>
        private void StartRemainTimeApreal(Common.SceneUI i_scene)
        {
            if (i_scene.RemainTime <= 0)
            {
                return;
            }
            int remainTine = i_scene.RemainTime;
            string hourText = Language.StringByID(R.MyInternationalizationString.Hour);
            string minuText = Language.StringByID(R.MyInternationalizationString.Minute);
            string secondText = Language.StringByID(R.MyInternationalizationString.Second);
 
            //时间图标不显示
            this.btnTimeIcon.Visible = false;
            //显示剩余的时间
            this.btnTimeView.Text = this.GetTimeString(i_scene.RemainTime, hourText, minuText, secondText);
 
            //开启内部延时时间线程(旨在全部地方的同一场景时间同步)
            HdlSceneLogic.Current.StartDelayTimeThread(i_scene);
 
            new System.Threading.Thread(() =>
            {
                while (remainTine > 0 && this.Parent != null)
                {
                    System.Threading.Thread.Sleep(1000);
                    Application.RunOnMainThread(() =>
                    {
                        this.btnTimeView.Text = this.GetTimeString(remainTine, hourText, minuText, secondText);
                    });
                    remainTine--;
                }
                Application.RunOnMainThread(() =>
                {
                    //倒计时结束时,时间图标显示
                    this.btnTimeIcon.Visible = true;
                    this.btnTimeView.Text = string.Empty;
                    //直接开启Loading特效
                    this.StartLoadingApreal();
                });
            })
            { IsBackground = true }.Start();
        }
 
        #endregion
 
        #region ■ Loading特效________________________
 
        /// <summary>
        /// 开启Loading特效
        /// </summary>
        private void StartLoadingApreal()
        {
            //弄个遮罩遮住
            var frameBack = new FrameLayout();
            frameBack.BackgroundImagePath = "Scene/SceneActionGroud.png";
            this.AddChidren(frameBack);
            //再加个转圈的
            var loadContr = new MyProgressLoading();
            loadContr.LoadingBackgroundColor = UserCenterColor.Current.Transparent;
            frameBack.AddChidren(loadContr);
            loadContr.StartLoading(1000);
            loadContr.DisponeEvent += () =>
            {
                frameBack.RemoveFromParent();
            };
        }
 
        #endregion
 
        #region ■ 控件摧毁___________________________
 
        /// <summary>
        /// 控件摧毁
        /// </summary>
        public override void RemoveFromParent()
        {
            this.CardNeedRemoveEvent = null;
 
            base.RemoveFromParent();
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 获取时间的翻译文本
        /// </summary>
        /// <param name="second"></param>
        /// <param name="hourText">小时的文本</param>
        /// <param name="minuText">分的文本</param>
        /// <param name="secondText">秒的文本</param>
        /// <returns></returns>
        private string GetTimeString(int second, string hourText, string minuText, string secondText)
        {
            string timeStr = string.Empty;
            int hour = second / 3600;
            int minu = second % 3600 / 60;
            int sec = second % 60;
            if (hour > 0)
            {
                timeStr += hour + hourText;
            }
            if (minu > 0)
            {
                timeStr += minu + minuText;
            }
            if (sec > 0)
            {
                timeStr += sec + secondText;
            }
            return timeStr;
        }
 
        #endregion
 
        #region ■ 自定义控件_________________________
 
        /// <summary>
        /// 自定义控件(需要的它移除事件)
        /// </summary>
        private class MyProgressLoading : ProgressLoading
        {
            /// <summary>
            /// 控件销毁的事件
            /// </summary>
            public Action DisponeEvent = null;
            /// <summary>
            /// 控件销毁
            /// </summary>
            public override void RemoveFromParent()
            {
                base.RemoveFromParent();
 
                this.DisponeEvent?.Invoke();
                this.DisponeEvent = null;
            }
        }
 
        #endregion
    }
}