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