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 ImageActor background = ImageActor::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 = ImageActor::New( heightBackground );
196 mHeightBox.SetOpacity( 0.2f );
197 stage.Add( mHeightBox );
199 mWidthBox = ImageActor::New( widthBackground );
200 mWidthBox.SetOpacity( 0.2f );
201 stage.Add( mWidthBox );
203 mDesiredBox = ImageActor::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 );
210 mDesiredBox.SetSortModifier(4.f);
212 mHeightBox.SetSize( stage.GetSize().width, (stage.GetSize() * mImageStageScale).height );
213 mHeightBox.SetParentOrigin( ParentOrigin::CENTER );
214 mHeightBox.SetAnchorPoint( AnchorPoint::CENTER );
215 mHeightBox.SetPosition( 0, 0, -1 );
216 mHeightBox.SetSortModifier(3.f);
218 mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
219 mWidthBox.SetParentOrigin( ParentOrigin::CENTER );
220 mWidthBox.SetAnchorPoint( AnchorPoint::CENTER );
221 mWidthBox.SetPosition( 0, 0, -1 );
222 mWidthBox.SetSortModifier(2.f);
224 // Make a grab-handle for resizing the image:
225 mGrabCorner = Toolkit::PushButton::New();
226 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
227 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
228 mGrabCorner.SetName( "GrabCorner" );
229 mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
230 mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
231 mGrabCorner.SetSize( Vector2( stage.GetSize().width*0.08f, stage.GetSize().width*0.08f ) );
232 mGrabCorner.SetOpacity( 0.6f );
234 Layer grabCornerLayer = Layer::New();
235 grabCornerLayer.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
236 grabCornerLayer.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
238 grabCornerLayer.Add( mGrabCorner );
239 mDesiredBox.Add( grabCornerLayer );
240 mPanGestureDetector = PanGestureDetector::New();
241 mPanGestureDetector.Attach( mGrabCorner );
242 mPanGestureDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPan );
244 // Initialize the actor
245 mImageActor = ImageActor::New();
247 // Reposition the actor
248 mImageActor.SetParentOrigin( ParentOrigin::CENTER );
249 mImageActor.SetAnchorPoint( AnchorPoint::CENTER );
250 mImageActor.SetSortModifier(5.f);
252 // Display the actor on the stage
253 stage.Add( mImageActor );
255 mImageActor.SetSize( stage.GetSize() * mImageStageScale );
257 // Setup the pinch detector for scaling the desired image load dimensions:
258 mPinchDetector = PinchGestureDetector::New();
259 mPinchDetector.Attach( mImageActor );
260 mPinchDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPinch );
262 // Tie-in input event handlers:
263 stage.KeyEventSignal().Connect( this, &ImageScalingAndFilteringController::OnKeyEvent );
271 * Create the GUI controls which float above the scene
273 void CreateControls()
275 Stage stage = Stage::GetCurrent();
277 Dali::Layer controlsLayer = Dali::Layer::New();
278 controlsLayer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
279 controlsLayer.SetSizeModeFactor( Vector3( 1.0f, 1.0f, 1.0f ) );
280 controlsLayer.SetAnchorPoint( AnchorPoint::TOP_LEFT);
281 controlsLayer.SetParentOrigin( ParentOrigin::TOP_LEFT);
282 stage.Add( controlsLayer );
284 // Back and next image buttons in corners of stage:
285 unsigned int playWidth = std::min( stage.GetSize().x * (1 / 5.0f), 58.0f );
286 Image playImage = ResourceImage::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ), FittingMode::SHRINK_TO_FIT, SamplingMode::BOX_THEN_LINEAR );
287 Actor imagePrevious = ImageActor::New( playImage );
289 // Last image button:
290 imagePrevious.SetAnchorPoint( AnchorPoint::TOP_LEFT );
291 imagePrevious.RotateBy( Radian(3.14159265358979323846f), Vector3( 0, 1.0f, 0 ) );
292 imagePrevious.SetY( playWidth * 0.5f );
293 imagePrevious.SetX( playWidth + playWidth * 0.5f );
294 imagePrevious.SetOpacity( 0.6f );
295 controlsLayer.Add( imagePrevious );
296 imagePrevious.SetName( PREVIOUS_BUTTON_ID );
297 imagePrevious.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
299 // Next image button:
300 Actor imageNext = ImageActor::New( playImage );
301 imageNext.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
302 imageNext.SetY( playWidth * 0.5f );
303 imageNext.SetX( stage.GetSize().x - playWidth * 0.5f );
304 imageNext.SetOpacity( 0.6f );
305 controlsLayer.Add( imageNext );
306 imageNext.SetName( NEXT_BUTTON_ID );
307 imageNext.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
309 // Buttons to popup selectors for fitting and sampling modes:
311 // Wrapper table to hold two buttons side by side:
312 Toolkit::TableView modesGroupBackground = Toolkit::TableView::New( 1, 2 );
313 modesGroupBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
314 modesGroupBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
315 modesGroupBackground.SetBackgroundColor( BACKGROUND_COLOUR );
316 modesGroupBackground.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE ) );
317 modesGroupBackground.SetFitHeight( 0 );
319 modesGroupBackground.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
320 modesGroupBackground.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
321 modesGroupBackground.SetPosition( 0.0f, 0.0f );
323 controlsLayer.Add( modesGroupBackground );
326 // Vertical table to hold label and button:
327 Toolkit::TableView fittingModeGroup = Toolkit::TableView::New( 2, 1 );
328 fittingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
329 fittingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
330 fittingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
331 fittingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
332 fittingModeGroup.SetFitHeight( 0 );
333 fittingModeGroup.SetFitHeight( 1 );
335 TextLabel label = TextLabel::New( "Image fitting mode:" );
336 label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
337 fittingModeGroup.Add( label );
339 Toolkit::PushButton button = CreateButton( FITTING_BUTTON_ID, StringFromScalingMode( mFittingMode ) );
340 button.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
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 button.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
363 samplingModeGroup.Add( button );
364 mSamplingModeButton = button;
366 modesGroupBackground.Add( samplingModeGroup );
370 Toolkit::PushButton CreateButton( const char * id, const char * label )
372 Toolkit::PushButton button = Toolkit::PushButton::New();
373 button.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
374 button.SetName( id );
375 button.SetLabel( label );
376 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
377 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
378 button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
382 Toolkit::Popup CreatePopup()
384 Stage stage = Stage::GetCurrent();
385 const float POPUP_WIDTH_DP = stage.GetSize().width * 0.75f;
387 Toolkit::Popup popup = Toolkit::Popup::New();
388 popup.SetName( "POPUP" );
389 popup.SetParentOrigin( ParentOrigin::CENTER );
390 popup.SetAnchorPoint( AnchorPoint::CENTER );
391 popup.SetSize( POPUP_WIDTH_DP, 0.0f );
394 popup.OutsideTouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnPopupOutsideTouched );
399 //void CreatePopupButton( Toolkit::Popup popup, const char* id )
400 Toolkit::PushButton CreatePopupButton( Actor parent, const char* id )
402 Toolkit::PushButton button = Toolkit::PushButton::New();
403 button.SetName( id );
404 button.SetLabel( id );
405 Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( button.GetLabel() );
406 textLabel.SetProperty( TextLabel::Property::POINT_SIZE, 12.0f );
408 button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
409 button.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
410 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
411 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
413 button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
415 parent.Add( button );
419 bool OnButtonClicked( Toolkit::Button button )
421 if( button.GetName() == FITTING_BUTTON_ID )
423 mPopup = CreatePopup();
425 // Four-row table to hold buttons:
426 Toolkit::TableView fittingModes = Toolkit::TableView::New( 4, 1 );
427 fittingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
428 fittingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
429 fittingModes.SetBackgroundColor( BACKGROUND_COLOUR );
430 fittingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
431 fittingModes.SetFitHeight( 0 );
432 fittingModes.SetFitHeight( 1 );
433 fittingModes.SetFitHeight( 2 );
434 fittingModes.SetFitHeight( 3 );
436 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SCALE_TO_FILL ) );
437 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SHRINK_TO_FIT ) );
438 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_WIDTH ) );
439 CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_HEIGHT ) );
441 mPopup.Add( fittingModes );
444 else if( button.GetName() == SAMPLING_BUTTON_ID )
446 mPopup = CreatePopup();
448 // Table to hold buttons for each sampling mode:
449 Toolkit::TableView samplingModes = Toolkit::TableView::New( 6, 1 );
450 samplingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
451 samplingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
452 samplingModes.SetBackgroundColor( BACKGROUND_COLOUR );
453 samplingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
454 samplingModes.SetFitHeight( 0 );
455 samplingModes.SetFitHeight( 1 );
456 samplingModes.SetFitHeight( 2 );
457 samplingModes.SetFitHeight( 3 );
458 samplingModes.SetFitHeight( 4 );
459 samplingModes.SetFitHeight( 5 );
461 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NEAREST ) );
462 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::LINEAR ) );
463 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX ) );
464 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_NEAREST ) );
465 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_LINEAR ) );
466 CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NO_FILTER ) );
468 mPopup.Add( samplingModes );
471 else if( CheckFittingModeButton( button, FittingMode::SCALE_TO_FILL) ||
472 CheckFittingModeButton( button, FittingMode::SHRINK_TO_FIT) ||
473 CheckFittingModeButton( button, FittingMode::FIT_WIDTH) ||
474 CheckFittingModeButton( button, FittingMode::FIT_HEIGHT) )
477 else if( CheckSamplingModeButton( button, SamplingMode::NEAREST ) ||
478 CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
479 CheckSamplingModeButton( button, SamplingMode::BOX ) ||
480 CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
481 CheckSamplingModeButton( button, SamplingMode::BOX_THEN_NEAREST ) ||
482 CheckSamplingModeButton( button, SamplingMode::BOX_THEN_LINEAR ) ||
483 CheckSamplingModeButton( button, SamplingMode::NO_FILTER ) )
489 bool CheckFittingModeButton( Actor &button, FittingMode::Type mode )
491 const char * const modeName = StringFromScalingMode( mode );
492 if( button.GetName() == modeName )
495 mFittingModeButton.SetLabel( modeName );
496 mFittingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
505 bool CheckSamplingModeButton( Actor &button, SamplingMode::Type mode )
507 const char * const modeName = StringFromFilterMode( mode );
508 if( button.GetName() == modeName )
510 mSamplingMode = mode;
511 mSamplingModeButton.SetLabel( modeName );
512 mSamplingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
521 void OnPopupOutsideTouched()
530 void OnImageLoaded( ResourceImage image )
532 DALI_ASSERT_DEBUG( image == mNextImage );
533 mImageActor.SetImage( image );
534 mImageActor.SetSize( Size( image.GetWidth(), image.GetHeight() ) );
537 bool OnControlTouched( Actor actor, const TouchEvent& event )
539 if(event.GetPointCount() > 0)
541 const TouchPoint& point = event.GetPoint(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( 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 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x + (gesture.displacement.x * 2.0f / stage.GetSize().width ) ) );
600 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y + (gesture.displacement.y * 2.0f / stage.GetSize().height ) ) );
604 void OnKeyEvent(const KeyEvent& event)
606 if( event.state == KeyEvent::Down )
608 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
610 if( mPopup && mPopup.IsVisible() )
620 else if ( event.keyPressedName == "Right" )
622 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
624 else if ( event.keyPressedName == "Left" )
626 mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
628 else if ( event.keyPressedName == "Up" )
630 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
632 else if ( event.keyPressedName == "Down" )
634 mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
636 else if ( event.keyPressedName == "o" )
638 mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
639 mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
641 else if ( event.keyPressedName == "p" )
643 mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
644 mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
646 else if ( event.keyPressedName == "n" )
648 mCurrentPath = mCurrentPath + 1;
649 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
651 else if ( event.keyPressedName == "b" )
653 mCurrentPath = mCurrentPath - 1;
654 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
656 // Cycle filter and scaling modes:
657 else if ( event.keyPressedName == "f" )
659 mSamplingMode = NextFilterMode( mSamplingMode );
660 mSamplingModeButton.SetLabel( StringFromFilterMode( mSamplingMode ) );
661 mSamplingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
663 // Cycle filter and scaling modes:
664 else if ( event.keyPressedName == "s" )
666 mFittingMode = NextScalingMode( mFittingMode );
667 mFittingModeButton.SetLabel( StringFromScalingMode( mFittingMode ) );
668 mFittingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
682 const char * const path = IMAGE_PATHS[mCurrentPath];
684 Stage stage = Stage::GetCurrent();
685 Size imageSize = stage.GetSize() * mImageStageScale;
686 const ImageDimensions imageSizeInt = ImageDimensions::FromFloatArray( &imageSize.x );
688 ResourceImage image = ResourceImage::New( path, imageSizeInt, mFittingMode, mSamplingMode );
689 image.LoadingFinishedSignal().Connect( this, &ImageScalingAndFilteringController::OnImageLoaded );
693 mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
694 mHeightBox.SetSize( stage.GetSize().width, (stage.GetSize() * mImageStageScale).height );
695 mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
699 Application& mApplication;
700 ImageActor mDesiredBox; //< Background rectangle to show requested image size.
701 ImageActor mHeightBox; //< Background horizontal stripe to show requested image height.
702 ImageActor mWidthBox; //< Background vertical stripe to show requested image width.
703 Toolkit::PushButton mFittingModeButton;
704 Toolkit::PushButton mSamplingModeButton;
705 Toolkit::Popup mPopup;
706 PinchGestureDetector mPinchDetector;
707 float mLastPinchScale;
708 Toolkit::PushButton mGrabCorner;
709 PanGestureDetector mPanGestureDetector;
710 ImageActor mImageActor;
711 ResourceImage mNextImage; //< Currently-loading image
712 Vector2 mImageStageScale;
714 FittingMode::Type mFittingMode;
715 SamplingMode::Type mSamplingMode;
718 void RunTest( Application& application )
720 ImageScalingAndFilteringController test( application );
722 application.MainLoop();
725 // Entry point for Linux & Tizen applications
726 int main( int argc, char **argv )
728 Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
730 RunTest( application );