Add an environment variable for long press gesture
[platform/core/uifw/dali-core.git] / dali / internal / event / events / long-press-gesture / long-press-gesture-detector-impl.cpp
1 /*
2  * Copyright (c) 2019 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 "long-press-gesture-detector-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/events/long-press-gesture.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/internal/event/events/gesture-event-processor.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace
36 {
37
38 // Signals
39
40 const char* const SIGNAL_LONG_PRESS_DETECTED = "longPressDetected";
41
42 BaseHandle Create()
43 {
44   return Dali::LongPressGestureDetector::New();
45 }
46
47 TypeRegistration mType( typeid(Dali::LongPressGestureDetector), typeid(Dali::GestureDetector), Create );
48
49 SignalConnectorType signalConnector1( mType, SIGNAL_LONG_PRESS_DETECTED, &LongPressGestureDetector::DoConnectSignal );
50
51 }
52
53
54 namespace
55 {
56 const unsigned int DEFAULT_TOUCHES_REQUIRED = 1;
57 } // unnamed namespace
58
59 LongPressGestureDetectorPtr LongPressGestureDetector::New()
60 {
61   return new LongPressGestureDetector;
62 }
63
64 LongPressGestureDetectorPtr LongPressGestureDetector::New(unsigned int touchesRequired)
65 {
66   return new LongPressGestureDetector(touchesRequired, touchesRequired);
67 }
68
69 LongPressGestureDetectorPtr LongPressGestureDetector::New(unsigned int minTouches, unsigned int maxTouches)
70 {
71   return new LongPressGestureDetector(minTouches, maxTouches);
72 }
73
74 LongPressGestureDetector::LongPressGestureDetector()
75 : GestureDetector(Gesture::LongPress),
76   mMinimumTouchesRequired(DEFAULT_TOUCHES_REQUIRED),
77   mMaximumTouchesRequired(DEFAULT_TOUCHES_REQUIRED)
78 {
79 }
80
81 LongPressGestureDetector::LongPressGestureDetector(unsigned int minTouches, unsigned int maxTouches)
82 : GestureDetector(Gesture::LongPress),
83   mMinimumTouchesRequired(minTouches),
84   mMaximumTouchesRequired(maxTouches)
85 {
86 }
87
88 LongPressGestureDetector::~LongPressGestureDetector()
89 {
90 }
91
92 void LongPressGestureDetector::SetTouchesRequired(unsigned int touches)
93 {
94   DALI_ASSERT_ALWAYS( touches > 0 && "Can only set a positive number of required touches" );
95
96   if (mMinimumTouchesRequired != touches || mMaximumTouchesRequired != touches)
97   {
98     mMinimumTouchesRequired = mMaximumTouchesRequired = touches;
99
100     if (!mAttachedActors.empty())
101     {
102       mGestureEventProcessor.GestureDetectorUpdated(this);
103     }
104   }
105 }
106
107 void LongPressGestureDetector::SetTouchesRequired(unsigned int minTouches, unsigned int maxTouches)
108 {
109   DALI_ASSERT_ALWAYS(minTouches > 0 && "Can only set a positive number of minimum touches");
110   DALI_ASSERT_ALWAYS(maxTouches > 0 && "Can only set a positive number of minimum touches");
111   DALI_ASSERT_ALWAYS(minTouches <= maxTouches && "Number of minimum touches must be less than maximum");
112
113   if (mMinimumTouchesRequired != minTouches || mMaximumTouchesRequired != maxTouches)
114   {
115     mMinimumTouchesRequired = minTouches;
116     mMaximumTouchesRequired = maxTouches;
117
118     if (!mAttachedActors.empty())
119     {
120       mGestureEventProcessor.GestureDetectorUpdated(this);
121     }
122   }
123 }
124
125 unsigned int LongPressGestureDetector::GetMinimumTouchesRequired() const
126 {
127   return mMinimumTouchesRequired;
128 }
129
130 unsigned int LongPressGestureDetector::GetMaximumTouchesRequired() const
131 {
132   return mMaximumTouchesRequired;
133 }
134
135 void LongPressGestureDetector::EmitLongPressGestureSignal(Dali::Actor pressedActor, const LongPressGesture& longPress)
136 {
137   // Guard against destruction during signal emission
138   Dali::LongPressGestureDetector handle( this );
139
140   mDetectedSignal.Emit( pressedActor, longPress );
141 }
142
143 bool LongPressGestureDetector::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
144 {
145   bool connected( true );
146   LongPressGestureDetector* gesture = static_cast< LongPressGestureDetector* >(object); // TypeRegistry guarantees that this is the correct type.
147
148   if ( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESS_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 LongPressGestureDetector::OnActorAttach(Actor& actor)
162 {
163   // Do nothing
164 }
165
166 void LongPressGestureDetector::OnActorDetach(Actor& actor)
167 {
168   // Do nothing
169 }
170
171 void LongPressGestureDetector::OnActorDestroyed(Object& object)
172 {
173   // Do nothing
174 }
175
176 uint32_t LongPressGestureDetector::GetMinimumHoldingTime() const
177 {
178   return mGestureEventProcessor.GetLongPressMinimumHoldingTime();
179 }
180
181 } // namespace Internal
182
183 } // namespace Dali