Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / image-view / image-view-example.cpp
1 /*
2  * Copyright (c) 2020 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/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>
24 #include <string>
25 #include "shared/view.h"
26
27 using namespace Dali;
28
29 namespace
30 {
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");
34
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"};
41
42 const unsigned int NUMBER_OF_IMAGES = 3;
43
44 enum CellPlacement
45 {
46   TOP_BUTTON,
47   MID_BUTTON,
48   LOWER_BUTTON,
49   IMAGE,
50   NUMBER_OF_ROWS
51 };
52
53 unsigned int GetButtonIndex(Toolkit::Button button)
54 {
55   std::string  buttonName = button.GetProperty<std::string>(Dali::Actor::Property::NAME);
56   unsigned int index      = 0;
57
58   if(buttonName != "")
59   {
60     index = std::stoul(buttonName);
61   }
62
63   return index;
64 }
65
66 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
67
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.";
71
72 const float CORNER_RADIUS_VALUE(20.0f);
73
74 } // namespace
75
76 class ImageViewController : public ConnectionTracker
77 {
78 public:
79   ImageViewController(Application& application)
80   : mApplication(application),
81     mCurrentPositionToggle(0, 0),
82     mCurrentPositionImage(0, 0)
83   {
84     // Connect to the Application's Init signal
85     mApplication.InitSignal().Connect(this, &ImageViewController::Create);
86   }
87
88   ~ImageViewController()
89   {
90     // Nothing to do here
91   }
92
93   void Create(Application& application)
94   {
95     // The Init signal is received once (only) during the Application lifetime
96
97     // Creates a default view with a default tool bar.
98     // The view is added to the window.
99     mContentLayer = DemoHelper::CreateView(application,
100                                            mView,
101                                            mToolBar,
102                                            BACKGROUND_IMAGE,
103                                            TOOLBAR_IMAGE,
104                                            APPLICATION_TITLE);
105
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);
117
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);
125
126     for(unsigned int x = 0; x < NUMBER_OF_IMAGES; x++)
127     {
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));
138
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));
148
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));
158
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);
164
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));
169
170       // Set changeable counter and toggle for each ImageView
171       mImageViewImageIndexStatus[x]    = 0;
172       mImageViewToggleStatus[x]        = true;
173       mImageViewRoundedCornerStatus[x] = false;
174     }
175
176     application.GetWindow().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent);
177   }
178
179 private:
180   void ImmediateLoadImage(const char* urlToLoad)
181   {
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);
186     visual.Reset();
187   }
188
189   bool ToggleImageOnWindow(Toolkit::Button button)
190   {
191     unsigned int buttonIndex = GetButtonIndex(button);
192
193     Toolkit::ImageView imageView = mImageViews[buttonIndex];
194
195     if(mImageViewToggleStatus[buttonIndex])
196     {
197       imageView.Unparent();
198     }
199     else
200     {
201       mTable.AddChild(imageView, Toolkit::TableView::CellPosition(CellPlacement::IMAGE, GetButtonIndex(button)));
202     }
203
204     mImageViewToggleStatus[buttonIndex] = !mImageViewToggleStatus[buttonIndex];
205
206     return true;
207   }
208
209   bool ChangeImageClicked(Toolkit::Button button)
210   {
211     unsigned int buttonIndex = GetButtonIndex(button);
212
213     if(mImageViews[buttonIndex].GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
214     {
215       ++mImageViewImageIndexStatus[buttonIndex];
216
217       if(mImageViewImageIndexStatus[buttonIndex] == NUMBER_OF_RESOURCES)
218       {
219         mImageViewImageIndexStatus[buttonIndex] = 0;
220       }
221
222       // Reset corner radius state value
223       mImageViewRoundedCornerStatus[buttonIndex] = false;
224
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);
229     }
230     return true;
231   }
232
233   bool RoundedCornerClicked(Toolkit::Button button)
234   {
235     unsigned int buttonIndex = GetButtonIndex(button);
236
237     if(mImageViews[buttonIndex].GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
238     {
239       mImageViewRoundedCornerStatus[buttonIndex] = !mImageViewRoundedCornerStatus[buttonIndex];
240
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);
245
246       mImageViews[buttonIndex].SetProperty(Toolkit::ImageView::Property::IMAGE, imagePropertyMap);
247     }
248     return true;
249   }
250
251   /**
252    * Main key event handler
253    */
254   void OnKeyEvent(const KeyEvent& event)
255   {
256     if(event.GetState() == KeyEvent::DOWN)
257     {
258       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
259       {
260         mApplication.Quit();
261       }
262     }
263   }
264
265 private:
266   Application& mApplication;
267
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];
276
277   Toolkit::TableView::CellPosition mCurrentPositionToggle;
278   Toolkit::TableView::CellPosition mCurrentPositionImage;
279 };
280
281 int DALI_EXPORT_API main(int argc, char** argv)
282 {
283   Application         application = Application::New(&argc, &argv, DEMO_THEME_PATH);
284   ImageViewController test(application);
285   application.MainLoop();
286   return 0;
287 }