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::SHRINK_TO_FIT;
103 /** Cycle through filter mode options. */
104 SamplingMode::Type NextFilterMode(SamplingMode::Type oldMode)
106 SamplingMode::Type newMode = SamplingMode::BOX;
110 case SamplingMode::BOX:
111 newMode = SamplingMode::NEAREST;
113 case SamplingMode::NEAREST:
114 newMode = SamplingMode::LINEAR;
116 case SamplingMode::LINEAR:
117 newMode = SamplingMode::BOX_THEN_NEAREST;
119 case SamplingMode::BOX_THEN_NEAREST:
120 newMode = SamplingMode::BOX_THEN_LINEAR;
122 case SamplingMode::BOX_THEN_LINEAR:
123 newMode = SamplingMode::NO_FILTER;
125 case SamplingMode::NO_FILTER:
126 newMode = SamplingMode::BOX;
128 case SamplingMode::DONT_CARE:
129 newMode = SamplingMode::BOX;
135 const char* StringFromScalingMode(FittingMode::Type scalingMode)
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";
143 const char* StringFromFilterMode(SamplingMode::Type filterMode)
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";
156 // This example shows the load-time image scaling and filtering features.
158 class ImageScalingAndFilteringController : public ConnectionTracker
161 ImageScalingAndFilteringController(Application& application)
162 : mApplication(application),
163 mLastPinchScale(1.0f),
164 mImageWindowScale(0.5f, 0.5f),
166 mFittingMode(FittingMode::FIT_WIDTH),
167 mSamplingMode(SamplingMode::BOX_THEN_LINEAR),
168 mImageLoading(false),
169 mQueuedImageLoad(false)
171 // Connect to the Application's Init signal
172 mApplication.InitSignal().Connect(this, &ImageScalingAndFilteringController::Create);
175 ~ImageScalingAndFilteringController()
177 // Nothing to do here;
180 // The Init signal is received once (only) during the Application lifetime
181 void Create(Application& application)
183 // Get a handle to the window
184 Window window = application.GetWindow();
185 Vector2 windowSize = window.GetSize();
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);
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);
202 mDesiredBox = Toolkit::ImageView::New(BORDER_IMAGE);
203 background.Add(mDesiredBox);
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);
209 // Initialize the actor
210 mImageView = Toolkit::ImageView::New(IMAGE_PATHS[0]);
212 // Reposition the actor
213 mImageView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
214 mImageView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
216 // Display the actor on the window
217 mDesiredBox.Add(mImageView);
219 mImageView.SetProperty(Actor::Property::SIZE, windowSize * mImageWindowScale);
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);
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);
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);
240 mPanGestureDetector = PanGestureDetector::New();
241 mPanGestureDetector.Attach(mGrabCorner);
242 mPanGestureDetector.DetectedSignal().Connect(this, &ImageScalingAndFilteringController::OnPan);
244 // Tie-in input event handlers:
245 window.KeyEventSignal().Connect(this, &ImageScalingAndFilteringController::OnKeyEvent);
253 * Create the GUI controls which float above the scene
255 void CreateControls()
257 Window window = mApplication.GetWindow();
258 Vector2 windowSize = window.GetSize();
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);
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));
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);
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);
291 // Buttons to popup selectors for fitting and sampling modes:
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);
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));
305 controlsLayer.Add(modesGroupBackground);
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);
317 TextLabel label = TextLabel::New("Image fitting mode:");
318 label.SetStyleName(STYLE_LABEL_TEXT);
319 fittingModeGroup.Add(label);
321 Toolkit::PushButton button = CreateButton(FITTING_BUTTON_ID, StringFromScalingMode(mFittingMode));
322 fittingModeGroup.Add(button);
323 mFittingModeButton = button;
325 modesGroupBackground.Add(fittingModeGroup);
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);
338 TextLabel label = TextLabel::New("Image sampling mode:");
339 label.SetStyleName(STYLE_LABEL_TEXT);
340 samplingModeGroup.Add(label);
342 Toolkit::PushButton button = CreateButton(SAMPLING_BUTTON_ID, StringFromFilterMode(mSamplingMode));
343 samplingModeGroup.Add(button);
344 mSamplingModeButton = button;
346 modesGroupBackground.Add(samplingModeGroup);
350 Toolkit::PushButton CreateButton(const char* id, const char* label)
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);
362 Toolkit::Popup CreatePopup()
364 Window window = mApplication.GetWindow();
365 const float POPUP_WIDTH_DP = window.GetSize().GetWidth() * 0.75f;
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));
373 popup.OutsideTouchedSignal().Connect(this, &ImageScalingAndFilteringController::OnPopupOutsideTouched);
378 Toolkit::PushButton CreatePopupButton(Actor parent, const char* id)
380 Toolkit::PushButton button = Toolkit::PushButton::New();
381 button.SetProperty(Dali::Actor::Property::NAME, id);
382 button.SetProperty(Toolkit::Button::Property::LABEL, id);
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);
389 button.ClickedSignal().Connect(this, &ImageScalingAndFilteringController::OnButtonClicked);
395 bool OnButtonClicked(Toolkit::Button button)
397 if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == FITTING_BUTTON_ID)
399 mPopup = CreatePopup();
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);
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));
416 mPopup.SetContent(fittingModes);
417 mApplication.GetWindow().Add(mPopup);
418 mPopup.SetDisplayState(Toolkit::Popup::SHOWN);
420 else if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == SAMPLING_BUTTON_ID)
422 mPopup = CreatePopup();
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);
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));
443 mPopup.SetContent(samplingModes);
444 mApplication.GetWindow().Add(mPopup);
445 mPopup.SetDisplayState(Toolkit::Popup::SHOWN);
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))
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))
465 bool CheckFittingModeButton(Actor& button, FittingMode::Type mode)
467 const char* const modeName = StringFromScalingMode(mode);
468 if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == modeName)
471 mFittingModeButton.SetProperty(Toolkit::Button::Property::LABEL, modeName);
473 mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
480 bool CheckSamplingModeButton(Actor& button, SamplingMode::Type mode)
482 const char* const modeName = StringFromFilterMode(mode);
483 if(button.GetProperty<std::string>(Dali::Actor::Property::NAME) == modeName)
485 mSamplingMode = mode;
486 mSamplingModeButton.SetProperty(Toolkit::Button::Property::LABEL, modeName);
488 mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
495 void OnPopupOutsideTouched()
499 mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
504 bool OnControlTouched(Actor actor, const TouchEvent& event)
506 if(event.GetPointCount() > 0)
508 switch(event.GetState(0))
512 const std::string& name = actor.GetProperty<std::string>(Dali::Actor::Property::NAME);
513 if(name == NEXT_BUTTON_ID)
515 mCurrentPath = mCurrentPath + 1;
516 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
519 else if(name == PREVIOUS_BUTTON_ID)
521 mCurrentPath = mCurrentPath - 1;
522 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
537 void OnPinch(Actor actor, const PinchGesture& pinch)
539 if(pinch.GetState() == GestureState::STARTED)
541 mLastPinchScale = pinch.GetScale();
543 const float scale = pinch.GetScale();
545 if(!Equals(scale, mLastPinchScale))
547 if(scale < mLastPinchScale)
549 mImageWindowScale.x = std::max(0.05f, mImageWindowScale.x * 0.9f);
550 mImageWindowScale.y = std::max(0.05f, mImageWindowScale.y * 0.9f);
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));
559 mLastPinchScale = scale;
562 void OnPan(Actor actor, const PanGesture& gesture)
564 Window window = mApplication.GetWindow();
565 Vector2 windowSize = window.GetSize();
566 const Vector2& displacement = gesture.GetDisplacement();
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)));
575 void OnKeyEvent(const KeyEvent& event)
577 if(event.GetState() == KeyEvent::DOWN)
579 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
581 if(mPopup && mPopup.GetCurrentProperty<bool>(Actor::Property::VISIBLE))
583 mPopup.SetDisplayState(Toolkit::Popup::HIDDEN);
591 else if(event.GetKeyName() == "Right")
593 mImageWindowScale.x = std::max(0.05f, std::min(1.0f, mImageWindowScale.x * 1.1f));
595 else if(event.GetKeyName() == "Left")
597 mImageWindowScale.x = std::max(0.05f, mImageWindowScale.x * 0.9f);
599 else if(event.GetKeyName() == "Up")
601 mImageWindowScale.y = std::max(0.05f, std::min(1.0f, mImageWindowScale.y * 1.1f));
603 else if(event.GetKeyName() == "Down")
605 mImageWindowScale.y = std::max(0.05f, mImageWindowScale.y * 0.9f);
607 else if(event.GetKeyName() == "o")
609 mImageWindowScale.x = std::max(0.05f, mImageWindowScale.x * 0.9f);
610 mImageWindowScale.y = std::max(0.05f, mImageWindowScale.y * 0.9f);
612 else if(event.GetKeyName() == "p")
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));
617 else if(event.GetKeyName() == "n")
619 mCurrentPath = mCurrentPath + 1;
620 mCurrentPath = mCurrentPath < NUM_IMAGE_PATHS ? mCurrentPath : 0;
622 else if(event.GetKeyName() == "b")
624 mCurrentPath = mCurrentPath - 1;
625 mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
627 // Cycle filter and scaling modes:
628 else if(event.GetKeyName() == "f")
630 mSamplingMode = NextFilterMode(mSamplingMode);
631 mSamplingModeButton.SetProperty(Toolkit::Button::Property::LABEL, StringFromFilterMode(mSamplingMode));
633 // Cycle filter and scaling modes:
634 else if(event.GetKeyName() == "s")
636 mFittingMode = NextScalingMode(mFittingMode);
637 mFittingModeButton.SetProperty(Toolkit::Button::Property::LABEL, StringFromScalingMode(mFittingMode));
651 mImageLoading = true;
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);
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;
665 mImageView.SetProperty(Toolkit::ImageView::Property::IMAGE, map);
670 Window window = mApplication.GetWindow();
671 Vector2 windowSize = window.GetSize();
672 Size imageSize = windowSize * mImageWindowScale;
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);
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;
694 FittingMode::Type mFittingMode;
695 SamplingMode::Type mSamplingMode;
697 bool mQueuedImageLoad;
700 int DALI_EXPORT_API main(int argc, char** argv)
702 Application application = Application::New(&argc, &argv, DEMO_THEME_PATH);
703 ImageScalingAndFilteringController test(application);
704 application.MainLoop();