diff options
author | chai <chaifix@163.com> | 2021-01-25 14:28:30 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-01-25 14:28:30 +0800 |
commit | 6eb915c129fc90c6f4c82ae097dd6ffad5239efc (patch) | |
tree | 7dd2be50edf41f36b60fac84696e731c13afe617 /Client/Assets/Scripts/UICommon/XUILongPress.cs |
+scripts
Diffstat (limited to 'Client/Assets/Scripts/UICommon/XUILongPress.cs')
-rw-r--r-- | Client/Assets/Scripts/UICommon/XUILongPress.cs | 75 |
1 files changed, 75 insertions, 0 deletions
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<XUISprite>();
+ 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;
+}
+
|