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 stage.Add( mSvgActor[i] );
105 mSvgActor[0].SetParentOrigin( ParentOrigin::CENTER );
106 mSvgActor[0].SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
107 mSvgActor[1].SetParentOrigin( ParentOrigin::CENTER );
108 mSvgActor[1].SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
109 mSvgActor[2].SetParentOrigin( ParentOrigin::CENTER );
110 mSvgActor[2].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
111 mSvgActor[3].SetParentOrigin( ParentOrigin::CENTER );
112 mSvgActor[3].SetAnchorPoint( AnchorPoint::TOP_LEFT );
114 // Connect pan gesture for moving the actors
115 mPanGestureDetector = PanGestureDetector::New();
116 mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
117 mPanGestureDetector.Attach( mStageBackground );
119 // Connect pinch gesture for resizing the actors
120 mPinchGestureDetector = PinchGestureDetector::New();
121 mPinchGestureDetector.Attach( mStageBackground);
122 mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
125 // Callback of push button, for changing image set
126 bool OnChangeButtonClicked( Toolkit::Button button )
128 mIndex = (mIndex+4) % NUM_SVG_IMAGES;
129 for( unsigned int i=0; i<4u; i++)
131 mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
137 // Callback of push button, for resetting image size and position
138 bool OnResetButtonClicked( Toolkit::Button button )
140 for( unsigned int i=0; i<4u; i++)
142 mSvgActor[i].SetSize(mActorSize);
143 mSvgActor[i].SetPosition( Vector3::ZERO );
150 // Callback of pan gesture, for moving the actors
151 void OnPanGesture( Actor actor, const PanGesture& gesture )
153 if( gesture.state == Gesture::Continuing )
155 for( unsigned int i=0; i<4u; i++)
157 mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
162 // Callback of pinch gesture, for resizing the actors
163 void OnPinch(Actor actor, const PinchGesture& gesture)
165 if (gesture.state == Gesture::Started)
167 mScaleAtPinchStart = mScale;
169 if( gesture.state == Gesture::Finished )
171 mScale = mScaleAtPinchStart * gesture.scale;
172 mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
173 for( unsigned int i=0; i<4u; i++)
175 mSvgActor[i].SetSize( mActorSize * mScale);
181 * Main key event handler
183 void OnKeyEvent(const KeyEvent& event)
185 if(event.state == KeyEvent::Down)
187 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
193 const char* keyName = event.keyPressedName.c_str();
194 if( strcmp(keyName, "Left") == 0 )
197 for( unsigned int i=0; i<4u; i++)
199 mSvgActor[i].SetSize( mActorSize * mScale);
202 else if( strcmp(keyName, "Right") == 0 )
204 if( mScale < MAX_SCALE )
208 for( unsigned int i=0; i<4u; i++)
210 mSvgActor[i].SetSize( mActorSize * mScale);
218 Application& mApplication;
219 Actor mStageBackground;
220 PanGestureDetector mPanGestureDetector;
221 PinchGestureDetector mPinchGestureDetector;
223 Toolkit::ImageView mSvgActor[4];
226 float mScaleAtPinchStart;
230 void RunTest( Application& application )
232 ImageSvgController test( application );
234 application.MainLoop();
237 // Entry point for Linux & Tizen applications
239 int DALI_EXPORT_API main( int argc, char **argv )
241 Application application = Application::New( &argc, &argv );
243 RunTest( application );