[dali_2.3.19] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / drag-drop-detector / drag-and-drop-detector-impl.h
1 #ifndef DALI_INTERNAL_DRAG_AND_DROP_DETECTOR_H
2 #define DALI_INTERNAL_DRAG_AND_DROP_DETECTOR_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23 #include <string>
24 #include <vector>
25
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/public-api/object/base-object.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/devel-api/drag-drop-detector/drag-and-drop-detector.h>
31 #include <dali/public-api/events/pan-gesture-detector.h>
32 #include <dali/public-api/events/pan-gesture.h>
33
34 namespace Dali
35 {
36 namespace Toolkit
37 {
38 namespace Internal
39 {
40 using DragAndDropDetectorPtr = IntrusivePtr<DragAndDropDetector>;
41
42 /**
43  * This class listens to Drag & Drop events.
44  */
45 class DragAndDropDetector : public Dali::BaseObject, public ConnectionTracker
46 {
47 public:
48   using DragAndDropSignal = Dali::Toolkit::DragAndDropDetector::DragAndDropSignal;
49
50   // Creation
51
52   /**
53    * @copydoc Toolkit::DragAndDropDetector::New()
54    */
55   static Dali::Toolkit::DragAndDropDetector New();
56
57   // Public API
58
59   /**
60    * @copydoc Dali::DragAndDropDetector::GetContent() const
61    */
62   const std::string& GetContent() const;
63
64   /**
65    * @copydoc Dali::DragAndDropDetector::GetCurrentScreenPosition() const
66    */
67   const Vector2& GetCurrentScreenPosition() const;
68
69   /**
70    * Attaches control to DragAndDropDetector.
71    * @param[in] control  control that will be attached to DragAndDropDetector.
72    */
73   void Attach(Dali::Toolkit::Control& control);
74
75   /**
76    * Detaches control to DragAndDropDetector.
77    * @param[in] control  control that will be Detached from DragAndDropDetector.
78    */
79   void Detach(Dali::Toolkit::Control& control);
80
81   /**
82    * Detaches all control attached to DragAndDropDetector.
83    */
84   void DetachAll();
85
86   /**
87    * Returns the number of controls attached to the DragAndDropDetector.
88    */
89   uint32_t GetAttachedControlCount() const;
90
91   /**
92    * Returns a control by index. An empty handle if the index is not valid.
93    */
94   Dali::Toolkit::Control GetAttachedControl(uint32_t index) const;
95
96   /**
97    * Sets the dragged content.
98    * @param[in] content  A string that represents the content that has been dropped.
99    */
100   void SetContent(const std::string& content);
101
102   /**
103    * Clears the stored content.
104    */
105   void ClearContent();
106
107   /**
108    * Sets the position the drop occurred.
109    */
110   void SetPosition(const Vector2& screenPosition);
111
112   /**
113    * Called when a draggable object start drag.
114    */
115   void EmitStartedSignal(Dali::Toolkit::Control& control);
116
117   /**
118    * Called when a draggable object enters other object.
119    */
120   void EmitEnteredSignal(Dali::Toolkit::Control& control);
121
122   /**
123    * Called when a draggable object leaves other object.
124    */
125   void EmitExitedSignal(Dali::Toolkit::Control& control);
126
127   /**
128    * Called when a draggable object leaves other object.
129    */
130   void EmitMovedSignal(Dali::Toolkit::Control& control);
131
132   /**
133    * Is called when a drop actually occurs.
134    */
135   void EmitDroppedSignal(Dali::Toolkit::Control& control);
136
137   /**
138    * Called when a draggable object drag ended.
139    */
140   void EmitEndedSignal(Dali::Toolkit::Control& control);
141
142 public: // Signals
143   /**
144    * @copydoc Dali::Toolkit::DragAndDropDetector::StartedSignal
145    */
146   DragAndDropSignal& StartedSignal()
147   {
148     return mStartedSignal;
149   }
150
151   /**
152    * @copydoc Dali::Toolkit::DragAndDropDetector::EnteredSignal
153    */
154   DragAndDropSignal& EnteredSignal()
155   {
156     return mEnteredSignal;
157   }
158
159   /**
160    * @copydoc Dali::Toolkit::DragAndDropDetector::ExitedSignal
161    */
162   DragAndDropSignal& ExitedSignal()
163   {
164     return mExitedSignal;
165   }
166
167   /**
168    * @copydoc Dali::Toolkit::DragAndDropDetector::MovedSignal
169    */
170   DragAndDropSignal& MovedSignal()
171   {
172     return mMovedSignal;
173   }
174
175   /**
176    * @copydoc Dali::Toolkit::DragAndDropDetector::DroppedSignal
177    */
178   DragAndDropSignal& DroppedSignal()
179   {
180     return mDroppedSignal;
181   }
182
183   /**
184    * @copydoc Dali::Toolkit::DragAndDropDetector::DroppedSignal
185    */
186   DragAndDropSignal& EndedSignal()
187   {
188     return mEndedSignal;
189   }
190
191 public:
192   bool OnDrag(Dali::Actor actor, const Dali::TouchEvent& data);
193   void OnPan(Dali::Actor actor, const PanGesture& gesture);
194
195 private:
196   // Construction & Destruction
197
198   /**
199    * Constructor.
200    */
201   DragAndDropDetector();
202
203   /**
204    * Destructor.
205    */
206   virtual ~DragAndDropDetector();
207
208   // Undefined
209   DragAndDropDetector(const DragAndDropDetector&) = delete;
210   DragAndDropDetector& operator                   =(DragAndDropDetector&);
211
212 private:
213   std::string mContent; ///< The current Drag & drop content.
214
215   DragAndDropSignal mStartedSignal;
216   DragAndDropSignal mEnteredSignal;
217   DragAndDropSignal mExitedSignal;
218   DragAndDropSignal mMovedSignal;
219   DragAndDropSignal mDroppedSignal;
220   DragAndDropSignal mEndedSignal;
221
222   std::vector<Dali::Toolkit::Control> mControls;           //controls attached by Attach interface for drag&drop
223   Dali::Toolkit::Control              mDragControl;        //the current drag control
224   Dali::Toolkit::Control              mShadowControl;      //a shadow control for indicating where the control is, same size as the dragged control
225   std::vector<uint32_t>               mFirstEnter;         //control id indicating if the cursor is enter
226   Dali::PanGestureDetector            mPanGestureDetector; //pangesture for calculating the shadow actor position
227
228   Vector2 mLocalPosition;
229   Vector2 mDragLocalPosition;
230   Vector2 mScreenPosition; ///< The screen position of the drop location.
231
232   bool mPointDown; //bool flag to indicate if PointState::DOWN have been processed
233 };
234
235 } // namespace Internal
236
237 // Helpers for public-api forwarding methods
238
239 inline Internal::DragAndDropDetector& GetImplementation(Dali::Toolkit::DragAndDropDetector& detector)
240 {
241   DALI_ASSERT_ALWAYS(detector && "DragAndDropDetector handle is empty");
242
243   BaseObject& handle = detector.GetBaseObject();
244
245   return static_cast<Internal::DragAndDropDetector&>(handle);
246 }
247
248 inline const Internal::DragAndDropDetector& GetImplementation(const Dali::Toolkit::DragAndDropDetector& detector)
249 {
250   DALI_ASSERT_ALWAYS(detector && "DragAndDropDetector handle is empty");
251
252   const BaseObject& handle = detector.GetBaseObject();
253
254   return static_cast<const Internal::DragAndDropDetector&>(handle);
255 }
256
257 } // namespace Toolkit
258
259 } // namespace Dali
260
261 #endif // DALI_INTERNAL_DRAG_AND_DROP_DETECTOR_H