2 * Copyright (c) 2017 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 MAX_SCALE = 6.f;
28 const char* SVG_IMAGES[] =
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"
39 const unsigned int NUM_SVG_IMAGES( sizeof( SVG_IMAGES ) / sizeof( SVG_IMAGES[0] ) );
42 // This example shows how to display svg images with ImageView
44 class ImageSvgController : public ConnectionTracker
48 ImageSvgController( Application& application )
49 : mApplication( application ),
51 mScaleAtPinchStart( 1.0f ),
54 // Connect to the Application's Init signal
55 mApplication.InitSignal().Connect( this, &ImageSvgController::Create );
62 // The Init signal is received once (only) during the Application lifetime
63 void Create( Application& application )
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;
71 // Hide the indicator bar
72 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
74 stage.KeyEventSignal().Connect(this, &ImageSvgController::OnKeyEvent);
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);
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 );
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 );
99 // Create and put imageViews to stage
100 for( unsigned int i=0; i<4u; i++)
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] );
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 );
116 // Connect pan gesture for moving the actors
117 mPanGestureDetector = PanGestureDetector::New();
118 mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
119 mPanGestureDetector.Attach( mStageBackground );
121 // Connect pinch gesture for resizing the actors
122 mPinchGestureDetector = PinchGestureDetector::New();
123 mPinchGestureDetector.Attach( mStageBackground);
124 mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
126 changeButton.RaiseToTop();
127 resetButton.RaiseToTop();
130 // Callback of push button, for changing image set
131 bool OnChangeButtonClicked( Toolkit::Button button )
133 mIndex = (mIndex+4) % NUM_SVG_IMAGES;
134 for( unsigned int i=0; i<4u; i++)
136 mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
142 // Callback of push button, for resetting image size and position
143 bool OnResetButtonClicked( Toolkit::Button button )
145 for( unsigned int i=0; i<4u; i++)
147 mSvgActor[i].SetSize(mActorSize);
148 mSvgActor[i].SetPosition( Vector3::ZERO );
155 // Callback of pan gesture, for moving the actors
156 void OnPanGesture( Actor actor, const PanGesture& gesture )
158 if( gesture.state == Gesture::Continuing )
160 for( unsigned int i=0; i<4u; i++)
162 mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
167 // Callback of pinch gesture, for resizing the actors
168 void OnPinch(Actor actor, const PinchGesture& gesture)
170 if (gesture.state == Gesture::Started)
172 mScaleAtPinchStart = mScale;
174 if( gesture.state == Gesture::Finished )
176 mScale = mScaleAtPinchStart * gesture.scale;
177 mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
178 for( unsigned int i=0; i<4u; i++)
180 mSvgActor[i].SetSize( mActorSize * mScale);
186 * Main key event handler
188 void OnKeyEvent(const KeyEvent& event)
190 if(event.state == KeyEvent::Down)
192 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
198 const char* keyName = event.keyPressedName.c_str();
199 if( strcmp(keyName, "Left") == 0 )
202 for( unsigned int i=0; i<4u; i++)
204 mSvgActor[i].SetSize( mActorSize * mScale);
207 else if( strcmp(keyName, "Right") == 0 )
209 if( mScale < MAX_SCALE )
213 for( unsigned int i=0; i<4u; i++)
215 mSvgActor[i].SetSize( mActorSize * mScale);
223 Application& mApplication;
224 Actor mStageBackground;
225 PanGestureDetector mPanGestureDetector;
226 PinchGestureDetector mPinchGestureDetector;
228 Toolkit::ImageView mSvgActor[4];
231 float mScaleAtPinchStart;
235 int DALI_EXPORT_API main( int argc, char **argv )
237 Application application = Application::New( &argc, &argv );
238 ImageSvgController test( application );
239 application.MainLoop();