Updated demo following Slider move to the public API
[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 "shared/view.h"
19 #include <dali/dali.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 using namespace Dali;
23
24 namespace
25 {
26
27 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
28 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
29 const char* APPLICATION_TITLE( "Image view" );
30
31 const char* IMAGE_PATH[] = {
32     DEMO_IMAGE_DIR "blocks-ball.png",
33     DEMO_IMAGE_DIR "gallery-small-23.jpg",
34     DEMO_IMAGE_DIR "selection-popup-bg.2.9.png",
35     DEMO_IMAGE_DIR "heartsframe.9.png",
36 };
37
38 const char* RESOURCE_IMAGE_PATH[] = {
39     DEMO_IMAGE_DIR "contacts-image.png",
40     DEMO_IMAGE_DIR "gallery-small-27.jpg",
41     DEMO_IMAGE_DIR "selection-popup-bg.8.9.png",
42     DEMO_IMAGE_DIR "heartsframe.9.png",
43 };
44
45 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATH) / sizeof(char*);
46 const unsigned int NUM_RESOURCE_IMAGES = sizeof(RESOURCE_IMAGE_PATH) / sizeof(char*);
47
48 const unsigned int COLUMNS = 3;
49 const unsigned int ROWS = 4;
50
51 }  // namespace
52
53 class ImageViewController: public ConnectionTracker
54 {
55  public:
56
57   ImageViewController( Application& application )
58     : mApplication( application ),
59       mCurrentPositionToggle( 0, 0 ),
60       mCurrentPositionImage( 0, 0 ),
61       mToggleOff( true ),
62       mUseResource( false ),
63       mImageIdx( 1 )
64   {
65     // Connect to the Application's Init signal
66     mApplication.InitSignal().Connect( this, &ImageViewController::Create );
67   }
68
69   ~ImageViewController()
70   {
71     // Nothing to do here
72   }
73
74   void Create( Application& application )
75   {
76     // The Init signal is received once (only) during the Application lifetime
77
78     // Creates a default view with a default tool bar.
79     // The view is added to the stage.
80     mContentLayer = DemoHelper::CreateView( application,
81                                             mView,
82                                             mToolBar,
83                                             BACKGROUND_IMAGE,
84                                             TOOLBAR_IMAGE,
85                                             APPLICATION_TITLE );
86
87
88     mTable = Toolkit::TableView::New( ROWS, COLUMNS );
89     mTable.SetAnchorPoint( AnchorPoint::CENTER );
90     mTable.SetParentOrigin( ParentOrigin::CENTER );
91     mTable.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
92     Vector3 offset( -50.0f, -350.0f, 0.0f );
93     mTable.SetSizeModeFactor( offset );
94
95     mContentLayer.Add( mTable );
96
97     for( unsigned int y = 0; y < ROWS; ++y )
98     {
99       for( unsigned int x = 0; x < COLUMNS; ++x )
100       {
101         mImageViews[x][y] = Toolkit::ImageView::New( IMAGE_PATH[ 0 ] );
102         mImageViews[x][y].SetParentOrigin( ParentOrigin::CENTER );
103         mImageViews[x][y].SetAnchorPoint( AnchorPoint::CENTER );
104         mImageViews[x][y].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
105
106         mTable.AddChild( mImageViews[x][y], Toolkit::TableView::CellPosition( y, x ) );
107       }
108     }
109
110     Toolkit::TableView buttonsTable = Toolkit::TableView::New( 3, 1 );
111     buttonsTable.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
112     buttonsTable.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
113     buttonsTable.SetFitHeight( 0 );
114     buttonsTable.SetFitHeight( 1 );
115     buttonsTable.SetFitHeight( 2 );
116     buttonsTable.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
117
118     Toolkit::PushButton button = Toolkit::PushButton::New();
119     button.SetLabelText( "Toggle on/off stage" );
120     button.SetParentOrigin( ParentOrigin::CENTER );
121     button.SetAnchorPoint( AnchorPoint::CENTER );
122     button.ClickedSignal().Connect( this, &ImageViewController::ToggleImageOnStage );
123     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
124     buttonsTable.AddChild( button, Toolkit::TableView::CellPosition( 0, 0 ) );
125
126     Toolkit::PushButton button2 = Toolkit::PushButton::New();
127     button2.SetLabelText( "Change Image" );
128     button2.SetParentOrigin( ParentOrigin::CENTER );
129     button2.SetAnchorPoint( AnchorPoint::CENTER );
130     button2.ClickedSignal().Connect( this, &ImageViewController::ChangeImageClicked );
131     button2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
132     buttonsTable.AddChild( button2, Toolkit::TableView::CellPosition( 1, 0 ) );
133
134     Toolkit::CheckBoxButton button3 = Toolkit::CheckBoxButton::New();
135     button3.SetLabelText( "Use Resource Images" );
136     button3.SetParentOrigin( ParentOrigin::CENTER );
137     button3.SetAnchorPoint( AnchorPoint::CENTER );
138     button3.ClickedSignal().Connect( this, &ImageViewController::UseResourceImagesClicked );
139     button3.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
140     buttonsTable.AddChild( button3, Toolkit::TableView::CellPosition( 2, 0 ) );
141
142     mContentLayer.Add(buttonsTable);
143
144     Stage::GetCurrent().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent);
145   }
146
147 private:
148   bool ToggleImageOnStage( Toolkit::Button button )
149   {
150     Toolkit::ImageView imageView =  mImageViews[ mCurrentPositionToggle.columnIndex ][ mCurrentPositionToggle.rowIndex ];
151
152     if( mToggleOff )
153     {
154       imageView.Unparent();
155     }
156     else
157     {
158       mTable.AddChild( imageView, mCurrentPositionToggle );
159     }
160
161     ++mCurrentPositionToggle.columnIndex;
162     if( mCurrentPositionToggle.columnIndex == COLUMNS )
163     {
164       mCurrentPositionToggle.columnIndex = 0;
165       ++mCurrentPositionToggle.rowIndex;
166     }
167     if( mCurrentPositionToggle.rowIndex == ROWS )
168     {
169       mCurrentPositionToggle.rowIndex = 0;
170       mToggleOff = !mToggleOff;
171     }
172
173     return true;
174   }
175
176   bool ChangeImageClicked( Toolkit::Button button )
177   {
178     Toolkit::ImageView imageView =  mImageViews[ mCurrentPositionImage.columnIndex ][ mCurrentPositionImage.rowIndex ];
179
180     if( mUseResource )
181     {
182       ResourceImage image = ResourceImage::New( RESOURCE_IMAGE_PATH[ mImageIdx ] );
183       imageView.SetImage( image );
184     }
185     else
186     {
187       imageView.SetImage( IMAGE_PATH[ mImageIdx ] );
188     }
189
190     ++mCurrentPositionImage.columnIndex;
191     if( mCurrentPositionImage.columnIndex == COLUMNS )
192     {
193       mCurrentPositionImage.columnIndex = 0;
194       ++mCurrentPositionImage.rowIndex;
195     }
196     if( mCurrentPositionImage.rowIndex == ROWS )
197     {
198       mCurrentPositionImage.rowIndex = 0;
199       ++mImageIdx;
200
201       int numImages = mUseResource ? NUM_RESOURCE_IMAGES : NUM_IMAGES;
202       if( mImageIdx == numImages )
203       {
204         mImageIdx = 0;
205       }
206     }
207
208     return true;
209   }
210
211   bool UseResourceImagesClicked( Toolkit::Button button )
212   {
213     mUseResource = !mUseResource;
214
215     int numImages = mUseResource ? NUM_RESOURCE_IMAGES : NUM_IMAGES;
216     if( mImageIdx >= numImages )
217     {
218       mImageIdx = 0;
219     }
220
221     return true;
222   }
223
224   /**
225    * Main key event handler
226    */
227   void OnKeyEvent(const KeyEvent& event)
228   {
229     if(event.state == KeyEvent::Down)
230     {
231       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
232       {
233         mApplication.Quit();
234       }
235     }
236   }
237
238 private:
239   Application&  mApplication;
240
241   Toolkit::Control           mView;                              ///< The View instance.
242   Toolkit::ToolBar           mToolBar;                           ///< The View's Toolbar.
243   Layer                      mContentLayer;                      ///< Content layer
244   Toolkit::TableView         mTable;
245   Toolkit::ImageView        mImageViews[ COLUMNS ][ ROWS ];
246
247   Toolkit::TableView::CellPosition mCurrentPositionToggle;
248   Toolkit::TableView::CellPosition mCurrentPositionImage;
249
250   bool mToggleOff;
251   bool mUseResource;
252   int mImageIdx;
253
254 };
255
256 void RunTest( Application& application )
257 {
258   ImageViewController test( application );
259
260   application.MainLoop();
261 }
262
263 // Entry point for Linux & Tizen applications
264 //
265 int DALI_EXPORT_API main( int argc, char **argv )
266 {
267   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
268
269   RunTest( application );
270
271   return 0;
272 }