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