Change sibbling order of buttons in svg example
[platform/core/uifw/dali-demo.git] / examples / image-view-svg / image-view-svg-example.cpp
1 /*
2  * Copyright (c) 2016 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 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/devel-api/actors/actor-devel.h>
20 #include <string.h>
21
22 using namespace Dali;
23
24 namespace
25 {
26 const float MAX_SCALE = 6.f;
27
28 const char* SVG_IMAGES[] =
29 {
30     DEMO_IMAGE_DIR "Camera.svg",
31     DEMO_IMAGE_DIR "Contacts.svg",
32     DEMO_IMAGE_DIR "Mail.svg",
33     DEMO_IMAGE_DIR "Message.svg",
34     DEMO_IMAGE_DIR "Phone.svg",
35     DEMO_IMAGE_DIR "Settings.svg",
36     DEMO_IMAGE_DIR "World.svg",
37     DEMO_IMAGE_DIR "Kid1.svg"
38 };
39 const unsigned int NUM_SVG_IMAGES( sizeof( SVG_IMAGES ) / sizeof( SVG_IMAGES[0] ) );
40 }
41
42 // This example shows how to display svg images with ImageView
43 //
44 class ImageSvgController : public ConnectionTracker
45 {
46 public:
47
48   ImageSvgController( Application& application )
49   : mApplication( application ),
50     mScale( 1.f ),
51     mScaleAtPinchStart( 1.0f ),
52     mIndex( 0 )
53   {
54     // Connect to the Application's Init signal
55     mApplication.InitSignal().Connect( this, &ImageSvgController::Create );
56   }
57
58   ~ImageSvgController()
59   {
60   }
61
62   // The Init signal is received once (only) during the Application lifetime
63   void Create( Application& application )
64   {
65     // Get a handle to the stage
66     Stage stage = Stage::GetCurrent();
67     stage.SetBackgroundColor( Color::WHITE );
68     Vector2 stageSize = stage.GetSize();
69     mActorSize = stageSize/2.f;
70
71     // Hide the indicator bar
72     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
73
74     stage.KeyEventSignal().Connect(this, &ImageSvgController::OnKeyEvent);
75
76     // Background, for receiving gestures
77     mStageBackground = Actor::New();
78     mStageBackground.SetAnchorPoint( AnchorPoint::TOP_CENTER );
79     mStageBackground.SetParentOrigin( ParentOrigin::TOP_CENTER );
80     mStageBackground.SetSize( stageSize.x, stageSize.y );
81     stage.Add(mStageBackground);
82
83     // Push button,  for changing the image set for displaying
84     Toolkit::PushButton changeButton = Toolkit::PushButton::New();
85     changeButton.SetLabelText( "Next" );
86     changeButton.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
87     changeButton.SetParentOrigin( ParentOrigin::TOP_RIGHT );
88     stage.Add( changeButton );
89     changeButton.ClickedSignal().Connect( this, &ImageSvgController::OnChangeButtonClicked );
90     changeButton.SetProperty( DevelActor::Property::SIBLING_ORDER, 1 );
91
92     // Push button, for resetting the actor size and position
93     Toolkit::PushButton resetButton = Toolkit::PushButton::New();
94     resetButton.SetLabelText( "Reset" );
95     resetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
96     resetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
97     stage.Add( resetButton );
98     resetButton.ClickedSignal().Connect( this, &ImageSvgController::OnResetButtonClicked );
99     resetButton.SetProperty( DevelActor::Property::SIBLING_ORDER, 1 );
100
101     // Create and put imageViews to stage
102     for( unsigned int i=0; i<4u; i++)
103     {
104       mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
105       mSvgActor[i].SetSize( mActorSize );
106       mSvgActor[i].TranslateBy( Vector3( 0.0, stageSize.height * 0.05, 0.0f ) );
107       stage.Add( mSvgActor[i] );
108     }
109     mSvgActor[0].SetParentOrigin( ParentOrigin::CENTER );
110     mSvgActor[0].SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
111     mSvgActor[1].SetParentOrigin( ParentOrigin::CENTER );
112     mSvgActor[1].SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
113     mSvgActor[2].SetParentOrigin( ParentOrigin::CENTER );
114     mSvgActor[2].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
115     mSvgActor[3].SetParentOrigin( ParentOrigin::CENTER );
116     mSvgActor[3].SetAnchorPoint( AnchorPoint::TOP_LEFT );
117
118     // Connect pan gesture for moving the actors
119     mPanGestureDetector = PanGestureDetector::New();
120     mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
121     mPanGestureDetector.Attach( mStageBackground );
122
123     // Connect pinch gesture for resizing the actors
124     mPinchGestureDetector = PinchGestureDetector::New();
125     mPinchGestureDetector.Attach( mStageBackground);
126     mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
127   }
128
129   // Callback of push button, for changing image set
130   bool OnChangeButtonClicked( Toolkit::Button button )
131   {
132     mIndex = (mIndex+4) % NUM_SVG_IMAGES;
133     for( unsigned int i=0; i<4u; i++)
134     {
135       mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
136     }
137
138     return true;
139   }
140
141   // Callback of push button, for resetting image size and position
142   bool OnResetButtonClicked( Toolkit::Button button )
143   {
144     for( unsigned int i=0; i<4u; i++)
145     {
146       mSvgActor[i].SetSize(mActorSize);
147       mSvgActor[i].SetPosition( Vector3::ZERO );
148       mScale = 1.f;
149     }
150
151     return true;
152   }
153
154   // Callback of pan gesture, for moving the actors
155   void OnPanGesture( Actor actor, const PanGesture& gesture )
156   {
157     if( gesture.state == Gesture::Continuing )
158     {
159       for( unsigned int i=0; i<4u; i++)
160       {
161         mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
162       }
163     }
164   }
165
166   // Callback of pinch gesture, for resizing the actors
167   void OnPinch(Actor actor, const PinchGesture& gesture)
168   {
169     if (gesture.state == Gesture::Started)
170     {
171       mScaleAtPinchStart = mScale;
172     }
173     if( gesture.state == Gesture::Finished )
174     {
175       mScale = mScaleAtPinchStart * gesture.scale;
176       mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
177       for( unsigned int i=0; i<4u; i++)
178       {
179         mSvgActor[i].SetSize( mActorSize * mScale);
180       }
181     }
182   }
183
184   /**
185     * Main key event handler
186     */
187    void OnKeyEvent(const KeyEvent& event)
188    {
189      if(event.state == KeyEvent::Down)
190      {
191        if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
192        {
193          mApplication.Quit();
194        }
195        else
196        {
197          const char* keyName = event.keyPressedName.c_str();
198          if( strcmp(keyName, "Left") == 0 )
199          {
200            mScale /= 1.1f;
201            for( unsigned int i=0; i<4u; i++)
202            {
203              mSvgActor[i].SetSize( mActorSize * mScale);
204            }
205          }
206          else if( strcmp(keyName, "Right") == 0 )
207          {
208            if( mScale < MAX_SCALE )
209            {
210              mScale *= 1.1f;
211            }
212            for( unsigned int i=0; i<4u; i++)
213            {
214              mSvgActor[i].SetSize( mActorSize * mScale);
215            }
216          }
217        }
218      }
219    }
220
221 private:
222   Application&         mApplication;
223   Actor                mStageBackground;
224   PanGestureDetector   mPanGestureDetector;
225   PinchGestureDetector mPinchGestureDetector;
226
227   Toolkit::ImageView  mSvgActor[4];
228   Vector2             mActorSize;
229   float               mScale;
230   float               mScaleAtPinchStart;
231   unsigned int        mIndex;
232 };
233
234 void RunTest( Application& application )
235 {
236   ImageSvgController test( application );
237
238   application.MainLoop();
239 }
240
241 // Entry point for Linux & Tizen applications
242 //
243 int DALI_EXPORT_API main( int argc, char **argv )
244 {
245   Application application = Application::New( &argc, &argv );
246
247   RunTest( application );
248
249   return 0;
250 }