using Shared; using HDL_ON.UI.CSS; using System; using System.Collections.Generic; using System.Text; namespace HDL_ON.Stan { /// /// 控制上下左右停止的图像控件 /// public class DirectionImageControl : NormalFrameLayout { #region ■ 变量声明___________________________ /// /// 控制事件(不支持Enable和Disable,都归为Mid) /// public Action ControlEvent = null; /// /// 是否使用点击特效,一旦设置为true,则点击之后,会自动还原回Enable状态 /// public bool UseClickStau = false; /// /// 能否控制方向 /// private bool CanDirection = false; /// /// 各图片路径 0:可以控制 1:不可以控制 2:上 3:下 4:左 5:右 /// private List listIconPath = new List(); #endregion #region ■ 初始化_____________________________ /// /// 初始化 /// /// 可以控制的背景图 /// 不可以控制的背景图 /// 点击向上的背景图 /// 点击向下的背景图 /// 点击向左的背景图 /// 点击向右的背景图 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(); } /// /// 初始化按钮 /// 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.ControlEvent?.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 ■ 外部设置图片_______________________ /// /// 设置图片显示 /// /// 方向枚举(不支持Mid) public void SetDirectionImage(DirectionEnum direction) { if (direction == DirectionEnum.Mid) { //不支持Mid return; } if (direction == DirectionEnum.Disable) { //不能控制方向 this.CanDirection = false; } else if (direction == DirectionEnum.Enable) { //能够控制方向 this.CanDirection = true; } string iconPath = this.listIconPath[((int)direction) - 1]; if (this.BackgroundImagePath != iconPath) { this.BackgroundImagePath = iconPath; } } #endregion #region ■ 方向点击___________________________ /// /// 方向点击 /// /// private void DirectionClickEvent(DirectionEnum direction) { //判断能否点击 if (this.CanClick == false || this.CanDirection == false) { return; } //如果使用特效的话 if (this.UseClickStau == true) { //切换图片 this.SetDirectionImage(direction); //开启等待线程(不允许狂点) this.StartWaitThread(); } else { //切换图片 this.SetDirectionImage(DirectionEnum.Enable); } this.ControlEvent?.Invoke(direction); } /// /// 方向按键按下事件 /// /// private void DirectionDownClickEvent(DirectionEnum direction) { //如果不使用特效的话 if (this.CanClick == false || this.CanDirection == false || this.UseClickStau == true) { return; } //切换图片 this.SetDirectionImage(direction); } #endregion #region ■ 一般方法___________________________ /// /// 开启等待线程(不允许狂点) /// private void StartWaitThread() { this.CanClick = false; HdlThreadLogic.Current.RunThread(() => { System.Threading.Thread.Sleep(300); this.CanClick = true; //如果不使用特效的话 if (this.UseClickStau == false) { return; } HdlThreadLogic.Current.RunMain(() => { //图片还原 if (this.BackgroundImagePath != this.listIconPath[0]) { this.BackgroundImagePath = this.listIconPath[0]; } }, ShowErrorMode.NO); }, ShowErrorMode.NO); } #endregion } }