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>
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.SetLabelText( "Next" );
86 changeButton.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
87 changeButton.SetParentOrigin( ParentOrigin::TOP_RIGHT );
88 stage.Add( changeButton );
89 changeButton.ClickedSignal().Connect( this, &ImageSvgController::OnChangeButtonClicked );
90 changeButton.SetProperty( DevelActor::Property::SIBLING_ORDER, 1 );
92 // Push button, for resetting the actor size and position
93 Toolkit::PushButton resetButton = Toolkit::PushButton::New();
94 resetButton.SetLabelText( "Reset" );
95 resetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
96 resetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
97 stage.Add( resetButton );
98 resetButton.ClickedSignal().Connect( this, &ImageSvgController::OnResetButtonClicked );
99 resetButton.SetProperty( DevelActor::Property::SIBLING_ORDER, 1 );
101 // Create and put imageViews to stage
102 for( unsigned int i=0; i<4u; i++)
104 mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
105 mSvgActor[i].SetSize( mActorSize );
106 mSvgActor[i].TranslateBy( Vector3( 0.0, stageSize.height * 0.05, 0.0f ) );
107 stage.Add( mSvgActor[i] );
109 mSvgActor[0].SetParentOrigin( ParentOrigin::CENTER );
110 mSvgActor[0].SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
111 mSvgActor[1].SetParentOrigin( ParentOrigin::CENTER );
112 mSvgActor[1].SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
113 mSvgActor[2].SetParentOrigin( ParentOrigin::CENTER );
114 mSvgActor[2].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
115 mSvgActor[3].SetParentOrigin( ParentOrigin::CENTER );
116 mSvgActor[3].SetAnchorPoint( AnchorPoint::TOP_LEFT );
118 // Connect pan gesture for moving the actors
119 mPanGestureDetector = PanGestureDetector::New();
120 mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
121 mPanGestureDetector.Attach( mStageBackground );
123 // Connect pinch gesture for resizing the actors
124 mPinchGestureDetector = PinchGestureDetector::New();
125 mPinchGestureDetector.Attach( mStageBackground);
126 mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
129 // Callback of push button, for changing image set
130 bool OnChangeButtonClicked( Toolkit::Button button )
132 mIndex = (mIndex+4) % NUM_SVG_IMAGES;
133 for( unsigned int i=0; i<4u; i++)
135 mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
141 // Callback of push button, for resetting image size and position
142 bool OnResetButtonClicked( Toolkit::Button button )
144 for( unsigned int i=0; i<4u; i++)
146 mSvgActor[i].SetSize(mActorSize);
147 mSvgActor[i].SetPosition( Vector3::ZERO );
154 // Callback of pan gesture, for moving the actors
155 void OnPanGesture( Actor actor, const PanGesture& gesture )
157 if( gesture.state == Gesture::Continuing )
159 for( unsigned int i=0; i<4u; i++)
161 mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
166 // Callback of pinch gesture, for resizing the actors
167 void OnPinch(Actor actor, const PinchGesture& gesture)
169 if (gesture.state == Gesture::Started)
171 mScaleAtPinchStart = mScale;
173 if( gesture.state == Gesture::Finished )
175 mScale = mScaleAtPinchStart * gesture.scale;
176 mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
177 for( unsigned int i=0; i<4u; i++)
179 mSvgActor[i].SetSize( mActorSize * mScale);
185 * Main key event handler
187 void OnKeyEvent(const KeyEvent& event)
189 if(event.state == KeyEvent::Down)
191 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
197 const char* keyName = event.keyPressedName.c_str();
198 if( strcmp(keyName, "Left") == 0 )
201 for( unsigned int i=0; i<4u; i++)
203 mSvgActor[i].SetSize( mActorSize * mScale);
206 else if( strcmp(keyName, "Right") == 0 )
208 if( mScale < MAX_SCALE )
212 for( unsigned int i=0; i<4u; i++)
214 mSvgActor[i].SetSize( mActorSize * mScale);
222 Application& mApplication;
223 Actor mStageBackground;
224 PanGestureDetector mPanGestureDetector;
225 PinchGestureDetector mPinchGestureDetector;
227 Toolkit::ImageView mSvgActor[4];
230 float mScaleAtPinchStart;
234 void RunTest( Application& application )
236 ImageSvgController test( application );
238 application.MainLoop();
241 // Entry point for Linux & Tizen applications
243 int DALI_EXPORT_API main( int argc, char **argv )
245 Application application = Application::New( &argc, &argv );
247 RunTest( application );