wxr
2021-07-01 43b0d5870d528f23ecd6aeceb6cfd4325188b46f
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
using Shared;
using HDL_ON.UI.CSS;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace HDL_ON.Stan
{
    /// <summary>
    /// 控制上下左右停止的图像控件
    /// </summary>
    public class DirectionImageControl : NormalFrameLayout
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 点击后自动还原状态的控制事件(只有Mid,left,right,up,down)
        /// </summary>
        public Action<DirectionEnum> AutoRecoverControlEvent = null;
        /// <summary>
        /// 点击后不会还原状态的控制事件(只有enable,Mid,left,right,up,down。 enable:手指松开方向键后触发)
        /// </summary>
        public Action<DirectionEnum> NotRecoverControlEvent = null;
        /// <summary>
        /// 能否控制方向
        /// </summary>
        private bool CanDirection = false;
        /// <summary>
        /// 各图片路径 0:可以控制 1:不可以控制 2:上 3:下 4:左 5:右
        /// </summary>
        private List<string> listIconPath = new List<string>();
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="i_enablePath">可以控制的背景图</param>
        /// <param name="i_disablePath">不可以控制的背景图</param>
        /// <param name="i_upPath">点击向上的背景图</param>
        /// <param name="i_downPath">点击向下的背景图</param>
        /// <param name="i_leftPath">点击向左的背景图</param>
        /// <param name="i_rightPath">点击向右的背景图</param>
        public void InitControl(string i_enablePath, string i_disablePath, string i_upPath, string i_downPath,
            string i_leftPath, string i_rightPath)
        {
            this.listIconPath.Add(i_enablePath);
            this.listIconPath.Add(i_disablePath);
            this.listIconPath.Add(i_upPath);
            this.listIconPath.Add(i_downPath);
            this.listIconPath.Add(i_leftPath);
            this.listIconPath.Add(i_rightPath);
 
            this.BackgroundImagePath = i_disablePath;
            this.CanDirection = false;
            //初始化按钮
            this.IntiButtion();
        }
 
        /// <summary>
        /// 初始化按钮
        /// </summary>
        private void IntiButtion()
        {
            //正中间的按钮
            var btnMid = new PicViewControl(70, 70);
            btnMid.Gravity = Gravity.Center;
            this.AddChidren(btnMid);
            btnMid.ButtonClickEvent += (sender, e) =>
            {
                //判断能否点击
                if (this.CanClick == false) { return; }
                //开启等待线程(不允许狂点)
                this.StartWaitThread();
 
                this.AutoRecoverControlEvent?.Invoke(DirectionEnum.Mid);
                this.NotRecoverControlEvent?.Invoke(DirectionEnum.Mid);
            };
 
            //上按钮
            var btnUp = new PicViewControl(52, 52);
            this.AddChidren(btnUp);
            btnUp.X = (this.Width - btnUp.Width) / 2;
            btnUp.ButtonClickEvent += (sender, e) =>
            {
                //方向点击
                this.DirectionClickEvent(DirectionEnum.Up);
            };
            btnUp.ButtonDownClickEvent += (sender, e) =>
            {
                //按键按下
                this.DirectionDownClickEvent(DirectionEnum.Up);
            };
 
            //下按钮
            var btnDown = new PicViewControl(btnUp.Width, btnUp.Height, false);
            btnDown.X = btnUp.X;
            this.AddChidren(btnDown);
            btnDown.Y = this.Height - btnDown.Height;
            btnDown.ButtonClickEvent += (sender, e) =>
            {
                //方向点击
                this.DirectionClickEvent(DirectionEnum.Down);
            };
            btnDown.ButtonDownClickEvent += (sender, e) =>
            {
                //按键按下
                this.DirectionDownClickEvent(DirectionEnum.Down);
            };
 
            //左按钮
            var btnLeft = new PicViewControl(btnUp.Width, btnUp.Height, false);
            this.AddChidren(btnLeft);
            btnLeft.Y = (this.Height - btnLeft.Height) / 2;
            btnLeft.ButtonClickEvent += (sender, e) =>
            {
                //方向点击
                this.DirectionClickEvent(DirectionEnum.Left);
            };
            btnLeft.ButtonDownClickEvent += (sender, e) =>
            {
                //按键按下
                this.DirectionDownClickEvent(DirectionEnum.Left);
            };
 
            //右按钮
            var btnRight = new PicViewControl(btnUp.Width, btnUp.Height, false);
            btnRight.Y = btnLeft.Y;
            this.AddChidren(btnRight);
            btnRight.X = this.Width - btnRight.Width;
            btnRight.ButtonClickEvent += (sender, e) =>
            {
                //方向点击
                this.DirectionClickEvent(DirectionEnum.Right);
            };
            btnRight.ButtonDownClickEvent += (sender, e) =>
            {
                //按键按下
                this.DirectionDownClickEvent(DirectionEnum.Right);
            };
        }
 
        #endregion
 
        #region ■ 外部设置图片_______________________
 
        /// <summary>
        /// 设置图片显示
        /// </summary>
        /// <param name="direction">方向枚举(不支持Mid)</param>
        public void SetDirectionImage(DirectionEnum direction)
        {
            if (direction == DirectionEnum.Mid)
            {
                //不支持Mid
                return;
            }
            if (direction == DirectionEnum.Disable)
            {
                //不能控制方向
                this.CanDirection = false;
            }
            else
            {
                //能够控制方向
                this.CanDirection = true;
            }
 
            string iconPath = this.listIconPath[((int)direction) - 1];
            if (this.BackgroundImagePath != iconPath)
            {
                this.BackgroundImagePath = iconPath;
            }
        }
 
        #endregion
 
        #region ■ 方向点击___________________________
 
        /// <summary>
        /// 方向点击
        /// </summary>
        /// <param name="direction"></param>
        private void DirectionClickEvent(DirectionEnum direction)
        {
            //判断能否点击
            if (this.CanClick == false || this.CanDirection == false)
            {
                return;
            }
 
            //如果使用特效的话
            if (this.AutoRecoverControlEvent != null)
            {
                //切换图片
                this.SetDirectionImage(direction);
                //开启等待线程(不允许狂点)
                this.StartWaitThread();
 
                this.AutoRecoverControlEvent?.Invoke(direction);
            }
            else
            {
                //切换图片
                this.SetDirectionImage(DirectionEnum.Enable);
                this.NotRecoverControlEvent?.Invoke(DirectionEnum.Enable);
            }
        }
 
        /// <summary>
        /// 方向按键按下事件
        /// </summary>
        /// <param name="direction"></param>
        private void DirectionDownClickEvent(DirectionEnum direction)
        {
            //如果不使用特效的话
            if (this.CanClick == false || this.CanDirection == false || this.NotRecoverControlEvent == null)
            {
                return;
            }
            //切换图片
            this.SetDirectionImage(direction);
            this.NotRecoverControlEvent?.Invoke(direction);
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 开启等待线程(不允许狂点)
        /// </summary>
        private void StartWaitThread()
        {
            this.CanClick = false;
            HdlThreadLogic.Current.RunThread(() =>
            {
                System.Threading.Thread.Sleep(300);
                this.CanClick = true;
 
                //如果不使用特效的话
                if (this.NotRecoverControlEvent != null)
                {
                    return;
                }
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //图片还原
                    if (this.BackgroundImagePath != this.listIconPath[0])
                    {
                        this.BackgroundImagePath = this.listIconPath[0];
                    }
 
                }, ShowErrorMode.NO);
 
            }, ShowErrorMode.NO);
        }
 
        #endregion
    }
}