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 stage.KeyEventSignal().Connect(this, &ImageSvgController::OnKeyEvent);
71 // Background, for receiving gestures
72 mStageBackground = Actor::New();
73 mStageBackground.SetAnchorPoint( AnchorPoint::TOP_CENTER );
74 mStageBackground.SetParentOrigin( ParentOrigin::TOP_CENTER );
75 mStageBackground.SetSize( stageSize.x, stageSize.y );
76 stage.Add(mStageBackground);
78 // Push button, for changing the image set for displaying
79 Toolkit::PushButton changeButton = Toolkit::PushButton::New();
80 changeButton.SetLabelText( "Next" );
81 changeButton.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
82 changeButton.SetParentOrigin( ParentOrigin::TOP_RIGHT );
83 stage.Add( changeButton );
84 changeButton.ClickedSignal().Connect( this, &ImageSvgController::OnChangeButtonClicked );
86 // Push button, for resetting the actor size and position
87 Toolkit::PushButton resetButton = Toolkit::PushButton::New();
88 resetButton.SetLabelText( "Reset" );
89 resetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
90 resetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
91 stage.Add( resetButton );
92 resetButton.ClickedSignal().Connect( this, &ImageSvgController::OnResetButtonClicked );
94 // Create and put imageViews to stage
95 for( unsigned int i=0; i<4u; i++)
97 mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
98 mSvgActor[i].SetSize( mActorSize );
99 stage.Add( mSvgActor[i] );
101 mSvgActor[0].SetParentOrigin( ParentOrigin::CENTER );
102 mSvgActor[0].SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
103 mSvgActor[1].SetParentOrigin( ParentOrigin::CENTER );
104 mSvgActor[1].SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
105 mSvgActor[2].SetParentOrigin( ParentOrigin::CENTER );
106 mSvgActor[2].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
107 mSvgActor[3].SetParentOrigin( ParentOrigin::CENTER );
108 mSvgActor[3].SetAnchorPoint( AnchorPoint::TOP_LEFT );
110 // Connect pan gesture for moving the actors
111 mPanGestureDetector = PanGestureDetector::New();
112 mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
113 mPanGestureDetector.Attach( mStageBackground );
115 // Connect pinch gesture for resizing the actors
116 mPinchGestureDetector = PinchGestureDetector::New();
117 mPinchGestureDetector.Attach( mStageBackground);
118 mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
121 // Callback of push button, for changing image set
122 bool OnChangeButtonClicked( Toolkit::Button button )
124 mIndex = (mIndex+4) % NUM_SVG_IMAGES;
125 for( unsigned int i=0; i<4u; i++)
127 mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
133 // Callback of push button, for resetting image size and position
134 bool OnResetButtonClicked( Toolkit::Button button )
136 for( unsigned int i=0; i<4u; i++)
138 mSvgActor[i].SetSize(mActorSize);
139 mSvgActor[i].SetPosition( Vector3::ZERO );
146 // Callback of pan gesture, for moving the actors
147 void OnPanGesture( Actor actor, const PanGesture& gesture )
149 if( gesture.state == Gesture::Continuing )
151 for( unsigned int i=0; i<4u; i++)
153 mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
158 // Callback of pinch gesture, for resizing the actors
159 void OnPinch(Actor actor, const PinchGesture& gesture)
161 if (gesture.state == Gesture::Started)
163 mScaleAtPinchStart = mScale;
165 if( gesture.state == Gesture::Finished )
167 mScale = mScaleAtPinchStart * gesture.scale;
168 mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
169 for( unsigned int i=0; i<4u; i++)
171 mSvgActor[i].SetSize( mActorSize * mScale);
177 * Main key event handler
179 void OnKeyEvent(const KeyEvent& event)
181 if(event.state == KeyEvent::Down)
183 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
189 const char* keyName = event.keyPressedName.c_str();
190 if( strcmp(keyName, "Left") == 0 )
193 for( unsigned int i=0; i<4u; i++)
195 mSvgActor[i].SetSize( mActorSize * mScale);
198 else if( strcmp(keyName, "Right") == 0 )
200 if( mScale < MAX_SCALE )
204 for( unsigned int i=0; i<4u; i++)
206 mSvgActor[i].SetSize( mActorSize * mScale);
214 Application& mApplication;
215 Actor mStageBackground;
216 PanGestureDetector mPanGestureDetector;
217 PinchGestureDetector mPinchGestureDetector;
219 Toolkit::ImageView mSvgActor[4];
222 float mScaleAtPinchStart;
226 void RunTest( Application& application )
228 ImageSvgController test( application );
230 application.MainLoop();
233 // Entry point for Linux & Tizen applications
235 int DALI_EXPORT_API main( int argc, char **argv )
237 Application application = Application::New( &argc, &argv );
239 RunTest( application );