summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/UICommon/XUILongPress.cs
blob: fa715e614151571e1db7f638151c40c251fc3ca3 (plain)
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
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;
}