[dali_2.3.38] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / image-scaling-and-filtering / image-scaling-and-filtering-example.cpp
1 /*
2  * Copyright (c) 2021 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-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/popup/popup.h>
20 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
21 #include <dali/dali.h>
22 #include <dali/devel-api/actors/actor-devel.h>
23 #include <iostream>
24 #include "shared/view.h"
25
26 using namespace Dali;
27 using Toolkit::TextLabel;
28
29 namespace
30 {
31 const char*   BACKGROUND_IMAGE(DEMO_IMAGE_DIR "background-gradient.jpg");
32 const Vector4 BACKGROUND_COLOUR(1.0f, 1.0f, 1.0f, 0.15f);
33
34 const char* BORDER_IMAGE(DEMO_IMAGE_DIR "border-4px.9.png");
35 const int   BORDER_WIDTH = (11.0f + 4.0f); // Shadow size = 11, border size = 4.
36 const char* RESIZE_HANDLE_IMAGE(DEMO_IMAGE_DIR "resize-handle.png");
37
38 const int MARGIN_SIZE = 10;
39
40 const char* const NEXT_BUTTON_ID     = "NEXT_BUTTON";
41 const char* const PREVIOUS_BUTTON_ID = "PREVIOUS_BUTTON";
42 const char* const DALI_ICON_PLAY     = DEMO_IMAGE_DIR "icon-play.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  = "ImageScalingGroupLabel";
50 const char* const STYLE_BUTTON_TEXT = "ImageScalingButton";
51
52 const char* IMAGE_PATHS[] =
53   {
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",
69
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",
73
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",
78     NULL};
79 const int NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u;
80
81 /** Cycle the scaling mode options. */
82 FittingMode::Type NextScalingMode(FittingMode::Type oldMode)
83 {
84   FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
85   switch(oldMode)
86   {
87     case FittingMode::SHRINK_TO_FIT:
88       newMode = FittingMode::SCALE_TO_FILL;
89       break;
90     case FittingMode::SCALE_TO_FILL:
91       newMode = FittingMode::FIT_WIDTH;
92       break;
93     case FittingMode::FIT_WIDTH:
94       newMode = FittingMode::FIT_HEIGHT;
95       break;
96     case FittingMode::FIT_HEIGHT:
97       newMode = FittingMode::VISUAL_FITTING;
98       break;
99     case FittingMode::VISUAL_FITTING:
100       newMode = FittingMode::SHRINK_TO_FIT;
101       break;
102   }
103   return newMode;
104 }
105
106 /** Cycle through filter mode options. */
107 SamplingMode::Type NextFilterMode(SamplingMode::Type oldMode)
108 {
109   SamplingMode::Type newMode = SamplingMode::BOX;
110
111   switch(oldMode)
112   {
113     case SamplingMode::BOX:
114       newMode = SamplingMode::NEAREST;
115       break;
116     case SamplingMode::NEAREST:
117       newMode = SamplingMode::LINEAR;
118       break;
119     case SamplingMode::LINEAR:
120       newMode = SamplingMode::BOX_THEN_NEAREST;
121       break;
122     case SamplingMode::BOX_THEN_NEAREST:
123       newMode = SamplingMode::BOX_THEN_LINEAR;
124       break;
125     case SamplingMode::BOX_THEN_LINEAR:
126       newMode = SamplingMode::NO_FILTER;
127       break;
128     case SamplingMode::NO_FILTER:
129       newMode = SamplingMode::BOX;
130       break;
131     case SamplingMode::DONT_CARE:
132       newMode = SamplingMode::BOX;
133       break;
134   }
135   return newMode;
136 }
137
138 const char* StringFromScalingMode(FittingMode::Type scalingMode)
139 {
140   return scalingMode == FittingMode::SCALE_TO_FILL ? "SCALE_TO_FILL" : scalingMode == FittingMode::SHRINK_TO_FIT ? "SHRINK_TO_FIT"
141                                                                      : scalingMode == FittingMode::FIT_WIDTH     ? "FIT_WIDTH"
142                                                                      : scalingMode == FittingMode::FIT_HEIGHT    ? "FIT_HEIGHT"
143                                                                                                                  : "UnknownScalingMode";
144 }
145
146 const char* StringFromFilterMode(SamplingMode::Type filterMode)
147 {
148   return filterMode == SamplingMode::BOX ? "BOX" : filterMode == SamplingMode::BOX_THEN_NEAREST ? "BOX_THEN_NEAREST"
149                                                  : filterMode == SamplingMode::BOX_THEN_LINEAR  ? "BOX_THEN_LINEAR"
150                                                  : filterMode == SamplingMode::NEAREST          ? "NEAREST"
151                                                  : filterMode == SamplingMode::LINEAR           ? "LINEAR"
152                                                  : filterMode == SamplingMode::NO_FILTER        ? "NO_FILTER"
153                                                  : filterMode == SamplingMode::DONT_CARE        ? "DONT_CARE"
154                                                                                                 : "UnknownFilterMode";
155 }
156
157 } // namespace
158
159 // This example shows the load-time image scaling and filtering features.
160 //
161 class ImageScalingAndFilteringController : public ConnectionTracker
162 {
163 public:
164   ImageScalingAndFilteringController(Application& application)
165   : mApplication(application),
166     mLastPinchScale(1.0f),
167     mImageWindowScale(0.5f, 0.5f),
168     mCurrentPath(0),
169     mFittingMode(FittingMode::FIT_WIDTH),
170     mSamplingMode(SamplingMode::BOX_THEN_LINEAR),
171     mImageLoading(false),
172     mQueuedImageLoad(false)
173   {
174     // Connect to the Application's Init signal
175     mApplication.InitSignal().Connect(this, &ImageScalingAndFilteringController::Create);
176   }
177
178   ~ImageScalingAndFilteringController()
179   {
180     // Nothing to do here;
181   }
182
183   // The Init signal is received once (only) during the Application lifetime
184   void Create(Application& application)
185   {
186     // Get a handle to the window
187     Window  window     = application.GetWindow();
188     Vector2 windowSize = window.GetSize();
189
190     // Background image:
191     Dali::Property::Map backgroundImage;
192     backgroundImage.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
193     backgroundImage.Insert(Toolkit::ImageVisual::Property::URL, BACKGROUND_IMAGE);
194     backgroundImage.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, windowSize.width);
195     backgroundImage.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, windowSize.height);
196     backgroundImage.Insert(Toolkit::ImageVisual::Property::FITTING_MODE, FittingMode::SCALE_TO_FILL);
197     backgroundImage.Insert(Toolkit::ImageVisual::Property::SAMPLING_MODE, SamplingMode::BOX_THEN_NEAREST);
198
199     Toolkit::ImageView background = Toolkit::ImageView::New();
200     background.SetProperty(Toolkit::ImageView::Property::IMAGE, backgroundImage);
201     background.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
202     background.SetProperty(Actor::Property::SIZE, windowSize);
203     window.Add(background);
204
205     mDesiredBox = Toolkit::ImageView::New(BORDER_IMAGE);
206     background.Add(mDesiredBox);
207
208     mDesiredBox.SetProperty(Actor::Property::SIZE, windowSize * mImageWindowScale);
209     mDesiredBox.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
210     mDesiredBox.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
211
212     // Initialize the actor
213     mImageView = Toolkit::ImageView::New(IMAGE_PATHS[0]);
214
215     // Reposition the actor
216     mImageView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
217     mImageView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
218
219     // Display the actor on the window
220     mDesiredBox.Add(mImageView);
221
222     mImageView.SetProperty(Actor::Property::SIZE, windowSize * mImageWindowScale);
223
224     // Setup the pinch detector for scaling the desired image load dimensions:
225     mPinchDetector = PinchGestureDetector::New();
226     mPinchDetector.Attach(mImageView);
227     mPinchDetector.DetectedSignal().Connect(this, &ImageScalingAndFilteringController::OnPinch);
228
229     mGrabCorner = Toolkit::ImageView::New(RESIZE_HANDLE_IMAGE);
230     mGrabCorner.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
231     mGrabCorner.SetProperty(Dali::Actor::Property::NAME, "GrabCorner");
232     mGrabCorner.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_RIGHT);
233     mGrabCorner.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_RIGHT);
234     mGrabCorner.SetProperty(Actor::Property::POSITION, Vector2(-BORDER_WIDTH, -BORDER_WIDTH));
235     mGrabCorner.SetProperty(Actor::Property::OPACITY, 0.6f);
236
237     Layer grabCornerLayer = Layer::New();
238     grabCornerLayer.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_RIGHT);
239     grabCornerLayer.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_RIGHT);
240     grabCornerLayer.Add(mGrabCorner);
241     mDesiredBox.Add(grabCornerLayer);
242
243     mPanGestureDetector = PanGestureDetector::New();
244     mPanGestureDetector.Attach(mGrabCorner);
245     mPanGestureDetector.DetectedSignal().Connect(this, &ImageScalingAndFilteringController::OnPan);
246
247     // Tie-in input event handlers:
248     window.KeyEventSignal().Connect(this, &ImageScalingAndFilteringController::OnKeyEvent);
249
250     CreateControls();
251
252     ResizeImage();
253   }
254
255   /**
256    * Create the GUI controls which float above the scene
257    */
258   void CreateControls()
259   {
260     Window  window     = mApplication.GetWindow();
261     Vector2 windowSize = window.GetSize();
262
263     Dali::Layer controlsLayer = Dali::Layer::New();
264     controlsLayer.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS);
265     controlsLayer.SetProperty(Actor::Property::SIZE_MODE_FACTOR, Vector3(1.0f, 1.0f, 1.0f));
266     controlsLayer.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
267     controlsLayer.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
268     window.Add(controlsLayer);
269
270     // Back and next image buttons in corners of window:
271     unsigned int       playWidth     = std::min(windowSize.x * (1 / 5.0f), 58.0f);
272     Toolkit::ImageView imagePrevious = Toolkit::ImageView::New(DALI_ICON_PLAY, ImageDimensions(playWidth, playWidth));
273
274     // Last image button:
275     imagePrevious.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
276     imagePrevious.RotateBy(Radian(3.14159265358979323846f), Vector3(0, 1.0f, 0));
277     imagePrevious.SetProperty(Actor::Property::POSITION_Y, playWidth * 0.5f);
278     imagePrevious.SetProperty(Actor::Property::POSITION_X, playWidth + playWidth * 0.5f);
279     imagePrevious.SetProperty(Actor::Property::OPACITY, 0.6f);
280     controlsLayer.Add(imagePrevious);
281     imagePrevious.SetProperty(Dali::Actor::Property::NAME, PREVIOUS_BUTTON_ID);
282     imagePrevious.TouchedSignal().Connect(this, &ImageScalingAndFilteringController::OnControlTouched);
283
284     // Next image button:
285     Toolkit::ImageView imageNext = Toolkit::ImageView::New(DALI_ICON_PLAY, ImageDimensions(playWidth, playWidth));
286     imageNext.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_RIGHT);
287     imageNext.SetProperty(Actor::Property::POSITION_Y, playWidth * 0.5f);
288     imageNext.SetProperty(Actor::Property::POSITION_X, windowSize.x - playWidth * 0.5f);
289     imageNext.SetProperty(Actor::Property::OPACITY, 0.6f);
290     controlsLayer.Add(imageNext);
291     imageNext.SetProperty(Dali::Actor::Property::NAME, NEXT_BUTTON_ID);
292     imageNext.TouchedSignal().Connect(this, &ImageScalingAndFilteringController::OnControlTouched);
293
294     // Buttons to popup selectors for fitting and sampling modes:
295
296     // Wrapper table to hold two buttons side by side:
297     Toolkit::TableView modesGroupBackground = Toolkit::TableView::New(1, 2);
298     modesGroupBackground.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
299     modesGroupBackground.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
300     modesGroupBackground.SetBackgroundColor(BACKGROUND_COLOUR);
301     modesGroupBackground.SetCellPadding(Size(MARGIN_SIZE * 0.5f, MARGIN_SIZE));
302     modesGroupBackground.SetFitHeight(0);
303
304     modesGroupBackground.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT);
305     modesGroupBackground.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_LEFT);
306     modesGroupBackground.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
307
308     controlsLayer.Add(modesGroupBackground);
309
310     {
311       // Vertical table to hold label and button:
312       Toolkit::TableView fittingModeGroup = Toolkit::TableView::New(2, 1);
313       fittingModeGroup.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
314       fittingModeGroup.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
315       fittingModeGroup.SetBackgroundColor(BACKGROUND_COLOUR);
316       fittingModeGroup.SetCellPadding(Size(MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f));
317       fittingModeGroup.SetFitHeight(0);
318       fittingModeGroup.SetFitHeight(1);
319
320       TextLabel label = TextLabel::New("Image fitting mode:");
321       label.SetStyleName(STYLE_LABEL_TEXT);
322       fittingModeGroup.Add(label);
323
324       Toolkit::PushButton button = CreateButton(FITTING_BUTTON_ID, StringFromScalingMode(mFittingMode));
325       fittingModeGroup.Add(button);
326       mFittingModeButton = button;
327
328       modesGroupBackground.Add(fittingModeGroup);
329     }
330
331     {
332       // Vertical table to hold label and button:
333       Toolkit::TableView samplingModeGroup = Toolkit::TableView::New(2, 1);
334       samplingModeGroup.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
335       samplingModeGroup.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
336       samplingModeGroup.SetBackgroundColor(BACKGROUND_COLOUR);
337       samplingModeGroup.SetCellPadding(Size(MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f));
338       samplingModeGroup.SetFitHeight(0);
339       samplingModeGroup.SetFitHeight(1);
340
341       TextLabel label = TextLabel::New("Image sampling mode:");
342       label.SetStyleName(STYLE_LABEL_TEXT);
343       samplingModeGroup.Add(label);
344
345       Toolkit::PushButton button = CreateButton(SAMPLING_BUTTON_ID, StringFromFilterMode(mSamplingMode));
346       samplingModeGroup.Add(button);
347       mSamplingModeButton = button;
348
349       modesGroupBackground.Add(samplingModeGroup);
350     }
351   }
352
353   Toolkit::PushButton CreateButton(const char* id, const char* label)
354   {
355     Toolkit::PushButton button = Toolkit::PushButton::New();
356     button.SetStyleName(STYLE_BUTTON_TEXT);
357     button.SetProperty(Dali::Actor::Property::NAME, id);
358     button.SetProperty(Toolkit::Button::Property::LABEL, label);
359     button.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
360     button.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
361     button.ClickedSignal().Connect(this, &ImageScalingAndFilteringController::OnButtonClicked);
362     return button;
363   }
364
365   Toolkit::Popup CreatePopup()
366   {
367     Window      window         = mApplication.GetWindow();
368     const float POPUP_WIDTH_DP = window.GetSize().GetWidth() * 0.75f;
369
370     Toolkit::Popup popup = Toolkit::Popup::New();
371     popup.SetProperty(Dali::Actor::Property::NAME, "POPUP");
372     popup.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
373     popup.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
374     popup.SetProperty(Actor::Property::SIZE, Vector2(POPUP_WIDTH_DP, 0.0f));
375
376     popup.OutsideTouchedSignal().Connect(this, &ImageScalingAndFilteringController::OnPopupOutsideTouched);
377
378     return popup;
379   }
380
381   Toolkit::PushButton CreatePopupButton(Actor parent, const char* id)
382   {
383     Toolkit::PushButton button = Toolkit::PushButton::New();
384     button.SetProperty(Dali::Actor::Property::NAME, id);
385     button.SetProperty(Toolkit::Button::Property::LABEL, id);
386
387     button.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
388     button.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_LEFT);
389     button.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
390     button.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
391
392     button.ClickedSignal().Connect(this, &ImageScalingAndFilteringController::OnButtonClicked);
393
394     parent.Add(button);
395     return button;
396   }
397
398   bool OnButtonClicked(Toolkit::Button button)
399   {
400     if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == FITTING_BUTTON_ID)
401     {
402       mPopup = CreatePopup();
403
404       // Four-row table to hold buttons:
405       Toolkit::TableView fittingModes = Toolkit::TableView::New(4, 1);
406       fittingModes.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
407       fittingModes.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
408       fittingModes.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE * 0.5));
409       fittingModes.SetFitHeight(0);
410       fittingModes.SetFitHeight(1);
411       fittingModes.SetFitHeight(2);
412       fittingModes.SetFitHeight(3);
413
414       CreatePopupButton(fittingModes, StringFromScalingMode(FittingMode::SCALE_TO_FILL));
415       CreatePopupButton(fittingModes, StringFromScalingMode(FittingMode::SHRINK_TO_FIT));
416       CreatePopupButton(fittingModes, StringFromScalingMode(FittingMode::FIT_WIDTH));
417       CreatePopupButton(fittingModes, StringFromScalingMode(FittingMode::FIT_HEIGHT));
418
419       mPopup.SetContent(fittingModes);
420       mApplication.GetWindow().Add(mPopup);
421       mPopup.SetDisplayState(Toolkit::Popup::SHOWN);
422     }
423     else if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == SAMPLING_BUTTON_ID)
424     {
425       mPopup = CreatePopup();
426
427       // Table to hold buttons for each sampling mode:
428       Toolkit::TableView samplingModes = Toolkit::TableView::New(6, 1);
429       samplingModes.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
430       samplingModes.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
431       samplingModes.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE * 0.5));
432       samplingModes.SetFitHeight(0);
433       samplingModes.SetFitHeight(1);
434       samplingModes.SetFitHeight(2);
435       samplingModes.SetFitHeight(3);
436       samplingModes.SetFitHeight(4);
437       samplingModes.SetFitHeight(5);
438
439       CreatePopupButton(samplingModes, StringFromFilterMode(SamplingMode::NEAREST));
440       CreatePopupButton(samplingModes, StringFromFilterMode(SamplingMode::LINEAR));
441       CreatePopupButton(samplingModes, StringFromFilterMode(SamplingMode::BOX));
442       CreatePopupButton(samplingModes, StringFromFilterMode(SamplingMode::BOX_THEN_NEAREST));
443       CreatePopupButton(samplingModes, StringFromFilterMode(SamplingMode::BOX_THEN_LINEAR));
444       CreatePopupButton(samplingModes, StringFromFilterMode(SamplingMode::NO_FILTER));
445
446       mPopup.SetContent(samplingModes);
447       mApplication.GetWindow().Add(mPopup);
448       mPopup.SetDisplayState(Toolkit::Popup::SHOWN);
449     }
450     else if(CheckFittingModeButton(button, FittingMode::SCALE_TO_FILL) ||
451             CheckFittingModeButton(button, FittingMode::SHRINK_TO_FIT) ||
452             CheckFittingModeButton(button, FittingMode::FIT_WIDTH) ||
453             CheckFittingModeButton(button, FittingMode::FIT_HEIGHT))
454     {
455     }
456     else if(CheckSamplingModeButton(button, SamplingMode::NEAREST) ||
457             CheckSamplingModeButton(button, SamplingMode::LINEAR) ||
458             CheckSamplingModeButton(button, SamplingMode::BOX) ||
459             CheckSamplingModeButton(button, SamplingMode::LINEAR) ||
460             CheckSamplingModeButton(button, SamplingMode::BOX_THEN_NEAREST) ||
461             CheckSamplingModeButton(button, SamplingMode::BOX_THEN_LINEAR) ||
462             CheckSamplingModeButton(button, SamplingMode::NO_FILTER))
463     {
464     }
465     return true;
466   }
467
468   bool CheckFittingModeButton(Actor& button, FittingMode::Type mode)
469   {
470     const char* const modeName = StringFromScalingMode(mode);
471     if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == modeName)
472     {
473       mFittingMode = mode;
474       mFittingModeButton.SetProperty(Toolkit::Button::Property::LABEL, modeName);
475       ResizeImage();
476       mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
477       mPopup.Reset();
478       return true;
479     }
480     return false;
481   }
482
483   bool CheckSamplingModeButton(Actor& button, SamplingMode::Type mode)
484   {
485     const char* const modeName = StringFromFilterMode(mode);
486     if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == modeName)
487     {
488       mSamplingMode = mode;
489       mSamplingModeButton.SetProperty(Toolkit::Button::Property::LABEL, modeName);
490       ResizeImage();
491       mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
492       mPopup.Reset();
493       return true;
494     }
495     return false;
496   }
497
498   void OnPopupOutsideTouched()
499   {
500     if(mPopup)
501     {
502       mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
503       mPopup.Reset();
504     }
505   }
506
507   bool OnControlTouched(Actor actor, const TouchEvent& event)
508   {
509     if(event.GetPointCount() > 0)
510     {
511       switch(event.GetState(0))
512       {
513         case PointState::UP:
514         {
515           const std::string& name = actor.GetProperty<std::string>(Dali::Actor::Property::NAME);
516           if(name == NEXT_BUTTON_ID)
517           {
518             mCurrentPath = mCurrentPath + 1;
519             mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
520             ResizeImage();
521           }
522           else if(name == PREVIOUS_BUTTON_ID)
523           {
524             mCurrentPath = mCurrentPath - 1;
525             mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
526             ResizeImage();
527           }
528           break;
529         }
530         default:
531         {
532           break;
533         }
534       } // end switch
535     }
536
537     return false;
538   }
539
540   void OnPinch(Actor actor, const PinchGesture& pinch)
541   {
542     if(pinch.GetState() == GestureState::STARTED)
543     {
544       mLastPinchScale = pinch.GetScale();
545     }
546     const float scale = pinch.GetScale();
547
548     if(!Equals(scale, mLastPinchScale))
549     {
550       if(scale < mLastPinchScale)
551       {
552         mImageWindowScale.x = std::max(0.05f, mImageWindowScale.x * 0.9f);
553         mImageWindowScale.y = std::max(0.05f, mImageWindowScale.y * 0.9f);
554       }
555       else
556       {
557         mImageWindowScale.x = std::max(0.05f, std::min(1.0f, mImageWindowScale.x * 1.1f));
558         mImageWindowScale.y = std::max(0.05f, std::min(1.0f, mImageWindowScale.y * 1.1f));
559       }
560       ResizeImage();
561     }
562     mLastPinchScale = scale;
563   }
564
565   void OnPan(Actor actor, const PanGesture& gesture)
566   {
567     Window         window       = mApplication.GetWindow();
568     Vector2        windowSize   = window.GetSize();
569     const Vector2& displacement = gesture.GetDisplacement();
570
571     // 1.0f and 0.75f are the maximum size caps of the resized image, as a factor of window-size.
572     mImageWindowScale.x = std::max(0.05f, std::min(0.95f, mImageWindowScale.x + (displacement.x * 2.0f / windowSize.width)));
573     mImageWindowScale.y = std::max(0.05f, std::min(0.70f, mImageWindowScale.y + (displacement.y * 2.0f / windowSize.height)));
574
575     ResizeImage();
576   }
577
578   void OnKeyEvent(const KeyEvent& event)
579   {
580     if(event.GetState() == KeyEvent::DOWN)
581     {
582       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
583       {
584         if(mPopup && mPopup.GetCurrentProperty<bool>(Actor::Property::VISIBLE))
585         {
586           mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
587           mPopup.Reset();
588         }
589         else
590         {
591           mApplication.Quit();
592         }
593       }
594       else if(event.GetKeyName() == "Right")
595       {
596         mImageWindowScale.x = std::max(0.05f, std::min(1.0f, mImageWindowScale.x * 1.1f));
597       }
598       else if(event.GetKeyName() == "Left")
599       {
600         mImageWindowScale.x = std::max(0.05f, mImageWindowScale.x * 0.9f);
601       }
602       else if(event.GetKeyName() == "Up")
603       {
604         mImageWindowScale.y = std::max(0.05f, std::min(1.0f, mImageWindowScale.y * 1.1f));
605       }
606       else if(event.GetKeyName() == "Down")
607       {
608         mImageWindowScale.y = std::max(0.05f, mImageWindowScale.y * 0.9f);
609       }
610       else if(event.GetKeyName() == "o")
611       {
612         mImageWindowScale.x = std::max(0.05f, mImageWindowScale.x * 0.9f);
613         mImageWindowScale.y = std::max(0.05f, mImageWindowScale.y * 0.9f);
614       }
615       else if(event.GetKeyName() == "p")
616       {
617         mImageWindowScale.x = std::max(0.05f, std::min(1.0f, mImageWindowScale.x * 1.1f));
618         mImageWindowScale.y = std::max(0.05f, std::min(1.0f, mImageWindowScale.y * 1.1f));
619       }
620       else if(event.GetKeyName() == "n")
621       {
622         mCurrentPath = mCurrentPath + 1;
623         mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
624       }
625       else if(event.GetKeyName() == "b")
626       {
627         mCurrentPath = mCurrentPath - 1;
628         mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
629       }
630       // Cycle filter and scaling modes:
631       else if(event.GetKeyName() == "f")
632       {
633         mSamplingMode = NextFilterMode(mSamplingMode);
634         mSamplingModeButton.SetProperty(Toolkit::Button::Property::LABEL, StringFromFilterMode(mSamplingMode));
635       }
636       // Cycle filter and scaling modes:
637       else if(event.GetKeyName() == "s")
638       {
639         mFittingMode = NextScalingMode(mFittingMode);
640         mFittingModeButton.SetProperty(Toolkit::Button::Property::LABEL, StringFromScalingMode(mFittingMode));
641       }
642       else
643       {
644         return;
645       }
646
647       ResizeImage();
648     }
649   }
650
651 private:
652   void LoadImage()
653   {
654     mImageLoading = true;
655
656     const char* const path      = IMAGE_PATHS[mCurrentPath];
657     Window            window    = mApplication.GetWindow();
658     Size              imageSize = Vector2(window.GetSize()) * mImageWindowScale;
659     mImageView.SetProperty(Actor::Property::SIZE, imageSize);
660
661     Property::Map map;
662     map[Toolkit::ImageVisual::Property::URL]            = path;
663     map[Toolkit::ImageVisual::Property::DESIRED_WIDTH]  = imageSize.x;
664     map[Toolkit::ImageVisual::Property::DESIRED_HEIGHT] = imageSize.y;
665     map[Toolkit::ImageVisual::Property::FITTING_MODE]   = mFittingMode;
666     map[Toolkit::ImageVisual::Property::SAMPLING_MODE]  = mSamplingMode;
667
668     mImageView.SetProperty(Toolkit::ImageView::Property::IMAGE, map);
669   }
670
671   void ResizeImage()
672   {
673     Window  window     = mApplication.GetWindow();
674     Vector2 windowSize = window.GetSize();
675     Size    imageSize  = windowSize * mImageWindowScale;
676
677     LoadImage();
678
679     // Border size needs to be modified to take into account the width of the frame.
680     Vector2 borderScale((imageSize + Vector2(BORDER_WIDTH * 2.0f, BORDER_WIDTH * 2.0f)) / windowSize);
681     mDesiredBox.SetProperty(Actor::Property::SIZE, windowSize * borderScale);
682   }
683
684 private:
685   Application&         mApplication;
686   Toolkit::ImageView   mDesiredBox; //< Background rectangle to show requested image size.
687   Toolkit::PushButton  mFittingModeButton;
688   Toolkit::PushButton  mSamplingModeButton;
689   Toolkit::Popup       mPopup;
690   PinchGestureDetector mPinchDetector;
691   float                mLastPinchScale;
692   Toolkit::ImageView   mGrabCorner;
693   PanGestureDetector   mPanGestureDetector;
694   Toolkit::ImageView   mImageView;
695   Vector2              mImageWindowScale;
696   int                  mCurrentPath;
697   FittingMode::Type    mFittingMode;
698   SamplingMode::Type   mSamplingMode;
699   bool                 mImageLoading;
700   bool                 mQueuedImageLoad;
701 };
702
703 int DALI_EXPORT_API main(int argc, char** argv)
704 {
705   Application                        application = Application::New(&argc, &argv, DEMO_THEME_PATH);
706   ImageScalingAndFilteringController test(application);
707   application.MainLoop();
708   return 0;
709 }