2 * Copyright (c) 2020 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>
22 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
23 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
24 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
25 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
32 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
33 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
34 const char* APPLICATION_TITLE( "Image view" );
36 const char* IMAGE_PATH[] = {
37 DEMO_IMAGE_DIR "gallery-small-23.jpg",
38 DEMO_IMAGE_DIR "woodEffect.jpg",
39 DEMO_IMAGE_DIR "wood.png", // 32bits image
40 DEMO_IMAGE_DIR "heartsframe.9.png",
41 DEMO_IMAGE_DIR "World.svg"
44 const unsigned int NUMBER_OF_IMAGES = 3;
56 unsigned int GetButtonIndex( Toolkit::Button button )
58 std::string buttonName = button.GetProperty< std::string >( Dali::Actor::Property::NAME );
59 unsigned int index = 0;
61 if ( buttonName != "")
63 index = std::stoul( buttonName );
70 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
72 std::string EXAMPLE_INSTRUCTIONS = "Instructions: Change button cycles through different image visuals, "
73 "on/off takes the ImageView and it's current visual on or off window.";
75 const float CORNER_RADIUS_VALUE( 20.0f );
79 class ImageViewController: public ConnectionTracker
83 ImageViewController( Application& application )
84 : mApplication( application ),
85 mCurrentPositionToggle( 0, 0 ),
86 mCurrentPositionImage( 0, 0 )
88 // Connect to the Application's Init signal
89 mApplication.InitSignal().Connect( this, &ImageViewController::Create );
92 ~ImageViewController()
97 void Create( Application& application )
99 // The Init signal is received once (only) during the Application lifetime
101 // Creates a default view with a default tool bar.
102 // The view is added to the window.
103 mContentLayer = DemoHelper::CreateView( application,
111 // Create a table view to show a pair of buttons above each image.
112 mTable = Toolkit::TableView::New( CellPlacement::NUMBER_OF_ROWS, NUMBER_OF_IMAGES );
113 mTable.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
114 mTable.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
115 mTable.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
116 Vector3 offset( 0.9f, 0.70f, 0.0f );
117 mTable.SetProperty( Actor::Property::SIZE_MODE_FACTOR, offset );
118 mTable.SetFitHeight(CellPlacement::TOP_BUTTON);
119 mTable.SetFitHeight(CellPlacement::MID_BUTTON);
120 mTable.SetFitHeight(CellPlacement::LOWER_BUTTON);
121 mContentLayer.Add( mTable );
123 Toolkit::TextLabel instructions = Toolkit::TextLabel::New( EXAMPLE_INSTRUCTIONS );
124 instructions.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
125 instructions.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::BOTTOM_CENTER);
126 instructions.SetProperty( Actor::Property::POSITION_Y, -50.0f);
127 instructions.SetProperty( Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL, true );
128 instructions.SetProperty( Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT, 10 );
129 mContentLayer.Add(instructions);
131 for( unsigned int x = 0; x < NUMBER_OF_IMAGES; x++ )
133 Toolkit::PushButton button = Toolkit::PushButton::New();
134 button.SetProperty( Toolkit::Button::Property::LABEL, "on/off" );
135 button.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
136 button.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
137 button.ClickedSignal().Connect( this, &ImageViewController::ToggleImageOnWindow );
138 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
139 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
140 std::string s = std::to_string(x);
141 button.SetProperty( Dali::Actor::Property::NAME, s );
142 mTable.AddChild( button, Toolkit::TableView::CellPosition( CellPlacement::TOP_BUTTON, x ) );
144 Toolkit::PushButton button2 = Toolkit::PushButton::New();
145 button2.SetProperty( Toolkit::Button::Property::LABEL, "Change" );
146 button2.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
147 button2.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
148 button2.ClickedSignal().Connect( this, &ImageViewController::ChangeImageClicked );
149 button2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
150 button2.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
151 button2.SetProperty( Dali::Actor::Property::NAME, s );
152 mTable.AddChild( button2, Toolkit::TableView::CellPosition( CellPlacement::MID_BUTTON, x ) );
154 Toolkit::PushButton button3 = Toolkit::PushButton::New();
155 button3.SetProperty( Toolkit::Button::Property::LABEL, "Round" );
156 button3.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
157 button3.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
158 button3.ClickedSignal().Connect( this, &ImageViewController::RoundedCornerClicked );
159 button3.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
160 button3.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
161 button3.SetProperty( Dali::Actor::Property::NAME, s );
162 mTable.AddChild( button3, Toolkit::TableView::CellPosition( CellPlacement::LOWER_BUTTON, x ) );
164 mImageViews[x] = Toolkit::ImageView::New( );
165 Property::Map imagePropertyMap;
166 imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
167 imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL, IMAGE_PATH[ 0 ] );
168 mImageViews[x].SetProperty(Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
171 mImageViews[x].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
172 mImageViews[x].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
173 mImageViews[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
174 mTable.AddChild( mImageViews[x], Toolkit::TableView::CellPosition( CellPlacement::IMAGE, x ) );
176 // Set changeable counter and toggle for each ImageView
177 mImageViewImageIndexStatus[x] = 0;
178 mImageViewToggleStatus[x] = true;
179 mImageViewRoundedCornerStatus[x] = false;
182 application.GetWindow().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent);
187 void ImmediateLoadImage( const char* urlToLoad )
189 Property::Map imagePropertyMap;
190 imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
191 imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL, urlToLoad );
192 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( imagePropertyMap );
196 bool ToggleImageOnWindow( Toolkit::Button button )
198 unsigned int buttonIndex = GetButtonIndex( button );
200 Toolkit::ImageView imageView = mImageViews[ buttonIndex ];
202 if( mImageViewToggleStatus[ buttonIndex ] )
204 imageView.Unparent();
208 mTable.AddChild( imageView, Toolkit::TableView::CellPosition( CellPlacement::IMAGE, GetButtonIndex( button ) ) );
211 mImageViewToggleStatus[ buttonIndex ] = !mImageViewToggleStatus[ buttonIndex ];
216 bool ChangeImageClicked( Toolkit::Button button )
218 unsigned int buttonIndex = GetButtonIndex( button );
220 if( mImageViews[buttonIndex].GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
222 ++mImageViewImageIndexStatus[buttonIndex];
224 if( mImageViewImageIndexStatus[buttonIndex] == NUMBER_OF_RESOURCES )
226 mImageViewImageIndexStatus[buttonIndex] = 0;
229 // Reset corner radius state value
230 mImageViewRoundedCornerStatus[buttonIndex] = false;
232 Property::Map imagePropertyMap;
233 imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
234 imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL, IMAGE_PATH[ mImageViewImageIndexStatus[buttonIndex] ] );
235 mImageViews[buttonIndex].SetProperty(Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
240 bool RoundedCornerClicked( Toolkit::Button button )
242 unsigned int buttonIndex = GetButtonIndex( button );
244 if( mImageViews[buttonIndex].GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
246 mImageViewRoundedCornerStatus[ buttonIndex ] = !mImageViewRoundedCornerStatus[ buttonIndex ];
248 Property::Map imagePropertyMap;
249 imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
250 imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL, IMAGE_PATH[ mImageViewImageIndexStatus[buttonIndex] ] );
251 imagePropertyMap.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImageViewRoundedCornerStatus[buttonIndex] ? CORNER_RADIUS_VALUE : 0.0f );
253 mImageViews[buttonIndex].SetProperty( Toolkit::ImageView::Property::IMAGE, imagePropertyMap );
259 * Main key event handler
261 void OnKeyEvent(const KeyEvent& event)
263 if(event.GetState() == KeyEvent::DOWN)
265 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
273 Application& mApplication;
275 Toolkit::Control mView; ///< The View instance.
276 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
277 Layer mContentLayer; ///< Content layer
278 Toolkit::TableView mTable;
279 Toolkit::ImageView mImageViews[ NUMBER_OF_IMAGES ];
280 bool mImageViewToggleStatus[ NUMBER_OF_IMAGES ];
281 bool mImageViewRoundedCornerStatus[ NUMBER_OF_IMAGES ];
282 unsigned int mImageViewImageIndexStatus[ NUMBER_OF_IMAGES ];
284 Toolkit::TableView::CellPosition mCurrentPositionToggle;
285 Toolkit::TableView::CellPosition mCurrentPositionImage;
289 int DALI_EXPORT_API main( int argc, char **argv )
291 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
292 ImageViewController test( application );
293 application.MainLoop();