Tanoda
AimerInputModule.cs
Go to the documentation of this file.
1
3
5{
6 [RequireComponent(typeof(EventSystem))]
7 [AddComponentMenu("Event/Extensions/Aimer Input Module")]
8 public class AimerInputModule : PointerInputModule
9 {
13 public string activateAxis = "Submit";
14
18 public Vector2 aimerOffset = new Vector2(0, 0);
19
24 public static GameObject objectUnderAimer;
25
26 protected AimerInputModule() { }
27
28 public override void ActivateModule()
29 {
30 StandaloneInputModule StandAloneSystem = GetComponent<StandaloneInputModule>();
31
32 if (StandAloneSystem != null && StandAloneSystem.enabled)
33 {
34 Debug.LogError("Aimer Input Module is incompatible with the StandAloneInputSystem, " +
35 "please remove it from the Event System in this scene or disable it when this module is in use");
36 }
37 }
38
39 public override void Process()
40 {
41 bool pressed = Input.GetButtonDown(activateAxis);
42 bool released = Input.GetButtonUp(activateAxis);
43
44 PointerEventData pointer = GetAimerPointerEventData();
45
46 ProcessInteraction(pointer, pressed, released);
47
48 if (!released)
49 ProcessMove(pointer);
50 else
51 RemovePointerData(pointer);
52 }
53
54 protected virtual PointerEventData GetAimerPointerEventData()
55 {
56 PointerEventData pointerData;
57
58 //Not certain on the use of this.
59 //I know that -1 is the mouse and anything positive would be a finger/touch, 0 being the first finger, 1 being the second and so one till the system limit is reached.
60 //So that is the reason I choose -2.
61 GetPointerData(-2, out pointerData, true);
62
63 pointerData.Reset();
64
65 pointerData.position = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f) + aimerOffset;
66
67 eventSystem.RaycastAll(pointerData, m_RaycastResultCache);
68 var raycast = FindFirstRaycast(m_RaycastResultCache);
69 pointerData.pointerCurrentRaycast = raycast;
70 m_RaycastResultCache.Clear();
71 return pointerData;
72 }
73
74 private void ProcessInteraction(PointerEventData pointer, bool pressed, bool released)
75 {
76 var currentOverGo = pointer.pointerCurrentRaycast.gameObject;
77
78 objectUnderAimer = ExecuteEvents.GetEventHandler<ISubmitHandler>(currentOverGo);//we only want objects that we can submit on.
79
80 if (pressed)
81 {
82 pointer.eligibleForClick = true;
83 pointer.delta = Vector2.zero;
84 pointer.pressPosition = pointer.position;
85 pointer.pointerPressRaycast = pointer.pointerCurrentRaycast;
86
87 // search for the control that will receive the press
88 // if we can't find a press handler set the press
89 // handler to be what would receive a click.
90 var newPressed = ExecuteEvents.ExecuteHierarchy(currentOverGo, pointer, ExecuteEvents.submitHandler);
91
92 // didn't find a press handler... search for a click handler
93 if (newPressed == null)
94 {
95 newPressed = ExecuteEvents.ExecuteHierarchy(currentOverGo, pointer, ExecuteEvents.pointerDownHandler);
96 if (newPressed == null)
97 newPressed = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
98 }
99 else
100 {
101 pointer.eligibleForClick = false;
102 }
103
104 if (newPressed != pointer.pointerPress)
105 {
106 pointer.pointerPress = newPressed;
107 pointer.rawPointerPress = currentOverGo;
108 pointer.clickCount = 0;
109 }
110
111 // Save the drag handler as well
112 pointer.pointerDrag = ExecuteEvents.GetEventHandler<IDragHandler>(currentOverGo);
113
114 if (pointer.pointerDrag != null)
115 ExecuteEvents.Execute<IBeginDragHandler>(pointer.pointerDrag, pointer, ExecuteEvents.beginDragHandler);
116 }
117
118 if (released)
119 {
120 //Debug.Log("Executing pressup on: " + pointer.pointerPress);
121 ExecuteEvents.Execute(pointer.pointerPress, pointer, ExecuteEvents.pointerUpHandler);
122
123 //Debug.Log("KeyCode: " + pointer.eventData.keyCode);
124
125 // see if we mouse up on the same element that we clicked on...
126 var pointerUpHandler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
127
128 // PointerClick
129 if (pointer.pointerPress == pointerUpHandler && pointer.eligibleForClick)
130 {
131 float time = Time.unscaledTime;
132
133 if (time - pointer.clickTime < 0.3f)
134 ++pointer.clickCount;
135 else
136 pointer.clickCount = 1;
137 pointer.clickTime = time;
138
139 ExecuteEvents.Execute(pointer.pointerPress, pointer, ExecuteEvents.pointerClickHandler);
140 }
141 else if (pointer.pointerDrag != null)
142 {
143 ExecuteEvents.ExecuteHierarchy(currentOverGo, pointer, ExecuteEvents.dropHandler);
144 }
145
146 pointer.eligibleForClick = false;
147 pointer.pointerPress = null;
148 pointer.rawPointerPress = null;
149
150 if (pointer.pointerDrag != null)
151 ExecuteEvents.Execute(pointer.pointerDrag, pointer, ExecuteEvents.endDragHandler);
152
153 pointer.pointerDrag = null;
154 }
155 }
156
157 public override void DeactivateModule()
158 {
159 base.DeactivateModule();
160 ClearSelection();
161 }
162 }
163}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
static GameObject objectUnderAimer
The object under aimer. A static access field that lets you know what is under the aimer....
Vector2 aimerOffset
The aimer offset position. Aimer is center screen use this offset to change that.
string activateAxis
The Input axis name used to activate the object under the reticle.