-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUIKeyBinding.cs
124 lines (112 loc) · 2.27 KB
/
UIKeyBinding.cs
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
using UnityEngine;
[AddComponentMenu("NGUI/Interaction/Key Binding")]
public class UIKeyBinding : MonoBehaviour
{
public enum Action
{
PressAndClick,
Select,
All
}
public enum Modifier
{
None,
Shift,
Control,
Alt
}
public KeyCode keyCode;
public Modifier modifier;
public Action action;
private bool mIgnoreUp;
private bool mIsInput;
private bool mPress;
private void Start()
{
UIInput component = GetComponent<UIInput>();
mIsInput = (component != null);
if (component != null)
{
EventDelegate.Add(component.onSubmit, OnSubmit);
}
}
private void OnSubmit()
{
if (UICamera.currentKey == keyCode && IsModifierActive())
{
mIgnoreUp = true;
}
}
private bool IsModifierActive()
{
if (modifier == Modifier.None)
{
return true;
}
if (modifier == Modifier.Alt)
{
if (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt))
{
return true;
}
}
else if (modifier == Modifier.Control)
{
if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
{
return true;
}
}
else if (modifier == Modifier.Shift && (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)))
{
return true;
}
return false;
}
private void Update()
{
if (keyCode != 0 && IsModifierActive())
{
if (action == Action.PressAndClick || action == Action.All)
{
if (UICamera.inputHasFocus)
{
return;
}
UICamera.currentTouch = UICamera.controller;
UICamera.currentScheme = UICamera.ControlScheme.Mouse;
UICamera.currentTouch.current = base.gameObject;
if (Input.GetKeyDown(keyCode))
{
mPress = true;
UICamera.Notify(base.gameObject, "OnPress", true);
}
if (Input.GetKeyUp(keyCode))
{
UICamera.Notify(base.gameObject, "OnPress", false);
if (mPress)
{
UICamera.Notify(base.gameObject, "OnClick", null);
mPress = false;
}
}
UICamera.currentTouch.current = null;
}
if ((action == Action.Select || action == Action.All) && Input.GetKeyUp(keyCode))
{
if (mIsInput)
{
if (!mIgnoreUp && !UICamera.inputHasFocus)
{
UICamera.selectedObject = base.gameObject;
}
mIgnoreUp = false;
}
else
{
UICamera.selectedObject = base.gameObject;
}
}
}
}
}