Changes required after TextLabel Devel properties were made public
[platform/core/uifw/dali-demo.git] / examples / image-view / image-view-example.cpp
1 /*
2  * Copyright (c) 2017 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
23 using namespace Dali;
24
25 namespace
26 {
27
28 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
29 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
30 const char* APPLICATION_TITLE( "Image view" );
31
32 const char* IMAGE_PATH[] = {
33     DEMO_IMAGE_DIR "gallery-small-23.jpg",
34     DEMO_IMAGE_DIR "woodEffect.jpg",
35     DEMO_IMAGE_DIR "heartsframe.9.png",
36     DEMO_IMAGE_DIR "World.svg"
37 };
38
39 const unsigned int NUMBER_OF_IMAGES = 3;
40
41 enum CellPlacement
42 {
43    TOP_BUTTON,
44    LOWER_BUTTON,
45    IMAGE,
46    NUMBER_OF_ROWS
47 };
48
49
50 unsigned int GetButtonIndex( Toolkit::Button button )
51 {
52   std::string buttonName = button.GetName();
53   unsigned int index = 0;
54
55   if ( buttonName != "")
56   {
57     index = std::stoul( buttonName );
58   }
59
60   return index;
61 }
62
63
64 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
65
66 std::string EXAMPLE_INSTRUCTIONS = "Instructions: Change button cycles through different image visuals, "
67                            "on/off takes the ImageView and it's current visual on or off stage.";
68
69 }  // namespace
70
71 class ImageViewController: public ConnectionTracker
72 {
73  public:
74
75   ImageViewController( Application& application )
76     : mApplication( application ),
77       mCurrentPositionToggle( 0, 0 ),
78       mCurrentPositionImage( 0, 0 )
79   {
80     // Connect to the Application's Init signal
81     mApplication.InitSignal().Connect( this, &ImageViewController::Create );
82   }
83
84   ~ImageViewController()
85   {
86     // Nothing to do here
87   }
88
89   void Create( Application& application )
90   {
91     // The Init signal is received once (only) during the Application lifetime
92
93     // Creates a default view with a default tool bar.
94     // The view is added to the stage.
95     mContentLayer = DemoHelper::CreateView( application,
96                                             mView,
97                                             mToolBar,
98                                             BACKGROUND_IMAGE,
99                                             TOOLBAR_IMAGE,
100                                             APPLICATION_TITLE );
101
102
103     // Create a table view to show a pair of buttons above each image.
104     mTable = Toolkit::TableView::New( CellPlacement::NUMBER_OF_ROWS, NUMBER_OF_IMAGES );
105     mTable.SetAnchorPoint( AnchorPoint::CENTER );
106     mTable.SetParentOrigin( ParentOrigin::CENTER );
107     mTable.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
108     Vector3 offset( 0.9f, 0.70f, 0.0f );
109     mTable.SetSizeModeFactor( offset );
110     mTable.SetFitHeight(CellPlacement::TOP_BUTTON);
111     mTable.SetFitHeight(CellPlacement::LOWER_BUTTON);
112     mContentLayer.Add( mTable );
113
114     Toolkit::TextLabel instructions = Toolkit::TextLabel::New( EXAMPLE_INSTRUCTIONS );
115     instructions.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
116     instructions.SetParentOrigin(ParentOrigin::BOTTOM_CENTER);
117     instructions.SetY(-50.0f);
118     instructions.SetProperty( Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL, true  );
119     instructions.SetProperty( Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT, 10  );
120     mContentLayer.Add(instructions);
121
122     for( unsigned int x = 0; x < NUMBER_OF_IMAGES; x++ )
123     {
124       Toolkit::PushButton button = Toolkit::PushButton::New();
125       button.SetProperty( Toolkit::Button::Property::LABEL, "on/off" );
126       button.SetParentOrigin( ParentOrigin::TOP_CENTER );
127       button.SetAnchorPoint( AnchorPoint::TOP_CENTER );
128       button.ClickedSignal().Connect( this, &ImageViewController::ToggleImageOnStage );
129       button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
130       button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
131       std::string s = std::to_string(x);
132       button.SetName( s );
133       mTable.AddChild( button, Toolkit::TableView::CellPosition( CellPlacement::TOP_BUTTON, x )  );
134
135       Toolkit::PushButton button2 = Toolkit::PushButton::New();
136       button2.SetProperty( Toolkit::Button::Property::LABEL, "Change" );
137       button2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
138       button2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
139       button2.ClickedSignal().Connect( this, &ImageViewController::ChangeImageClicked );
140       button2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
141       button2.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
142       button2.SetName( s );
143       mTable.AddChild( button2, Toolkit::TableView::CellPosition( CellPlacement::LOWER_BUTTON, x )  );
144
145       mImageViews[x] = Toolkit::ImageView::New( IMAGE_PATH[ 0 ] );
146       mImageViews[x].SetParentOrigin( ParentOrigin::CENTER );
147       mImageViews[x].SetAnchorPoint( AnchorPoint::CENTER );
148       mImageViews[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
149       mTable.AddChild( mImageViews[x], Toolkit::TableView::CellPosition( CellPlacement::IMAGE, x ) );
150
151       // Set changeable counter and toggle for each ImageView
152       mImageViewImageIndexStatus[x] = true;
153       mImageViewToggleStatus[x] = true;
154     }
155
156     Stage::GetCurrent().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent);
157   }
158
159 private:
160
161   bool ToggleImageOnStage( Toolkit::Button button )
162   {
163     unsigned int buttonIndex = GetButtonIndex( button );
164
165     Toolkit::ImageView imageView =  mImageViews[ buttonIndex ];
166
167     if( mImageViewToggleStatus[ buttonIndex ] )
168     {
169       imageView.Unparent();
170     }
171     else
172     {
173       mTable.AddChild( imageView, Toolkit::TableView::CellPosition( 2, GetButtonIndex( button ) ) );
174     }
175
176     mImageViewToggleStatus[ buttonIndex ] = !mImageViewToggleStatus[ buttonIndex ];
177
178     return true;
179   }
180
181   bool ChangeImageClicked( Toolkit::Button button )
182   {
183     unsigned int buttonIndex = GetButtonIndex( button );
184
185     if (  mImageViews[buttonIndex].OnStage() )
186     {
187       mImageViews[buttonIndex].SetImage( IMAGE_PATH[ mImageViewImageIndexStatus[buttonIndex] ] );
188
189       ++mImageViewImageIndexStatus[buttonIndex];
190
191       if( mImageViewImageIndexStatus[buttonIndex] == NUMBER_OF_RESOURCES )
192       {
193         mImageViewImageIndexStatus[buttonIndex] = 0;
194       }
195     }
196     return true;
197   }
198
199   /**
200    * Main key event handler
201    */
202   void OnKeyEvent(const KeyEvent& event)
203   {
204     if(event.state == KeyEvent::Down)
205     {
206       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
207       {
208         mApplication.Quit();
209       }
210     }
211   }
212
213 private:
214   Application&  mApplication;
215
216   Toolkit::Control           mView;                              ///< The View instance.
217   Toolkit::ToolBar           mToolBar;                           ///< The View's Toolbar.
218   Layer                      mContentLayer;                      ///< Content layer
219   Toolkit::TableView         mTable;
220   Toolkit::ImageView         mImageViews[ NUMBER_OF_IMAGES ];
221   bool                       mImageViewToggleStatus[ NUMBER_OF_IMAGES ];
222   unsigned int               mImageViewImageIndexStatus[ NUMBER_OF_IMAGES ];
223
224   Toolkit::TableView::CellPosition mCurrentPositionToggle;
225   Toolkit::TableView::CellPosition mCurrentPositionImage;
226
227 };
228
229 void RunTest( Application& application )
230 {
231   ImageViewController test( application );
232
233   application.MainLoop();
234 }
235
236 // Entry point for Linux & Tizen applications
237 //
238 int DALI_EXPORT_API main( int argc, char **argv )
239 {
240   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
241
242   RunTest( application );
243
244   return 0;
245 }