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( DALI_IMAGE_DIR "background-gradient.jpg" );
31 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
33 const int MARGIN_SIZE = 10;
35 const char* const NEXT_BUTTON_ID = "NEXT_BUTTON";
36 const char* const PREVIOUS_BUTTON_ID = "PREVIOUS_BUTTON";
37 const char * const DALI_ICON_PLAY = DALI_IMAGE_DIR "icon-play.png";
39 const char* const FITTING_BUTTON_ID = "FITTING_BUTTON";
40 const char* const SAMPLING_BUTTON_ID = "SAMPLING_BUTTON";
41 const char* const FITTING_BUTTON_TEXT = "Fitting";
42 const char* const SAMPLING_BUTTON_TEXT = "Sampling";
44 const char* const STYLE_LABEL_TEXT = "grouplabel";
45 const char* const STYLE_BUTTON_TEXT = "buttonlabel";
49 const char* IMAGE_PATHS[] =
51 // Worst case for aliasing in downscaling, 2k x 2k 1 bit per pixel dithered
52 // black and white image:
53 DALI_IMAGE_DIR "gallery-large-14.wbmp",
54 // Variety of sizes, shapes and formats:
55 DALI_IMAGE_DIR "animation-list.png",
56 DALI_IMAGE_DIR "layer1.png",
57 DALI_IMAGE_DIR "layer2.png",
58 DALI_IMAGE_DIR "music-libray-main-screen.png",
59 DALI_IMAGE_DIR "music-libray-record-cover.png",
60 DALI_IMAGE_DIR "contacts-background.png",
61 DALI_IMAGE_DIR "portrait_screen_primitive_shapes.gif",
62 DALI_IMAGE_DIR "landscape_screen_primitive_shapes.gif",
63 DALI_IMAGE_DIR "square_primitive_shapes.bmp",
64 DALI_IMAGE_DIR "dali-logo.png",
65 DALI_IMAGE_DIR "com.samsung.dali-demo.ico",
66 DALI_IMAGE_DIR "gallery-large-14.jpg",
67 DALI_IMAGE_DIR "book-landscape-cover.jpg",
68 DALI_IMAGE_DIR "book-portrait-p1.jpg",
69 DALI_IMAGE_DIR "book-landscape-cover-back.jpg",
70 DALI_IMAGE_DIR "background-1.jpg",
71 DALI_IMAGE_DIR "background-blocks.jpg",
72 DALI_IMAGE_DIR "background-magnifier.jpg",
73 DALI_IMAGE_DIR "gallery-large-14.jpg",
76 const int NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u;
78 /** Cycle the scaling mode options. */
79 FittingMode::Type NextScalingMode( FittingMode::Type oldMode )
81 FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
84 case FittingMode::SHRINK_TO_FIT:
85 newMode = FittingMode::SCALE_TO_FILL;
87 case FittingMode::SCALE_TO_FILL:
88 newMode = FittingMode::FIT_WIDTH;
90 case FittingMode::FIT_WIDTH:
91 newMode = FittingMode::FIT_HEIGHT;
93 case FittingMode::FIT_HEIGHT:
94 newMode = FittingMode::SHRINK_TO_FIT;
100 /** Cycle through filter mode options. */
101 SamplingMode::Type NextFilterMode( SamplingMode::Type oldMode )
103 SamplingMode::Type newMode = SamplingMode::BOX;
107 case SamplingMode::BOX:
108 newMode = SamplingMode::NEAREST;
110 case SamplingMode::NEAREST:
111 newMode = SamplingMode::LINEAR;
113 case SamplingMode::LINEAR:
114 newMode = SamplingMode::BOX_THEN_NEAREST;
116 case SamplingMode::BOX_THEN_NEAREST:
117 newMode = SamplingMode::BOX_THEN_LINEAR;
119 case SamplingMode::BOX_THEN_LINEAR:
120 newMode = SamplingMode::NO_FILTER;
122 case SamplingMode::NO_FILTER:
123 newMode = SamplingMode::BOX;
125 case SamplingMode::DONT_CARE:
126 newMode = SamplingMode::BOX;
132 const char* StringFromScalingMode( FittingMode::Type scalingMode )
134 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";
137 const char* StringFromFilterMode( SamplingMode::Type filterMode )
139 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";
144 // This example shows the load-time image scaling and filtering features.
146 class ImageScalingAndFilteringController : public ConnectionTracker
150 ImageScalingAndFilteringController( Application& application )
151 : mApplication( application ),
152 mImageStageScale( 0.5f, 0.5f ),
154 mFittingMode( FittingMode::SCALE_TO_FILL ),
155 mSamplingMode( SamplingMode::BOX_THEN_LINEAR)
157 // Connect to the Application's Init signal
158 mApplication.InitSignal().Connect( this, &ImageScalingAndFilteringController::Create );
161 ~ImageScalingAndFilteringController()
163 // Nothing to do here;
166 // The Init signal is received once (only) during the Application lifetime
167 void Create( Application& application )
169 // Get a handle to the stage
170 Stage stage = Stage::GetCurrent();
173 ResourceImage backgroundImage = ResourceImage::New( BACKGROUND_IMAGE, ImageDimensions( stage.GetSize().width, stage.GetSize().height ), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR );
174 Toolkit::ImageView background = Toolkit::ImageView::New( backgroundImage );
175 background.SetZ( -2.0f );
176 background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
177 background.SetSize( stage.GetSize() );
178 stage.Add( background );
180 // Make grey pixels for the desired box, the desired height the desired width:
181 BufferImage desiredBackground = BufferImage::WHITE();
183 BufferImage heightBackground = BufferImage::WHITE();
184 PixelBuffer* const heightPixel = heightBackground.GetBuffer();
185 heightPixel[0] = 0x8f;
186 heightPixel[1] = 0x8f;
187 heightPixel[2] = 0x8f;
189 BufferImage widthBackground = BufferImage::WHITE();
190 PixelBuffer* const widthPixel = widthBackground.GetBuffer();
191 widthPixel[0] = 0x4f;
192 widthPixel[1] = 0x4f;
193 widthPixel[2] = 0x4f;
195 mHeightBox = Toolkit::ImageView::New( heightBackground );
196 mHeightBox.SetOpacity( 0.2f );
197 stage.Add( mHeightBox );
199 mWidthBox = Toolkit::ImageView::New( widthBackground );
200 mWidthBox.SetOpacity( 0.2f );
201 stage.Add( mWidthBox );
203 mDesiredBox = Toolkit::ImageView::New( desiredBackground );
204 stage.Add( mDesiredBox );
206 mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
207 mDesiredBox.SetParentOrigin( ParentOrigin::CENTER );
208 mDesiredBox.SetAnchorPoint( AnchorPoint::CENTER );
209 mDesiredBox.SetPosition( 0, 0, -1 );
211 mHeightBox.SetSize( stage.GetSize().width, (stage.GetSize() * mImageStageScale).height );
212 mHeightBox.SetParentOrigin( ParentOrigin::CENTER );
213 mHeightBox.SetAnchorPoint( AnchorPoint::CENTER );
214 mHeightBox.SetPosition( 0, 0, -1 );
216 mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
217 mWidthBox.SetParentOrigin( ParentOrigin::CENTER );
218 mWidthBox.SetAnchorPoint( AnchorPoint::CENTER );
219 mWidthBox.SetPosition( 0, 0, -1 );
221 // Make a grab-handle for resizing the image:
222 mGrabCorner = Toolkit::PushButton::New();
223 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
224 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
225 mGrabCorner.SetName( "GrabCorner" );
226 mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
227 mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
228 mGrabCorner.SetSize( Vector2( stage.GetSize().width*0.08f, stage.GetSize().width*0.08f ) );
229 mGrabCorner.SetOpacity( 0.6f );
231 Layer grabCornerLayer = Layer::New();
232 grabCornerLayer.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
233 grabCornerLayer.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
235 grabCornerLayer.Add( mGrabCorner );
236 mDesiredBox.Add( grabCornerLayer );
237 mPanGestureDetector = PanGestureDetector::New();
238 mPanGestureDetector.Attach( mGrabCorner );
239 mPanGestureDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPan );
241 // Initialize the actor
242 mImageActor = ImageActor::New();
244 // Reposition the actor
245 mImageActor.SetParentOrigin( ParentOrigin::CENTER );
246 mImageActor.SetAnchorPoint( AnchorPoint::CENTER );
247 mImageActor.SetSortModifier(5.f);
249 // Display the actor on the stage
250 stage.Add( mImageActor );
252 mImageActor.SetSize( stage.GetSize() * mImageStageScale );
254 // Setup the pinch detector for scaling the desired image load dimensions:
255 mPinchDetector = PinchGestureDetector::New();
256 mPinchDetector.Attach( mImageActor );
257 mPinchDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPinch );
259 // Tie-in input event handlers:
260 stage.KeyEventSignal().Connect( this, &ImageScalingAndFilteringController::OnKeyEvent );
268 * Create the GUI controls which float above the scene
270 void CreateControls()
272 Stage stage = Stage::GetCurrent();
274 Dali::Layer controlsLayer = Dali::Layer::New();
275 controlsLayer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
276 controlsLayer.SetSizeModeFactor( Vector3( 1.0f, 1.0f, 1.0f ) );
277 controlsLayer.SetAnchorPoint( AnchorPoint::TOP_LEFT);
278 controlsLayer.SetParentOrigin( ParentOrigin::TOP_LEFT);
279 stage.Add( controlsLayer );
281 // Back and next image buttons in corners of stage:
282 unsigned int playWidth = std::min( stage.GetSize().x * (1 / 5.0f), 58.0f );
283 Image playImage = ResourceImage::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ), FittingMode::SHRINK_TO_FIT, SamplingMode::BOX_THEN_LINEAR );
284 Toolkit::ImageView imagePrevious = Toolkit::ImageView::New( playImage );
286 // Last image button:
287 imagePrevious.SetAnchorPoint( AnchorPoint::TOP_LEFT );
288 imagePrevious.RotateBy( Radian(3.14159265358979323846f), Vector3( 0, 1.0f, 0 ) );
289 imagePrevious.SetY( playWidth * 0.5f );
290 imagePrevious.SetX( playWidth + playWidth * 0.5f );
291 imagePrevious.SetOpacity( 0.6f );
292 controlsLayer.Add( imagePrevious );
293 imagePrevious.SetName( PREVIOUS_BUTTON_ID );
294 imagePrevious.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
296 // Next image button:
297 Toolkit::ImageView imageNext = Toolkit::ImageView::New( playImage );
298 imageNext.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
299 imageNext.SetY( playWidth * 0.5f );
300 imageNext.SetX( stage.GetSize().x - playWidth * 0.5f );
301 imageNext.SetOpacity( 0.6f );
302 controlsLayer.Add( imageNext );
303 imageNext.SetName( NEXT_BUTTON_ID );
304 imageNext.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
306 // Buttons to popup selectors for fitting and sampling modes:
308 // Wrapper table to hold two buttons side by side:
309 Toolkit::TableView modesGroupBackground = Toolkit::TableView::New( 1, 2 );
310 modesGroupBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
311 modesGroupBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
312 modesGroupBackground.SetBackgroundColor( BACKGROUND_COLOUR );
313 modesGroupBackground.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE ) );
314 modesGroupBackground.SetFitHeight( 0 );
316 modesGroupBackground.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
317 modesGroupBackground.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
318 modesGroupBackground.SetPosition( 0.0f, 0.0f );
320 controlsLayer.Add( modesGroupBackground );
323 // Vertical table to hold label and button:
324 Toolkit::TableView fittingModeGroup = Toolkit::TableView::New( 2, 1 );
325 fittingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
326 fittingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
327 fittingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
328 fittingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
329 fittingModeGroup.SetFitHeight( 0 );
330 fittingModeGroup.SetFitHeight( 1 );
332 TextLabel label = TextLabel::New( "Image fitting mode:" );
333 label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
334 fittingModeGroup.Add( label );
336 Toolkit::PushButton button = CreateButton( FITTING_BUTTON_ID, StringFromScalingMode( mFittingMode ) );
337 button.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
338 fittingModeGroup.Add( button );
339 mFittingModeButton = button;
341 modesGroupBackground.Add( fittingModeGroup );
345 // Vertical table to hold label and button:
346 Toolkit::TableView samplingModeGroup = Toolkit::TableView::New( 2, 1 );
347 samplingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
348 samplingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
349 samplingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
350 samplingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
351 samplingModeGroup.SetFitHeight( 0 );
352 samplingModeGroup.SetFitHeight( 1 );
354 TextLabel label = TextLabel::New( "Image sampling mode:" );
355 label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
356 samplingModeGroup.Add( label );
358 Toolkit::PushButton button = CreateButton( SAMPLING_BUTTON_ID, StringFromFilterMode( mSamplingMode ) );
359 button.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
360 samplingModeGroup.Add( button );
361 mSamplingModeButton = button;
363 modesGroupBackground.Add( samplingModeGroup );
367 Toolkit::PushButton CreateButton( const char * id, const char * label )
369 Toolkit::PushButton button = Toolkit::PushButton::New();
370 button.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
371 button.SetName( id );
372 button.SetLabel( label );
373 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
374 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
375 button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
379 Toolkit::Popup CreatePopup()
381 Stage stage = Stage::GetCurrent();
382 const float POPUP_WIDTH_DP = stage.GetSize().width * 0.75f;
384 Toolkit::Popup popup = Toolkit::Popup::New();
385 popup.SetName( "POPUP" );
386 popup.SetParentOrigin( ParentOrigin::CENTER );
387 popup.SetAnchorPoint( AnchorPoint::CENTER );
388 popup.SetSize( POPUP_WIDTH_DP, 0.0f );
391 popup.OutsideTouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnPopupOutsideTouched );
396 //void CreatePopupButton( Toolkit::Popup popup, const char* id )
397 Toolkit::PushButton CreatePopupButton( Actor parent, const char* id )
399 Toolkit::PushButton button = Toolkit::PushButton::New();
400 button.SetName( id );
401 button.SetLabel( id );
402 Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( button.GetLabel() );
403 textLabel.SetProperty( TextLabel::Property::POINT_SIZE, 12.0f );
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.SetBackgroundColor( BACKGROUND_COLOUR );
427 fittingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
428 fittingModes.SetFitHeight( 0 );
429 fittingModes.SetFitHeight( 1 );
430 fittingModes.SetFitHeight( 2 );
431 fittingModes.SetFitHeight( 3 );
433 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SCALE_TO_FILL ) );
434 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SHRINK_TO_FIT ) );
435 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_WIDTH ) );
436 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_HEIGHT ) );
438 mPopup.Add( fittingModes );
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.SetBackgroundColor( BACKGROUND_COLOUR );
450 samplingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
451 samplingModes.SetFitHeight( 0 );
452 samplingModes.SetFitHeight( 1 );
453 samplingModes.SetFitHeight( 2 );
454 samplingModes.SetFitHeight( 3 );
455 samplingModes.SetFitHeight( 4 );
456 samplingModes.SetFitHeight( 5 );
458 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NEAREST ) );
459 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::LINEAR ) );
460 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX ) );
461 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_NEAREST ) );
462 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_LINEAR ) );
463 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NO_FILTER ) );
465 mPopup.Add( samplingModes );
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.SetLabel( modeName );
493 mFittingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
502 bool CheckSamplingModeButton( Actor &button, SamplingMode::Type mode )
504 const char * const modeName = StringFromFilterMode( mode );
505 if( button.GetName() == modeName )
507 mSamplingMode = mode;
508 mSamplingModeButton.SetLabel( modeName );
509 mSamplingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
518 void OnPopupOutsideTouched()
527 void OnImageLoaded( ResourceImage image )
529 DALI_ASSERT_DEBUG( image == mNextImage );
530 mImageActor.SetImage( image );
531 mImageActor.SetSize( Size( image.GetWidth(), image.GetHeight() ) );
534 bool OnControlTouched( Actor actor, const TouchEvent& event )
536 if(event.GetPointCount() > 0)
538 const TouchPoint& point = event.GetPoint(0);
543 const std::string & name = actor.GetName();
544 if( name == NEXT_BUTTON_ID )
546 mCurrentPath = mCurrentPath + 1;
547 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
550 else if( name == PREVIOUS_BUTTON_ID )
552 mCurrentPath = mCurrentPath - 1;
553 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
568 void OnPinch( Actor actor, const PinchGesture& pinch )
570 if( pinch.state == Gesture::Started )
572 mLastPinchScale = pinch.scale;
574 const float scale = pinch.scale;
576 if( scale != mLastPinchScale )
578 if ( scale < mLastPinchScale )
580 mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
581 mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
585 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
586 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
590 mLastPinchScale = scale;
593 void OnPan( Actor actor, const PanGesture& gesture )
595 Stage stage = Stage::GetCurrent();
596 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x + (gesture.displacement.x * 2.0f / stage.GetSize().width ) ) );
597 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y + (gesture.displacement.y * 2.0f / stage.GetSize().height ) ) );
601 void OnKeyEvent(const KeyEvent& event)
603 if( event.state == KeyEvent::Down )
605 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
607 if( mPopup && mPopup.IsVisible() )
617 else if ( event.keyPressedName == "Right" )
619 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
621 else if ( event.keyPressedName == "Left" )
623 mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
625 else if ( event.keyPressedName == "Up" )
627 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
629 else if ( event.keyPressedName == "Down" )
631 mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
633 else if ( event.keyPressedName == "o" )
635 mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
636 mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
638 else if ( event.keyPressedName == "p" )
640 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
641 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
643 else if ( event.keyPressedName == "n" )
645 mCurrentPath = mCurrentPath + 1;
646 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
648 else if ( event.keyPressedName == "b" )
650 mCurrentPath = mCurrentPath - 1;
651 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
653 // Cycle filter and scaling modes:
654 else if ( event.keyPressedName == "f" )
656 mSamplingMode = NextFilterMode( mSamplingMode );
657 mSamplingModeButton.SetLabel( StringFromFilterMode( mSamplingMode ) );
658 mSamplingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
660 // Cycle filter and scaling modes:
661 else if ( event.keyPressedName == "s" )
663 mFittingMode = NextScalingMode( mFittingMode );
664 mFittingModeButton.SetLabel( StringFromScalingMode( mFittingMode ) );
665 mFittingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
679 const char * const path = IMAGE_PATHS[mCurrentPath];
681 Stage stage = Stage::GetCurrent();
682 Size imageSize = stage.GetSize() * mImageStageScale;
683 const ImageDimensions imageSizeInt = ImageDimensions::FromFloatArray( &imageSize.x );
685 ResourceImage image = ResourceImage::New( path, imageSizeInt, mFittingMode, mSamplingMode );
686 image.LoadingFinishedSignal().Connect( this, &ImageScalingAndFilteringController::OnImageLoaded );
690 mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
691 mHeightBox.SetSize( stage.GetSize().width, (stage.GetSize() * mImageStageScale).height );
692 mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
696 Application& mApplication;
697 Toolkit::ImageView mDesiredBox; //< Background rectangle to show requested image size.
698 Toolkit::ImageView mHeightBox; //< Background horizontal stripe to show requested image height.
699 Toolkit::ImageView mWidthBox; //< Background vertical stripe to show requested image width.
700 Toolkit::PushButton mFittingModeButton;
701 Toolkit::PushButton mSamplingModeButton;
702 Toolkit::Popup mPopup;
703 PinchGestureDetector mPinchDetector;
704 float mLastPinchScale;
705 Toolkit::PushButton mGrabCorner;
706 PanGestureDetector mPanGestureDetector;
707 ImageActor mImageActor;
708 ResourceImage mNextImage; //< Currently-loading image
709 Vector2 mImageStageScale;
711 FittingMode::Type mFittingMode;
712 SamplingMode::Type mSamplingMode;
715 void RunTest( Application& application )
717 ImageScalingAndFilteringController test( application );
719 application.MainLoop();
722 // Entry point for Linux & Tizen applications
723 int main( int argc, char **argv )
725 Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
727 RunTest( application );