Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / events / wheel-event-processor.cpp
1 /*
2  * Copyright (c) 2017 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/wheel-event-processor.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/events/wheel-event.h>
23
24 #include <dali/public-api/math/vector2.h>
25 #include <dali/integration-api/events/wheel-event-integ.h>
26 #include <dali/internal/event/actors/actor-impl.h>
27 #include <dali/internal/event/common/scene-impl.h>
28 #include <dali/internal/event/events/hit-test-algorithm-impl.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace
37 {
38
39 #if defined(DEBUG_ENABLED)
40 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_WHEEL_PROCESSOR");
41 #endif
42
43 /**
44  *  Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
45  */
46 Dali::Actor EmitWheelSignals( Dali::Actor actor, const WheelEvent& event )
47 {
48   Dali::Actor consumedActor;
49
50   if ( actor )
51   {
52     Dali::Actor oldParent( actor.GetParent() );
53
54     Actor& actorImpl( GetImplementation(actor) );
55
56     bool consumed( false );
57
58     // Only do the conversion and emit the signal if the actor's wheel signal has connections.
59     if ( actorImpl.GetWheelEventRequired() )
60     {
61       // Emit the signal to the parent
62       consumed = actorImpl.EmitWheelEventSignal( event );
63     }
64
65     if ( consumed )
66     {
67       // One of this actor's listeners has consumed the event so set this actor as the consumed actor.
68       consumedActor = Dali::Actor( &actorImpl );
69     }
70     else
71     {
72       // The actor may have been removed/reparented during the signal callbacks.
73       Dali::Actor parent = actor.GetParent();
74
75       if ( parent &&
76            (parent == oldParent) )
77       {
78         // One of the actor's parents may consumed the event and they should be set as the consumed actor.
79         consumedActor = EmitWheelSignals( parent, event );
80       }
81     }
82   }
83
84   return consumedActor;
85 }
86
87 /**
88  * The function to be used in the hit-test algorithm to check whether the actor is wheelable.
89  */
90 bool IsActorWheelableFunction(Dali::Actor actor, Dali::HitTestAlgorithm::TraverseType type)
91 {
92   bool hittable = false;
93
94   switch (type)
95   {
96     case Dali::HitTestAlgorithm::CHECK_ACTOR:
97     {
98       if( GetImplementation(actor).GetWheelEventRequired() && // Does the Application or derived actor type require a wheel event?
99           GetImplementation(actor).IsHittable() )
100       {
101         hittable = true;
102       }
103       break;
104     }
105     case Dali::HitTestAlgorithm::DESCEND_ACTOR_TREE:
106     {
107       if( actor.IsVisible() ) // Actor is visible, if not visible then none of its children are visible.
108       {
109         hittable = true;
110       }
111       break;
112     }
113     default:
114     {
115       break;
116     }
117   }
118
119   return hittable;
120 };
121
122 } // unnamed namespace
123
124
125 WheelEventProcessor::WheelEventProcessor( Scene& scene )
126 : mScene( scene )
127 {
128 }
129
130 WheelEventProcessor::~WheelEventProcessor()
131 {
132 }
133
134 void WheelEventProcessor::ProcessWheelEvent( const Integration::WheelEvent& event )
135 {
136   WheelEvent wheelEvent( static_cast< WheelEvent::Type >( event.type ), event.direction, event.modifiers, event.point, event.z, event.timeStamp );
137
138   if( wheelEvent.type == WheelEvent::MOUSE_WHEEL )
139   {
140     Dali::HitTestAlgorithm::Results hitTestResults;
141     HitTestAlgorithm::HitTest( mScene.GetSize(), mScene.GetRenderTaskList(), mScene.GetLayerList(), event.point, hitTestResults, IsActorWheelableFunction );
142
143     DALI_LOG_INFO( gLogFilter, Debug::General, "  Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
144                    event.point.x, event.point.y,
145                    ( hitTestResults.actor ? reinterpret_cast< void* >( &hitTestResults.actor.GetBaseObject() ) : NULL ),
146                    ( hitTestResults.actor ? hitTestResults.actor.GetName().c_str() : "" ),
147                    hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
148
149     // Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
150     Dali::Actor consumedActor = EmitWheelSignals( hitTestResults.actor, wheelEvent );
151
152     DALI_LOG_INFO( gLogFilter, Debug::Concise, "HitActor:      (%p) %s\n", hitTestResults.actor ? reinterpret_cast< void* >( &hitTestResults.actor.GetBaseObject() ) : NULL, hitTestResults.actor ? hitTestResults.actor.GetName().c_str() : "" );
153     DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor: (%p) %s\n", consumedActor ? reinterpret_cast< void* >( &consumedActor.GetBaseObject() ) : NULL, consumedActor ? consumedActor.GetName().c_str() : "" );
154   }
155   else
156   {
157     // if CUSTOM_WHEEL, emit the wheel event signal from the scene.
158     mScene.EmitWheelEventSignal( wheelEvent );
159   }
160 }
161
162 } // namespace Internal
163
164 } // namespace Dali