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