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