0177d50094b1e69a29387e23a179d6b8375430fb
[platform/core/uifw/dali-core.git] / dali / internal / event / events / tap-gesture / tap-gesture-detector-impl.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/events/tap-gesture/tap-gesture-detector-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/event/events/gesture-event-processor.h>
27 #include <dali/public-api/events/tap-gesture.h>
28 #include <dali/public-api/object/type-registry.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 namespace
35 {
36 const unsigned int DEFAULT_TAPS_REQUIRED    = 1u;
37 const unsigned int DEFAULT_TOUCHES_REQUIRED = 1u;
38
39 // Signals
40 const char* const SIGNAL_TAP_DETECTED = "tapDetected";
41
42 BaseHandle Create()
43 {
44   return Dali::TapGestureDetector::New();
45 }
46
47 TypeRegistration mType(typeid(Dali::TapGestureDetector), typeid(Dali::GestureDetector), Create);
48
49 SignalConnectorType signalConnector1(mType, SIGNAL_TAP_DETECTED, &TapGestureDetector::DoConnectSignal);
50
51 } // namespace
52
53 TapGestureDetectorPtr TapGestureDetector::New()
54 {
55   return new TapGestureDetector;
56 }
57
58 TapGestureDetectorPtr TapGestureDetector::New(unsigned int tapsRequired)
59 {
60   return new TapGestureDetector(tapsRequired);
61 }
62
63 TapGestureDetector::TapGestureDetector()
64 : GestureDetector(GestureType::TAP),
65   mMinimumTapsRequired(DEFAULT_TAPS_REQUIRED),
66   mMaximumTapsRequired(DEFAULT_TAPS_REQUIRED),
67   mTouchesRequired(DEFAULT_TOUCHES_REQUIRED)
68 {
69 }
70
71 TapGestureDetector::TapGestureDetector(unsigned int tapsRequired)
72 : GestureDetector(GestureType::TAP),
73   mMinimumTapsRequired(tapsRequired),
74   mMaximumTapsRequired(tapsRequired),
75   mTouchesRequired(DEFAULT_TOUCHES_REQUIRED)
76 {
77 }
78
79 TapGestureDetector::~TapGestureDetector() = default;
80
81 void TapGestureDetector::SetMinimumTapsRequired(unsigned int taps)
82 {
83   if(mMinimumTapsRequired != taps)
84   {
85     mMinimumTapsRequired = taps;
86
87     if(!mAttachedActors.empty())
88     {
89       mGestureEventProcessor.GestureDetectorUpdated(this);
90     }
91   }
92 }
93
94 void TapGestureDetector::SetMaximumTapsRequired(unsigned int taps)
95 {
96   if(mMaximumTapsRequired != taps)
97   {
98     mMaximumTapsRequired = taps;
99
100     if(!mAttachedActors.empty())
101     {
102       mGestureEventProcessor.GestureDetectorUpdated(this);
103     }
104   }
105 }
106
107 void TapGestureDetector::SetTouchesRequired(unsigned int touches)
108 {
109   if(mTouchesRequired != touches)
110   {
111     mTouchesRequired = touches;
112
113     if(!mAttachedActors.empty())
114     {
115       mGestureEventProcessor.GestureDetectorUpdated(this);
116     }
117   }
118 }
119
120 unsigned int TapGestureDetector::GetMinimumTapsRequired() const
121 {
122   return mMinimumTapsRequired;
123 }
124
125 unsigned int TapGestureDetector::GetMaximumTapsRequired() const
126 {
127   return mMaximumTapsRequired;
128 }
129
130 unsigned int TapGestureDetector::GetTouchesRequired() const
131 {
132   return mTouchesRequired;
133 }
134
135 void TapGestureDetector::EmitTapGestureSignal(Dali::Actor tappedActor, const Dali::TapGesture& tap)
136 {
137   // Guard against destruction during signal emission
138   Dali::TapGestureDetector handle(this);
139
140   mDetectedSignal.Emit(tappedActor, tap);
141 }
142
143 bool TapGestureDetector::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
144 {
145   bool                connected(true);
146   TapGestureDetector* gesture = static_cast<TapGestureDetector*>(object); // TypeRegistry guarantees that this is the correct type.
147
148   if(0 == strcmp(signalName.c_str(), SIGNAL_TAP_DETECTED))
149   {
150     gesture->DetectedSignal().Connect(tracker, functor);
151   }
152   else
153   {
154     // signalName does not match any signal
155     connected = false;
156   }
157
158   return connected;
159 }
160
161 void TapGestureDetector::OnActorAttach(Actor& actor)
162 {
163   // Do nothing
164 }
165
166 void TapGestureDetector::OnActorDetach(Actor& actor)
167 {
168   // Do nothing
169 }
170
171 void TapGestureDetector::OnActorDestroyed(Object& object)
172 {
173   // Do nothing
174 }
175
176 } // namespace Internal
177
178 } // namespace Dali