2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/devel-api/actors/actor-devel.h>
26 const float MIN_SCALE = 0.6f;
27 const float MAX_SCALE = 6.f;
29 const char* SVG_IMAGES[] =
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"
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
44 // This example shows how to display svg images with ImageView.
46 class ImageSvgController : public ConnectionTracker
50 ImageSvgController( Application& application )
51 : mApplication( application ),
55 // Connect to the Application's Init signal
56 mApplication.InitSignal().Connect( this, &ImageSvgController::Create );
63 // The Init signal is received once (only) during the Application lifetime
64 void Create( Application& application )
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;
72 // Hide the indicator bar
73 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
75 stage.KeyEventSignal().Connect(this, &ImageSvgController::OnKeyEvent);
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.SetProperty( Actor::Property::SIZE, Vector2( stageSize.x, stageSize.y ) );
82 stage.Add(mStageBackground);
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 );
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 );
100 // Create and put imageViews to stage
101 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
103 mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
104 mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize );
105 mSvgActor[i].TranslateBy( Vector3( 0.0, stageSize.height * 0.05, 0.0f ) );
106 stage.Add( mSvgActor[i] );
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 );
117 // Connect pan gesture for moving the actors
118 mPanGestureDetector = PanGestureDetector::New();
119 mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
120 mPanGestureDetector.Attach( mStageBackground );
122 // Connect pinch gesture for resizing the actors
123 mPinchGestureDetector = PinchGestureDetector::New();
124 mPinchGestureDetector.Attach( mStageBackground);
125 mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
127 changeButton.RaiseToTop();
128 resetButton.RaiseToTop();
131 // Callback of push button, for changing image set
132 bool OnChangeButtonClicked( Toolkit::Button button )
134 mIndex = ( mIndex + NUM_IMAGES_DISPLAYED ) % NUM_SVG_IMAGES;
135 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
137 mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
143 // Callback of push button, for resetting image size and position
144 bool OnResetButtonClicked( Toolkit::Button button )
146 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED ; i++ )
148 mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize);
149 mSvgActor[i].SetProperty( Actor::Property::POSITION, Vector3::ZERO );
156 // Callback of pan gesture, for moving the actors
157 void OnPanGesture( Actor actor, const PanGesture& gesture )
159 if( gesture.state == Gesture::Continuing )
161 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
163 mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
168 // Callback of pinch gesture, for resizing the actors
169 void OnPinch(Actor actor, const PinchGesture& gesture)
171 switch( gesture.state )
173 // Only scale the image when we start or continue pinching
175 case Gesture::Started:
176 case Gesture::Continuing:
178 float scale = std::max( gesture.scale, MIN_SCALE / mScale );
179 scale = std::min( MAX_SCALE / mScale, scale );
181 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
183 mSvgActor[i].SetProperty( Actor::Property::SCALE, scale );
188 case Gesture::Finished:
190 // Resize the image when pinching is complete, this will rasterize the SVG to the new size
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++ )
197 mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize * mScale );
198 mSvgActor[i].SetProperty( Actor::Property::SCALE, 1.0f );
203 case Gesture::Cancelled:
205 case Gesture::Possible:
211 * Main key event handler
213 void OnKeyEvent(const KeyEvent& event)
215 if(event.state == KeyEvent::Down)
217 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
223 const char* keyName = event.keyPressedName.c_str();
224 if( strcmp(keyName, "Left") == 0 )
226 if( mScale > MIN_SCALE )
230 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
232 mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize * mScale );
235 else if( strcmp(keyName, "Right") == 0 )
237 if( mScale < MAX_SCALE )
241 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
243 mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize * mScale );
251 Application& mApplication;
252 Actor mStageBackground;
253 PanGestureDetector mPanGestureDetector;
254 PinchGestureDetector mPinchGestureDetector;
256 Toolkit::ImageView mSvgActor[4];
262 int DALI_EXPORT_API main( int argc, char **argv )
264 Application application = Application::New( &argc, &argv );
265 ImageSvgController test( application );
266 application.MainLoop();