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 unsigned int NUM_IMAGES = sizeof(IMAGE_PATH) / sizeof(char*);
40 const unsigned int COLUMNS = 3;
41 const unsigned int ROWS = 4;
45 class ImageViewController: public ConnectionTracker
49 ImageViewController( Application& application )
50 : mApplication( application ),
51 mCurrentPositionToggle( 0, 0 ),
52 mCurrentPositionImage( 0, 0 ),
56 // Connect to the Application's Init signal
57 mApplication.InitSignal().Connect( this, &ImageViewController::Create );
60 ~ImageViewController()
65 void Create( Application& application )
67 // The Init signal is received once (only) during the Application lifetime
69 // Creates a default view with a default tool bar.
70 // The view is added to the stage.
71 mContentLayer = DemoHelper::CreateView( application,
79 mTable = Toolkit::TableView::New( ROWS, COLUMNS );
80 mTable.SetAnchorPoint( AnchorPoint::CENTER );
81 mTable.SetParentOrigin( ParentOrigin::CENTER );
82 mTable.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
83 Vector3 offset( -50.0f, -350.0f, 0.0f );
84 mTable.SetSizeModeFactor( offset );
86 mContentLayer.Add( mTable );
88 for( unsigned int y = 0; y < ROWS; ++y )
90 for( unsigned int x = 0; x < COLUMNS; ++x )
92 mImageViews[x][y] = Toolkit::ImageView::New( IMAGE_PATH[ 0 ] );
93 mImageViews[x][y].SetParentOrigin( ParentOrigin::CENTER );
94 mImageViews[x][y].SetAnchorPoint( AnchorPoint::CENTER );
95 mImageViews[x][y].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
97 mTable.AddChild( mImageViews[x][y], Toolkit::TableView::CellPosition( y, x ) );
101 Toolkit::TableView buttonsTable = Toolkit::TableView::New( 3, 1 );
102 buttonsTable.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
103 buttonsTable.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
104 buttonsTable.SetFitHeight( 0 );
105 buttonsTable.SetFitHeight( 1 );
106 buttonsTable.SetFitHeight( 2 );
107 buttonsTable.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
109 Toolkit::PushButton button = Toolkit::PushButton::New();
110 button.SetLabelText( "Toggle on/off stage" );
111 button.SetParentOrigin( ParentOrigin::CENTER );
112 button.SetAnchorPoint( AnchorPoint::CENTER );
113 button.ClickedSignal().Connect( this, &ImageViewController::ToggleImageOnStage );
114 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
115 buttonsTable.AddChild( button, Toolkit::TableView::CellPosition( 0, 0 ) );
117 Toolkit::PushButton button2 = Toolkit::PushButton::New();
118 button2.SetLabelText( "Change Image" );
119 button2.SetParentOrigin( ParentOrigin::CENTER );
120 button2.SetAnchorPoint( AnchorPoint::CENTER );
121 button2.ClickedSignal().Connect( this, &ImageViewController::ChangeImageClicked );
122 button2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
123 buttonsTable.AddChild( button2, Toolkit::TableView::CellPosition( 1, 0 ) );
125 mContentLayer.Add(buttonsTable);
127 Stage::GetCurrent().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent);
131 bool ToggleImageOnStage( Toolkit::Button button )
133 Toolkit::ImageView imageView = mImageViews[ mCurrentPositionToggle.columnIndex ][ mCurrentPositionToggle.rowIndex ];
137 imageView.Unparent();
141 mTable.AddChild( imageView, mCurrentPositionToggle );
144 ++mCurrentPositionToggle.columnIndex;
145 if( mCurrentPositionToggle.columnIndex == COLUMNS )
147 mCurrentPositionToggle.columnIndex = 0;
148 ++mCurrentPositionToggle.rowIndex;
150 if( mCurrentPositionToggle.rowIndex == ROWS )
152 mCurrentPositionToggle.rowIndex = 0;
153 mToggleOff = !mToggleOff;
159 bool ChangeImageClicked( Toolkit::Button button )
161 Toolkit::ImageView imageView = mImageViews[ mCurrentPositionImage.columnIndex ][ mCurrentPositionImage.rowIndex ];
163 imageView.SetImage( IMAGE_PATH[ mImageIdx ] );
165 ++mCurrentPositionImage.columnIndex;
166 if( mCurrentPositionImage.columnIndex == COLUMNS )
168 mCurrentPositionImage.columnIndex = 0;
169 ++mCurrentPositionImage.rowIndex;
171 if( mCurrentPositionImage.rowIndex == ROWS )
173 mCurrentPositionImage.rowIndex = 0;
176 if( mImageIdx == NUM_IMAGES )
186 * Main key event handler
188 void OnKeyEvent(const KeyEvent& event)
190 if(event.state == KeyEvent::Down)
192 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
200 Application& mApplication;
202 Toolkit::Control mView; ///< The View instance.
203 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
204 Layer mContentLayer; ///< Content layer
205 Toolkit::TableView mTable;
206 Toolkit::ImageView mImageViews[ COLUMNS ][ ROWS ];
208 Toolkit::TableView::CellPosition mCurrentPositionToggle;
209 Toolkit::TableView::CellPosition mCurrentPositionImage;
216 void RunTest( Application& application )
218 ImageViewController test( application );
220 application.MainLoop();
223 // Entry point for Linux & Tizen applications
225 int DALI_EXPORT_API main( int argc, char **argv )
227 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
229 RunTest( application );