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