2 * Copyright (c) 2020 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/table-view/table-view.h>
20 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
21 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
22 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
23 #include <dali/dali.h>
25 #include "shared/view.h"
31 const char* BACKGROUND_IMAGE(DEMO_IMAGE_DIR "background-gradient.jpg");
32 const char* TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png");
33 const char* APPLICATION_TITLE("Image view");
35 const char* IMAGE_PATH[] = {
36 DEMO_IMAGE_DIR "gallery-small-23.jpg",
37 DEMO_IMAGE_DIR "woodEffect.jpg",
38 DEMO_IMAGE_DIR "wood.png", // 32bits image
39 DEMO_IMAGE_DIR "heartsframe.9.png",
40 DEMO_IMAGE_DIR "World.svg"};
42 const unsigned int NUMBER_OF_IMAGES = 3;
53 unsigned int GetButtonIndex(Toolkit::Button button)
55 std::string buttonName = button.GetProperty<std::string>(Dali::Actor::Property::NAME);
56 unsigned int index = 0;
60 index = std::stoul(buttonName);
66 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
68 std::string EXAMPLE_INSTRUCTIONS =
69 "Instructions: Change button cycles through different image visuals, "
70 "on/off takes the ImageView and it's current visual on or off window.";
72 const float CORNER_RADIUS_VALUE(20.0f);
76 class ImageViewController : public ConnectionTracker
79 ImageViewController(Application& application)
80 : mApplication(application),
81 mCurrentPositionToggle(0, 0),
82 mCurrentPositionImage(0, 0)
84 // Connect to the Application's Init signal
85 mApplication.InitSignal().Connect(this, &ImageViewController::Create);
88 ~ImageViewController()
93 void Create(Application& application)
95 // The Init signal is received once (only) during the Application lifetime
97 // Creates a default view with a default tool bar.
98 // The view is added to the window.
99 mContentLayer = DemoHelper::CreateView(application,
106 // Create a table view to show a pair of buttons above each image.
107 mTable = Toolkit::TableView::New(CellPlacement::NUMBER_OF_ROWS, NUMBER_OF_IMAGES);
108 mTable.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
109 mTable.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
110 mTable.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS);
111 Vector3 offset(0.9f, 0.70f, 0.0f);
112 mTable.SetProperty(Actor::Property::SIZE_MODE_FACTOR, offset);
113 mTable.SetFitHeight(CellPlacement::TOP_BUTTON);
114 mTable.SetFitHeight(CellPlacement::MID_BUTTON);
115 mTable.SetFitHeight(CellPlacement::LOWER_BUTTON);
116 mContentLayer.Add(mTable);
118 Toolkit::TextLabel instructions = Toolkit::TextLabel::New(EXAMPLE_INSTRUCTIONS);
119 instructions.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
120 instructions.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
121 instructions.SetProperty(Actor::Property::POSITION_Y, -50.0f);
122 instructions.SetProperty(Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL, true);
123 instructions.SetProperty(Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT, 10);
124 mContentLayer.Add(instructions);
126 for(unsigned int x = 0; x < NUMBER_OF_IMAGES; x++)
128 Toolkit::PushButton button = Toolkit::PushButton::New();
129 button.SetProperty(Toolkit::Button::Property::LABEL, "on/off");
130 button.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
131 button.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
132 button.ClickedSignal().Connect(this, &ImageViewController::ToggleImageOnWindow);
133 button.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
134 button.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
135 std::string s = std::to_string(x);
136 button.SetProperty(Dali::Actor::Property::NAME, s);
137 mTable.AddChild(button, Toolkit::TableView::CellPosition(CellPlacement::TOP_BUTTON, x));
139 Toolkit::PushButton button2 = Toolkit::PushButton::New();
140 button2.SetProperty(Toolkit::Button::Property::LABEL, "Change");
141 button2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
142 button2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
143 button2.ClickedSignal().Connect(this, &ImageViewController::ChangeImageClicked);
144 button2.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
145 button2.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
146 button2.SetProperty(Dali::Actor::Property::NAME, s);
147 mTable.AddChild(button2, Toolkit::TableView::CellPosition(CellPlacement::MID_BUTTON, x));
149 Toolkit::PushButton button3 = Toolkit::PushButton::New();
150 button3.SetProperty(Toolkit::Button::Property::LABEL, "Round");
151 button3.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
152 button3.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
153 button3.ClickedSignal().Connect(this, &ImageViewController::RoundedCornerClicked);
154 button3.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
155 button3.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
156 button3.SetProperty(Dali::Actor::Property::NAME, s);
157 mTable.AddChild(button3, Toolkit::TableView::CellPosition(CellPlacement::LOWER_BUTTON, x));
159 mImageViews[x] = Toolkit::ImageView::New();
160 Property::Map imagePropertyMap;
161 imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
162 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, IMAGE_PATH[0]);
163 mImageViews[x].SetProperty(Toolkit::ImageView::Property::IMAGE, imagePropertyMap);
165 mImageViews[x].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
166 mImageViews[x].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
167 mImageViews[x].SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
168 mTable.AddChild(mImageViews[x], Toolkit::TableView::CellPosition(CellPlacement::IMAGE, x));
170 // Set changeable counter and toggle for each ImageView
171 mImageViewImageIndexStatus[x] = 0;
172 mImageViewToggleStatus[x] = true;
173 mImageViewRoundedCornerStatus[x] = false;
176 application.GetWindow().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent);
180 void ImmediateLoadImage(const char* urlToLoad)
182 Property::Map imagePropertyMap;
183 imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
184 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, urlToLoad);
185 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap);
189 bool ToggleImageOnWindow(Toolkit::Button button)
191 unsigned int buttonIndex = GetButtonIndex(button);
193 Toolkit::ImageView imageView = mImageViews[buttonIndex];
195 if(mImageViewToggleStatus[buttonIndex])
197 imageView.Unparent();
201 mTable.AddChild(imageView, Toolkit::TableView::CellPosition(CellPlacement::IMAGE, GetButtonIndex(button)));
204 mImageViewToggleStatus[buttonIndex] = !mImageViewToggleStatus[buttonIndex];
209 bool ChangeImageClicked(Toolkit::Button button)
211 unsigned int buttonIndex = GetButtonIndex(button);
213 if(mImageViews[buttonIndex].GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
215 ++mImageViewImageIndexStatus[buttonIndex];
217 if(mImageViewImageIndexStatus[buttonIndex] == NUMBER_OF_RESOURCES)
219 mImageViewImageIndexStatus[buttonIndex] = 0;
222 // Reset corner radius state value
223 mImageViewRoundedCornerStatus[buttonIndex] = false;
225 Property::Map imagePropertyMap;
226 imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
227 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, IMAGE_PATH[mImageViewImageIndexStatus[buttonIndex]]);
228 mImageViews[buttonIndex].SetProperty(Toolkit::ImageView::Property::IMAGE, imagePropertyMap);
233 bool RoundedCornerClicked(Toolkit::Button button)
235 unsigned int buttonIndex = GetButtonIndex(button);
237 if(mImageViews[buttonIndex].GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
239 mImageViewRoundedCornerStatus[buttonIndex] = !mImageViewRoundedCornerStatus[buttonIndex];
241 Property::Map imagePropertyMap;
242 imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
243 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, IMAGE_PATH[mImageViewImageIndexStatus[buttonIndex]]);
244 imagePropertyMap.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImageViewRoundedCornerStatus[buttonIndex] ? CORNER_RADIUS_VALUE : 0.0f);
246 mImageViews[buttonIndex].SetProperty(Toolkit::ImageView::Property::IMAGE, imagePropertyMap);
252 * Main key event handler
254 void OnKeyEvent(const KeyEvent& event)
256 if(event.GetState() == KeyEvent::DOWN)
258 if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
266 Application& mApplication;
268 Toolkit::Control mView; ///< The View instance.
269 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
270 Layer mContentLayer; ///< Content layer
271 Toolkit::TableView mTable;
272 Toolkit::ImageView mImageViews[NUMBER_OF_IMAGES];
273 bool mImageViewToggleStatus[NUMBER_OF_IMAGES];
274 bool mImageViewRoundedCornerStatus[NUMBER_OF_IMAGES];
275 unsigned int mImageViewImageIndexStatus[NUMBER_OF_IMAGES];
277 Toolkit::TableView::CellPosition mCurrentPositionToggle;
278 Toolkit::TableView::CellPosition mCurrentPositionImage;
281 int DALI_EXPORT_API main(int argc, char** argv)
283 Application application = Application::New(&argc, &argv, DEMO_THEME_PATH);
284 ImageViewController test(application);
285 application.MainLoop();