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-toolkit/devel-api/controls/renderer-factory/renderer-factory.h>
20 #include <dali-toolkit/devel-api/controls/renderer-factory/control-renderer.h>
27 const float MAX_SCALE = 6.f;
29 const char* SVG_IMAGES[] =
31 DEMO_IMAGE_DIR "Camera.svg",
32 DEMO_IMAGE_DIR "Contacts.svg",
33 DEMO_IMAGE_DIR "Mail.svg",
34 DEMO_IMAGE_DIR "Message.svg",
35 DEMO_IMAGE_DIR "Phone.svg",
36 DEMO_IMAGE_DIR "Settings.svg",
37 DEMO_IMAGE_DIR "World.svg",
38 DEMO_IMAGE_DIR "Kid1.svg"
40 const unsigned int NUM_SVG_IMAGES( sizeof( SVG_IMAGES ) / sizeof( SVG_IMAGES[0] ) );
43 // This example shows how to display svg images with ImageView
45 class ImageSvgController : public ConnectionTracker
49 ImageSvgController( Application& application )
50 : mApplication( application ),
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 stage.KeyEventSignal().Connect(this, &ImageSvgController::OnKeyEvent);
73 // Background, for receiving gestures
74 mStageBackground = Actor::New();
75 mStageBackground.SetAnchorPoint( AnchorPoint::TOP_CENTER );
76 mStageBackground.SetParentOrigin( ParentOrigin::TOP_CENTER );
77 mStageBackground.SetSize( stageSize.x, stageSize.y );
78 stage.Add(mStageBackground);
80 // Push button, for changing the image set for displaying
81 Toolkit::PushButton changeButton = Toolkit::PushButton::New();
82 changeButton.SetLabelText( "Next" );
83 changeButton.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
84 changeButton.SetParentOrigin( ParentOrigin::TOP_RIGHT );
85 stage.Add( changeButton );
86 changeButton.ClickedSignal().Connect( this, &ImageSvgController::OnChangeButtonClicked );
88 // Push button, for resetting the actor size and position
89 Toolkit::PushButton resetButton = Toolkit::PushButton::New();
90 resetButton.SetLabelText( "Reset" );
91 resetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
92 resetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
93 stage.Add( resetButton );
94 resetButton.ClickedSignal().Connect( this, &ImageSvgController::OnResetButtonClicked );
96 // Create and put imageViews to stage
97 for( unsigned int i=0; i<4u; i++)
99 mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
100 mSvgActor[i].SetSize( mActorSize );
101 stage.Add( mSvgActor[i] );
103 mSvgActor[0].SetParentOrigin( ParentOrigin::CENTER );
104 mSvgActor[0].SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
105 mSvgActor[1].SetParentOrigin( ParentOrigin::CENTER );
106 mSvgActor[1].SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
107 mSvgActor[2].SetParentOrigin( ParentOrigin::CENTER );
108 mSvgActor[2].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
109 mSvgActor[3].SetParentOrigin( ParentOrigin::CENTER );
110 mSvgActor[3].SetAnchorPoint( AnchorPoint::TOP_LEFT );
112 // Connect pan gesture for moving the actors
113 mPanGestureDetector = PanGestureDetector::New();
114 mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
115 mPanGestureDetector.Attach( mStageBackground );
117 // Connect pinch gesture for resizing the actors
118 mPinchGestureDetector = PinchGestureDetector::New();
119 mPinchGestureDetector.Attach( mStageBackground);
120 mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
123 // Callback of push button, for changing image set
124 bool OnChangeButtonClicked( Toolkit::Button button )
126 mIndex = (mIndex+4) % NUM_SVG_IMAGES;
127 for( unsigned int i=0; i<4u; i++)
129 mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
135 // Callback of push button, for resetting image size and position
136 bool OnResetButtonClicked( Toolkit::Button button )
138 for( unsigned int i=0; i<4u; i++)
140 mSvgActor[i].SetSize(mActorSize);
141 mSvgActor[i].SetPosition( Vector3::ZERO );
148 // Callback of pan gesture, for moving the actors
149 void OnPanGesture( Actor actor, const PanGesture& gesture )
151 if( gesture.state == Gesture::Continuing )
153 for( unsigned int i=0; i<4u; i++)
155 mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
160 // Callback of pinch gesture, for resizing the actors
161 void OnPinch(Actor actor, const PinchGesture& gesture)
163 if (gesture.state == Gesture::Started)
165 mScaleAtPinchStart = mScale;
167 if( gesture.state == Gesture::Finished )
169 mScale = mScaleAtPinchStart * gesture.scale;
170 mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
171 for( unsigned int i=0; i<4u; i++)
173 mSvgActor[i].SetSize( mActorSize * mScale);
179 * Main key event handler
181 void OnKeyEvent(const KeyEvent& event)
183 if(event.state == KeyEvent::Down)
185 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
191 const char* keyName = event.keyPressedName.c_str();
192 if( strcmp(keyName, "Left") == 0 )
195 for( unsigned int i=0; i<4u; i++)
197 mSvgActor[i].SetSize( mActorSize * mScale);
200 else if( strcmp(keyName, "Right") == 0 )
202 if( mScale < MAX_SCALE )
206 for( unsigned int i=0; i<4u; i++)
208 mSvgActor[i].SetSize( mActorSize * mScale);
216 Application& mApplication;
217 Actor mStageBackground;
218 PanGestureDetector mPanGestureDetector;
219 PinchGestureDetector mPinchGestureDetector;
221 Toolkit::ImageView mSvgActor[4];
224 float mScaleAtPinchStart;
228 void RunTest( Application& application )
230 ImageSvgController test( application );
232 application.MainLoop();
235 // Entry point for Linux & Tizen applications
237 int main( int argc, char **argv )
239 Application application = Application::New( &argc, &argv );
241 RunTest( application );