Supply stylesheet using Application constructor
[platform/core/uifw/dali-demo.git] / examples / image-scaling-and-filtering / image-scaling-and-filtering-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali-toolkit/devel-api/controls/popup/popup.h>
21 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
22 #include "shared/view.h"
23 #include <iostream>
24
25 using namespace Dali;
26 using Toolkit::TextLabel;
27
28 namespace
29 {
30
31 const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-gradient.jpg" );
32 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
33
34 const int MARGIN_SIZE = 10;
35
36 const char* const NEXT_BUTTON_ID = "NEXT_BUTTON";
37 const char* const PREVIOUS_BUTTON_ID = "PREVIOUS_BUTTON";
38 const char * const DALI_ICON_PLAY = DALI_IMAGE_DIR "icon-play.png";
39
40 const char* const PUSHBUTTON_PRESS_IMAGE = DALI_IMAGE_DIR "button-down.9.png";
41 const char* const PUSHBUTTON_BUTTON_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
42 const char* const PUSHBUTTON_DISABLED_IMAGE = DALI_IMAGE_DIR "button-disabled.9.png";
43
44 const char* const FITTING_BUTTON_ID = "FITTING_BUTTON";
45 const char* const SAMPLING_BUTTON_ID = "SAMPLING_BUTTON";
46 const char* const FITTING_BUTTON_TEXT = "Fitting";
47 const char* const SAMPLING_BUTTON_TEXT = "Sampling";
48
49 const char* const STYLE_LABEL_TEXT  = "grouplabel";
50 const char* const STYLE_BUTTON_TEXT = "buttonlabel";
51
52
53
54 const char* IMAGE_PATHS[] =
55 {
56   // Worst case for aliasing in downscaling, 2k x 2k 1 bit per pixel dithered
57   // black and white image:
58   DALI_IMAGE_DIR "gallery-large-14.wbmp",
59   // Variety of sizes, shapes and formats:
60   DALI_IMAGE_DIR "animation-list.png",
61   DALI_IMAGE_DIR "layer1.png",
62   DALI_IMAGE_DIR "layer2.png",
63   DALI_IMAGE_DIR "music-libray-main-screen.png",
64   DALI_IMAGE_DIR "music-libray-record-cover.png",
65   DALI_IMAGE_DIR "contacts-background.png",
66   DALI_IMAGE_DIR "portrait_screen_primitive_shapes.gif",
67   DALI_IMAGE_DIR "landscape_screen_primitive_shapes.gif",
68   DALI_IMAGE_DIR "square_primitive_shapes.bmp",
69   DALI_IMAGE_DIR "dali-logo.png",
70   DALI_IMAGE_DIR "com.samsung.dali-demo.ico",
71   DALI_IMAGE_DIR "gallery-large-14.jpg",
72   DALI_IMAGE_DIR "book-landscape-cover.jpg",
73   DALI_IMAGE_DIR "book-portrait-p1.jpg",
74   DALI_IMAGE_DIR "book-landscape-cover-back.jpg",
75   DALI_IMAGE_DIR "background-1.jpg",
76   DALI_IMAGE_DIR "background-blocks.jpg",
77   DALI_IMAGE_DIR "background-magnifier.jpg",
78   DALI_IMAGE_DIR "gallery-large-14.jpg",
79   NULL
80 };
81 const int NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u;
82
83 /** Cycle the scaling mode options. */
84 FittingMode::Type NextScalingMode( FittingMode::Type oldMode )
85 {
86   FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
87   switch ( oldMode )
88   {
89     case FittingMode::SHRINK_TO_FIT:
90       newMode = FittingMode::SCALE_TO_FILL;
91       break;
92     case FittingMode::SCALE_TO_FILL:
93       newMode = FittingMode::FIT_WIDTH;
94       break;
95     case FittingMode::FIT_WIDTH:
96       newMode = FittingMode::FIT_HEIGHT;
97       break;
98     case FittingMode::FIT_HEIGHT:
99       newMode = FittingMode::SHRINK_TO_FIT;
100       break;
101   }
102   return newMode;
103 }
104
105 /** Cycle through filter mode options. */
106 SamplingMode::Type NextFilterMode( SamplingMode::Type oldMode )
107 {
108   SamplingMode::Type newMode = SamplingMode::BOX;
109
110   switch ( oldMode )
111   {
112     case SamplingMode::BOX:
113       newMode = SamplingMode::NEAREST;
114       break;
115     case SamplingMode::NEAREST:
116       newMode = SamplingMode::LINEAR;
117       break;
118     case SamplingMode::LINEAR:
119       newMode = SamplingMode::BOX_THEN_NEAREST;
120       break;
121     case SamplingMode::BOX_THEN_NEAREST:
122       newMode = SamplingMode::BOX_THEN_LINEAR;
123       break;
124     case SamplingMode::BOX_THEN_LINEAR:
125       newMode = SamplingMode::NO_FILTER;
126       break;
127     case SamplingMode::NO_FILTER:
128       newMode = SamplingMode::BOX;
129       break;
130     case SamplingMode::DONT_CARE:
131       newMode = SamplingMode::BOX;
132       break;
133   }
134   return newMode;
135 }
136
137 const char* StringFromScalingMode( FittingMode::Type scalingMode )
138 {
139   return scalingMode == FittingMode::SCALE_TO_FILL ? "SCALE_TO_FILL" : scalingMode == FittingMode::SHRINK_TO_FIT ? "SHRINK_TO_FIT" : scalingMode == FittingMode::FIT_WIDTH ? "FIT_WIDTH" : scalingMode == FittingMode::FIT_HEIGHT ? "FIT_HEIGHT" : "UnknownScalingMode";
140 }
141
142 const char* StringFromFilterMode( SamplingMode::Type filterMode )
143 {
144   return filterMode == SamplingMode::BOX ? "BOX" : filterMode == SamplingMode::BOX_THEN_NEAREST ? "BOX_THEN_NEAREST" : filterMode == SamplingMode::BOX_THEN_LINEAR ? "BOX_THEN_LINEAR" : filterMode == SamplingMode::NEAREST ? "NEAREST" : filterMode == SamplingMode::LINEAR ? "LINEAR" : filterMode == SamplingMode::NO_FILTER ? "NO_FILTER" : filterMode == SamplingMode::DONT_CARE ? "DONT_CARE" : "UnknownFilterMode";
145 }
146
147 }
148
149 // This example shows the load-time image scaling and filtering features.
150 //
151 class ImageScalingAndFilteringController : public ConnectionTracker
152 {
153 public:
154
155   ImageScalingAndFilteringController( Application& application )
156   : mApplication( application ),
157     mImageStageScale( 0.5f, 0.5f ),
158     mCurrentPath( 0 ),
159     mFittingMode( FittingMode::SCALE_TO_FILL ),
160     mSamplingMode( SamplingMode::BOX_THEN_LINEAR)
161   {
162     // Connect to the Application's Init signal
163     mApplication.InitSignal().Connect( this, &ImageScalingAndFilteringController::Create );
164   }
165
166   ~ImageScalingAndFilteringController()
167   {
168     // Nothing to do here;
169   }
170
171   // The Init signal is received once (only) during the Application lifetime
172   void Create( Application& application )
173   {
174     // Get a handle to the stage
175     Stage stage = Stage::GetCurrent();
176
177     // Background image:
178     ResourceImage backgroundImage = ResourceImage::New( BACKGROUND_IMAGE, ImageDimensions( stage.GetSize().width, stage.GetSize().height ), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR );
179     ImageActor background = ImageActor::New( backgroundImage );
180     background.SetZ( -2.0f );
181     background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
182     background.SetSize( stage.GetSize() );
183     stage.Add( background );
184
185     // Make grey pixels for the desired box, the desired height the desired width:
186     BufferImage desiredBackground = BufferImage::WHITE();
187
188     BufferImage heightBackground = BufferImage::WHITE();
189     PixelBuffer* const heightPixel = heightBackground.GetBuffer();
190     heightPixel[0] = 0x8f;
191     heightPixel[1] = 0x8f;
192     heightPixel[2] = 0x8f;
193
194     BufferImage widthBackground = BufferImage::WHITE();
195     PixelBuffer* const widthPixel = widthBackground.GetBuffer();
196     widthPixel[0] = 0x4f;
197     widthPixel[1] = 0x4f;
198     widthPixel[2] = 0x4f;
199
200     mHeightBox = ImageActor::New( heightBackground );
201     mHeightBox.SetOpacity( 0.2f );
202     stage.Add( mHeightBox );
203
204     mWidthBox = ImageActor::New( widthBackground );
205     mWidthBox.SetOpacity( 0.2f );
206     stage.Add( mWidthBox );
207
208     mDesiredBox = ImageActor::New( desiredBackground );
209     stage.Add( mDesiredBox );
210
211     mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
212     mDesiredBox.SetParentOrigin( ParentOrigin::CENTER );
213     mDesiredBox.SetAnchorPoint( AnchorPoint::CENTER );
214     mDesiredBox.SetPosition( 0, 0, -1 );
215
216     mHeightBox.SetSize( stage.GetSize().width,  (stage.GetSize() * mImageStageScale).height );
217     mHeightBox.SetParentOrigin( ParentOrigin::CENTER );
218     mHeightBox.SetAnchorPoint( AnchorPoint::CENTER );
219     mHeightBox.SetPosition( 0, 0, -1 );
220
221     mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
222     mWidthBox.SetParentOrigin( ParentOrigin::CENTER );
223     mWidthBox.SetAnchorPoint( AnchorPoint::CENTER );
224     mWidthBox.SetPosition( 0, 0, -1 );
225
226     // Make a grab-handle for resizing the image:
227     mGrabCorner = Toolkit::PushButton::New();
228     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
229     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
230     mGrabCorner.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
231     mGrabCorner.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
232     mGrabCorner.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
233     mGrabCorner.SetName( "GrabCorner" );
234     mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
235     mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
236     mGrabCorner.SetSize( Vector2( stage.GetSize().width*0.08f, stage.GetSize().width*0.08f ) );
237     mGrabCorner.SetZ( 1.0f );
238     mGrabCorner.SetOpacity( 0.6f );
239     mDesiredBox.Add( mGrabCorner );
240     mPanGestureDetector = PanGestureDetector::New();
241     mPanGestureDetector.Attach( mGrabCorner );
242     mPanGestureDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPan );
243
244     // Initialize the actor
245     mImageActor = ImageActor::New();
246
247     // Reposition the actor
248     mImageActor.SetParentOrigin( ParentOrigin::CENTER );
249     mImageActor.SetAnchorPoint( AnchorPoint::CENTER );
250
251     // Display the actor on the stage
252     stage.Add( mImageActor );
253
254     mImageActor.SetSize( stage.GetSize() * mImageStageScale );
255
256     // Setup the pinch detector for scaling the desired image load dimensions:
257     mPinchDetector = PinchGestureDetector::New();
258     mPinchDetector.Attach( mImageActor );
259     mPinchDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPinch );
260
261     // Tie-in input event handlers:
262     stage.KeyEventSignal().Connect( this, &ImageScalingAndFilteringController::OnKeyEvent );
263
264     CreateControls();
265
266     ResizeImage();
267   }
268
269   /**
270    * Create the GUI controls which float above the scene
271    */
272   void CreateControls()
273   {
274     Stage stage = Stage::GetCurrent();
275
276     Dali::Layer controlsLayer = Dali::Layer::New();
277     controlsLayer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
278     controlsLayer.SetSizeModeFactor( Vector3( 1.0f, 1.0f, 1.0f ) );
279     controlsLayer.SetAnchorPoint( AnchorPoint::TOP_LEFT);
280     controlsLayer.SetParentOrigin( ParentOrigin::TOP_LEFT);
281     stage.Add( controlsLayer );
282
283     // Back and next image buttons in corners of stage:
284     unsigned int playWidth = std::min( stage.GetSize().x * (1 / 5.0f), 58.0f );
285     Image playImage = ResourceImage::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ), FittingMode::SHRINK_TO_FIT, SamplingMode::BOX_THEN_LINEAR );
286     Actor imagePrevious = ImageActor::New( playImage );
287
288     // Last image button:
289     imagePrevious.SetAnchorPoint( AnchorPoint::TOP_LEFT );
290     imagePrevious.RotateBy( Radian(3.14159265358979323846f), Vector3( 0, 1.0f, 0 ) );
291     imagePrevious.SetY( playWidth * 0.5f );
292     imagePrevious.SetX( playWidth + playWidth * 0.5f );
293     imagePrevious.SetOpacity( 0.6f );
294     controlsLayer.Add( imagePrevious );
295     imagePrevious.SetName( PREVIOUS_BUTTON_ID );
296     imagePrevious.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
297
298     // Next image button:
299     Actor imageNext = ImageActor::New( playImage );
300     imageNext.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
301     imageNext.SetY( playWidth * 0.5f );
302     imageNext.SetX( stage.GetSize().x - playWidth * 0.5f );
303     imageNext.SetOpacity( 0.6f );
304     controlsLayer.Add( imageNext );
305     imageNext.SetName( NEXT_BUTTON_ID );
306     imageNext.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
307
308     // Buttons to popup selectors for fitting and sampling modes:
309
310     // Wrapper table to hold two buttons side by side:
311     Toolkit::TableView modesGroupBackground = Toolkit::TableView::New( 1, 2 );
312     modesGroupBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
313     modesGroupBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
314     modesGroupBackground.SetBackgroundColor( BACKGROUND_COLOUR );
315     modesGroupBackground.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE ) );
316     modesGroupBackground.SetFitHeight( 0 );
317
318     modesGroupBackground.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
319     modesGroupBackground.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
320     modesGroupBackground.SetPosition( 0.0f, 0.0f );
321
322     controlsLayer.Add( modesGroupBackground );
323
324     {
325       // Vertical table to hold label and button:
326       Toolkit::TableView fittingModeGroup = Toolkit::TableView::New( 2, 1 );
327       fittingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
328       fittingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
329       fittingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
330       fittingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
331       fittingModeGroup.SetFitHeight( 0 );
332       fittingModeGroup.SetFitHeight( 1 );
333
334       TextLabel label = TextLabel::New( "Image fitting mode:" );
335       label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
336       fittingModeGroup.Add( label );
337
338       Toolkit::PushButton button = CreateButton( FITTING_BUTTON_ID, StringFromScalingMode( mFittingMode ) );
339       button.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
340       fittingModeGroup.Add( button );
341       mFittingModeButton = button;
342
343       modesGroupBackground.Add( fittingModeGroup );
344     }
345
346     {
347       // Vertical table to hold label and button:
348       Toolkit::TableView samplingModeGroup = Toolkit::TableView::New( 2, 1 );
349       samplingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
350       samplingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
351       samplingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
352       samplingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
353       samplingModeGroup.SetFitHeight( 0 );
354       samplingModeGroup.SetFitHeight( 1 );
355
356       TextLabel label = TextLabel::New( "Image sampling mode:" );
357       label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
358       samplingModeGroup.Add( label );
359
360       Toolkit::PushButton button = CreateButton( SAMPLING_BUTTON_ID, StringFromFilterMode( mSamplingMode ) );
361       button.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
362       samplingModeGroup.Add( button );
363       mSamplingModeButton = button;
364
365       modesGroupBackground.Add( samplingModeGroup );
366     }
367   }
368
369   Toolkit::PushButton CreateButton( const char * id, const char * label )
370   {
371     Toolkit::PushButton button = Toolkit::PushButton::New();
372     button.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
373     button.SetName( id );
374     button.SetLabel( label );
375     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
376     button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
377     button.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
378     button.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
379     button.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
380     button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
381     return button;
382   }
383
384   Toolkit::Popup CreatePopup()
385   {
386     Stage stage = Stage::GetCurrent();
387     const float POPUP_WIDTH_DP = stage.GetSize().width * 0.75f;
388
389     Toolkit::Popup popup = Toolkit::Popup::New();
390     popup.SetName( "POPUP" );
391     popup.SetParentOrigin( ParentOrigin::CENTER );
392     popup.SetAnchorPoint( AnchorPoint::CENTER );
393     popup.SetSize( POPUP_WIDTH_DP, 0.0f );
394     popup.HideTail();
395
396     popup.OutsideTouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnPopupOutsideTouched );
397
398     return popup;
399   }
400
401   //void CreatePopupButton( Toolkit::Popup popup, const char* id )
402   Toolkit::PushButton CreatePopupButton( Actor parent, const char* id )
403   {
404     Toolkit::PushButton button = Toolkit::PushButton::New();
405     button.SetName( id );
406     button.SetLabel( id );
407     button.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
408     button.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
409     Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( button.GetLabel() );
410     textLabel.SetProperty( TextLabel::Property::POINT_SIZE, 12.0f );
411
412     button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
413     button.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
414     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
415     button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
416
417     button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
418
419     parent.Add( button );
420     return button;
421   }
422
423   bool OnButtonClicked( Toolkit::Button button )
424   {
425     if( button.GetName() == FITTING_BUTTON_ID )
426     {
427       mPopup = CreatePopup();
428
429       // Four-row table to hold buttons:
430       Toolkit::TableView fittingModes = Toolkit::TableView::New( 4, 1 );
431       fittingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
432       fittingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
433       fittingModes.SetBackgroundColor( BACKGROUND_COLOUR );
434       fittingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
435       fittingModes.SetFitHeight( 0 );
436       fittingModes.SetFitHeight( 1 );
437       fittingModes.SetFitHeight( 2 );
438       fittingModes.SetFitHeight( 3 );
439
440       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SCALE_TO_FILL ) );
441       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SHRINK_TO_FIT ) );
442       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_WIDTH ) );
443       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_HEIGHT ) );
444
445       mPopup.Add( fittingModes );
446       mPopup.Show();
447     }
448     else if( button.GetName() == SAMPLING_BUTTON_ID )
449     {
450       mPopup = CreatePopup();
451
452       // Table to hold buttons for each sampling mode:
453       Toolkit::TableView samplingModes = Toolkit::TableView::New( 6, 1 );
454       samplingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
455       samplingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
456       samplingModes.SetBackgroundColor( BACKGROUND_COLOUR );
457       samplingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
458       samplingModes.SetFitHeight( 0 );
459       samplingModes.SetFitHeight( 1 );
460       samplingModes.SetFitHeight( 2 );
461       samplingModes.SetFitHeight( 3 );
462       samplingModes.SetFitHeight( 4 );
463       samplingModes.SetFitHeight( 5 );
464
465       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NEAREST ) );
466       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::LINEAR ) );
467       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX ) );
468       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_NEAREST ) );
469       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_LINEAR ) );
470       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NO_FILTER ) );
471
472       mPopup.Add( samplingModes );
473       mPopup.Show();
474     }
475     else if( CheckFittingModeButton( button, FittingMode::SCALE_TO_FILL) ||
476              CheckFittingModeButton( button, FittingMode::SHRINK_TO_FIT) ||
477              CheckFittingModeButton( button, FittingMode::FIT_WIDTH) ||
478              CheckFittingModeButton( button, FittingMode::FIT_HEIGHT) )
479     {
480     }
481     else if( CheckSamplingModeButton( button, SamplingMode::NEAREST ) ||
482              CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
483              CheckSamplingModeButton( button, SamplingMode::BOX ) ||
484              CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
485              CheckSamplingModeButton( button, SamplingMode::BOX_THEN_NEAREST ) ||
486              CheckSamplingModeButton( button, SamplingMode::BOX_THEN_LINEAR ) ||
487              CheckSamplingModeButton( button, SamplingMode::NO_FILTER ) )
488     {
489     }
490     return true;
491   }
492
493   bool CheckFittingModeButton( Actor &button, FittingMode::Type mode )
494   {
495     const char * const modeName = StringFromScalingMode( mode );
496     if( button.GetName() == modeName )
497     {
498       mFittingMode = mode;
499       mFittingModeButton.SetLabel( modeName );
500       mFittingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
501       ResizeImage();
502       mPopup.Hide();
503       mPopup.Reset();
504       return true;
505     }
506     return false;
507   }
508
509   bool CheckSamplingModeButton( Actor &button, SamplingMode::Type mode )
510   {
511     const char * const modeName = StringFromFilterMode( mode );
512     if( button.GetName() == modeName )
513     {
514       mSamplingMode = mode;
515       mSamplingModeButton.SetLabel( modeName );
516       mSamplingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
517       ResizeImage();
518       mPopup.Hide();
519       mPopup.Reset();
520       return true;
521     }
522     return false;
523   }
524
525   void OnPopupOutsideTouched()
526   {
527     if( mPopup )
528     {
529       mPopup.Hide();
530       mPopup.Reset();
531     }
532   }
533
534   void OnImageLoaded( ResourceImage image )
535   {
536       DALI_ASSERT_DEBUG( image == mNextImage );
537       mImageActor.SetImage( image );
538       mImageActor.SetSize( Size( image.GetWidth(), image.GetHeight() ) );
539   }
540
541   bool OnControlTouched( Actor actor, const TouchEvent& event )
542   {
543     if(event.GetPointCount() > 0)
544     {
545       const TouchPoint& point = event.GetPoint(0);
546       switch(point.state)
547       {
548         case TouchPoint::Up:
549         {
550           const std::string & name = actor.GetName();
551           if( name == NEXT_BUTTON_ID )
552           {
553             mCurrentPath = mCurrentPath + 1;
554             mCurrentPath = mCurrentPath <  NUM_IMAGE_PATHS ? mCurrentPath : 0;
555             ResizeImage();
556           }
557           else if( name == PREVIOUS_BUTTON_ID )
558           {
559             mCurrentPath = mCurrentPath - 1;
560             mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
561             ResizeImage();
562           }
563           break;
564         }
565         default:
566         {
567           break;
568         }
569       } // end switch
570     }
571
572     return false;
573   }
574
575   void OnPinch( Actor actor, const PinchGesture& pinch )
576   {
577     if( pinch.state == Gesture::Started )
578     {
579       mLastPinchScale = pinch.scale;
580     }
581     const float scale = pinch.scale;
582
583     if( scale != mLastPinchScale )
584     {
585       if ( scale < mLastPinchScale )
586       {
587         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
588         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
589       }
590       else
591       {
592         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
593         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
594       }
595       ResizeImage();
596     }
597     mLastPinchScale = scale;
598   }
599
600   void OnPan( Actor actor, const PanGesture& gesture )
601   {
602     Stage stage = Stage::GetCurrent();
603     mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x + (gesture.displacement.x * 2.0f / stage.GetSize().width ) ) );
604     mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y + (gesture.displacement.y * 2.0f / stage.GetSize().height ) ) );
605     ResizeImage();
606   }
607
608   void OnKeyEvent(const KeyEvent& event)
609   {
610     if( event.state == KeyEvent::Down )
611     {
612       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
613       {
614         if( mPopup && mPopup.IsVisible() )
615         {
616           mPopup.Hide();
617           mPopup.Reset();
618         }
619         else
620         {
621           mApplication.Quit();
622         }
623       }
624       else if ( event.keyPressedName == "Right" )
625       {
626         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
627       }
628       else if ( event.keyPressedName == "Left" )
629       {
630         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
631       }
632       else if ( event.keyPressedName == "Up" )
633       {
634         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
635       }
636       else if ( event.keyPressedName == "Down" )
637       {
638         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
639       }
640       else if ( event.keyPressedName == "o" )
641       {
642         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
643         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
644       }
645       else if ( event.keyPressedName == "p" )
646       {
647         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
648         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
649       }
650       else if ( event.keyPressedName == "n" )
651       {
652         mCurrentPath = mCurrentPath + 1;
653         mCurrentPath = mCurrentPath <  NUM_IMAGE_PATHS ? mCurrentPath : 0;
654       }
655       else if ( event.keyPressedName == "b" )
656       {
657         mCurrentPath = mCurrentPath - 1;
658         mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
659       }
660       // Cycle filter and scaling modes:
661       else if ( event.keyPressedName == "f" )
662       {
663         mSamplingMode = NextFilterMode( mSamplingMode );
664         mSamplingModeButton.SetLabel( StringFromFilterMode( mSamplingMode ) );
665         mSamplingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
666       }
667       // Cycle filter and scaling modes:
668       else if ( event.keyPressedName == "s" )
669       {
670         mFittingMode = NextScalingMode( mFittingMode );
671         mFittingModeButton.SetLabel( StringFromScalingMode( mFittingMode ) );
672         mFittingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
673       }
674       else
675       {
676         return;
677       }
678
679       ResizeImage();
680     }
681   }
682
683 private:
684   void ResizeImage()
685   {
686     const char * const path = IMAGE_PATHS[mCurrentPath];
687
688     Stage stage = Stage::GetCurrent();
689     Size imageSize = stage.GetSize() * mImageStageScale;
690     const ImageDimensions imageSizeInt = ImageDimensions::FromFloatArray( &imageSize.x );
691
692     ResourceImage image = ResourceImage::New( path, imageSizeInt, mFittingMode, mSamplingMode );
693     image.LoadingFinishedSignal().Connect( this, &ImageScalingAndFilteringController::OnImageLoaded );
694
695     mNextImage = image;
696
697     mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
698     mHeightBox.SetSize( stage.GetSize().width,  (stage.GetSize() * mImageStageScale).height );
699     mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
700   }
701
702 private:
703   Application&  mApplication;
704   Actor mDesiredBox; //< Background rectangle to show requested image size.
705   Actor mHeightBox;  //< Background horizontal stripe to show requested image height.
706   Actor mWidthBox;   //< Background vertical stripe to show requested image width.
707   Toolkit::PushButton mFittingModeButton;
708   Toolkit::PushButton mSamplingModeButton;
709   Toolkit::Popup mPopup;
710   PinchGestureDetector mPinchDetector;
711   float mLastPinchScale;
712   Toolkit::PushButton  mGrabCorner;
713   PanGestureDetector mPanGestureDetector;
714   ImageActor mImageActor;
715   ResourceImage mNextImage; //< Currently-loading image
716   Vector2 mImageStageScale;
717   int mCurrentPath;
718   FittingMode::Type mFittingMode;
719   SamplingMode::Type mSamplingMode;
720 };
721
722 void RunTest( Application& application )
723 {
724   ImageScalingAndFilteringController test( application );
725
726   application.MainLoop();
727 }
728
729 // Entry point for Linux & Tizen applications
730 int main( int argc, char **argv )
731 {
732   Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
733
734   RunTest( application );
735
736   return 0;
737 }