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