GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
touchzone_component.h
Go to the documentation of this file.
1#pragma once
4#include "ga/graph/node.h"
5#include "ga/graph/scene.h"
6#include "ga/signal.h"
7namespace ga {
8
9class TouchZone : public ga::Component
10{
11public:
12 enum class State
13 {
14 DISABLED = -1, // disabled
15 INACTIVE = 0, // enabled, no touch
16 ACTIVE = 1 // enabled, touch
17 };
18
19 struct Event
20 {
21 enum class Type
22 {
23 PRESS,
24 RELEASE,
29
30 TouchZone* touchZone; // source zone (this)
31 TouchEvent touchEvent; // raw event
32 vec3 localPosition; // local touch position
34 };
35
37 {
38 if ( m_state == State::DISABLED )
39 return;
40
41 if ( touchEvt.captured && m_isCapturingTouch ) {
42 return;
43 }
44
45 auto node = getNode();
46 if ( !node )
47 return;
48
49 // transform touchEvt position into local node position
50 TouchZone::Event event;
51 event.touchZone = this;
52 event.touchEvent = touchEvt;
53 event.scenePosition = ga::vec3( touchEvt.position.x, touchEvt.position.y, 0 );
54 event.localPosition = node->scenePosToLocal( vec3( touchEvt.position, 0 ) );
55 bool isInBounds = m_customBoundsTest
56 ? m_customBoundsTest( event )
57 : node->component<Bounds>().contains( event.localPosition ); // todo: 3D bounds / ray cast...
58 if ( m_boundsInverted )
59 isInBounds = !isInBounds; // e.g. for click-off
60
61 switch ( touchEvt.type ) {
63 if ( isInBounds ) {
64 if ( m_isCapturingTouch ) {
65 touchEvt.captured = true;
66 }
67 // tap on
70 onTouchEvent( event );
71 }
72 break;
73 }
74
76 if ( isInBounds ) {
77 if ( m_isCapturingTouch ) {
78 touchEvt.captured = true;
79 }
80 if ( getState() == State::ACTIVE ) {
81 // dragged within bounds
83 } else {
84 // drag into zone
85 // setState( State::ACTIVE );
87 }
88 onTouchEvent( event );
89 } else if ( getState() == State::ACTIVE ) {
90 // drag off of zone
91 if ( m_allowLosingFocus ) {
93 }
95 onTouchEvent( event );
96 }
97 break;
98 }
99
101 if ( getState() == State::ACTIVE ) {
102 // tap release
103 if ( m_isCapturingTouch ) {
104 touchEvt.captured = true;
105 }
108 onTouchEvent( event );
109 } else {
111 }
112 break;
113 }
114
116 // not sure how to handle this...
117 if ( m_isCapturingTouch ) {
118 touchEvt.captured = true;
119 }
121 break;
122 }
123 }
124 }
125
126 void setState( State state )
127 {
128 m_state = state;
129 }
131 {
132 return m_state;
133 }
134
135 inline void enable()
136 {
137 if ( m_state == State::DISABLED ) {
139 }
140 }
141
142 inline void disable()
143 {
145 }
146
147 inline void setAllowLosingFocus( bool isAllowed = true )
148 {
149 m_allowLosingFocus = isAllowed;
150 }
151
152 inline void setEnableCapturingTouch( bool isEnabled = true )
153 {
154 m_isCapturingTouch = isEnabled;
155 }
156
158 {
159 return m_isCapturingTouch;
160 }
161
162 inline void invertBounds( bool invert = true )
163 {
164 m_boundsInverted = invert;
165 }
166
167 inline bool getAreBoundsInverted() const
168 {
169 return m_boundsInverted;
170 }
171
172 inline void setCustomBoundsTest( std::function<bool( TouchZone::Event )> testFn )
173 {
174 m_customBoundsTest = testFn;
175 }
176
177 // will test against ga::Bounds component attached to owner node
179 {
180 m_customBoundsTest = nullptr;
181 }
182
183 inline bool getIsUsingCustomBoundsTest() const
184 {
185 return m_customBoundsTest != nullptr;
186 }
187
188 void connectTouch( std::shared_ptr<Scene> scene, int groupId )
189 {
190 if ( !scene )
191 return;
192 m_touchConnection = scene->onTouchEvent.connect_scoped( [this]( TouchEvent& te ) { handleTouchEvent( te ); }, groupId );
193 }
194
196 {
198 }
199
200 // signals
202
203protected:
204 virtual void setScene( std::shared_ptr<Scene> scene )
205 {
207 connectTouch( scene, 0 );
208 }
209
212 bool m_boundsInverted = false;
214 bool m_allowLosingFocus = false;
215 std::function<bool( TouchZone::Event )> m_customBoundsTest = nullptr;
216};
217} // namespace ga
Definition: bounds_component.h:10
Definition: component.h:11
std::shared_ptr< Node > getNode() const
Definition: component.h:27
Definition: touchzone_component.h:10
std::function< bool(TouchZone::Event)> m_customBoundsTest
Definition: touchzone_component.h:215
void disconnectTouch()
Definition: touchzone_component.h:195
void setAllowLosingFocus(bool isAllowed=true)
Definition: touchzone_component.h:147
void enable()
Definition: touchzone_component.h:135
State getState() const
Definition: touchzone_component.h:130
bool m_allowLosingFocus
Definition: touchzone_component.h:214
bool getIsCapturingTouch()
Definition: touchzone_component.h:157
ga::Signal< TouchZone::Event & > onTouchEvent
Definition: touchzone_component.h:201
State m_state
Definition: touchzone_component.h:210
void invertBounds(bool invert=true)
Definition: touchzone_component.h:162
ga::Connection m_touchConnection
Definition: touchzone_component.h:211
void setUseDefaultBoundsTest()
Definition: touchzone_component.h:178
void connectTouch(std::shared_ptr< Scene > scene, int groupId)
Definition: touchzone_component.h:188
State
Definition: touchzone_component.h:13
void setCustomBoundsTest(std::function< bool(TouchZone::Event)> testFn)
Definition: touchzone_component.h:172
virtual void setScene(std::shared_ptr< Scene > scene)
Definition: touchzone_component.h:204
bool getAreBoundsInverted() const
Definition: touchzone_component.h:167
void setEnableCapturingTouch(bool isEnabled=true)
Definition: touchzone_component.h:152
bool getIsUsingCustomBoundsTest() const
Definition: touchzone_component.h:183
void disable()
Definition: touchzone_component.h:142
bool m_isCapturingTouch
Definition: touchzone_component.h:213
void handleTouchEvent(ga::TouchEvent &touchEvt)
Definition: touchzone_component.h:36
void setState(State state)
Definition: touchzone_component.h:126
bool m_boundsInverted
Definition: touchzone_component.h:212
Definition: sigslot.hpp:629
bool disconnect() noexcept
Definition: sigslot.hpp:648
Definition: sigslot.hpp:1134
Definition: color.h:9
bool captured
Definition: events.h:16
Definition: events.h:47
Type type
Definition: events.h:56
ga::vec2 position
Definition: events.h:57
Definition: touchzone_component.h:20
enum ga::TouchZone::Event::Type type
TouchZone * touchZone
Definition: touchzone_component.h:30
Type
Definition: touchzone_component.h:22
TouchEvent touchEvent
Definition: touchzone_component.h:31
vec3 scenePosition
Definition: touchzone_component.h:33
vec3 localPosition
Definition: touchzone_component.h:32