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 ),
50 mScaleAtPinchStart( 1.0f ),
53 // Connect to the Application's Init signal
54 mApplication.InitSignal().Connect( this, &ImageSvgController::Create );
61 // The Init signal is received once (only) during the Application lifetime
62 void Create( Application& application )
64 // Get a handle to the stage
65 Stage stage = Stage::GetCurrent();
66 stage.SetBackgroundColor( Color::WHITE );
67 Vector2 stageSize = stage.GetSize();
68 mActorSize = stageSize/2.f;
70 // Hide the indicator bar
71 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
73 stage.KeyEventSignal().Connect(this, &ImageSvgController::OnKeyEvent);
75 // Background, for receiving gestures
76 mStageBackground = Actor::New();
77 mStageBackground.SetAnchorPoint( AnchorPoint::TOP_CENTER );
78 mStageBackground.SetParentOrigin( ParentOrigin::TOP_CENTER );
79 mStageBackground.SetSize( stageSize.x, stageSize.y );
80 stage.Add(mStageBackground);
82 // Push button, for changing the image set for displaying
83 Toolkit::PushButton changeButton = Toolkit::PushButton::New();
84 changeButton.SetLabelText( "Next" );
85 changeButton.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
86 changeButton.SetParentOrigin( ParentOrigin::TOP_RIGHT );
87 stage.Add( changeButton );
88 changeButton.ClickedSignal().Connect( this, &ImageSvgController::OnChangeButtonClicked );
90 // Push button, for resetting the actor size and position
91 Toolkit::PushButton resetButton = Toolkit::PushButton::New();
92 resetButton.SetLabelText( "Reset" );
93 resetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
94 resetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
95 stage.Add( resetButton );
96 resetButton.ClickedSignal().Connect( this, &ImageSvgController::OnResetButtonClicked );
98 // Create and put imageViews to stage
99 for( unsigned int i=0; i<4u; i++)
101 mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
102 mSvgActor[i].SetSize( mActorSize );
103 mSvgActor[i].TranslateBy( Vector3( 0.0, stageSize.height * 0.05, 0.0f ) );
104 stage.Add( mSvgActor[i] );
106 mSvgActor[0].SetParentOrigin( ParentOrigin::CENTER );
107 mSvgActor[0].SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
108 mSvgActor[1].SetParentOrigin( ParentOrigin::CENTER );
109 mSvgActor[1].SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
110 mSvgActor[2].SetParentOrigin( ParentOrigin::CENTER );
111 mSvgActor[2].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
112 mSvgActor[3].SetParentOrigin( ParentOrigin::CENTER );
113 mSvgActor[3].SetAnchorPoint( AnchorPoint::TOP_LEFT );
115 // Connect pan gesture for moving the actors
116 mPanGestureDetector = PanGestureDetector::New();
117 mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
118 mPanGestureDetector.Attach( mStageBackground );
120 // Connect pinch gesture for resizing the actors
121 mPinchGestureDetector = PinchGestureDetector::New();
122 mPinchGestureDetector.Attach( mStageBackground);
123 mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
126 // Callback of push button, for changing image set
127 bool OnChangeButtonClicked( Toolkit::Button button )
129 mIndex = (mIndex+4) % NUM_SVG_IMAGES;
130 for( unsigned int i=0; i<4u; i++)
132 mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
138 // Callback of push button, for resetting image size and position
139 bool OnResetButtonClicked( Toolkit::Button button )
141 for( unsigned int i=0; i<4u; i++)
143 mSvgActor[i].SetSize(mActorSize);
144 mSvgActor[i].SetPosition( Vector3::ZERO );
151 // Callback of pan gesture, for moving the actors
152 void OnPanGesture( Actor actor, const PanGesture& gesture )
154 if( gesture.state == Gesture::Continuing )
156 for( unsigned int i=0; i<4u; i++)
158 mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
163 // Callback of pinch gesture, for resizing the actors
164 void OnPinch(Actor actor, const PinchGesture& gesture)
166 if (gesture.state == Gesture::Started)
168 mScaleAtPinchStart = mScale;
170 if( gesture.state == Gesture::Finished )
172 mScale = mScaleAtPinchStart * gesture.scale;
173 mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
174 for( unsigned int i=0; i<4u; i++)
176 mSvgActor[i].SetSize( mActorSize * mScale);
182 * Main key event handler
184 void OnKeyEvent(const KeyEvent& event)
186 if(event.state == KeyEvent::Down)
188 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
194 const char* keyName = event.keyPressedName.c_str();
195 if( strcmp(keyName, "Left") == 0 )
198 for( unsigned int i=0; i<4u; i++)
200 mSvgActor[i].SetSize( mActorSize * mScale);
203 else if( strcmp(keyName, "Right") == 0 )
205 if( mScale < MAX_SCALE )
209 for( unsigned int i=0; i<4u; i++)
211 mSvgActor[i].SetSize( mActorSize * mScale);
219 Application& mApplication;
220 Actor mStageBackground;
221 PanGestureDetector mPanGestureDetector;
222 PinchGestureDetector mPinchGestureDetector;
224 Toolkit::ImageView mSvgActor[4];
227 float mScaleAtPinchStart;
231 void RunTest( Application& application )
233 ImageSvgController test( application );
235 application.MainLoop();
238 // Entry point for Linux & Tizen applications
240 int DALI_EXPORT_API main( int argc, char **argv )
242 Application application = Application::New( &argc, &argv );
244 RunTest( application );