2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali-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>
24 #include "shared/view.h"
27 using Toolkit::TextLabel;
31 const char* BACKGROUND_IMAGE(DEMO_IMAGE_DIR "background-gradient.jpg");
32 const Vector4 BACKGROUND_COLOUR(1.0f, 1.0f, 1.0f, 0.15f);
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");
38 const int MARGIN_SIZE = 10;
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";
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";
49 const char* const STYLE_LABEL_TEXT = "ImageScalingGroupLabel";
50 const char* const STYLE_BUTTON_TEXT = "ImageScalingButton";
52 const char* IMAGE_PATHS[] =
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",
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",
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",
79 const int NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u;
81 /** Cycle the scaling mode options. */
82 FittingMode::Type NextScalingMode(FittingMode::Type oldMode)
84 FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
87 case FittingMode::SHRINK_TO_FIT:
88 newMode = FittingMode::SCALE_TO_FILL;
90 case FittingMode::SCALE_TO_FILL:
91 newMode = FittingMode::FIT_WIDTH;
93 case FittingMode::FIT_WIDTH:
94 newMode = FittingMode::FIT_HEIGHT;
96 case FittingMode::FIT_HEIGHT:
97 newMode = FittingMode::VISUAL_FITTING;
99 case FittingMode::VISUAL_FITTING:
100 newMode = FittingMode::SHRINK_TO_FIT;
106 /** Cycle through filter mode options. */
107 SamplingMode::Type NextFilterMode(SamplingMode::Type oldMode)
109 SamplingMode::Type newMode = SamplingMode::BOX;
113 case SamplingMode::BOX:
114 newMode = SamplingMode::NEAREST;
116 case SamplingMode::NEAREST:
117 newMode = SamplingMode::LINEAR;
119 case SamplingMode::LINEAR:
120 newMode = SamplingMode::BOX_THEN_NEAREST;
122 case SamplingMode::BOX_THEN_NEAREST:
123 newMode = SamplingMode::BOX_THEN_LINEAR;
125 case SamplingMode::BOX_THEN_LINEAR:
126 newMode = SamplingMode::NO_FILTER;
128 case SamplingMode::NO_FILTER:
129 newMode = SamplingMode::BOX;
131 case SamplingMode::DONT_CARE:
132 newMode = SamplingMode::BOX;
138 const char* StringFromScalingMode(FittingMode::Type scalingMode)
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";
146 const char* StringFromFilterMode(SamplingMode::Type filterMode)
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";
159 // This example shows the load-time image scaling and filtering features.
161 class ImageScalingAndFilteringController : public ConnectionTracker
164 ImageScalingAndFilteringController(Application& application)
165 : mApplication(application),
166 mLastPinchScale(1.0f),
167 mImageWindowScale(0.5f, 0.5f),
169 mFittingMode(FittingMode::FIT_WIDTH),
170 mSamplingMode(SamplingMode::BOX_THEN_LINEAR),
171 mImageLoading(false),
172 mQueuedImageLoad(false)
174 // Connect to the Application's Init signal
175 mApplication.InitSignal().Connect(this, &ImageScalingAndFilteringController::Create);
178 ~ImageScalingAndFilteringController()
180 // Nothing to do here;
183 // The Init signal is received once (only) during the Application lifetime
184 void Create(Application& application)
186 // Get a handle to the window
187 Window window = application.GetWindow();
188 Vector2 windowSize = window.GetSize();
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);
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);
205 mDesiredBox = Toolkit::ImageView::New(BORDER_IMAGE);
206 background.Add(mDesiredBox);
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);
212 // Initialize the actor
213 mImageView = Toolkit::ImageView::New(IMAGE_PATHS[0]);
215 // Reposition the actor
216 mImageView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
217 mImageView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
219 // Display the actor on the window
220 mDesiredBox.Add(mImageView);
222 mImageView.SetProperty(Actor::Property::SIZE, windowSize * mImageWindowScale);
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);
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);
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);
243 mPanGestureDetector = PanGestureDetector::New();
244 mPanGestureDetector.Attach(mGrabCorner);
245 mPanGestureDetector.DetectedSignal().Connect(this, &ImageScalingAndFilteringController::OnPan);
247 // Tie-in input event handlers:
248 window.KeyEventSignal().Connect(this, &ImageScalingAndFilteringController::OnKeyEvent);
256 * Create the GUI controls which float above the scene
258 void CreateControls()
260 Window window = mApplication.GetWindow();
261 Vector2 windowSize = window.GetSize();
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);
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));
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);
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);
294 // Buttons to popup selectors for fitting and sampling modes:
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);
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));
308 controlsLayer.Add(modesGroupBackground);
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);
320 TextLabel label = TextLabel::New("Image fitting mode:");
321 label.SetStyleName(STYLE_LABEL_TEXT);
322 fittingModeGroup.Add(label);
324 Toolkit::PushButton button = CreateButton(FITTING_BUTTON_ID, StringFromScalingMode(mFittingMode));
325 fittingModeGroup.Add(button);
326 mFittingModeButton = button;
328 modesGroupBackground.Add(fittingModeGroup);
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);
341 TextLabel label = TextLabel::New("Image sampling mode:");
342 label.SetStyleName(STYLE_LABEL_TEXT);
343 samplingModeGroup.Add(label);
345 Toolkit::PushButton button = CreateButton(SAMPLING_BUTTON_ID, StringFromFilterMode(mSamplingMode));
346 samplingModeGroup.Add(button);
347 mSamplingModeButton = button;
349 modesGroupBackground.Add(samplingModeGroup);
353 Toolkit::PushButton CreateButton(const char* id, const char* label)
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);
365 Toolkit::Popup CreatePopup()
367 Window window = mApplication.GetWindow();
368 const float POPUP_WIDTH_DP = window.GetSize().GetWidth() * 0.75f;
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));
376 popup.OutsideTouchedSignal().Connect(this, &ImageScalingAndFilteringController::OnPopupOutsideTouched);
381 Toolkit::PushButton CreatePopupButton(Actor parent, const char* id)
383 Toolkit::PushButton button = Toolkit::PushButton::New();
384 button.SetProperty(Dali::Actor::Property::NAME, id);
385 button.SetProperty(Toolkit::Button::Property::LABEL, id);
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);
392 button.ClickedSignal().Connect(this, &ImageScalingAndFilteringController::OnButtonClicked);
398 bool OnButtonClicked(Toolkit::Button button)
400 if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == FITTING_BUTTON_ID)
402 mPopup = CreatePopup();
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);
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));
419 mPopup.SetContent(fittingModes);
420 mApplication.GetWindow().Add(mPopup);
421 mPopup.SetDisplayState(Toolkit::Popup::SHOWN);
423 else if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == SAMPLING_BUTTON_ID)
425 mPopup = CreatePopup();
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);
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));
446 mPopup.SetContent(samplingModes);
447 mApplication.GetWindow().Add(mPopup);
448 mPopup.SetDisplayState(Toolkit::Popup::SHOWN);
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))
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))
468 bool CheckFittingModeButton(Actor& button, FittingMode::Type mode)
470 const char* const modeName = StringFromScalingMode(mode);
471 if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == modeName)
474 mFittingModeButton.SetProperty(Toolkit::Button::Property::LABEL, modeName);
476 mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
483 bool CheckSamplingModeButton(Actor& button, SamplingMode::Type mode)
485 const char* const modeName = StringFromFilterMode(mode);
486 if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == modeName)
488 mSamplingMode = mode;
489 mSamplingModeButton.SetProperty(Toolkit::Button::Property::LABEL, modeName);
491 mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
498 void OnPopupOutsideTouched()
502 mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
507 bool OnControlTouched(Actor actor, const TouchEvent& event)
509 if(event.GetPointCount() > 0)
511 switch(event.GetState(0))
515 const std::string& name = actor.GetProperty<std::string>(Dali::Actor::Property::NAME);
516 if(name == NEXT_BUTTON_ID)
518 mCurrentPath = mCurrentPath + 1;
519 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
522 else if(name == PREVIOUS_BUTTON_ID)
524 mCurrentPath = mCurrentPath - 1;
525 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
540 void OnPinch(Actor actor, const PinchGesture& pinch)
542 if(pinch.GetState() == GestureState::STARTED)
544 mLastPinchScale = pinch.GetScale();
546 const float scale = pinch.GetScale();
548 if(!Equals(scale, mLastPinchScale))
550 if(scale < mLastPinchScale)
552 mImageWindowScale.x = std::max(0.05f, mImageWindowScale.x * 0.9f);
553 mImageWindowScale.y = std::max(0.05f, mImageWindowScale.y * 0.9f);
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));
562 mLastPinchScale = scale;
565 void OnPan(Actor actor, const PanGesture& gesture)
567 Window window = mApplication.GetWindow();
568 Vector2 windowSize = window.GetSize();
569 const Vector2& displacement = gesture.GetDisplacement();
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)));
578 void OnKeyEvent(const KeyEvent& event)
580 if(event.GetState() == KeyEvent::DOWN)
582 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
584 if(mPopup && mPopup.GetCurrentProperty<bool>(Actor::Property::VISIBLE))
586 mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
594 else if(event.GetKeyName() == "Right")
596 mImageWindowScale.x = std::max(0.05f, std::min(1.0f, mImageWindowScale.x * 1.1f));
598 else if(event.GetKeyName() == "Left")
600 mImageWindowScale.x = std::max(0.05f, mImageWindowScale.x * 0.9f);
602 else if(event.GetKeyName() == "Up")
604 mImageWindowScale.y = std::max(0.05f, std::min(1.0f, mImageWindowScale.y * 1.1f));
606 else if(event.GetKeyName() == "Down")
608 mImageWindowScale.y = std::max(0.05f, mImageWindowScale.y * 0.9f);
610 else if(event.GetKeyName() == "o")
612 mImageWindowScale.x = std::max(0.05f, mImageWindowScale.x * 0.9f);
613 mImageWindowScale.y = std::max(0.05f, mImageWindowScale.y * 0.9f);
615 else if(event.GetKeyName() == "p")
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));
620 else if(event.GetKeyName() == "n")
622 mCurrentPath = mCurrentPath + 1;
623 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
625 else if(event.GetKeyName() == "b")
627 mCurrentPath = mCurrentPath - 1;
628 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
630 // Cycle filter and scaling modes:
631 else if(event.GetKeyName() == "f")
633 mSamplingMode = NextFilterMode(mSamplingMode);
634 mSamplingModeButton.SetProperty(Toolkit::Button::Property::LABEL, StringFromFilterMode(mSamplingMode));
636 // Cycle filter and scaling modes:
637 else if(event.GetKeyName() == "s")
639 mFittingMode = NextScalingMode(mFittingMode);
640 mFittingModeButton.SetProperty(Toolkit::Button::Property::LABEL, StringFromScalingMode(mFittingMode));
654 mImageLoading = true;
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);
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;
668 mImageView.SetProperty(Toolkit::ImageView::Property::IMAGE, map);
673 Window window = mApplication.GetWindow();
674 Vector2 windowSize = window.GetSize();
675 Size imageSize = windowSize * mImageWindowScale;
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);
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;
697 FittingMode::Type mFittingMode;
698 SamplingMode::Type mSamplingMode;
700 bool mQueuedImageLoad;
703 int DALI_EXPORT_API main(int argc, char** argv)
705 Application application = Application::New(&argc, &argv, DEMO_THEME_PATH);
706 ImageScalingAndFilteringController test(application);
707 application.MainLoop();