Refactoring Gestures Class
[platform/core/uifw/dali-demo.git] / examples / text-overlap / text-overlap-example.cpp
1
2 #include <dali-toolkit/dali-toolkit.h>
3 #include <dali/devel-api/actors/actor-devel.h>
4 #include "text-overlap-example.h"
5
6 #include <iostream>
7
8 using namespace Dali;
9 using namespace Dali::Toolkit;
10
11 static const int NUMBER_OF_LABELS(2);
12
13
14
15 namespace Demo
16 {
17
18 TextOverlapController::TextOverlapController( Application& app )
19 : mApplication( app ),
20   mTopmostLabel( 1 )
21 {
22   app.InitSignal().Connect( this, &TextOverlapController::Create );
23   app.TerminateSignal().Connect( this, &TextOverlapController::Destroy );
24 }
25
26 void TextOverlapController::Create( Application& app )
27 {
28   Window window = app.GetWindow();
29   window.KeyEventSignal().Connect( this, &TextOverlapController::OnKeyEvent );
30
31   Vector2 windowSize = window.GetSize();
32
33   mLabels[0] = TextLabel::New("Text Label 1");
34   mLabels[1] = TextLabel::New("Text Label 2");
35
36   mLabels[0].SetProperty( Dali::Actor::Property::NAME,"Label1");
37   mLabels[1].SetProperty( Dali::Actor::Property::NAME,"Label2");
38
39   mLabels[0].SetProperty( Dali::DevelActor::Property::SIBLING_ORDER, 1 );
40   mLabels[1].SetProperty( Dali::DevelActor::Property::SIBLING_ORDER, 2 );
41
42   mLabels[0].SetProperty( Control::Property::BACKGROUND, Color::RED );
43   mLabels[1].SetProperty( Control::Property::BACKGROUND, Color::YELLOW );
44
45   for(int i=0; i<NUMBER_OF_LABELS; ++i )
46   {
47     mLabels[i].SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
48     mLabels[i].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
49     mLabels[i].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
50     mLabels[i].SetProperty( Actor::Property::POSITION, Vector2( 0, (i*2+1) * windowSize.height * 0.25f ));
51   }
52
53   window.Add( mLabels[0] );
54   window.Add( mLabels[1] );
55
56   mSwapButton = PushButton::New();
57   mSwapButton.SetProperty( Button::Property::LABEL, "Swap depth order");
58   mSwapButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
59   mSwapButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
60   mSwapButton.ClickedSignal().Connect( this, &TextOverlapController::OnClicked );
61   mSwapButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
62   mSwapButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
63   window.Add( mSwapButton );
64
65
66   Layer rootLayer = window.GetRootLayer();
67   rootLayer.SetProperty( Dali::Actor::Property::NAME,"RootLayer");
68
69   mPanDetector = PanGestureDetector::New();
70   mPanDetector.Attach( rootLayer );
71   mPanDetector.AddAngle( Radian(-0.5f * Math::PI ));
72   mPanDetector.AddAngle( Radian( 0.5f * Math::PI ));
73   mPanDetector.DetectedSignal().Connect( this, &TextOverlapController::OnPan );
74 }
75
76 void TextOverlapController::OnPan( Actor actor, const PanGesture& gesture )
77 {
78   const Gesture::State state = gesture.GetState();
79   if( ! mGrabbedActor || state == PanGesture::Started )
80   {
81     const Vector2& gesturePosition = gesture.GetPosition();
82     for( int i=0; i<NUMBER_OF_LABELS; ++i )
83     {
84       Vector3 position = mLabels[i].GetCurrentProperty< Vector3 >( Actor::Property::POSITION );
85       Vector3 size = mLabels[i].GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
86       if( gesturePosition.y > position.y - size.y * 0.5f &&
87           gesturePosition.y <= position.y + size.y * 0.5f )
88       {
89         mGrabbedActor = mLabels[i];
90         break;
91       }
92     }
93   }
94   else if( mGrabbedActor && state == PanGesture::Continuing )
95   {
96     Vector2 windowSize = mApplication.GetWindow().GetSize();
97     Vector3 size = mGrabbedActor.GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
98     const Vector2& gesturePosition = gesture.GetPosition();
99
100     float y = Clamp( gesturePosition.y, size.y * 0.5f, windowSize.y - size.y*0.5f );
101     mGrabbedActor.SetProperty( Actor::Property::POSITION, Vector2( 0, y ));
102   }
103   else
104   {
105     mGrabbedActor.Reset();
106   }
107 }
108
109 void TextOverlapController::Destroy( Application& app )
110 {
111   mPanDetector.DetachAll();
112   UnparentAndReset(mLabels[0]);
113   UnparentAndReset(mLabels[1]);
114   mGrabbedActor.Reset();
115 }
116
117 bool TextOverlapController::OnClicked( Button button )
118 {
119   mTopmostLabel = 1-mTopmostLabel; // toggles between 0 and 1
120   mLabels[mTopmostLabel].RaiseToTop();
121   return false;
122 }
123
124
125 void TextOverlapController::OnKeyEvent( const KeyEvent& keyEvent )
126 {
127   if( keyEvent.GetState() == KeyEvent::DOWN &&
128       ( IsKey( keyEvent, DALI_KEY_BACK ) ||
129         IsKey( keyEvent, DALI_KEY_ESCAPE ) ) )
130   {
131     mApplication.Quit();
132   }
133   else
134   {
135     Dali::Layer l = mApplication.GetWindow().GetRootLayer();
136     int so = l.GetProperty<int>(Dali::DevelActor::Property::SIBLING_ORDER);
137     l.SetProperty(Dali::DevelActor::Property::SIBLING_ORDER, so+1);
138   }
139 }
140
141
142 } // namespace Demo
143
144 int DALI_EXPORT_API main( int argc, char** argv )
145 {
146   {
147     Application app = Application::New( &argc, &argv );
148     Demo::TextOverlapController controller( app );
149     app.MainLoop();
150   }
151   exit( 0 );
152 }