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