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