[Tizen] Add InterceptKeyEvent
[platform/core/uifw/dali-core.git] / dali / integration-api / scene.h
1 #ifndef DALI_SCENE_H
2 #define DALI_SCENE_H
3
4 /*
5  * Copyright (c) 2022 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 <memory>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/vector-wrapper.h>
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/public-api/math/vector4.h>
28 #include <dali/public-api/object/handle.h>
29
30 namespace Dali
31 {
32 class Actor;
33 class KeyEvent;
34 class Layer;
35 class RenderTaskList;
36 class TouchEvent;
37 class WheelEvent;
38
39 namespace Internal DALI_INTERNAL
40 {
41 class Scene;
42 }
43
44 namespace Graphics
45 {
46 class RenderTarget;
47 }
48
49 namespace Integration
50 {
51 struct Event;
52
53 /**
54  * @brief
55  *
56  * Scene creates a "world" that can be bound to a surface for rendering.
57  *
58  */
59 class DALI_CORE_API Scene : public BaseHandle
60 {
61 public:
62   using EventProcessingFinishedSignalType = Signal<void()>;                        ///< Event Processing finished signal type
63   using KeyEventSignalType                = Signal<void(const Dali::KeyEvent&)>;   ///< Key event signal type
64   using KeyEventGeneratedSignalType       = Signal<bool(const Dali::KeyEvent&)>;   ///< key event generated signal type
65   using TouchEventSignalType              = Signal<void(const Dali::TouchEvent&)>; ///< Touch signal type
66   using WheelEventSignalType              = Signal<void(const Dali::WheelEvent&)>; ///< WheelEvent signal type
67   using WheelEventGeneratedSignalType     = Signal<bool(const Dali::WheelEvent&)>; ///< WheelEvent generated signal type
68
69   using FrameCallbackContainer = std::vector<std::pair<std::unique_ptr<CallbackBase>, int32_t> >;
70
71   /**
72    * @brief Create an initialized Scene handle.
73    *
74    * @param[in] size The size of the set surface for this scene
75    * @param[in] orientation The rotated angle of the set surface for this scene
76    *
77    * @return a handle to a newly allocated Dali resource.
78    */
79   static Scene New(Size size, int32_t orientation = 0);
80
81   /**
82    * @brief Downcast an Object handle to Scene handle.
83    *
84    * If handle points to a Scene object the downcast produces
85    * valid handle. If not the returned handle is left uninitialized.
86    * @param[in] handle to An object
87    * @return handle to a Scene object or an uninitialized handle
88    */
89   static Scene DownCast(BaseHandle handle);
90
91   /**
92    * @brief Create an uninitialized Scene handle.
93    *
94    * This can be initialized with Scene::New(). Calling member
95    * functions with an uninitialized Dali::Object is not allowed.
96    */
97   Scene();
98
99   /**
100    * @brief Destructor
101    *
102    * This is non-virtual since derived Handle types must not contain data or virtual methods.
103    */
104   ~Scene();
105
106   /**
107    * @brief This copy constructor is required for (smart) pointer semantics.
108    *
109    * @param [in] handle A reference to the copied handle
110    */
111   Scene(const Scene& handle);
112
113   /**
114    * @brief This assignment operator is required for (smart) pointer semantics.
115    *
116    * @param [in] rhs  A reference to the copied handle
117    * @return A reference to this
118    */
119   Scene& operator=(const Scene& rhs);
120
121   /**
122    * @brief Adds a child Actor to the Scene.
123    *
124    * The child will be referenced.
125    * @param[in] actor The child
126    * @pre The actor has been initialized.
127    * @pre The actor does not have a parent.
128    */
129   void Add(Actor actor);
130
131   /**
132    * @brief Removes a child Actor from the Scene.
133    *
134    * The child will be unreferenced.
135    * @param[in] actor The child
136    * @pre The actor has been added to the stage.
137    */
138   void Remove(Actor actor);
139
140   /**
141    * @brief Returns the size of the Scene in pixels as a Vector.
142    *
143    * The x component will be the width of the Scene in pixels.
144    * The y component will be the height of the Scene in pixels.
145    *
146    * @return The size of the Scene as a Vector
147    */
148   Size GetSize() const;
149
150   /**
151    * Sets horizontal and vertical pixels per inch value that is used by the display
152    * @param[in] dpi Horizontal and vertical dpi value
153    */
154   void SetDpi(Vector2 dpi);
155
156   /**
157    * @brief Retrieves the DPI of the display device to which the scene is connected.
158    *
159    * @return The horizontal and vertical DPI
160    */
161   Vector2 GetDpi() const;
162
163   /**
164    * @brief Sets the background color.
165    *
166    * @param[in] color The new background color
167    */
168   void SetBackgroundColor(const Vector4& color);
169
170   /**
171    * @brief Gets the background color of the render surface.
172    *
173    * @return The background color
174    */
175   Vector4 GetBackgroundColor() const;
176
177   /**
178    * @brief Retrieves the list of render-tasks.
179    *
180    * @return A valid handle to a RenderTaskList
181    */
182   Dali::RenderTaskList GetRenderTaskList() const;
183
184   /**
185    * @brief Returns the Scene's Root Layer.
186    *
187    * @return The root layer
188    */
189   Layer GetRootLayer() const;
190
191   /**
192    * @brief Queries the number of on-stage layers.
193    *
194    * Note that a default layer is always provided (count >= 1).
195    * @return The number of layers
196    */
197   uint32_t GetLayerCount() const;
198
199   /**
200    * @brief Retrieves the layer at a specified depth.
201    *
202    * @param[in] depth The depth
203    * @return The layer found at the given depth
204    * @pre Depth is less than layer count; see GetLayerCount().
205    */
206   Layer GetLayer(uint32_t depth) const;
207
208   /**
209    * @brief Informs the scene that the set surface has been resized.
210    *
211    * @param[in] width The new width of the set surface
212    * @param[in] height The new height of the set surface
213    */
214   void SurfaceResized(float width, float height);
215
216   /**
217    * @brief Informs the scene that the surface has been replaced.
218    */
219   void SurfaceReplaced();
220
221   /**
222    * @brief Discards this Scene from the Core.
223    */
224   void Discard();
225
226   /**
227    * @brief Sets the render target for the surface.
228    *
229    * @param[in] renderTarget The render target for the surface
230    */
231   void SetSurfaceRenderTarget(Graphics::RenderTarget* renderTarget);
232
233   /**
234    * @brief Retrieve the Scene that the given actor belongs to.
235    * @return The Scene.
236    */
237   static Integration::Scene Get(Actor actor);
238
239   /**
240    * This function is called when an event is queued.
241    * @param[in] event A event to queue.
242    */
243   void QueueEvent(const Integration::Event& event);
244
245   /**
246    * This function is called by Core when events are processed.
247    */
248   void ProcessEvents();
249
250   /**
251    * @brief Adds a callback that is called when the frame rendering is done by the graphics driver.
252    *
253    * @param[in] callback The function to call
254    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
255    *
256    * @note A callback of the following type may be used:
257    * @code
258    *   void MyFunction( int32_t frameId );
259    * @endcode
260    * This callback will be deleted once it is called.
261    *
262    * @note Ownership of the callback is passed onto this class.
263    */
264   void AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId);
265
266   /**
267    * @brief Adds a callback that is called when the frame is displayed on the display.
268    *
269    * @param[in] callback The function to call
270    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
271    *
272    * @note A callback of the following type may be used:
273    * @code
274    *   void MyFunction( int32_t frameId );
275    * @endcode
276    * This callback will be deleted once it is called.
277    *
278    * @note Ownership of the callback is passed onto this class.
279    */
280   void AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId);
281
282   /**
283    * @brief Gets the callback list that is called when the frame rendering is done by the graphics driver.
284    *
285    * @param[out] callbacks The callback list
286    *
287    * @note This is called in the update thread.
288    */
289   void GetFrameRenderedCallback(FrameCallbackContainer& callbacks);
290
291   /**
292    * @brief Gets the callback list that is called when the frame is displayed on the display.
293    *
294    * @param[out] callbacks The callback list
295    *
296    * @note This is called in the update thread.
297    */
298   void GetFramePresentedCallback(FrameCallbackContainer& callbacks);
299
300   /**
301    * @brief Informs the scene that the set surface has been rotated.
302    *
303    * @param[in] width The width of rotated surface
304    * @param[in] height The height of rotated surface
305    * @param[in] orientation The orientation of rotated surface
306    */
307   void SurfaceRotated(float width, float height, int32_t orientation);
308
309   /**
310    * @brief Gets the current surface orientation. It gets the value from the scene object.
311    *
312    * @return The current surface orientation.
313    */
314   int32_t GetCurrentSurfaceOrientation() const;
315
316   /**
317    * @brief Gets the current surface rectangle. It gets the value from the scene object.
318    *
319    * @return The current surface rectangle
320    */
321   const Rect<int32_t>& GetCurrentSurfaceRect() const;
322
323   /**
324    * Query wheter the surface rect is changed or not.
325    * @return true if the surface rect is changed.
326    */
327   bool IsSurfaceRectChanged() const;
328
329   /**
330    * @brief Send message to acknowledge for completing window rotation with current window orientation.
331    *
332    * If this function is called, the message is sent to render thread, then mSurfaceRectChanged in scene-graph-scene is set with true.
333    * After that, render thread checks whether window rotation event is received, mSurfaceRectChanged and the neccessary flag are set.
334    * If they are all true, rotation done function is called to complete window rotation.
335    */
336   void SetRotationCompletedAcknowledgement();
337
338   /**
339    * @brief Query wheter is set to acknowledge for completing surface rotation.
340    * @return true it should be acknowledged.
341    *
342    * If SetRotationCompletedAcknowledgement() is called and the related message is received to scene-graph-scene,
343    * then mSurfaceRectChanged in scene-graph-scene is set with true.
344    *
345    * When this function is called, the mSurfaceRectChanged in scene-graph-scene is return.
346    * Then, the flag will be reset.
347    *
348    * @note This function should be not called the application's main thread.
349    * Because this function should be called in windwow surface's postrender() function to complete window rotation manually.
350    */
351   bool IsRotationCompletedAcknowledgementSet() const;
352
353   /**
354    * @brief This signal is emitted just after the event processing is finished.
355    *
356    * @return The signal to connect to
357    */
358   EventProcessingFinishedSignalType& EventProcessingFinishedSignal();
359
360   /**
361    * @brief This signal is emitted when key event is received.
362    *
363    * A callback of the following type may be connected:
364    * @code
365    *   void YourCallbackName(const KeyEvent& event);
366    * @endcode
367    * @return The signal to connect to
368    */
369   KeyEventSignalType& KeyEventSignal();
370
371   /**
372    * @brief The user would connect to this signal to get a KeyEvent when KeyEvent is generated.
373    *
374    * If the control already consumed key event, KeyEventProcessor do not need to Emit keyEvent.
375    * Therefore, KeyinputManager first checks whether KeyEvent is generated as KeyEventGeneratedSignal.
376    * After that keyEventProcessor must invoke KeyEvent only if KeyEventGeneratedSignal () is not consumed.
377    *
378    * A callback of the following type may be connected:
379    * @code
380    *   bool YourCallbackName(const KeyEvent& event);
381    * @endcode
382    *
383    * @return The return is true if KeyEvent is consumed, otherwise false.
384    */
385   KeyEventGeneratedSignalType& KeyEventGeneratedSignal();
386
387   /**
388    * @brief The user would connect to this signal to intercept a KeyEvent at window.
389    *
390    * Intercepts KeyEvents in the window before dispatching KeyEvents to the control.
391    * If a KeyEvent is consumed, no KeyEvent is delivered to the control.
392    *
393    * A callback of the following type may be connected:
394    * @code
395    *   bool YourCallbackName(const KeyEvent& event);
396    * @endcode
397    *
398    * @return The return is true if KeyEvent is consumed, otherwise false.
399    */
400   KeyEventGeneratedSignalType& InterceptKeyEventSignal();
401
402   /**
403    * @brief This signal is emitted when the screen is touched and when the touch ends
404    * (i.e. the down & up touch events only).
405    *
406    * If there are multiple touch points, then this will be emitted when the first touch occurs and
407    * then when the last finger is lifted.
408    * An interrupted event will also be emitted (if it occurs).
409    * A callback of the following type may be connected:
410    * @code
411    *   void YourCallbackName( TouchEvent event );
412    * @endcode
413    *
414    * @return The touch signal to connect to
415    * @note Motion events are not emitted.
416    */
417   TouchEventSignalType& TouchedSignal();
418
419   /**
420    * @brief This signal is emitted when wheel event is received.
421    *
422    * A callback of the following type may be connected:
423    * @code
424    *   void YourCallbackName(const WheelEvent& event);
425    * @endcode
426    * @return The signal to connect to
427    */
428   WheelEventSignalType& WheelEventSignal();
429
430   /**
431    * @brief When a custom wheel event occurs, it need to process the focused actor first.
432    *
433    * Therefore, KeyboardFocusManager first checks whether WheelEvent is generated as WheelEventGeneratedSignal.
434    * After that wheelEventProcessor must invoke WheelEvent only if wheelEventGeneratedSignal () is not consumed.
435    *
436    * This is only valid for custom wheel events.
437    *
438    * A callback of the following type may be connected:
439    * @code
440    *   bool YourCallbackName(const WheelEvent& event);
441    * @endcode
442    *
443    * @return The return is true if WheelEvent is consumed, otherwise false.
444    */
445   WheelEventGeneratedSignalType& WheelEventGeneratedSignal();
446
447 public: // Not intended for application developers
448   /**
449    * @brief This constructor is used by Dali::New() methods.
450    *
451    * @param[in] scene A pointer to an internal Scene resource
452    */
453   explicit DALI_INTERNAL Scene(Internal::Scene* scene);
454 };
455
456 } // namespace Integration
457
458 } // namespace Dali
459
460 #endif // DALI_SCENE_H