Commit e15cb069bed92bd938195d02b4fd4044d77c5cfa

Authored by Adeel Kazmi
1 parent 87fcf735

(SVG Images Example) Add ability to scale while pinching

Rasterization only happens when the pinching stops.

Change-Id: I67e44be2a7685de5d85213b002d7a737eb8668da
examples/image-view-svg/image-view-svg-example.cpp
1 1 /*
2   - * Copyright (c) 2017 Samsung Electronics Co., Ltd.
  2 + * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3 3 *
4 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 5 * you may not use this file except in compliance with the License.
... ... @@ -23,6 +23,7 @@ using namespace Dali;
23 23  
24 24 namespace
25 25 {
  26 +const float MIN_SCALE = 0.6f;
26 27 const float MAX_SCALE = 6.f;
27 28  
28 29 const char* SVG_IMAGES[] =
... ... @@ -37,9 +38,10 @@ const char* SVG_IMAGES[] =
37 38 DEMO_IMAGE_DIR "Kid1.svg"
38 39 };
39 40 const unsigned int NUM_SVG_IMAGES( sizeof( SVG_IMAGES ) / sizeof( SVG_IMAGES[0] ) );
40   -}
  41 +const unsigned int NUM_IMAGES_DISPLAYED = 4u;
  42 +} // unnamed namespace
41 43  
42   -// This example shows how to display svg images with ImageView
  44 +// This example shows how to display svg images with ImageView.
43 45 //
44 46 class ImageSvgController : public ConnectionTracker
45 47 {
... ... @@ -48,7 +50,6 @@ public:
48 50 ImageSvgController( Application& application )
49 51 : mApplication( application ),
50 52 mScale( 1.f ),
51   - mScaleAtPinchStart( 1.0f ),
52 53 mIndex( 0 )
53 54 {
54 55 // Connect to the Application's Init signal
... ... @@ -97,7 +98,7 @@ public:
97 98 resetButton.ClickedSignal().Connect( this, &ImageSvgController::OnResetButtonClicked );
98 99  
99 100 // Create and put imageViews to stage
100   - for( unsigned int i=0; i<4u; i++)
  101 + for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
101 102 {
102 103 mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
103 104 mSvgActor[i].SetSize( mActorSize );
... ... @@ -130,8 +131,8 @@ public:
130 131 // Callback of push button, for changing image set
131 132 bool OnChangeButtonClicked( Toolkit::Button button )
132 133 {
133   - mIndex = (mIndex+4) % NUM_SVG_IMAGES;
134   - for( unsigned int i=0; i<4u; i++)
  134 + mIndex = ( mIndex + NUM_IMAGES_DISPLAYED ) % NUM_SVG_IMAGES;
  135 + for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
135 136 {
136 137 mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
137 138 }
... ... @@ -142,7 +143,7 @@ public:
142 143 // Callback of push button, for resetting image size and position
143 144 bool OnResetButtonClicked( Toolkit::Button button )
144 145 {
145   - for( unsigned int i=0; i<4u; i++)
  146 + for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED ; i++ )
146 147 {
147 148 mSvgActor[i].SetSize(mActorSize);
148 149 mSvgActor[i].SetPosition( Vector3::ZERO );
... ... @@ -157,7 +158,7 @@ public:
157 158 {
158 159 if( gesture.state == Gesture::Continuing )
159 160 {
160   - for( unsigned int i=0; i<4u; i++)
  161 + for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
161 162 {
162 163 mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
163 164 }
... ... @@ -167,18 +168,42 @@ public:
167 168 // Callback of pinch gesture, for resizing the actors
168 169 void OnPinch(Actor actor, const PinchGesture& gesture)
169 170 {
170   - if (gesture.state == Gesture::Started)
171   - {
172   - mScaleAtPinchStart = mScale;
173   - }
174   - if( gesture.state == Gesture::Finished )
  171 + switch( gesture.state )
175 172 {
176   - mScale = mScaleAtPinchStart * gesture.scale;
177   - mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
178   - for( unsigned int i=0; i<4u; i++)
  173 + // Only scale the image when we start or continue pinching
  174 +
  175 + case Gesture::Started:
  176 + case Gesture::Continuing:
  177 + {
  178 + float scale = std::max( gesture.scale, MIN_SCALE / mScale );
  179 + scale = std::min( MAX_SCALE / mScale, scale );
  180 +
  181 + for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
  182 + {
  183 + mSvgActor[i].SetScale( scale );
  184 + }
  185 + break;
  186 + }
  187 +
  188 + case Gesture::Finished:
179 189 {
180   - mSvgActor[i].SetSize( mActorSize * mScale);
  190 + // Resize the image when pinching is complete, this will rasterize the SVG to the new size
  191 +
  192 + mScale = mScale * gesture.scale;
  193 + mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
  194 + mScale = mScale < MIN_SCALE ? MIN_SCALE : mScale;
  195 + for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
  196 + {
  197 + mSvgActor[i].SetSize( mActorSize * mScale );
  198 + mSvgActor[i].SetScale( 1.0f );
  199 + }
  200 + break;
181 201 }
  202 +
  203 + case Gesture::Cancelled:
  204 + case Gesture::Clear:
  205 + case Gesture::Possible:
  206 + break;
182 207 }
183 208 }
184 209  
... ... @@ -198,10 +223,13 @@ public:
198 223 const char* keyName = event.keyPressedName.c_str();
199 224 if( strcmp(keyName, "Left") == 0 )
200 225 {
201   - mScale /= 1.1f;
202   - for( unsigned int i=0; i<4u; i++)
  226 + if( mScale > MIN_SCALE )
  227 + {
  228 + mScale /= 1.1f;
  229 + }
  230 + for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
203 231 {
204   - mSvgActor[i].SetSize( mActorSize * mScale);
  232 + mSvgActor[i].SetSize( mActorSize * mScale );
205 233 }
206 234 }
207 235 else if( strcmp(keyName, "Right") == 0 )
... ... @@ -210,9 +238,9 @@ public:
210 238 {
211 239 mScale *= 1.1f;
212 240 }
213   - for( unsigned int i=0; i<4u; i++)
  241 + for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
214 242 {
215   - mSvgActor[i].SetSize( mActorSize * mScale);
  243 + mSvgActor[i].SetSize( mActorSize * mScale );
216 244 }
217 245 }
218 246 }
... ... @@ -228,7 +256,6 @@ private:
228 256 Toolkit::ImageView mSvgActor[4];
229 257 Vector2 mActorSize;
230 258 float mScale;
231   - float mScaleAtPinchStart;
232 259 unsigned int mIndex;
233 260 };
234 261  
... ...