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