2 #include <dali-toolkit/dali-toolkit.h>
3 #include <dali/devel-api/actors/actor-devel.h>
4 #include "text-overlap-example.h"
9 using namespace Dali::Toolkit;
11 static const int NUMBER_OF_LABELS(2);
18 TextOverlapController::TextOverlapController( Application& app )
19 : mApplication( app ),
22 app.InitSignal().Connect( this, &TextOverlapController::Create );
23 app.TerminateSignal().Connect( this, &TextOverlapController::Destroy );
26 void TextOverlapController::Create( Application& app )
28 Window window = app.GetWindow();
29 window.KeyEventSignal().Connect( this, &TextOverlapController::OnKeyEvent );
31 Vector2 windowSize = window.GetSize();
33 mLabels[0] = TextLabel::New("Text Label 1");
34 mLabels[1] = TextLabel::New("Text Label 2");
36 mLabels[0].SetProperty( Dali::Actor::Property::NAME,"Label1");
37 mLabels[1].SetProperty( Dali::Actor::Property::NAME,"Label2");
39 mLabels[0].SetProperty( Dali::DevelActor::Property::SIBLING_ORDER, 1 );
40 mLabels[1].SetProperty( Dali::DevelActor::Property::SIBLING_ORDER, 2 );
42 mLabels[0].SetProperty( Control::Property::BACKGROUND, Color::RED );
43 mLabels[1].SetProperty( Control::Property::BACKGROUND, Color::YELLOW );
45 for(int i=0; i<NUMBER_OF_LABELS; ++i )
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 ));
53 window.Add( mLabels[0] );
54 window.Add( mLabels[1] );
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 );
66 Layer rootLayer = window.GetRootLayer();
67 rootLayer.SetProperty( Dali::Actor::Property::NAME,"RootLayer");
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 );
76 void TextOverlapController::OnPan( Actor actor, const PanGesture& gesture )
78 const Gesture::State state = gesture.GetState();
79 if( ! mGrabbedActor || state == PanGesture::Started )
81 const Vector2& gesturePosition = gesture.GetPosition();
82 for( int i=0; i<NUMBER_OF_LABELS; ++i )
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 )
89 mGrabbedActor = mLabels[i];
94 else if( mGrabbedActor && state == PanGesture::Continuing )
96 Vector2 windowSize = mApplication.GetWindow().GetSize();
97 Vector3 size = mGrabbedActor.GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
98 const Vector2& gesturePosition = gesture.GetPosition();
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 ));
105 mGrabbedActor.Reset();
109 void TextOverlapController::Destroy( Application& app )
111 mPanDetector.DetachAll();
112 UnparentAndReset(mLabels[0]);
113 UnparentAndReset(mLabels[1]);
114 mGrabbedActor.Reset();
117 bool TextOverlapController::OnClicked( Button button )
119 mTopmostLabel = 1-mTopmostLabel; // toggles between 0 and 1
120 mLabels[mTopmostLabel].RaiseToTop();
125 void TextOverlapController::OnKeyEvent( const KeyEvent& keyEvent )
127 if( keyEvent.GetState() == KeyEvent::DOWN &&
128 ( IsKey( keyEvent, DALI_KEY_BACK ) ||
129 IsKey( keyEvent, DALI_KEY_ESCAPE ) ) )
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);
144 int DALI_EXPORT_API main( int argc, char** argv )
147 Application app = Application::New( &argc, &argv );
148 Demo::TextOverlapController controller( app );