2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "shared/view.h"
19 #include <dali/dali.h>
20 #include <dali-toolkit/dali-toolkit.h>
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" );
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",
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",
45 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATH) / sizeof(char*);
46 const unsigned int NUM_RESOURCE_IMAGES = sizeof(RESOURCE_IMAGE_PATH) / sizeof(char*);
48 const unsigned int COLUMNS = 3;
49 const unsigned int ROWS = 4;
53 class ImageViewController: public ConnectionTracker
57 ImageViewController( Application& application )
58 : mApplication( application ),
59 mCurrentPositionToggle( 0, 0 ),
60 mCurrentPositionImage( 0, 0 ),
62 mUseResource( false ),
65 // Connect to the Application's Init signal
66 mApplication.InitSignal().Connect( this, &ImageViewController::Create );
69 ~ImageViewController()
74 void Create( Application& application )
76 // The Init signal is received once (only) during the Application lifetime
78 // Creates a default view with a default tool bar.
79 // The view is added to the stage.
80 mContentLayer = DemoHelper::CreateView( application,
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 );
95 mContentLayer.Add( mTable );
97 for( unsigned int y = 0; y < ROWS; ++y )
99 for( unsigned int x = 0; x < COLUMNS; ++x )
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 );
106 mTable.AddChild( mImageViews[x][y], Toolkit::TableView::CellPosition( y, x ) );
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 );
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 ) );
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 ) );
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 ) );
142 mContentLayer.Add(buttonsTable);
144 Stage::GetCurrent().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent);
148 bool ToggleImageOnStage( Toolkit::Button button )
150 Toolkit::ImageView imageView = mImageViews[ mCurrentPositionToggle.columnIndex ][ mCurrentPositionToggle.rowIndex ];
154 imageView.Unparent();
158 mTable.AddChild( imageView, mCurrentPositionToggle );
161 ++mCurrentPositionToggle.columnIndex;
162 if( mCurrentPositionToggle.columnIndex == COLUMNS )
164 mCurrentPositionToggle.columnIndex = 0;
165 ++mCurrentPositionToggle.rowIndex;
167 if( mCurrentPositionToggle.rowIndex == ROWS )
169 mCurrentPositionToggle.rowIndex = 0;
170 mToggleOff = !mToggleOff;
176 bool ChangeImageClicked( Toolkit::Button button )
178 Toolkit::ImageView imageView = mImageViews[ mCurrentPositionImage.columnIndex ][ mCurrentPositionImage.rowIndex ];
182 ResourceImage image = ResourceImage::New( RESOURCE_IMAGE_PATH[ mImageIdx ] );
183 imageView.SetImage( image );
187 imageView.SetImage( IMAGE_PATH[ mImageIdx ] );
190 ++mCurrentPositionImage.columnIndex;
191 if( mCurrentPositionImage.columnIndex == COLUMNS )
193 mCurrentPositionImage.columnIndex = 0;
194 ++mCurrentPositionImage.rowIndex;
196 if( mCurrentPositionImage.rowIndex == ROWS )
198 mCurrentPositionImage.rowIndex = 0;
201 int numImages = mUseResource ? NUM_RESOURCE_IMAGES : NUM_IMAGES;
202 if( mImageIdx == numImages )
211 bool UseResourceImagesClicked( Toolkit::Button button )
213 mUseResource = !mUseResource;
215 int numImages = mUseResource ? NUM_RESOURCE_IMAGES : NUM_IMAGES;
216 if( mImageIdx >= numImages )
225 * Main key event handler
227 void OnKeyEvent(const KeyEvent& event)
229 if(event.state == KeyEvent::Down)
231 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
239 Application& mApplication;
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 ];
247 Toolkit::TableView::CellPosition mCurrentPositionToggle;
248 Toolkit::TableView::CellPosition mCurrentPositionImage;
256 void RunTest( Application& application )
258 ImageViewController test( application );
260 application.MainLoop();
263 // Entry point for Linux & Tizen applications
265 int DALI_EXPORT_API main( int argc, char **argv )
267 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
269 RunTest( application );