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