Revert "Revert "Renamed KeyEvent enum values to comply with coding standards.""
[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 <string>
19 #include "shared/view.h"
20 #include <dali/dali.h>
21 #include <dali-toolkit/dali-toolkit.h>
22 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
23 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
24 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
25 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
26
27 using namespace Dali;
28
29 namespace
30 {
31
32 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
33 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
34 const char* APPLICATION_TITLE( "Image view" );
35
36 const char* IMAGE_PATH[] = {
37     DEMO_IMAGE_DIR "gallery-small-23.jpg",
38     DEMO_IMAGE_DIR "woodEffect.jpg",
39     DEMO_IMAGE_DIR "wood.png",       // 32bits image
40     DEMO_IMAGE_DIR "heartsframe.9.png",
41     DEMO_IMAGE_DIR "World.svg"
42 };
43
44 const unsigned int NUMBER_OF_IMAGES = 3;
45
46 enum CellPlacement
47 {
48    TOP_BUTTON,
49    MID_BUTTON,
50    LOWER_BUTTON,
51    IMAGE,
52    NUMBER_OF_ROWS
53 };
54
55
56 unsigned int GetButtonIndex( Toolkit::Button button )
57 {
58   std::string buttonName = button.GetProperty< std::string >( Dali::Actor::Property::NAME );
59   unsigned int index = 0;
60
61   if ( buttonName != "")
62   {
63     index = std::stoul( buttonName );
64   }
65
66   return index;
67 }
68
69
70 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
71
72 std::string EXAMPLE_INSTRUCTIONS = "Instructions: Change button cycles through different image visuals, "
73                            "on/off takes the ImageView and it's current visual on or off window.";
74
75 const float CORNER_RADIUS_VALUE( 20.0f );
76
77 }  // namespace
78
79 class ImageViewController: public ConnectionTracker
80 {
81  public:
82
83   ImageViewController( Application& application )
84     : mApplication( application ),
85       mCurrentPositionToggle( 0, 0 ),
86       mCurrentPositionImage( 0, 0 )
87   {
88     // Connect to the Application's Init signal
89     mApplication.InitSignal().Connect( this, &ImageViewController::Create );
90   }
91
92   ~ImageViewController()
93   {
94     // Nothing to do here
95   }
96
97   void Create( Application& application )
98   {
99     // The Init signal is received once (only) during the Application lifetime
100
101     // Creates a default view with a default tool bar.
102     // The view is added to the window.
103     mContentLayer = DemoHelper::CreateView( application,
104                                             mView,
105                                             mToolBar,
106                                             BACKGROUND_IMAGE,
107                                             TOOLBAR_IMAGE,
108                                             APPLICATION_TITLE );
109
110
111     // Create a table view to show a pair of buttons above each image.
112     mTable = Toolkit::TableView::New( CellPlacement::NUMBER_OF_ROWS, NUMBER_OF_IMAGES );
113     mTable.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
114     mTable.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
115     mTable.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
116     Vector3 offset( 0.9f, 0.70f, 0.0f );
117     mTable.SetProperty( Actor::Property::SIZE_MODE_FACTOR, offset );
118     mTable.SetFitHeight(CellPlacement::TOP_BUTTON);
119     mTable.SetFitHeight(CellPlacement::MID_BUTTON);
120     mTable.SetFitHeight(CellPlacement::LOWER_BUTTON);
121     mContentLayer.Add( mTable );
122
123     Toolkit::TextLabel instructions = Toolkit::TextLabel::New( EXAMPLE_INSTRUCTIONS );
124     instructions.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
125     instructions.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::BOTTOM_CENTER);
126     instructions.SetProperty( Actor::Property::POSITION_Y, -50.0f);
127     instructions.SetProperty( Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL, true  );
128     instructions.SetProperty( Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT, 10  );
129     mContentLayer.Add(instructions);
130
131     for( unsigned int x = 0; x < NUMBER_OF_IMAGES; x++ )
132     {
133       Toolkit::PushButton button = Toolkit::PushButton::New();
134       button.SetProperty( Toolkit::Button::Property::LABEL, "on/off" );
135       button.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
136       button.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
137       button.ClickedSignal().Connect( this, &ImageViewController::ToggleImageOnWindow );
138       button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
139       button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
140       std::string s = std::to_string(x);
141       button.SetProperty( Dali::Actor::Property::NAME, s );
142       mTable.AddChild( button, Toolkit::TableView::CellPosition( CellPlacement::TOP_BUTTON, x )  );
143
144       Toolkit::PushButton button2 = Toolkit::PushButton::New();
145       button2.SetProperty( Toolkit::Button::Property::LABEL, "Change" );
146       button2.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
147       button2.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
148       button2.ClickedSignal().Connect( this, &ImageViewController::ChangeImageClicked );
149       button2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
150       button2.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
151       button2.SetProperty( Dali::Actor::Property::NAME, s );
152       mTable.AddChild( button2, Toolkit::TableView::CellPosition( CellPlacement::MID_BUTTON, x )  );
153
154       Toolkit::PushButton button3 = Toolkit::PushButton::New();
155       button3.SetProperty( Toolkit::Button::Property::LABEL, "Round" );
156       button3.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
157       button3.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
158       button3.ClickedSignal().Connect( this, &ImageViewController::RoundedCornerClicked );
159       button3.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
160       button3.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
161       button3.SetProperty( Dali::Actor::Property::NAME, s );
162       mTable.AddChild( button3, Toolkit::TableView::CellPosition( CellPlacement::LOWER_BUTTON, x )  );
163
164       mImageViews[x] = Toolkit::ImageView::New( );
165       Property::Map imagePropertyMap;
166       imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE,  Toolkit::Visual::IMAGE );
167       imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL,  IMAGE_PATH[ 0 ]  );
168       mImageViews[x].SetProperty(Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
169
170
171       mImageViews[x].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
172       mImageViews[x].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
173       mImageViews[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
174       mTable.AddChild( mImageViews[x], Toolkit::TableView::CellPosition( CellPlacement::IMAGE, x ) );
175
176       // Set changeable counter and toggle for each ImageView
177       mImageViewImageIndexStatus[x] = 0;
178       mImageViewToggleStatus[x] = true;
179       mImageViewRoundedCornerStatus[x] = false;
180     }
181
182     application.GetWindow().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent);
183   }
184
185 private:
186
187   void ImmediateLoadImage( const char* urlToLoad )
188   {
189     Property::Map imagePropertyMap;
190     imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE,  Toolkit::Visual::IMAGE );
191     imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL, urlToLoad );
192     Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( imagePropertyMap );
193     visual.Reset();
194   }
195
196   bool ToggleImageOnWindow( Toolkit::Button button )
197   {
198     unsigned int buttonIndex = GetButtonIndex( button );
199
200     Toolkit::ImageView imageView =  mImageViews[ buttonIndex ];
201
202     if( mImageViewToggleStatus[ buttonIndex ] )
203     {
204       imageView.Unparent();
205     }
206     else
207     {
208       mTable.AddChild( imageView, Toolkit::TableView::CellPosition( CellPlacement::IMAGE, GetButtonIndex( button ) ) );
209     }
210
211     mImageViewToggleStatus[ buttonIndex ] = !mImageViewToggleStatus[ buttonIndex ];
212
213     return true;
214   }
215
216   bool ChangeImageClicked( Toolkit::Button button )
217   {
218     unsigned int buttonIndex = GetButtonIndex( button );
219
220     if( mImageViews[buttonIndex].GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
221     {
222       ++mImageViewImageIndexStatus[buttonIndex];
223
224       if( mImageViewImageIndexStatus[buttonIndex] == NUMBER_OF_RESOURCES )
225       {
226         mImageViewImageIndexStatus[buttonIndex] = 0;
227       }
228
229       // Reset corner radius state value
230       mImageViewRoundedCornerStatus[buttonIndex] = false;
231
232       Property::Map imagePropertyMap;
233       imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE,  Toolkit::Visual::IMAGE );
234       imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL,  IMAGE_PATH[ mImageViewImageIndexStatus[buttonIndex] ]  );
235       mImageViews[buttonIndex].SetProperty(Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
236     }
237     return true;
238   }
239
240   bool RoundedCornerClicked( Toolkit::Button button )
241   {
242     unsigned int buttonIndex = GetButtonIndex( button );
243
244     if( mImageViews[buttonIndex].GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
245     {
246       mImageViewRoundedCornerStatus[ buttonIndex ] = !mImageViewRoundedCornerStatus[ buttonIndex ];
247
248       Property::Map imagePropertyMap;
249       imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
250       imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL, IMAGE_PATH[ mImageViewImageIndexStatus[buttonIndex] ]  );
251       imagePropertyMap.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImageViewRoundedCornerStatus[buttonIndex] ? CORNER_RADIUS_VALUE : 0.0f );
252
253       mImageViews[buttonIndex].SetProperty( Toolkit::ImageView::Property::IMAGE, imagePropertyMap );
254     }
255     return true;
256   }
257
258   /**
259    * Main key event handler
260    */
261   void OnKeyEvent(const KeyEvent& event)
262   {
263     if(event.GetState() == KeyEvent::DOWN)
264     {
265       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
266       {
267         mApplication.Quit();
268       }
269     }
270   }
271
272 private:
273   Application&  mApplication;
274
275   Toolkit::Control           mView;                              ///< The View instance.
276   Toolkit::ToolBar           mToolBar;                           ///< The View's Toolbar.
277   Layer                      mContentLayer;                      ///< Content layer
278   Toolkit::TableView         mTable;
279   Toolkit::ImageView         mImageViews[ NUMBER_OF_IMAGES ];
280   bool                       mImageViewToggleStatus[ NUMBER_OF_IMAGES ];
281   bool                       mImageViewRoundedCornerStatus[ NUMBER_OF_IMAGES ];
282   unsigned int               mImageViewImageIndexStatus[ NUMBER_OF_IMAGES ];
283
284   Toolkit::TableView::CellPosition mCurrentPositionToggle;
285   Toolkit::TableView::CellPosition mCurrentPositionImage;
286
287 };
288
289 int DALI_EXPORT_API main( int argc, char **argv )
290 {
291   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
292   ImageViewController test( application );
293   application.MainLoop();
294   return 0;
295 }