2 * Copyright (c) 2020 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 MIN_SCALE = 0.6f;
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] ) );
41 const unsigned int NUM_IMAGES_DISPLAYED = 4u;
42 } // unnamed namespace
44 // This example shows how to display svg images with ImageView.
46 class ImageSvgController : public ConnectionTracker
50 ImageSvgController( Application& application )
51 : mApplication( application ),
55 // Connect to the Application's Init signal
56 mApplication.InitSignal().Connect( this, &ImageSvgController::Create );
63 // The Init signal is received once (only) during the Application lifetime
64 void Create( Application& application )
66 // Get a handle to the window
67 Window window = application.GetWindow();
68 window.SetBackgroundColor( Color::WHITE );
69 Vector2 windowSize = window.GetSize();
70 mActorSize = windowSize/2.f;
72 window.KeyEventSignal().Connect(this, &ImageSvgController::OnKeyEvent);
74 // Background, for receiving gestures
75 mWindowBackground = Actor::New();
76 mWindowBackground.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
77 mWindowBackground.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
78 mWindowBackground.SetProperty( Actor::Property::SIZE, Vector2( windowSize.x, windowSize.y ) );
79 window.Add(mWindowBackground);
81 // Push button, for changing the image set for displaying
82 Toolkit::PushButton changeButton = Toolkit::PushButton::New();
83 changeButton.SetProperty( Toolkit::Button::Property::LABEL, "Next" );
84 changeButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_RIGHT );
85 changeButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_RIGHT );
86 window.Add( changeButton );
87 changeButton.ClickedSignal().Connect( this, &ImageSvgController::OnChangeButtonClicked );
89 // Push button, for resetting the actor size and position
90 Toolkit::PushButton resetButton = Toolkit::PushButton::New();
91 resetButton.SetProperty( Toolkit::Button::Property::LABEL, "Reset" );
92 resetButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
93 resetButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
94 window.Add( resetButton );
95 resetButton.ClickedSignal().Connect( this, &ImageSvgController::OnResetButtonClicked );
97 // Create and put imageViews to window
98 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
100 mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
101 mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize );
102 mSvgActor[i].TranslateBy( Vector3( 0.0, windowSize.height * 0.05, 0.0f ) );
103 window.Add( mSvgActor[i] );
105 mSvgActor[0].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
106 mSvgActor[0].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_RIGHT );
107 mSvgActor[1].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
108 mSvgActor[1].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT );
109 mSvgActor[2].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
110 mSvgActor[2].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_RIGHT );
111 mSvgActor[3].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
112 mSvgActor[3].SetProperty( Actor::Property::ANCHOR_POINT, 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( mWindowBackground );
119 // Connect pinch gesture for resizing the actors
120 mPinchGestureDetector = PinchGestureDetector::New();
121 mPinchGestureDetector.Attach( mWindowBackground);
122 mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
124 changeButton.RaiseToTop();
125 resetButton.RaiseToTop();
128 // Callback of push button, for changing image set
129 bool OnChangeButtonClicked( Toolkit::Button button )
131 mIndex = ( mIndex + NUM_IMAGES_DISPLAYED ) % NUM_SVG_IMAGES;
132 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
134 mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
140 // Callback of push button, for resetting image size and position
141 bool OnResetButtonClicked( Toolkit::Button button )
143 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED ; i++ )
145 mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize);
146 mSvgActor[i].SetProperty( Actor::Property::POSITION, Vector3::ZERO );
153 // Callback of pan gesture, for moving the actors
154 void OnPanGesture( Actor actor, const PanGesture& gesture )
156 if( gesture.state == Gesture::Continuing )
158 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
160 mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
165 // Callback of pinch gesture, for resizing the actors
166 void OnPinch(Actor actor, const PinchGesture& gesture)
168 switch( gesture.state )
170 // Only scale the image when we start or continue pinching
172 case Gesture::Started:
173 case Gesture::Continuing:
175 float scale = std::max( gesture.scale, MIN_SCALE / mScale );
176 scale = std::min( MAX_SCALE / mScale, scale );
178 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
180 mSvgActor[i].SetProperty( Actor::Property::SCALE, scale );
185 case Gesture::Finished:
187 // Resize the image when pinching is complete, this will rasterize the SVG to the new size
189 mScale = mScale * gesture.scale;
190 mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
191 mScale = mScale < MIN_SCALE ? MIN_SCALE : mScale;
192 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
194 mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize * mScale );
195 mSvgActor[i].SetProperty( Actor::Property::SCALE, 1.0f );
200 case Gesture::Cancelled:
202 case Gesture::Possible:
208 * Main key event handler
210 void OnKeyEvent(const KeyEvent& event)
212 if(event.state == KeyEvent::Down)
214 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
220 const char* keyName = event.keyPressedName.c_str();
221 if( strcmp(keyName, "Left") == 0 )
223 if( mScale > MIN_SCALE )
227 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
229 mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize * mScale );
232 else if( strcmp(keyName, "Right") == 0 )
234 if( mScale < MAX_SCALE )
238 for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
240 mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize * mScale );
248 Application& mApplication;
249 Actor mWindowBackground;
250 PanGestureDetector mPanGestureDetector;
251 PinchGestureDetector mPinchGestureDetector;
253 Toolkit::ImageView mSvgActor[4];
259 int DALI_EXPORT_API main( int argc, char **argv )
261 Application application = Application::New( &argc, &argv );
262 ImageSvgController test( application );
263 application.MainLoop();