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 Stage stage = Stage::GetCurrent();
29 stage.KeyEventSignal().Connect( this, &TextOverlapController::OnKeyEvent );
31 Vector2 stageSize = stage.GetSize();
33 mLabels[0] = TextLabel::New("Text Label 1");
34 mLabels[1] = TextLabel::New("Text Label 2");
36 mLabels[0].SetName("Label1");
37 mLabels[1].SetName("Label2");
39 mLabels[0].SetProperty( DevelActor::Property::SIBLING_ORDER, 1 );
40 mLabels[1].SetProperty( 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].SetAnchorPoint( AnchorPoint::TOP_LEFT );
49 mLabels[i].SetParentOrigin( ParentOrigin::TOP_LEFT );
50 mLabels[i].SetPosition( 0, (i*2+1) * stageSize.height * 0.25f );
53 stage.Add( mLabels[0] );
54 stage.Add( mLabels[1] );
56 mSwapButton = PushButton::New();
57 mSwapButton.SetProperty( Button::Property::LABEL, "Swap depth order");
58 mSwapButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
59 mSwapButton.SetAnchorPoint( 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 stage.Add( mSwapButton );
66 Layer rootLayer = stage.GetRootLayer();
67 rootLayer.SetName("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 if( ! mGrabbedActor || gesture.state == PanGesture::Started )
80 for( int i=0; i<NUMBER_OF_LABELS; ++i )
82 Vector3 position = mLabels[i].GetCurrentPosition();
83 Vector3 size = mLabels[i].GetCurrentSize();
84 if( gesture.position.y > position.y - size.y * 0.5f &&
85 gesture.position.y <= position.y + size.y * 0.5f )
87 mGrabbedActor = mLabels[i];
92 else if( mGrabbedActor && gesture.state == PanGesture::Continuing )
94 Vector2 stageSize = Stage::GetCurrent().GetSize();
95 Vector3 size = mGrabbedActor.GetCurrentSize();
96 float y = Clamp( gesture.position.y, size.y * 0.5f, stageSize.y - size.y*0.5f );
97 mGrabbedActor.SetPosition( 0, y );
101 mGrabbedActor.Reset();
105 void TextOverlapController::Destroy( Application& app )
107 mPanDetector.DetachAll();
108 UnparentAndReset(mLabels[0]);
109 UnparentAndReset(mLabels[1]);
110 mGrabbedActor.Reset();
113 bool TextOverlapController::OnClicked( Button button )
115 mTopmostLabel = 1-mTopmostLabel; // toggles between 0 and 1
116 mLabels[mTopmostLabel].RaiseToTop();
121 void TextOverlapController::OnKeyEvent( const KeyEvent& keyEvent )
123 if( keyEvent.state == KeyEvent::Down &&
124 ( IsKey( keyEvent, DALI_KEY_BACK ) ||
125 IsKey( keyEvent, DALI_KEY_ESCAPE ) ) )
131 Dali::Layer l = Dali::Stage::GetCurrent().GetRootLayer();
132 int so = l.GetProperty<int>(Dali::DevelActor::Property::SIBLING_ORDER);
133 l.SetProperty(Dali::DevelActor::Property::SIBLING_ORDER, so+1);
140 int DALI_EXPORT_API main( int argc, char** argv )
143 Application app = Application::New( &argc, &argv );
144 Demo::TextOverlapController controller( app );