2 * Copyright (c) 2017 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.
19 #include "shared/view.h"
20 #include <dali/dali.h>
21 #include <dali-toolkit/dali-toolkit.h>
28 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
29 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
30 const char* APPLICATION_TITLE( "Image view" );
32 const char* IMAGE_PATH[] = {
33 DEMO_IMAGE_DIR "gallery-small-23.jpg",
34 DEMO_IMAGE_DIR "woodEffect.jpg",
35 DEMO_IMAGE_DIR "heartsframe.9.png",
36 DEMO_IMAGE_DIR "World.svg"
39 const unsigned int NUMBER_OF_IMAGES = 3;
50 unsigned int GetButtonIndex( Toolkit::Button button )
52 std::string buttonName = button.GetName();
53 unsigned int index = 0;
55 if ( buttonName != "")
57 index = std::stoul( buttonName );
64 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
66 std::string EXAMPLE_INSTRUCTIONS = "Instructions: Change button cycles through different image visuals, "
67 "on/off takes the ImageView and it's current visual on or off stage.";
71 class ImageViewController: public ConnectionTracker
75 ImageViewController( Application& application )
76 : mApplication( application ),
77 mCurrentPositionToggle( 0, 0 ),
78 mCurrentPositionImage( 0, 0 )
80 // Connect to the Application's Init signal
81 mApplication.InitSignal().Connect( this, &ImageViewController::Create );
84 ~ImageViewController()
89 void Create( Application& application )
91 // The Init signal is received once (only) during the Application lifetime
93 // Creates a default view with a default tool bar.
94 // The view is added to the stage.
95 mContentLayer = DemoHelper::CreateView( application,
103 // Create a table view to show a pair of buttons above each image.
104 mTable = Toolkit::TableView::New( CellPlacement::NUMBER_OF_ROWS, NUMBER_OF_IMAGES );
105 mTable.SetAnchorPoint( AnchorPoint::CENTER );
106 mTable.SetParentOrigin( ParentOrigin::CENTER );
107 mTable.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
108 Vector3 offset( 0.9f, 0.70f, 0.0f );
109 mTable.SetSizeModeFactor( offset );
110 mTable.SetFitHeight(CellPlacement::TOP_BUTTON);
111 mTable.SetFitHeight(CellPlacement::LOWER_BUTTON);
112 mContentLayer.Add( mTable );
114 Toolkit::TextLabel instructions = Toolkit::TextLabel::New( EXAMPLE_INSTRUCTIONS );
115 instructions.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
116 instructions.SetParentOrigin(ParentOrigin::BOTTOM_CENTER);
117 instructions.SetY(-50.0f);
118 instructions.SetProperty( Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL, true );
119 instructions.SetProperty( Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT, 10 );
120 mContentLayer.Add(instructions);
122 for( unsigned int x = 0; x < NUMBER_OF_IMAGES; x++ )
124 Toolkit::PushButton button = Toolkit::PushButton::New();
125 button.SetProperty( Toolkit::Button::Property::LABEL, "on/off" );
126 button.SetParentOrigin( ParentOrigin::TOP_CENTER );
127 button.SetAnchorPoint( AnchorPoint::TOP_CENTER );
128 button.ClickedSignal().Connect( this, &ImageViewController::ToggleImageOnStage );
129 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
130 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
131 std::string s = std::to_string(x);
133 mTable.AddChild( button, Toolkit::TableView::CellPosition( CellPlacement::TOP_BUTTON, x ) );
135 Toolkit::PushButton button2 = Toolkit::PushButton::New();
136 button2.SetProperty( Toolkit::Button::Property::LABEL, "Change" );
137 button2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
138 button2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
139 button2.ClickedSignal().Connect( this, &ImageViewController::ChangeImageClicked );
140 button2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
141 button2.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
142 button2.SetName( s );
143 mTable.AddChild( button2, Toolkit::TableView::CellPosition( CellPlacement::LOWER_BUTTON, x ) );
145 mImageViews[x] = Toolkit::ImageView::New( IMAGE_PATH[ 0 ] );
146 mImageViews[x].SetParentOrigin( ParentOrigin::CENTER );
147 mImageViews[x].SetAnchorPoint( AnchorPoint::CENTER );
148 mImageViews[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
149 mTable.AddChild( mImageViews[x], Toolkit::TableView::CellPosition( CellPlacement::IMAGE, x ) );
151 // Set changeable counter and toggle for each ImageView
152 mImageViewImageIndexStatus[x] = true;
153 mImageViewToggleStatus[x] = true;
156 Stage::GetCurrent().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent);
161 bool ToggleImageOnStage( Toolkit::Button button )
163 unsigned int buttonIndex = GetButtonIndex( button );
165 Toolkit::ImageView imageView = mImageViews[ buttonIndex ];
167 if( mImageViewToggleStatus[ buttonIndex ] )
169 imageView.Unparent();
173 mTable.AddChild( imageView, Toolkit::TableView::CellPosition( 2, GetButtonIndex( button ) ) );
176 mImageViewToggleStatus[ buttonIndex ] = !mImageViewToggleStatus[ buttonIndex ];
181 bool ChangeImageClicked( Toolkit::Button button )
183 unsigned int buttonIndex = GetButtonIndex( button );
185 if ( mImageViews[buttonIndex].OnStage() )
187 mImageViews[buttonIndex].SetImage( IMAGE_PATH[ mImageViewImageIndexStatus[buttonIndex] ] );
189 ++mImageViewImageIndexStatus[buttonIndex];
191 if( mImageViewImageIndexStatus[buttonIndex] == NUMBER_OF_RESOURCES )
193 mImageViewImageIndexStatus[buttonIndex] = 0;
200 * Main key event handler
202 void OnKeyEvent(const KeyEvent& event)
204 if(event.state == KeyEvent::Down)
206 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
214 Application& mApplication;
216 Toolkit::Control mView; ///< The View instance.
217 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
218 Layer mContentLayer; ///< Content layer
219 Toolkit::TableView mTable;
220 Toolkit::ImageView mImageViews[ NUMBER_OF_IMAGES ];
221 bool mImageViewToggleStatus[ NUMBER_OF_IMAGES ];
222 unsigned int mImageViewImageIndexStatus[ NUMBER_OF_IMAGES ];
224 Toolkit::TableView::CellPosition mCurrentPositionToggle;
225 Toolkit::TableView::CellPosition mCurrentPositionImage;
229 void RunTest( Application& application )
231 ImageViewController test( application );
233 application.MainLoop();
236 // Entry point for Linux & Tizen applications
238 int DALI_EXPORT_API main( int argc, char **argv )
240 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
242 RunTest( application );