From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- Client/Assets/Scripts/UICommon/XUILongPress.cs | 75 ++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Client/Assets/Scripts/UICommon/XUILongPress.cs (limited to 'Client/Assets/Scripts/UICommon/XUILongPress.cs') diff --git a/Client/Assets/Scripts/UICommon/XUILongPress.cs b/Client/Assets/Scripts/UICommon/XUILongPress.cs new file mode 100644 index 00000000..fa715e61 --- /dev/null +++ b/Client/Assets/Scripts/UICommon/XUILongPress.cs @@ -0,0 +1,75 @@ +using UILib; +using UnityEngine; +using XUtliPoolLib; + +public class XUILongPress : XUIObject, IXUILongPress +{ + public void RegisterSpriteLongPressEventHandler(SpriteClickEventHandler eventHandler) + { + if (eventHandler != null) + { + UIEventListener.Get(this.gameObject).onPress -= OnSpritePress; + UIEventListener.Get(this.gameObject).onPress += OnSpritePress; + UIEventListener.Get(this.gameObject).onDrag -= OnSpriteDrag; + UIEventListener.Get(this.gameObject).onDrag += OnSpriteDrag; + } + + m_spriteLongPressEventHandler = eventHandler; + } + + protected override void OnAwake() + { + base.OnAwake(); + m_XUISprite = GetComponent(); + if (null == m_XUISprite) + { + Debug.LogError("null == XUISprite, " + this.gameObject.name); + } + } + + void Update() + { + if (m_spriteLongPressEventHandler != null && _lastPress > 0.0f && Time.time - _lastPress > _longClickDuration) + { + m_spriteLongPressEventHandler(m_XUISprite); + _lastPress = -1.0f; + //bPressed = false; + m_XUISprite.ClickCanceled = true; + } + } + + void OnSpritePress(GameObject button, bool isPressed) + { + if (m_spriteLongPressEventHandler != null) + { + if (isPressed) + { + _lastPress = Time.time; + //bPressed = true; + } + if (!isPressed) + { + _lastPress = -1.0f; + } + } + } + void OnSpriteDrag(GameObject button, Vector2 delta) + { + if (m_spriteLongPressEventHandler != null) + { + if (_lastPress > 0) + { + //bPressed = false; + _lastPress = -1.0f; + } + } + } + + private SpriteClickEventHandler m_spriteLongPressEventHandler = null; + private XUISprite m_XUISprite = null; + + private static readonly float _longClickDuration = 0.5f; + float _lastPress = -1f; + //bool bPressed = false; +} + -- cgit v1.1-26-g67d0