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