2 * Copyright (c) 2014 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 = "grouplabel";
49 const char* const STYLE_BUTTON_TEXT = "buttonlabel";
52 const char* IMAGE_PATHS[] =
54 // Variety of sizes, shapes and formats:
55 DEMO_IMAGE_DIR "dali-logo.png",
56 DEMO_IMAGE_DIR "layer1.png",
57 DEMO_IMAGE_DIR "layer2.png",
58 DEMO_IMAGE_DIR "animation-list.png",
59 DEMO_IMAGE_DIR "music-libray-main-screen.png",
60 DEMO_IMAGE_DIR "music-libray-record-cover.png",
61 DEMO_IMAGE_DIR "contacts-background.png",
62 DEMO_IMAGE_DIR "portrait_screen_primitive_shapes.gif",
63 DEMO_IMAGE_DIR "landscape_screen_primitive_shapes.gif",
64 DEMO_IMAGE_DIR "square_primitive_shapes.bmp",
65 DEMO_IMAGE_DIR "gallery-large-14.jpg",
66 DEMO_IMAGE_DIR "book-landscape-cover.jpg",
67 DEMO_IMAGE_DIR "book-portrait-p1.jpg",
68 DEMO_IMAGE_DIR "book-landscape-cover-back.jpg",
70 // Worst case for aliasing in downscaling, 2k x 2k 1 bit per pixel dithered
71 // black and white image:
72 DEMO_IMAGE_DIR "gallery-large-14.wbmp",
74 DEMO_IMAGE_DIR "background-1.jpg",
75 DEMO_IMAGE_DIR "background-blocks.jpg",
76 DEMO_IMAGE_DIR "background-magnifier.jpg",
77 DEMO_IMAGE_DIR "gallery-large-14.jpg",
80 const int NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u;
82 /** Cycle the scaling mode options. */
83 FittingMode::Type NextScalingMode( FittingMode::Type oldMode )
85 FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
88 case FittingMode::SHRINK_TO_FIT:
89 newMode = FittingMode::SCALE_TO_FILL;
91 case FittingMode::SCALE_TO_FILL:
92 newMode = FittingMode::FIT_WIDTH;
94 case FittingMode::FIT_WIDTH:
95 newMode = FittingMode::FIT_HEIGHT;
97 case FittingMode::FIT_HEIGHT:
98 newMode = FittingMode::SHRINK_TO_FIT;
104 /** Cycle through filter mode options. */
105 SamplingMode::Type NextFilterMode( SamplingMode::Type oldMode )
107 SamplingMode::Type newMode = SamplingMode::BOX;
111 case SamplingMode::BOX:
112 newMode = SamplingMode::NEAREST;
114 case SamplingMode::NEAREST:
115 newMode = SamplingMode::LINEAR;
117 case SamplingMode::LINEAR:
118 newMode = SamplingMode::BOX_THEN_NEAREST;
120 case SamplingMode::BOX_THEN_NEAREST:
121 newMode = SamplingMode::BOX_THEN_LINEAR;
123 case SamplingMode::BOX_THEN_LINEAR:
124 newMode = SamplingMode::NO_FILTER;
126 case SamplingMode::NO_FILTER:
127 newMode = SamplingMode::BOX;
129 case SamplingMode::DONT_CARE:
130 newMode = SamplingMode::BOX;
136 const char* StringFromScalingMode( FittingMode::Type scalingMode )
138 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";
141 const char* StringFromFilterMode( SamplingMode::Type filterMode )
143 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";
148 // This example shows the load-time image scaling and filtering features.
150 class ImageScalingAndFilteringController : public ConnectionTracker
154 ImageScalingAndFilteringController( Application& application )
155 : mApplication( application ),
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();
179 Dali::Property::Map backgroundImage;
180 backgroundImage.Insert( "rendererType", "image" );
181 backgroundImage.Insert( "url", BACKGROUND_IMAGE );
182 backgroundImage.Insert( "desiredWidth", stage.GetSize().width );
183 backgroundImage.Insert( "desiredHeight", stage.GetSize().height );
184 backgroundImage.Insert( "fittingMode", "SCALE_TO_FILL" );
185 backgroundImage.Insert( "samplingMode", "BOX_THEN_NEAREST" );
187 Toolkit::ImageView background = Toolkit::ImageView::New();
188 background.SetProperty( Toolkit::ImageView::Property::IMAGE, backgroundImage );
189 background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
190 background.SetSize( stage.GetSize() );
191 stage.Add( background );
193 BufferImage heightBackground = BufferImage::WHITE();
194 PixelBuffer* const heightPixel = heightBackground.GetBuffer();
195 heightPixel[0] = 0x8f;
196 heightPixel[1] = 0x8f;
197 heightPixel[2] = 0x8f;
199 BufferImage widthBackground = BufferImage::WHITE();
200 PixelBuffer* const widthPixel = widthBackground.GetBuffer();
201 widthPixel[0] = 0x4f;
202 widthPixel[1] = 0x4f;
203 widthPixel[2] = 0x4f;
205 mHeightBox = Toolkit::ImageView::New( heightBackground );
206 mHeightBox.SetOpacity( 0.2f );
207 background.Add( mHeightBox );
209 mWidthBox = Toolkit::ImageView::New( widthBackground );
210 mWidthBox.SetOpacity( 0.2f );
211 background.Add( mWidthBox );
213 mDesiredBox = Toolkit::ImageView::New( BORDER_IMAGE );
214 background.Add( mDesiredBox );
216 mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
217 mDesiredBox.SetParentOrigin( ParentOrigin::CENTER );
218 mDesiredBox.SetAnchorPoint( AnchorPoint::CENTER );
220 mHeightBox.SetSize( stage.GetSize().width, (stage.GetSize() * mImageStageScale).height );
221 mHeightBox.SetParentOrigin( ParentOrigin::CENTER );
222 mHeightBox.SetAnchorPoint( AnchorPoint::CENTER );
224 mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
225 mWidthBox.SetParentOrigin( ParentOrigin::CENTER );
226 mWidthBox.SetAnchorPoint( AnchorPoint::CENTER );
228 // Initialize the actor
229 mImageView = Toolkit::ImageView::New( IMAGE_PATHS[ 0 ] );
231 // Reposition the actor
232 mImageView.SetParentOrigin( ParentOrigin::CENTER );
233 mImageView.SetAnchorPoint( AnchorPoint::CENTER );
235 // Display the actor on the stage
236 mDesiredBox.Add( mImageView );
238 mImageView.SetSize( stage.GetSize() * mImageStageScale );
240 // Setup the pinch detector for scaling the desired image load dimensions:
241 mPinchDetector = PinchGestureDetector::New();
242 mPinchDetector.Attach( mImageView );
243 mPinchDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPinch );
245 mGrabCorner = Toolkit::ImageView::New( RESIZE_HANDLE_IMAGE );
246 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
247 mGrabCorner.SetName( "GrabCorner" );
248 mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
249 mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
250 mGrabCorner.SetPosition( -BORDER_WIDTH, -BORDER_WIDTH );
251 mGrabCorner.SetOpacity( 0.6f );
253 Layer grabCornerLayer = Layer::New();
254 grabCornerLayer.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
255 grabCornerLayer.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
256 grabCornerLayer.Add( mGrabCorner );
257 mDesiredBox.Add( grabCornerLayer );
259 mPanGestureDetector = PanGestureDetector::New();
260 mPanGestureDetector.Attach( mGrabCorner );
261 mPanGestureDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPan );
263 // Tie-in input event handlers:
264 stage.KeyEventSignal().Connect( this, &ImageScalingAndFilteringController::OnKeyEvent );
272 * Create the GUI controls which float above the scene
274 void CreateControls()
276 Stage stage = Stage::GetCurrent();
278 Dali::Layer controlsLayer = Dali::Layer::New();
279 controlsLayer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
280 controlsLayer.SetSizeModeFactor( Vector3( 1.0f, 1.0f, 1.0f ) );
281 controlsLayer.SetAnchorPoint( AnchorPoint::TOP_LEFT);
282 controlsLayer.SetParentOrigin( ParentOrigin::TOP_LEFT);
283 stage.Add( controlsLayer );
285 // Back and next image buttons in corners of stage:
286 unsigned int playWidth = std::min( stage.GetSize().x * (1 / 5.0f), 58.0f );
287 Image playImage = ResourceImage::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ), FittingMode::SHRINK_TO_FIT, SamplingMode::BOX_THEN_LINEAR );
288 Toolkit::ImageView imagePrevious = Toolkit::ImageView::New( playImage );
290 // Last image button:
291 imagePrevious.SetAnchorPoint( AnchorPoint::TOP_LEFT );
292 imagePrevious.RotateBy( Radian(3.14159265358979323846f), Vector3( 0, 1.0f, 0 ) );
293 imagePrevious.SetY( playWidth * 0.5f );
294 imagePrevious.SetX( playWidth + playWidth * 0.5f );
295 imagePrevious.SetOpacity( 0.6f );
296 controlsLayer.Add( imagePrevious );
297 imagePrevious.SetName( PREVIOUS_BUTTON_ID );
298 imagePrevious.TouchSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
300 // Next image button:
301 Toolkit::ImageView imageNext = Toolkit::ImageView::New( playImage );
302 imageNext.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
303 imageNext.SetY( playWidth * 0.5f );
304 imageNext.SetX( stage.GetSize().x - playWidth * 0.5f );
305 imageNext.SetOpacity( 0.6f );
306 controlsLayer.Add( imageNext );
307 imageNext.SetName( NEXT_BUTTON_ID );
308 imageNext.TouchSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
310 // Buttons to popup selectors for fitting and sampling modes:
312 // Wrapper table to hold two buttons side by side:
313 Toolkit::TableView modesGroupBackground = Toolkit::TableView::New( 1, 2 );
314 modesGroupBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
315 modesGroupBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
316 modesGroupBackground.SetBackgroundColor( BACKGROUND_COLOUR );
317 modesGroupBackground.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE ) );
318 modesGroupBackground.SetFitHeight( 0 );
320 modesGroupBackground.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
321 modesGroupBackground.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
322 modesGroupBackground.SetPosition( 0.0f, 0.0f );
324 controlsLayer.Add( modesGroupBackground );
327 // Vertical table to hold label and button:
328 Toolkit::TableView fittingModeGroup = Toolkit::TableView::New( 2, 1 );
329 fittingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
330 fittingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
331 fittingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
332 fittingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
333 fittingModeGroup.SetFitHeight( 0 );
334 fittingModeGroup.SetFitHeight( 1 );
336 TextLabel label = TextLabel::New( "Image fitting mode:" );
337 label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
338 fittingModeGroup.Add( label );
340 Toolkit::PushButton button = CreateButton( FITTING_BUTTON_ID, StringFromScalingMode( mFittingMode ) );
341 fittingModeGroup.Add( button );
342 mFittingModeButton = button;
344 modesGroupBackground.Add( fittingModeGroup );
348 // Vertical table to hold label and button:
349 Toolkit::TableView samplingModeGroup = Toolkit::TableView::New( 2, 1 );
350 samplingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
351 samplingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
352 samplingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
353 samplingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
354 samplingModeGroup.SetFitHeight( 0 );
355 samplingModeGroup.SetFitHeight( 1 );
357 TextLabel label = TextLabel::New( "Image sampling mode:" );
358 label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
359 samplingModeGroup.Add( label );
361 Toolkit::PushButton button = CreateButton( SAMPLING_BUTTON_ID, StringFromFilterMode( mSamplingMode ) );
362 samplingModeGroup.Add( button );
363 mSamplingModeButton = button;
365 modesGroupBackground.Add( samplingModeGroup );
369 Toolkit::PushButton CreateButton( const char * id, const char * label )
371 Toolkit::PushButton button = Toolkit::PushButton::New();
372 button.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
373 button.SetName( id );
374 button.SetLabelText( label );
375 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
376 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
377 button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
381 Toolkit::Popup CreatePopup()
383 Stage stage = Stage::GetCurrent();
384 const float POPUP_WIDTH_DP = stage.GetSize().width * 0.75f;
386 Toolkit::Popup popup = Toolkit::Popup::New();
387 popup.SetName( "POPUP" );
388 popup.SetParentOrigin( ParentOrigin::CENTER );
389 popup.SetAnchorPoint( AnchorPoint::CENTER );
390 popup.SetSize( POPUP_WIDTH_DP, 0.0f );
392 popup.OutsideTouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnPopupOutsideTouched );
397 Toolkit::PushButton CreatePopupButton( Actor parent, const char* id )
399 Toolkit::PushButton button = Toolkit::PushButton::New();
400 button.SetName( id );
401 button.SetLabelText( id );
403 button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
404 button.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
405 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
406 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
408 button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
410 parent.Add( button );
414 bool OnButtonClicked( Toolkit::Button button )
416 if( button.GetName() == FITTING_BUTTON_ID )
418 mPopup = CreatePopup();
420 // Four-row table to hold buttons:
421 Toolkit::TableView fittingModes = Toolkit::TableView::New( 4, 1 );
422 fittingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
423 fittingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
424 fittingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
425 fittingModes.SetFitHeight( 0 );
426 fittingModes.SetFitHeight( 1 );
427 fittingModes.SetFitHeight( 2 );
428 fittingModes.SetFitHeight( 3 );
430 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SCALE_TO_FILL ) );
431 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SHRINK_TO_FIT ) );
432 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_WIDTH ) );
433 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_HEIGHT ) );
435 mPopup.SetContent( fittingModes );
436 Stage::GetCurrent().Add( mPopup );
437 mPopup.SetDisplayState( Toolkit::Popup::SHOWN );
439 else if( button.GetName() == SAMPLING_BUTTON_ID )
441 mPopup = CreatePopup();
443 // Table to hold buttons for each sampling mode:
444 Toolkit::TableView samplingModes = Toolkit::TableView::New( 6, 1 );
445 samplingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
446 samplingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
447 samplingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
448 samplingModes.SetFitHeight( 0 );
449 samplingModes.SetFitHeight( 1 );
450 samplingModes.SetFitHeight( 2 );
451 samplingModes.SetFitHeight( 3 );
452 samplingModes.SetFitHeight( 4 );
453 samplingModes.SetFitHeight( 5 );
455 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NEAREST ) );
456 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::LINEAR ) );
457 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX ) );
458 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_NEAREST ) );
459 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_LINEAR ) );
460 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NO_FILTER ) );
462 mPopup.SetContent( samplingModes );
463 Stage::GetCurrent().Add( mPopup );
464 mPopup.SetDisplayState( Toolkit::Popup::SHOWN );
466 else if( CheckFittingModeButton( button, FittingMode::SCALE_TO_FILL) ||
467 CheckFittingModeButton( button, FittingMode::SHRINK_TO_FIT) ||
468 CheckFittingModeButton( button, FittingMode::FIT_WIDTH) ||
469 CheckFittingModeButton( button, FittingMode::FIT_HEIGHT) )
472 else if( CheckSamplingModeButton( button, SamplingMode::NEAREST ) ||
473 CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
474 CheckSamplingModeButton( button, SamplingMode::BOX ) ||
475 CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
476 CheckSamplingModeButton( button, SamplingMode::BOX_THEN_NEAREST ) ||
477 CheckSamplingModeButton( button, SamplingMode::BOX_THEN_LINEAR ) ||
478 CheckSamplingModeButton( button, SamplingMode::NO_FILTER ) )
484 bool CheckFittingModeButton( Actor &button, FittingMode::Type mode )
486 const char * const modeName = StringFromScalingMode( mode );
487 if( button.GetName() == modeName )
490 mFittingModeButton.SetLabelText( modeName );
492 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
499 bool CheckSamplingModeButton( Actor &button, SamplingMode::Type mode )
501 const char * const modeName = StringFromFilterMode( mode );
502 if( button.GetName() == modeName )
504 mSamplingMode = mode;
505 mSamplingModeButton.SetLabelText( modeName );
507 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
514 void OnPopupOutsideTouched()
518 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
523 void OnImageLoaded( ResourceImage image )
525 DALI_ASSERT_DEBUG( image == mNextImage );
526 mImageView.SetImage( image );
527 mImageView.SetSize( Size( image.GetWidth(), image.GetHeight() ) );
528 mImageLoading = false;
530 // We have finished loading, if a resize had occured during the load, trigger another load now.
531 if( mQueuedImageLoad )
533 mQueuedImageLoad = false;
538 bool OnControlTouched( Actor actor, const TouchData& event )
540 if(event.GetPointCount() > 0)
542 switch( event.GetState( 0 ) )
546 const std::string & name = actor.GetName();
547 if( name == NEXT_BUTTON_ID )
549 mCurrentPath = mCurrentPath + 1;
550 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
553 else if( name == PREVIOUS_BUTTON_ID )
555 mCurrentPath = mCurrentPath - 1;
556 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
571 void OnPinch( Actor actor, const PinchGesture& pinch )
573 if( pinch.state == Gesture::Started )
575 mLastPinchScale = pinch.scale;
577 const float scale = pinch.scale;
579 if( ! Equals( scale, mLastPinchScale ) )
581 if ( scale < mLastPinchScale )
583 mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
584 mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
588 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
589 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
593 mLastPinchScale = scale;
596 void OnPan( Actor actor, const PanGesture& gesture )
598 Stage stage = Stage::GetCurrent();
599 // 1.0f and 0.75f are the maximum size caps of the resized image, as a factor of stage-size.
600 mImageStageScale.x = std::max( 0.05f, std::min( 0.95f, mImageStageScale.x + ( gesture.displacement.x * 2.0f / stage.GetSize().width ) ) );
601 mImageStageScale.y = std::max( 0.05f, std::min( 0.70f, mImageStageScale.y + ( gesture.displacement.y * 2.0f / stage.GetSize().height ) ) );
606 void OnKeyEvent(const KeyEvent& event)
608 if( event.state == KeyEvent::Down )
610 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
612 if( mPopup && mPopup.IsVisible() )
614 mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
622 else if ( event.keyPressedName == "Right" )
624 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
626 else if ( event.keyPressedName == "Left" )
628 mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
630 else if ( event.keyPressedName == "Up" )
632 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
634 else if ( event.keyPressedName == "Down" )
636 mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
638 else if ( event.keyPressedName == "o" )
640 mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
641 mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
643 else if ( event.keyPressedName == "p" )
645 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
646 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
648 else if ( event.keyPressedName == "n" )
650 mCurrentPath = mCurrentPath + 1;
651 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
653 else if ( event.keyPressedName == "b" )
655 mCurrentPath = mCurrentPath - 1;
656 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
658 // Cycle filter and scaling modes:
659 else if ( event.keyPressedName == "f" )
661 mSamplingMode = NextFilterMode( mSamplingMode );
662 mSamplingModeButton.SetLabelText( StringFromFilterMode( mSamplingMode ) );
664 // Cycle filter and scaling modes:
665 else if ( event.keyPressedName == "s" )
667 mFittingMode = NextScalingMode( mFittingMode );
668 mFittingModeButton.SetLabelText( StringFromScalingMode( mFittingMode ) );
683 mImageLoading = true;
685 const char * const path = IMAGE_PATHS[ mCurrentPath ];
686 Stage stage = Stage::GetCurrent();
687 Size imageSize = stage.GetSize() * mImageStageScale;
688 const ImageDimensions imageSizeInt = ImageDimensions::FromFloatArray( &imageSize.x );
690 ResourceImage image = ResourceImage::New( path, imageSizeInt, mFittingMode, mSamplingMode );
692 // If the image was cached, the load has already occured, bypass hooking the signal.
693 if( image.GetLoadingState() )
695 OnImageLoaded( image );
699 image.LoadingFinishedSignal().Connect( this, &ImageScalingAndFilteringController::OnImageLoaded );
708 Stage stage = Stage::GetCurrent();
709 Size imageSize = stage.GetSize() * mImageStageScale;
711 // If an image is already loading, queue another load when it has finished.
712 // This way we get continuous updates instead of constantly re-requesting loads.
715 mQueuedImageLoad = true;
722 // Border size needs to be modified to take into account the width of the frame.
723 Vector2 borderScale( ( imageSize + Vector2( BORDER_WIDTH * 2.0f, BORDER_WIDTH * 2.0f ) ) / stage.GetSize() );
724 mDesiredBox.SetSize( stage.GetSize() * borderScale );
726 mHeightBox.SetSize( stage.GetSize().width, (stage.GetSize() * mImageStageScale).height );
727 mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
731 Application& mApplication;
732 Toolkit::ImageView mDesiredBox; //< Background rectangle to show requested image size.
733 Toolkit::ImageView mHeightBox; //< Background horizontal stripe to show requested image height.
734 Toolkit::ImageView mWidthBox; //< Background vertical stripe to show requested image width.
735 Toolkit::PushButton mFittingModeButton;
736 Toolkit::PushButton mSamplingModeButton;
737 Toolkit::Popup mPopup;
738 PinchGestureDetector mPinchDetector;
739 float mLastPinchScale;
740 Toolkit::ImageView mGrabCorner;
741 PanGestureDetector mPanGestureDetector;
742 Toolkit::ImageView mImageView;
743 ResourceImage mNextImage; //< Currently-loading image
744 Vector2 mImageStageScale;
746 FittingMode::Type mFittingMode;
747 SamplingMode::Type mSamplingMode;
749 bool mQueuedImageLoad;
753 void RunTest( Application& application )
755 ImageScalingAndFilteringController test( application );
757 application.MainLoop();
760 // Entry point for Linux & Tizen applications
761 int DALI_EXPORT_API main( int argc, char **argv )
763 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
765 RunTest( application );