2 * Copyright (c) 2016 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>
25 const float MAX_SCALE = 6.f;
27 const char* SVG_IMAGES[] =
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"
38 const unsigned int NUM_SVG_IMAGES( sizeof( SVG_IMAGES ) / sizeof( SVG_IMAGES[0] ) );
41 // This example shows how to display svg images with ImageView
43 class ImageSvgController : public ConnectionTracker
47 ImageSvgController( Application& application )
48 : mApplication( application ),
52 // Connect to the Application's Init signal
53 mApplication.InitSignal().Connect( this, &ImageSvgController::Create );
60 // The Init signal is received once (only) during the Application lifetime
61 void Create( Application& application )
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;
69 // Hide the indicator bar
70 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
72 stage.KeyEventSignal().Connect(this, &ImageSvgController::OnKeyEvent);
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);
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 );
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 );
97 // Create and put imageViews to stage
98 for( unsigned int i=0; i<4u; i++)
100 mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
101 mSvgActor[i].SetSize( mActorSize );
102 stage.Add( mSvgActor[i] );
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 );
113 // Connect pan gesture for moving the actors
114 mPanGestureDetector = PanGestureDetector::New();
115 mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
116 mPanGestureDetector.Attach( mStageBackground );
118 // Connect pinch gesture for resizing the actors
119 mPinchGestureDetector = PinchGestureDetector::New();
120 mPinchGestureDetector.Attach( mStageBackground);
121 mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
124 // Callback of push button, for changing image set
125 bool OnChangeButtonClicked( Toolkit::Button button )
127 mIndex = (mIndex+4) % NUM_SVG_IMAGES;
128 for( unsigned int i=0; i<4u; i++)
130 mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
136 // Callback of push button, for resetting image size and position
137 bool OnResetButtonClicked( Toolkit::Button button )
139 for( unsigned int i=0; i<4u; i++)
141 mSvgActor[i].SetSize(mActorSize);
142 mSvgActor[i].SetPosition( Vector3::ZERO );
149 // Callback of pan gesture, for moving the actors
150 void OnPanGesture( Actor actor, const PanGesture& gesture )
152 if( gesture.state == Gesture::Continuing )
154 for( unsigned int i=0; i<4u; i++)
156 mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
161 // Callback of pinch gesture, for resizing the actors
162 void OnPinch(Actor actor, const PinchGesture& gesture)
164 if (gesture.state == Gesture::Started)
166 mScaleAtPinchStart = mScale;
168 if( gesture.state == Gesture::Finished )
170 mScale = mScaleAtPinchStart * gesture.scale;
171 mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
172 for( unsigned int i=0; i<4u; i++)
174 mSvgActor[i].SetSize( mActorSize * mScale);
180 * Main key event handler
182 void OnKeyEvent(const KeyEvent& event)
184 if(event.state == KeyEvent::Down)
186 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
192 const char* keyName = event.keyPressedName.c_str();
193 if( strcmp(keyName, "Left") == 0 )
196 for( unsigned int i=0; i<4u; i++)
198 mSvgActor[i].SetSize( mActorSize * mScale);
201 else if( strcmp(keyName, "Right") == 0 )
203 if( mScale < MAX_SCALE )
207 for( unsigned int i=0; i<4u; i++)
209 mSvgActor[i].SetSize( mActorSize * mScale);
217 Application& mApplication;
218 Actor mStageBackground;
219 PanGestureDetector mPanGestureDetector;
220 PinchGestureDetector mPinchGestureDetector;
222 Toolkit::ImageView mSvgActor[4];
225 float mScaleAtPinchStart;
229 void RunTest( Application& application )
231 ImageSvgController test( application );
233 application.MainLoop();
236 // Entry point for Linux & Tizen applications
238 int DALI_EXPORT_API main( int argc, char **argv )
240 Application application = Application::New( &argc, &argv );
242 RunTest( application );