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/dali.h>
19 #include <dali/devel-api/actors/actor-devel.h>
20 #include <dali-toolkit/dali-toolkit.h>
21 #include <dali-toolkit/devel-api/controls/popup/popup.h>
22 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
23 #include "shared/view.h"
27 using Toolkit::TextLabel;
32 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
33 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
35 const char* BORDER_IMAGE( DEMO_IMAGE_DIR "border-4px.9.png" );
36 const int BORDER_WIDTH = ( 11.0f + 4.0f ); // Shadow size = 11, border size = 4.
37 const char* RESIZE_HANDLE_IMAGE( DEMO_IMAGE_DIR "resize-handle.png" );
39 const int MARGIN_SIZE = 10;
41 const char* const NEXT_BUTTON_ID = "NEXT_BUTTON";
42 const char* const PREVIOUS_BUTTON_ID = "PREVIOUS_BUTTON";
43 const char * const DALI_ICON_PLAY = DEMO_IMAGE_DIR "icon-play.png";
45 const char* const FITTING_BUTTON_ID = "FITTING_BUTTON";
46 const char* const SAMPLING_BUTTON_ID = "SAMPLING_BUTTON";
47 const char* const FITTING_BUTTON_TEXT = "Fitting";
48 const char* const SAMPLING_BUTTON_TEXT = "Sampling";
50 const char* const STYLE_LABEL_TEXT = "ImageScalingGroupLabel";
51 const char* const STYLE_BUTTON_TEXT = "ImageScalingButton";
53 const char* IMAGE_PATHS[] =
55 // Variety of sizes, shapes and formats:
56 DEMO_IMAGE_DIR "dali-logo.png",
57 DEMO_IMAGE_DIR "layer1.png",
58 DEMO_IMAGE_DIR "layer2.png",
59 DEMO_IMAGE_DIR "animation-list.png",
60 DEMO_IMAGE_DIR "music-libray-main-screen.png",
61 DEMO_IMAGE_DIR "music-libray-record-cover.png",
62 DEMO_IMAGE_DIR "contacts-background.png",
63 DEMO_IMAGE_DIR "portrait_screen_primitive_shapes.gif",
64 DEMO_IMAGE_DIR "landscape_screen_primitive_shapes.gif",
65 DEMO_IMAGE_DIR "square_primitive_shapes.bmp",
66 DEMO_IMAGE_DIR "gallery-large-14.jpg",
67 DEMO_IMAGE_DIR "book-landscape-cover.jpg",
68 DEMO_IMAGE_DIR "book-portrait-p1.jpg",
69 DEMO_IMAGE_DIR "book-landscape-cover-back.jpg",
71 // Worst case for aliasing in downscaling, 2k x 2k 1 bit per pixel dithered
72 // black and white image:
73 DEMO_IMAGE_DIR "gallery-large-14.wbmp",
75 DEMO_IMAGE_DIR "background-1.jpg",
76 DEMO_IMAGE_DIR "background-blocks.jpg",
77 DEMO_IMAGE_DIR "background-magnifier.jpg",
78 DEMO_IMAGE_DIR "gallery-large-14.jpg",
81 const int NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u;
83 /** Cycle the scaling mode options. */
84 FittingMode::Type NextScalingMode( FittingMode::Type oldMode )
86 FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
89 case FittingMode::SHRINK_TO_FIT:
90 newMode = FittingMode::SCALE_TO_FILL;
92 case FittingMode::SCALE_TO_FILL:
93 newMode = FittingMode::FIT_WIDTH;
95 case FittingMode::FIT_WIDTH:
96 newMode = FittingMode::FIT_HEIGHT;
98 case FittingMode::FIT_HEIGHT:
99 newMode = FittingMode::SHRINK_TO_FIT;
105 /** Cycle through filter mode options. */
106 SamplingMode::Type NextFilterMode( SamplingMode::Type oldMode )
108 SamplingMode::Type newMode = SamplingMode::BOX;
112 case SamplingMode::BOX:
113 newMode = SamplingMode::NEAREST;
115 case SamplingMode::NEAREST:
116 newMode = SamplingMode::LINEAR;
118 case SamplingMode::LINEAR:
119 newMode = SamplingMode::BOX_THEN_NEAREST;
121 case SamplingMode::BOX_THEN_NEAREST:
122 newMode = SamplingMode::BOX_THEN_LINEAR;
124 case SamplingMode::BOX_THEN_LINEAR:
125 newMode = SamplingMode::NO_FILTER;
127 case SamplingMode::NO_FILTER:
128 newMode = SamplingMode::BOX;
130 case SamplingMode::DONT_CARE:
131 newMode = SamplingMode::BOX;
137 const char* StringFromScalingMode( FittingMode::Type scalingMode )
139 return scalingMode == FittingMode::SCALE_TO_FILL ? "SCALE_TO_FILL" : scalingMode == FittingMode::SHRINK_TO_FIT ? "SHRINK_TO_FIT" : scalingMode == FittingMode::FIT_WIDTH ? "FIT_WIDTH" : scalingMode == FittingMode::FIT_HEIGHT ? "FIT_HEIGHT" : "UnknownScalingMode";
142 const char* StringFromFilterMode( SamplingMode::Type filterMode )
144 return filterMode == SamplingMode::BOX ? "BOX" : filterMode == SamplingMode::BOX_THEN_NEAREST ? "BOX_THEN_NEAREST" : filterMode == SamplingMode::BOX_THEN_LINEAR ? "BOX_THEN_LINEAR" : filterMode == SamplingMode::NEAREST ? "NEAREST" : filterMode == SamplingMode::LINEAR ? "LINEAR" : filterMode == SamplingMode::NO_FILTER ? "NO_FILTER" : filterMode == SamplingMode::DONT_CARE ? "DONT_CARE" : "UnknownFilterMode";
149 // This example shows the load-time image scaling and filtering features.
151 class ImageScalingAndFilteringController : public ConnectionTracker
155 ImageScalingAndFilteringController( Application& application )
156 : mApplication( application ),
157 mLastPinchScale( 1.0f ),
158 mImageWindowScale( 0.5f, 0.5f ),
160 mFittingMode( FittingMode::FIT_WIDTH ),
161 mSamplingMode( SamplingMode::BOX_THEN_LINEAR),
162 mImageLoading( false ),
163 mQueuedImageLoad( false )
165 // Connect to the Application's Init signal
166 mApplication.InitSignal().Connect( this, &ImageScalingAndFilteringController::Create );
169 ~ImageScalingAndFilteringController()
171 // Nothing to do here;
174 // The Init signal is received once (only) during the Application lifetime
175 void Create( Application& application )
177 // Get a handle to the window
178 Window window = application.GetWindow();
179 Vector2 windowSize = window.GetSize();
182 Dali::Property::Map backgroundImage;
183 backgroundImage.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
184 backgroundImage.Insert( Toolkit::ImageVisual::Property::URL, BACKGROUND_IMAGE );
185 backgroundImage.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, windowSize.width );
186 backgroundImage.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, windowSize.height );
187 backgroundImage.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, FittingMode::SCALE_TO_FILL );
188 backgroundImage.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, SamplingMode::BOX_THEN_NEAREST );
190 Toolkit::ImageView background = Toolkit::ImageView::New();
191 background.SetProperty( Toolkit::ImageView::Property::IMAGE, backgroundImage );
192 background.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
193 background.SetProperty( Actor::Property::SIZE, windowSize );
194 window.Add( background );
196 mDesiredBox = Toolkit::ImageView::New( BORDER_IMAGE );
197 background.Add( mDesiredBox );
199 mDesiredBox.SetProperty( Actor::Property::SIZE, windowSize * mImageWindowScale );
200 mDesiredBox.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
201 mDesiredBox.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
203 // Initialize the actor
204 mImageView = Toolkit::ImageView::New( IMAGE_PATHS[ 0 ] );
206 // Reposition the actor
207 mImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
208 mImageView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
210 // Display the actor on the window
211 mDesiredBox.Add( mImageView );
213 mImageView.SetProperty( Actor::Property::SIZE, windowSize * mImageWindowScale );
215 // Setup the pinch detector for scaling the desired image load dimensions:
216 mPinchDetector = PinchGestureDetector::New();
217 mPinchDetector.Attach( mImageView );
218 mPinchDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPinch );
220 mGrabCorner = Toolkit::ImageView::New( RESIZE_HANDLE_IMAGE );
221 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
222 mGrabCorner.SetProperty( Dali::Actor::Property::NAME, "GrabCorner" );
223 mGrabCorner.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_RIGHT );
224 mGrabCorner.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_RIGHT );
225 mGrabCorner.SetProperty( Actor::Property::POSITION, Vector2( -BORDER_WIDTH, -BORDER_WIDTH ));
226 mGrabCorner.SetProperty( Actor::Property::OPACITY, 0.6f );
228 Layer grabCornerLayer = Layer::New();
229 grabCornerLayer.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_RIGHT );
230 grabCornerLayer.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_RIGHT );
231 grabCornerLayer.Add( mGrabCorner );
232 mDesiredBox.Add( grabCornerLayer );
234 mPanGestureDetector = PanGestureDetector::New();
235 mPanGestureDetector.Attach( mGrabCorner );
236 mPanGestureDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPan );
238 // Tie-in input event handlers:
239 window.KeyEventSignal().Connect( this, &ImageScalingAndFilteringController::OnKeyEvent );
247 * Create the GUI controls which float above the scene
249 void CreateControls()
251 Window window = mApplication.GetWindow();
252 Vector2 windowSize = window.GetSize();
254 Dali::Layer controlsLayer = Dali::Layer::New();
255 controlsLayer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
256 controlsLayer.SetProperty( Actor::Property::SIZE_MODE_FACTOR, Vector3( 1.0f, 1.0f, 1.0f ) );
257 controlsLayer.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
258 controlsLayer.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
259 window.Add( controlsLayer );
261 // Back and next image buttons in corners of window:
262 unsigned int playWidth = std::min( windowSize.x * (1 / 5.0f), 58.0f );
263 Toolkit::ImageView imagePrevious = Toolkit::ImageView::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ) );
265 // Last image button:
266 imagePrevious.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
267 imagePrevious.RotateBy( Radian(3.14159265358979323846f), Vector3( 0, 1.0f, 0 ) );
268 imagePrevious.SetProperty( Actor::Property::POSITION_Y, playWidth * 0.5f );
269 imagePrevious.SetProperty( Actor::Property::POSITION_X, playWidth + playWidth * 0.5f );
270 imagePrevious.SetProperty( Actor::Property::OPACITY, 0.6f );
271 controlsLayer.Add( imagePrevious );
272 imagePrevious.SetProperty( Dali::Actor::Property::NAME, PREVIOUS_BUTTON_ID );
273 imagePrevious.TouchSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
275 // Next image button:
276 Toolkit::ImageView imageNext = Toolkit::ImageView::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ) );
277 imageNext.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_RIGHT );
278 imageNext.SetProperty( Actor::Property::POSITION_Y, playWidth * 0.5f );
279 imageNext.SetProperty( Actor::Property::POSITION_X, windowSize.x - playWidth * 0.5f );
280 imageNext.SetProperty( Actor::Property::OPACITY, 0.6f );
281 controlsLayer.Add( imageNext );
282 imageNext.SetProperty( Dali::Actor::Property::NAME, NEXT_BUTTON_ID );
283 imageNext.TouchSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
285 // Buttons to popup selectors for fitting and sampling modes:
287 // Wrapper table to hold two buttons side by side:
288 Toolkit::TableView modesGroupBackground = Toolkit::TableView::New( 1, 2 );
289 modesGroupBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
290 modesGroupBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
291 modesGroupBackground.SetBackgroundColor( BACKGROUND_COLOUR );
292 modesGroupBackground.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE ) );
293 modesGroupBackground.SetFitHeight( 0 );
295 modesGroupBackground.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT );
296 modesGroupBackground.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_LEFT );
297 modesGroupBackground.SetProperty( Actor::Property::POSITION, Vector2( 0.0f, 0.0f ));
299 controlsLayer.Add( modesGroupBackground );
302 // Vertical table to hold label and button:
303 Toolkit::TableView fittingModeGroup = Toolkit::TableView::New( 2, 1 );
304 fittingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
305 fittingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
306 fittingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
307 fittingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
308 fittingModeGroup.SetFitHeight( 0 );
309 fittingModeGroup.SetFitHeight( 1 );
311 TextLabel label = TextLabel::New( "Image fitting mode:" );
312 label.SetStyleName( STYLE_LABEL_TEXT );
313 fittingModeGroup.Add( label );
315 Toolkit::PushButton button = CreateButton( FITTING_BUTTON_ID, StringFromScalingMode( mFittingMode ) );
316 fittingModeGroup.Add( button );
317 mFittingModeButton = button;
319 modesGroupBackground.Add( fittingModeGroup );
323 // Vertical table to hold label and button:
324 Toolkit::TableView samplingModeGroup = Toolkit::TableView::New( 2, 1 );
325 samplingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
326 samplingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
327 samplingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
328 samplingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
329 samplingModeGroup.SetFitHeight( 0 );
330 samplingModeGroup.SetFitHeight( 1 );
332 TextLabel label = TextLabel::New( "Image sampling mode:" );
333 label.SetStyleName( STYLE_LABEL_TEXT );
334 samplingModeGroup.Add( label );
336 Toolkit::PushButton button = CreateButton( SAMPLING_BUTTON_ID, StringFromFilterMode( mSamplingMode ) );
337 samplingModeGroup.Add( button );
338 mSamplingModeButton = button;
340 modesGroupBackground.Add( samplingModeGroup );
344 Toolkit::PushButton CreateButton( const char * id, const char * label )
346 Toolkit::PushButton button = Toolkit::PushButton::New();
347 button.SetStyleName( STYLE_BUTTON_TEXT );
348 button.SetProperty( Dali::Actor::Property::NAME, id );
349 button.SetProperty( Toolkit::Button::Property::LABEL, label );
350 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
351 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
352 button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
356 Toolkit::Popup CreatePopup()
358 Window window = mApplication.GetWindow();
359 const float POPUP_WIDTH_DP = window.GetSize().GetWidth() * 0.75f;
361 Toolkit::Popup popup = Toolkit::Popup::New();
362 popup.SetProperty( Dali::Actor::Property::NAME, "POPUP" );
363 popup.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
364 popup.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
365 popup.SetProperty( Actor::Property::SIZE, Vector2( POPUP_WIDTH_DP, 0.0f ) );
367 popup.OutsideTouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnPopupOutsideTouched );
372 Toolkit::PushButton CreatePopupButton( Actor parent, const char* id )
374 Toolkit::PushButton button = Toolkit::PushButton::New();
375 button.SetProperty( Dali::Actor::Property::NAME, id );
376 button.SetProperty( Toolkit::Button::Property::LABEL, id );
378 button.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
379 button.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_LEFT );
380 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
381 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
383 button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
385 parent.Add( button );
389 bool OnButtonClicked( Toolkit::Button button )
391 if( button.GetProperty< std::string >( Dali::Actor::Property::NAME ) == FITTING_BUTTON_ID )
393 mPopup = CreatePopup();
395 // Four-row table to hold buttons:
396 Toolkit::TableView fittingModes = Toolkit::TableView::New( 4, 1 );
397 fittingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
398 fittingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
399 fittingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
400 fittingModes.SetFitHeight( 0 );
401 fittingModes.SetFitHeight( 1 );
402 fittingModes.SetFitHeight( 2 );
403 fittingModes.SetFitHeight( 3 );
405 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SCALE_TO_FILL ) );
406 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SHRINK_TO_FIT ) );
407 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_WIDTH ) );
408 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_HEIGHT ) );
410 mPopup.SetContent( fittingModes );
411 mApplication.GetWindow().Add( mPopup );
412 mPopup.SetDisplayState( Toolkit::Popup::SHOWN );
414 else if( button.GetProperty< std::string >( Dali::Actor::Property::NAME ) == SAMPLING_BUTTON_ID )
416 mPopup = CreatePopup();
418 // Table to hold buttons for each sampling mode:
419 Toolkit::TableView samplingModes = Toolkit::TableView::New( 6, 1 );
420 samplingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
421 samplingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
422 samplingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
423 samplingModes.SetFitHeight( 0 );
424 samplingModes.SetFitHeight( 1 );
425 samplingModes.SetFitHeight( 2 );
426 samplingModes.SetFitHeight( 3 );
427 samplingModes.SetFitHeight( 4 );
428 samplingModes.SetFitHeight( 5 );
430 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NEAREST ) );
431 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::LINEAR ) );
432 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX ) );
433 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_NEAREST ) );
434 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_LINEAR ) );
435 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NO_FILTER ) );
437 mPopup.SetContent( samplingModes );
438 mApplication.GetWindow().Add( mPopup );
439 mPopup.SetDisplayState( Toolkit::Popup::SHOWN );
441 else if( CheckFittingModeButton( button, FittingMode::SCALE_TO_FILL) ||
442 CheckFittingModeButton( button, FittingMode::SHRINK_TO_FIT) ||
443 CheckFittingModeButton( button, FittingMode::FIT_WIDTH) ||
444 CheckFittingModeButton( button, FittingMode::FIT_HEIGHT) )
447 else if( CheckSamplingModeButton( button, SamplingMode::NEAREST ) ||
448 CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
449 CheckSamplingModeButton( button, SamplingMode::BOX ) ||
450 CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
451 CheckSamplingModeButton( button, SamplingMode::BOX_THEN_NEAREST ) ||
452 CheckSamplingModeButton( button, SamplingMode::BOX_THEN_LINEAR ) ||
453 CheckSamplingModeButton( button, SamplingMode::NO_FILTER ) )
459 bool CheckFittingModeButton( Actor &button, FittingMode::Type mode )
461 const char * const modeName = StringFromScalingMode( mode );
462 if( button.GetProperty< std::string >( Dali::Actor::Property::NAME ) == modeName )
465 mFittingModeButton.SetProperty( Toolkit::Button::Property::LABEL, modeName );
467 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
474 bool CheckSamplingModeButton( Actor &button, SamplingMode::Type mode )
476 const char * const modeName = StringFromFilterMode( mode );
477 if( button.GetProperty< std::string >( Dali::Actor::Property::NAME ) == modeName )
479 mSamplingMode = mode;
480 mSamplingModeButton.SetProperty( Toolkit::Button::Property::LABEL, modeName );
482 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
489 void OnPopupOutsideTouched()
493 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
498 bool OnControlTouched( Actor actor, const TouchEvent& event )
500 if(event.GetPointCount() > 0)
502 switch( event.GetState( 0 ) )
506 const std::string & name = actor.GetProperty< std::string >( Dali::Actor::Property::NAME );
507 if( name == NEXT_BUTTON_ID )
509 mCurrentPath = mCurrentPath + 1;
510 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
513 else if( name == PREVIOUS_BUTTON_ID )
515 mCurrentPath = mCurrentPath - 1;
516 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
531 void OnPinch( Actor actor, const PinchGesture& pinch )
533 if( pinch.state == Gesture::Started )
535 mLastPinchScale = pinch.scale;
537 const float scale = pinch.scale;
539 if( ! Equals( scale, mLastPinchScale ) )
541 if ( scale < mLastPinchScale )
543 mImageWindowScale.x = std::max( 0.05f, mImageWindowScale.x * 0.9f );
544 mImageWindowScale.y = std::max( 0.05f, mImageWindowScale.y * 0.9f );
548 mImageWindowScale.x = std::max( 0.05f, std::min( 1.0f, mImageWindowScale.x * 1.1f ) );
549 mImageWindowScale.y = std::max( 0.05f, std::min( 1.0f, mImageWindowScale.y * 1.1f ) );
553 mLastPinchScale = scale;
556 void OnPan( Actor actor, const PanGesture& gesture )
558 Window window = mApplication.GetWindow();
559 Vector2 windowSize = window.GetSize();
560 // 1.0f and 0.75f are the maximum size caps of the resized image, as a factor of window-size.
561 mImageWindowScale.x = std::max( 0.05f, std::min( 0.95f, mImageWindowScale.x + ( gesture.displacement.x * 2.0f / windowSize.width ) ) );
562 mImageWindowScale.y = std::max( 0.05f, std::min( 0.70f, mImageWindowScale.y + ( gesture.displacement.y * 2.0f / windowSize.height ) ) );
567 void OnKeyEvent(const KeyEvent& event)
569 if( event.GetState() == KeyEvent::DOWN )
571 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
573 if( mPopup && mPopup.GetCurrentProperty< bool >( Actor::Property::VISIBLE ) )
575 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
583 else if ( event.GetKeyName() == "Right" )
585 mImageWindowScale.x = std::max( 0.05f, std::min( 1.0f, mImageWindowScale.x * 1.1f ) );
587 else if ( event.GetKeyName() == "Left" )
589 mImageWindowScale.x = std::max( 0.05f, mImageWindowScale.x * 0.9f );
591 else if ( event.GetKeyName() == "Up" )
593 mImageWindowScale.y = std::max( 0.05f, std::min( 1.0f, mImageWindowScale.y * 1.1f ) );
595 else if ( event.GetKeyName() == "Down" )
597 mImageWindowScale.y = std::max( 0.05f, mImageWindowScale.y * 0.9f );
599 else if ( event.GetKeyName() == "o" )
601 mImageWindowScale.x = std::max( 0.05f, mImageWindowScale.x * 0.9f );
602 mImageWindowScale.y = std::max( 0.05f, mImageWindowScale.y * 0.9f );
604 else if ( event.GetKeyName() == "p" )
606 mImageWindowScale.x = std::max( 0.05f, std::min( 1.0f, mImageWindowScale.x * 1.1f ) );
607 mImageWindowScale.y = std::max( 0.05f, std::min( 1.0f, mImageWindowScale.y * 1.1f ) );
609 else if ( event.GetKeyName() == "n" )
611 mCurrentPath = mCurrentPath + 1;
612 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
614 else if ( event.GetKeyName() == "b" )
616 mCurrentPath = mCurrentPath - 1;
617 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
619 // Cycle filter and scaling modes:
620 else if ( event.GetKeyName() == "f" )
622 mSamplingMode = NextFilterMode( mSamplingMode );
623 mSamplingModeButton.SetProperty( Toolkit::Button::Property::LABEL, StringFromFilterMode( mSamplingMode ) );
625 // Cycle filter and scaling modes:
626 else if ( event.GetKeyName() == "s" )
628 mFittingMode = NextScalingMode( mFittingMode );
629 mFittingModeButton.SetProperty( Toolkit::Button::Property::LABEL, StringFromScalingMode( mFittingMode ) );
644 mImageLoading = true;
646 const char * const path = IMAGE_PATHS[ mCurrentPath ];
647 Window window = mApplication.GetWindow();
648 Size imageSize = Vector2(window.GetSize()) * mImageWindowScale;
649 mImageView.SetProperty( Actor::Property::SIZE, imageSize );
652 map[Toolkit::ImageVisual::Property::URL] = path;
653 map[Toolkit::ImageVisual::Property::DESIRED_WIDTH] = imageSize.x;
654 map[Toolkit::ImageVisual::Property::DESIRED_HEIGHT] = imageSize.y;
655 map[Toolkit::ImageVisual::Property::FITTING_MODE] = mFittingMode;
656 map[Toolkit::ImageVisual::Property::SAMPLING_MODE] = mSamplingMode;
658 mImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, map );
665 Window window = mApplication.GetWindow();
666 Vector2 windowSize = window.GetSize();
667 Size imageSize = windowSize * mImageWindowScale;
671 // Border size needs to be modified to take into account the width of the frame.
672 Vector2 borderScale( ( imageSize + Vector2( BORDER_WIDTH * 2.0f, BORDER_WIDTH * 2.0f ) ) / windowSize );
673 mDesiredBox.SetProperty( Actor::Property::SIZE, windowSize * borderScale );
677 Application& mApplication;
678 Toolkit::ImageView mDesiredBox; //< Background rectangle to show requested image size.
679 Toolkit::PushButton mFittingModeButton;
680 Toolkit::PushButton mSamplingModeButton;
681 Toolkit::Popup mPopup;
682 PinchGestureDetector mPinchDetector;
683 float mLastPinchScale;
684 Toolkit::ImageView mGrabCorner;
685 PanGestureDetector mPanGestureDetector;
686 Toolkit::ImageView mImageView;
687 Vector2 mImageWindowScale;
689 FittingMode::Type mFittingMode;
690 SamplingMode::Type mSamplingMode;
692 bool mQueuedImageLoad;
696 int DALI_EXPORT_API main( int argc, char **argv )
698 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
699 ImageScalingAndFilteringController test( application );
700 application.MainLoop();