2 * Copyright (c) 2017 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-toolkit/dali-toolkit.h>
20 #include <dali-toolkit/devel-api/controls/popup/popup.h>
21 #include "shared/view.h"
25 using Toolkit::TextLabel;
30 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
31 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
33 const char* BORDER_IMAGE( DEMO_IMAGE_DIR "border-4px.9.png" );
34 const int BORDER_WIDTH = ( 11.0f + 4.0f ); // Shadow size = 11, border size = 4.
35 const char* RESIZE_HANDLE_IMAGE( DEMO_IMAGE_DIR "resize-handle.png" );
37 const int MARGIN_SIZE = 10;
39 const char* const NEXT_BUTTON_ID = "NEXT_BUTTON";
40 const char* const PREVIOUS_BUTTON_ID = "PREVIOUS_BUTTON";
41 const char * const DALI_ICON_PLAY = DEMO_IMAGE_DIR "icon-play.png";
43 const char* const FITTING_BUTTON_ID = "FITTING_BUTTON";
44 const char* const SAMPLING_BUTTON_ID = "SAMPLING_BUTTON";
45 const char* const FITTING_BUTTON_TEXT = "Fitting";
46 const char* const SAMPLING_BUTTON_TEXT = "Sampling";
48 const char* const STYLE_LABEL_TEXT = "ImageScalingGroupLabel";
49 const char* const STYLE_BUTTON_TEXT = "ImageScalingButton";
51 const char* IMAGE_PATHS[] =
53 // Variety of sizes, shapes and formats:
54 DEMO_IMAGE_DIR "dali-logo.png",
55 DEMO_IMAGE_DIR "layer1.png",
56 DEMO_IMAGE_DIR "layer2.png",
57 DEMO_IMAGE_DIR "animation-list.png",
58 DEMO_IMAGE_DIR "music-libray-main-screen.png",
59 DEMO_IMAGE_DIR "music-libray-record-cover.png",
60 DEMO_IMAGE_DIR "contacts-background.png",
61 DEMO_IMAGE_DIR "portrait_screen_primitive_shapes.gif",
62 DEMO_IMAGE_DIR "landscape_screen_primitive_shapes.gif",
63 DEMO_IMAGE_DIR "square_primitive_shapes.bmp",
64 DEMO_IMAGE_DIR "gallery-large-14.jpg",
65 DEMO_IMAGE_DIR "book-landscape-cover.jpg",
66 DEMO_IMAGE_DIR "book-portrait-p1.jpg",
67 DEMO_IMAGE_DIR "book-landscape-cover-back.jpg",
69 // Worst case for aliasing in downscaling, 2k x 2k 1 bit per pixel dithered
70 // black and white image:
71 DEMO_IMAGE_DIR "gallery-large-14.wbmp",
73 DEMO_IMAGE_DIR "background-1.jpg",
74 DEMO_IMAGE_DIR "background-blocks.jpg",
75 DEMO_IMAGE_DIR "background-magnifier.jpg",
76 DEMO_IMAGE_DIR "gallery-large-14.jpg",
79 const int NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u;
81 /** Cycle the scaling mode options. */
82 FittingMode::Type NextScalingMode( FittingMode::Type oldMode )
84 FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
87 case FittingMode::SHRINK_TO_FIT:
88 newMode = FittingMode::SCALE_TO_FILL;
90 case FittingMode::SCALE_TO_FILL:
91 newMode = FittingMode::FIT_WIDTH;
93 case FittingMode::FIT_WIDTH:
94 newMode = FittingMode::FIT_HEIGHT;
96 case FittingMode::FIT_HEIGHT:
97 newMode = FittingMode::SHRINK_TO_FIT;
103 /** Cycle through filter mode options. */
104 SamplingMode::Type NextFilterMode( SamplingMode::Type oldMode )
106 SamplingMode::Type newMode = SamplingMode::BOX;
110 case SamplingMode::BOX:
111 newMode = SamplingMode::NEAREST;
113 case SamplingMode::NEAREST:
114 newMode = SamplingMode::LINEAR;
116 case SamplingMode::LINEAR:
117 newMode = SamplingMode::BOX_THEN_NEAREST;
119 case SamplingMode::BOX_THEN_NEAREST:
120 newMode = SamplingMode::BOX_THEN_LINEAR;
122 case SamplingMode::BOX_THEN_LINEAR:
123 newMode = SamplingMode::NO_FILTER;
125 case SamplingMode::NO_FILTER:
126 newMode = SamplingMode::BOX;
128 case SamplingMode::DONT_CARE:
129 newMode = SamplingMode::BOX;
135 const char* StringFromScalingMode( FittingMode::Type scalingMode )
137 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";
140 const char* StringFromFilterMode( SamplingMode::Type filterMode )
142 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";
147 // This example shows the load-time image scaling and filtering features.
149 class ImageScalingAndFilteringController : public ConnectionTracker
153 ImageScalingAndFilteringController( Application& application )
154 : mApplication( application ),
155 mLastPinchScale( 1.0f ),
156 mImageStageScale( 0.5f, 0.5f ),
158 mFittingMode( FittingMode::FIT_WIDTH ),
159 mSamplingMode( SamplingMode::BOX_THEN_LINEAR),
160 mImageLoading( false ),
161 mQueuedImageLoad( false )
163 // Connect to the Application's Init signal
164 mApplication.InitSignal().Connect( this, &ImageScalingAndFilteringController::Create );
167 ~ImageScalingAndFilteringController()
169 // Nothing to do here;
172 // The Init signal is received once (only) during the Application lifetime
173 void Create( Application& application )
175 // Get a handle to the stage
176 Stage stage = Stage::GetCurrent();
178 // Hide the indicator bar
179 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
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, stage.GetSize().width );
186 backgroundImage.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, stage.GetSize().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.SetAnchorPoint( AnchorPoint::TOP_LEFT );
193 background.SetSize( stage.GetSize() );
194 stage.Add( background );
196 BufferImage heightBackground = BufferImage::WHITE();
197 PixelBuffer* const heightPixel = heightBackground.GetBuffer();
198 heightPixel[0] = 0x8f;
199 heightPixel[1] = 0x8f;
200 heightPixel[2] = 0x8f;
202 BufferImage widthBackground = BufferImage::WHITE();
203 PixelBuffer* const widthPixel = widthBackground.GetBuffer();
204 widthPixel[0] = 0x4f;
205 widthPixel[1] = 0x4f;
206 widthPixel[2] = 0x4f;
208 mHeightBox = Toolkit::ImageView::New( heightBackground );
209 mHeightBox.SetOpacity( 0.2f );
210 background.Add( mHeightBox );
212 mWidthBox = Toolkit::ImageView::New( widthBackground );
213 mWidthBox.SetOpacity( 0.2f );
214 background.Add( mWidthBox );
216 mDesiredBox = Toolkit::ImageView::New( BORDER_IMAGE );
217 background.Add( mDesiredBox );
219 mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
220 mDesiredBox.SetParentOrigin( ParentOrigin::CENTER );
221 mDesiredBox.SetAnchorPoint( AnchorPoint::CENTER );
223 mHeightBox.SetSize( stage.GetSize().width, (stage.GetSize() * mImageStageScale).height );
224 mHeightBox.SetParentOrigin( ParentOrigin::CENTER );
225 mHeightBox.SetAnchorPoint( AnchorPoint::CENTER );
227 mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
228 mWidthBox.SetParentOrigin( ParentOrigin::CENTER );
229 mWidthBox.SetAnchorPoint( AnchorPoint::CENTER );
231 // Initialize the actor
232 mImageView = Toolkit::ImageView::New( IMAGE_PATHS[ 0 ] );
234 // Reposition the actor
235 mImageView.SetParentOrigin( ParentOrigin::CENTER );
236 mImageView.SetAnchorPoint( AnchorPoint::CENTER );
238 // Display the actor on the stage
239 mDesiredBox.Add( mImageView );
241 mImageView.SetSize( stage.GetSize() * mImageStageScale );
243 // Setup the pinch detector for scaling the desired image load dimensions:
244 mPinchDetector = PinchGestureDetector::New();
245 mPinchDetector.Attach( mImageView );
246 mPinchDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPinch );
248 mGrabCorner = Toolkit::ImageView::New( RESIZE_HANDLE_IMAGE );
249 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
250 mGrabCorner.SetName( "GrabCorner" );
251 mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
252 mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
253 mGrabCorner.SetPosition( -BORDER_WIDTH, -BORDER_WIDTH );
254 mGrabCorner.SetOpacity( 0.6f );
256 Layer grabCornerLayer = Layer::New();
257 grabCornerLayer.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
258 grabCornerLayer.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
259 grabCornerLayer.Add( mGrabCorner );
260 mDesiredBox.Add( grabCornerLayer );
262 mPanGestureDetector = PanGestureDetector::New();
263 mPanGestureDetector.Attach( mGrabCorner );
264 mPanGestureDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPan );
266 // Tie-in input event handlers:
267 stage.KeyEventSignal().Connect( this, &ImageScalingAndFilteringController::OnKeyEvent );
275 * Create the GUI controls which float above the scene
277 void CreateControls()
279 Stage stage = Stage::GetCurrent();
281 Dali::Layer controlsLayer = Dali::Layer::New();
282 controlsLayer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
283 controlsLayer.SetSizeModeFactor( Vector3( 1.0f, 1.0f, 1.0f ) );
284 controlsLayer.SetAnchorPoint( AnchorPoint::TOP_LEFT);
285 controlsLayer.SetParentOrigin( ParentOrigin::TOP_LEFT);
286 stage.Add( controlsLayer );
288 // Back and next image buttons in corners of stage:
289 unsigned int playWidth = std::min( stage.GetSize().x * (1 / 5.0f), 58.0f );
290 Toolkit::ImageView imagePrevious = Toolkit::ImageView::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ) );
292 // Last image button:
293 imagePrevious.SetAnchorPoint( AnchorPoint::TOP_LEFT );
294 imagePrevious.RotateBy( Radian(3.14159265358979323846f), Vector3( 0, 1.0f, 0 ) );
295 imagePrevious.SetY( playWidth * 0.5f );
296 imagePrevious.SetX( playWidth + playWidth * 0.5f );
297 imagePrevious.SetOpacity( 0.6f );
298 controlsLayer.Add( imagePrevious );
299 imagePrevious.SetName( PREVIOUS_BUTTON_ID );
300 imagePrevious.TouchSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
302 // Next image button:
303 Toolkit::ImageView imageNext = Toolkit::ImageView::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ) );
304 imageNext.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
305 imageNext.SetY( playWidth * 0.5f );
306 imageNext.SetX( stage.GetSize().x - playWidth * 0.5f );
307 imageNext.SetOpacity( 0.6f );
308 controlsLayer.Add( imageNext );
309 imageNext.SetName( NEXT_BUTTON_ID );
310 imageNext.TouchSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
312 // Buttons to popup selectors for fitting and sampling modes:
314 // Wrapper table to hold two buttons side by side:
315 Toolkit::TableView modesGroupBackground = Toolkit::TableView::New( 1, 2 );
316 modesGroupBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
317 modesGroupBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
318 modesGroupBackground.SetBackgroundColor( BACKGROUND_COLOUR );
319 modesGroupBackground.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE ) );
320 modesGroupBackground.SetFitHeight( 0 );
322 modesGroupBackground.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
323 modesGroupBackground.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
324 modesGroupBackground.SetPosition( 0.0f, 0.0f );
326 controlsLayer.Add( modesGroupBackground );
329 // Vertical table to hold label and button:
330 Toolkit::TableView fittingModeGroup = Toolkit::TableView::New( 2, 1 );
331 fittingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
332 fittingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
333 fittingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
334 fittingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
335 fittingModeGroup.SetFitHeight( 0 );
336 fittingModeGroup.SetFitHeight( 1 );
338 TextLabel label = TextLabel::New( "Image fitting mode:" );
339 label.SetStyleName( STYLE_LABEL_TEXT );
340 fittingModeGroup.Add( label );
342 Toolkit::PushButton button = CreateButton( FITTING_BUTTON_ID, StringFromScalingMode( mFittingMode ) );
343 fittingModeGroup.Add( button );
344 mFittingModeButton = button;
346 modesGroupBackground.Add( fittingModeGroup );
350 // Vertical table to hold label and button:
351 Toolkit::TableView samplingModeGroup = Toolkit::TableView::New( 2, 1 );
352 samplingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
353 samplingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
354 samplingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
355 samplingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
356 samplingModeGroup.SetFitHeight( 0 );
357 samplingModeGroup.SetFitHeight( 1 );
359 TextLabel label = TextLabel::New( "Image sampling mode:" );
360 label.SetStyleName( STYLE_LABEL_TEXT );
361 samplingModeGroup.Add( label );
363 Toolkit::PushButton button = CreateButton( SAMPLING_BUTTON_ID, StringFromFilterMode( mSamplingMode ) );
364 samplingModeGroup.Add( button );
365 mSamplingModeButton = button;
367 modesGroupBackground.Add( samplingModeGroup );
371 Toolkit::PushButton CreateButton( const char * id, const char * label )
373 Toolkit::PushButton button = Toolkit::PushButton::New();
374 button.SetStyleName( STYLE_BUTTON_TEXT );
375 button.SetName( id );
376 button.SetProperty( Toolkit::Button::Property::LABEL, label );
377 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
378 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
379 button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
383 Toolkit::Popup CreatePopup()
385 Stage stage = Stage::GetCurrent();
386 const float POPUP_WIDTH_DP = stage.GetSize().width * 0.75f;
388 Toolkit::Popup popup = Toolkit::Popup::New();
389 popup.SetName( "POPUP" );
390 popup.SetParentOrigin( ParentOrigin::CENTER );
391 popup.SetAnchorPoint( AnchorPoint::CENTER );
392 popup.SetSize( POPUP_WIDTH_DP, 0.0f );
394 popup.OutsideTouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnPopupOutsideTouched );
399 Toolkit::PushButton CreatePopupButton( Actor parent, const char* id )
401 Toolkit::PushButton button = Toolkit::PushButton::New();
402 button.SetName( id );
403 button.SetProperty( Toolkit::Button::Property::LABEL, id );
405 button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
406 button.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
407 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
408 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
410 button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
412 parent.Add( button );
416 bool OnButtonClicked( Toolkit::Button button )
418 if( button.GetName() == FITTING_BUTTON_ID )
420 mPopup = CreatePopup();
422 // Four-row table to hold buttons:
423 Toolkit::TableView fittingModes = Toolkit::TableView::New( 4, 1 );
424 fittingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
425 fittingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
426 fittingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
427 fittingModes.SetFitHeight( 0 );
428 fittingModes.SetFitHeight( 1 );
429 fittingModes.SetFitHeight( 2 );
430 fittingModes.SetFitHeight( 3 );
432 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SCALE_TO_FILL ) );
433 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SHRINK_TO_FIT ) );
434 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_WIDTH ) );
435 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_HEIGHT ) );
437 mPopup.SetContent( fittingModes );
438 Stage::GetCurrent().Add( mPopup );
439 mPopup.SetDisplayState( Toolkit::Popup::SHOWN );
441 else if( button.GetName() == SAMPLING_BUTTON_ID )
443 mPopup = CreatePopup();
445 // Table to hold buttons for each sampling mode:
446 Toolkit::TableView samplingModes = Toolkit::TableView::New( 6, 1 );
447 samplingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
448 samplingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
449 samplingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
450 samplingModes.SetFitHeight( 0 );
451 samplingModes.SetFitHeight( 1 );
452 samplingModes.SetFitHeight( 2 );
453 samplingModes.SetFitHeight( 3 );
454 samplingModes.SetFitHeight( 4 );
455 samplingModes.SetFitHeight( 5 );
457 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NEAREST ) );
458 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::LINEAR ) );
459 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX ) );
460 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_NEAREST ) );
461 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_LINEAR ) );
462 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NO_FILTER ) );
464 mPopup.SetContent( samplingModes );
465 Stage::GetCurrent().Add( mPopup );
466 mPopup.SetDisplayState( Toolkit::Popup::SHOWN );
468 else if( CheckFittingModeButton( button, FittingMode::SCALE_TO_FILL) ||
469 CheckFittingModeButton( button, FittingMode::SHRINK_TO_FIT) ||
470 CheckFittingModeButton( button, FittingMode::FIT_WIDTH) ||
471 CheckFittingModeButton( button, FittingMode::FIT_HEIGHT) )
474 else if( CheckSamplingModeButton( button, SamplingMode::NEAREST ) ||
475 CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
476 CheckSamplingModeButton( button, SamplingMode::BOX ) ||
477 CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
478 CheckSamplingModeButton( button, SamplingMode::BOX_THEN_NEAREST ) ||
479 CheckSamplingModeButton( button, SamplingMode::BOX_THEN_LINEAR ) ||
480 CheckSamplingModeButton( button, SamplingMode::NO_FILTER ) )
486 bool CheckFittingModeButton( Actor &button, FittingMode::Type mode )
488 const char * const modeName = StringFromScalingMode( mode );
489 if( button.GetName() == modeName )
492 mFittingModeButton.SetProperty( Toolkit::Button::Property::LABEL, modeName );
494 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
501 bool CheckSamplingModeButton( Actor &button, SamplingMode::Type mode )
503 const char * const modeName = StringFromFilterMode( mode );
504 if( button.GetName() == modeName )
506 mSamplingMode = mode;
507 mSamplingModeButton.SetProperty( Toolkit::Button::Property::LABEL, modeName );
509 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
516 void OnPopupOutsideTouched()
520 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
525 bool OnControlTouched( Actor actor, const TouchData& event )
527 if(event.GetPointCount() > 0)
529 switch( event.GetState( 0 ) )
533 const std::string & name = actor.GetName();
534 if( name == NEXT_BUTTON_ID )
536 mCurrentPath = mCurrentPath + 1;
537 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
540 else if( name == PREVIOUS_BUTTON_ID )
542 mCurrentPath = mCurrentPath - 1;
543 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
558 void OnPinch( Actor actor, const PinchGesture& pinch )
560 if( pinch.state == Gesture::Started )
562 mLastPinchScale = pinch.scale;
564 const float scale = pinch.scale;
566 if( ! Equals( scale, mLastPinchScale ) )
568 if ( scale < mLastPinchScale )
570 mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
571 mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
575 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
576 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
580 mLastPinchScale = scale;
583 void OnPan( Actor actor, const PanGesture& gesture )
585 Stage stage = Stage::GetCurrent();
586 // 1.0f and 0.75f are the maximum size caps of the resized image, as a factor of stage-size.
587 mImageStageScale.x = std::max( 0.05f, std::min( 0.95f, mImageStageScale.x + ( gesture.displacement.x * 2.0f / stage.GetSize().width ) ) );
588 mImageStageScale.y = std::max( 0.05f, std::min( 0.70f, mImageStageScale.y + ( gesture.displacement.y * 2.0f / stage.GetSize().height ) ) );
593 void OnKeyEvent(const KeyEvent& event)
595 if( event.state == KeyEvent::Down )
597 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
599 if( mPopup && mPopup.IsVisible() )
601 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
609 else if ( event.keyPressedName == "Right" )
611 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
613 else if ( event.keyPressedName == "Left" )
615 mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
617 else if ( event.keyPressedName == "Up" )
619 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
621 else if ( event.keyPressedName == "Down" )
623 mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
625 else if ( event.keyPressedName == "o" )
627 mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
628 mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
630 else if ( event.keyPressedName == "p" )
632 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
633 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
635 else if ( event.keyPressedName == "n" )
637 mCurrentPath = mCurrentPath + 1;
638 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
640 else if ( event.keyPressedName == "b" )
642 mCurrentPath = mCurrentPath - 1;
643 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
645 // Cycle filter and scaling modes:
646 else if ( event.keyPressedName == "f" )
648 mSamplingMode = NextFilterMode( mSamplingMode );
649 mSamplingModeButton.SetProperty( Toolkit::Button::Property::LABEL, StringFromFilterMode( mSamplingMode ) );
651 // Cycle filter and scaling modes:
652 else if ( event.keyPressedName == "s" )
654 mFittingMode = NextScalingMode( mFittingMode );
655 mFittingModeButton.SetProperty( Toolkit::Button::Property::LABEL, StringFromScalingMode( mFittingMode ) );
670 mImageLoading = true;
672 const char * const path = IMAGE_PATHS[ mCurrentPath ];
673 Stage stage = Stage::GetCurrent();
674 Size imageSize = stage.GetSize() * mImageStageScale;
675 mImageView.SetSize( imageSize );
678 map[Toolkit::ImageVisual::Property::URL] = path;
679 map[Toolkit::ImageVisual::Property::DESIRED_WIDTH] = imageSize.x;
680 map[Toolkit::ImageVisual::Property::DESIRED_HEIGHT] = imageSize.y;
681 map[Toolkit::ImageVisual::Property::FITTING_MODE] = mFittingMode;
682 map[Toolkit::ImageVisual::Property::SAMPLING_MODE] = mSamplingMode;
684 mImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, map );
691 Stage stage = Stage::GetCurrent();
692 Size imageSize = stage.GetSize() * mImageStageScale;
696 // Border size needs to be modified to take into account the width of the frame.
697 Vector2 borderScale( ( imageSize + Vector2( BORDER_WIDTH * 2.0f, BORDER_WIDTH * 2.0f ) ) / stage.GetSize() );
698 mDesiredBox.SetSize( stage.GetSize() * borderScale );
700 mHeightBox.SetSize( stage.GetSize().width, (stage.GetSize() * mImageStageScale).height );
701 mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
705 Application& mApplication;
706 Toolkit::ImageView mDesiredBox; //< Background rectangle to show requested image size.
707 Toolkit::ImageView mHeightBox; //< Background horizontal stripe to show requested image height.
708 Toolkit::ImageView mWidthBox; //< Background vertical stripe to show requested image width.
709 Toolkit::PushButton mFittingModeButton;
710 Toolkit::PushButton mSamplingModeButton;
711 Toolkit::Popup mPopup;
712 PinchGestureDetector mPinchDetector;
713 float mLastPinchScale;
714 Toolkit::ImageView mGrabCorner;
715 PanGestureDetector mPanGestureDetector;
716 Toolkit::ImageView mImageView;
717 Vector2 mImageStageScale;
719 FittingMode::Type mFittingMode;
720 SamplingMode::Type mSamplingMode;
722 bool mQueuedImageLoad;
726 void RunTest( Application& application )
728 ImageScalingAndFilteringController test( application );
730 application.MainLoop();
733 // Entry point for Linux & Tizen applications
734 int DALI_EXPORT_API main( int argc, char **argv )
736 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
738 RunTest( application );