[dali_1.2.61] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / image-view-svg / image-view-svg-example.cpp
1 /*
2  * Copyright (c) 2017 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.SetProperty( Toolkit::Button::Property::LABEL, "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
91     // Push button, for resetting the actor size and position
92     Toolkit::PushButton resetButton = Toolkit::PushButton::New();
93     resetButton.SetProperty( Toolkit::Button::Property::LABEL, "Reset" );
94     resetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
95     resetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
96     stage.Add( resetButton );
97     resetButton.ClickedSignal().Connect( this, &ImageSvgController::OnResetButtonClicked );
98
99     // Create and put imageViews to stage
100     for( unsigned int i=0; i<4u; i++)
101     {
102       mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
103       mSvgActor[i].SetSize( mActorSize );
104       mSvgActor[i].TranslateBy( Vector3( 0.0, stageSize.height * 0.05, 0.0f ) );
105       stage.Add( mSvgActor[i] );
106     }
107     mSvgActor[0].SetParentOrigin( ParentOrigin::CENTER );
108     mSvgActor[0].SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
109     mSvgActor[1].SetParentOrigin( ParentOrigin::CENTER );
110     mSvgActor[1].SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
111     mSvgActor[2].SetParentOrigin( ParentOrigin::CENTER );
112     mSvgActor[2].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
113     mSvgActor[3].SetParentOrigin( ParentOrigin::CENTER );
114     mSvgActor[3].SetAnchorPoint( AnchorPoint::TOP_LEFT );
115
116     // Connect pan gesture for moving the actors
117     mPanGestureDetector = PanGestureDetector::New();
118     mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
119     mPanGestureDetector.Attach( mStageBackground );
120
121     // Connect pinch gesture for resizing the actors
122     mPinchGestureDetector = PinchGestureDetector::New();
123     mPinchGestureDetector.Attach( mStageBackground);
124     mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
125
126     changeButton.RaiseToTop();
127     resetButton.RaiseToTop();
128   }
129
130   // Callback of push button, for changing image set
131   bool OnChangeButtonClicked( Toolkit::Button button )
132   {
133     mIndex = (mIndex+4) % NUM_SVG_IMAGES;
134     for( unsigned int i=0; i<4u; i++)
135     {
136       mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
137     }
138
139     return true;
140   }
141
142   // Callback of push button, for resetting image size and position
143   bool OnResetButtonClicked( Toolkit::Button button )
144   {
145     for( unsigned int i=0; i<4u; i++)
146     {
147       mSvgActor[i].SetSize(mActorSize);
148       mSvgActor[i].SetPosition( Vector3::ZERO );
149       mScale = 1.f;
150     }
151
152     return true;
153   }
154
155   // Callback of pan gesture, for moving the actors
156   void OnPanGesture( Actor actor, const PanGesture& gesture )
157   {
158     if( gesture.state == Gesture::Continuing )
159     {
160       for( unsigned int i=0; i<4u; i++)
161       {
162         mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
163       }
164     }
165   }
166
167   // Callback of pinch gesture, for resizing the actors
168   void OnPinch(Actor actor, const PinchGesture& gesture)
169   {
170     if (gesture.state == Gesture::Started)
171     {
172       mScaleAtPinchStart = mScale;
173     }
174     if( gesture.state == Gesture::Finished )
175     {
176       mScale = mScaleAtPinchStart * gesture.scale;
177       mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
178       for( unsigned int i=0; i<4u; i++)
179       {
180         mSvgActor[i].SetSize( mActorSize * mScale);
181       }
182     }
183   }
184
185   /**
186     * Main key event handler
187     */
188    void OnKeyEvent(const KeyEvent& event)
189    {
190      if(event.state == KeyEvent::Down)
191      {
192        if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
193        {
194          mApplication.Quit();
195        }
196        else
197        {
198          const char* keyName = event.keyPressedName.c_str();
199          if( strcmp(keyName, "Left") == 0 )
200          {
201            mScale /= 1.1f;
202            for( unsigned int i=0; i<4u; i++)
203            {
204              mSvgActor[i].SetSize( mActorSize * mScale);
205            }
206          }
207          else if( strcmp(keyName, "Right") == 0 )
208          {
209            if( mScale < MAX_SCALE )
210            {
211              mScale *= 1.1f;
212            }
213            for( unsigned int i=0; i<4u; i++)
214            {
215              mSvgActor[i].SetSize( mActorSize * mScale);
216            }
217          }
218        }
219      }
220    }
221
222 private:
223   Application&         mApplication;
224   Actor                mStageBackground;
225   PanGestureDetector   mPanGestureDetector;
226   PinchGestureDetector mPinchGestureDetector;
227
228   Toolkit::ImageView  mSvgActor[4];
229   Vector2             mActorSize;
230   float               mScale;
231   float               mScaleAtPinchStart;
232   unsigned int        mIndex;
233 };
234
235 int DALI_EXPORT_API main( int argc, char **argv )
236 {
237   Application application = Application::New( &argc, &argv );
238   ImageSvgController test( application );
239   application.MainLoop();
240   return 0;
241 }