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/visuals/image-visual-properties-devel.h>
23 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
24 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
31 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
32 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
33 const char* APPLICATION_TITLE( "Image view" );
35 const char* IMAGE_PATH[] = {
36 DEMO_IMAGE_DIR "gallery-small-23.jpg",
37 DEMO_IMAGE_DIR "woodEffect.jpg",
38 DEMO_IMAGE_DIR "wood.png", // 32bits image
39 DEMO_IMAGE_DIR "heartsframe.9.png",
40 DEMO_IMAGE_DIR "World.svg"
43 const unsigned int NUMBER_OF_IMAGES = 3;
55 unsigned int GetButtonIndex( Toolkit::Button button )
57 std::string buttonName = button.GetProperty< std::string >( Dali::Actor::Property::NAME );
58 unsigned int index = 0;
60 if ( buttonName != "")
62 index = std::stoul( buttonName );
69 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
71 std::string EXAMPLE_INSTRUCTIONS = "Instructions: Change button cycles through different image visuals, "
72 "on/off takes the ImageView and it's current visual on or off stage.";
74 const float CORNER_RADIUS_VALUE( 20.0f );
78 class ImageViewController: public ConnectionTracker
82 ImageViewController( Application& application )
83 : mApplication( application ),
84 mCurrentPositionToggle( 0, 0 ),
85 mCurrentPositionImage( 0, 0 )
87 // Connect to the Application's Init signal
88 mApplication.InitSignal().Connect( this, &ImageViewController::Create );
91 ~ImageViewController()
96 void Create( Application& application )
98 // The Init signal is received once (only) during the Application lifetime
100 // Creates a default view with a default tool bar.
101 // The view is added to the stage.
102 mContentLayer = DemoHelper::CreateView( application,
110 // Create a table view to show a pair of buttons above each image.
111 mTable = Toolkit::TableView::New( CellPlacement::NUMBER_OF_ROWS, NUMBER_OF_IMAGES );
112 mTable.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
113 mTable.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
114 mTable.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
115 Vector3 offset( 0.9f, 0.70f, 0.0f );
116 mTable.SetProperty( Actor::Property::SIZE_MODE_FACTOR, offset );
117 mTable.SetFitHeight(CellPlacement::TOP_BUTTON);
118 mTable.SetFitHeight(CellPlacement::MID_BUTTON);
119 mTable.SetFitHeight(CellPlacement::LOWER_BUTTON);
120 mContentLayer.Add( mTable );
122 Toolkit::TextLabel instructions = Toolkit::TextLabel::New( EXAMPLE_INSTRUCTIONS );
123 instructions.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
124 instructions.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::BOTTOM_CENTER);
125 instructions.SetY(-50.0f);
126 instructions.SetProperty( Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL, true );
127 instructions.SetProperty( Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT, 10 );
128 mContentLayer.Add(instructions);
130 for( unsigned int x = 0; x < NUMBER_OF_IMAGES; x++ )
132 Toolkit::PushButton button = Toolkit::PushButton::New();
133 button.SetProperty( Toolkit::Button::Property::LABEL, "on/off" );
134 button.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
135 button.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
136 button.ClickedSignal().Connect( this, &ImageViewController::ToggleImageOnStage );
137 button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
138 button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
139 std::string s = std::to_string(x);
140 button.SetProperty( Dali::Actor::Property::NAME, s );
141 mTable.AddChild( button, Toolkit::TableView::CellPosition( CellPlacement::TOP_BUTTON, x ) );
143 Toolkit::PushButton button2 = Toolkit::PushButton::New();
144 button2.SetProperty( Toolkit::Button::Property::LABEL, "Change" );
145 button2.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
146 button2.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
147 button2.ClickedSignal().Connect( this, &ImageViewController::ChangeImageClicked );
148 button2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
149 button2.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
150 button2.SetProperty( Dali::Actor::Property::NAME, s );
151 mTable.AddChild( button2, Toolkit::TableView::CellPosition( CellPlacement::MID_BUTTON, x ) );
153 Toolkit::PushButton button3 = Toolkit::PushButton::New();
154 button3.SetProperty( Toolkit::Button::Property::LABEL, "Round" );
155 button3.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
156 button3.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
157 button3.ClickedSignal().Connect( this, &ImageViewController::RoundedCornerClicked );
158 button3.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
159 button3.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
160 button3.SetProperty( Dali::Actor::Property::NAME, s );
161 mTable.AddChild( button3, Toolkit::TableView::CellPosition( CellPlacement::LOWER_BUTTON, x ) );
163 mImageViews[x] = Toolkit::ImageView::New( );
164 Property::Map imagePropertyMap;
165 imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
166 imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL, IMAGE_PATH[ 0 ] );
167 mImageViews[x].SetProperty(Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
170 mImageViews[x].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
171 mImageViews[x].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
172 mImageViews[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
173 mTable.AddChild( mImageViews[x], Toolkit::TableView::CellPosition( CellPlacement::IMAGE, x ) );
175 // Set changeable counter and toggle for each ImageView
176 mImageViewImageIndexStatus[x] = 0;
177 mImageViewToggleStatus[x] = true;
178 mImageViewRoundedCornerStatus[x] = false;
181 Stage::GetCurrent().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent);
186 void ImmediateLoadImage( const char* urlToLoad )
188 Property::Map imagePropertyMap;
189 imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
190 imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL, urlToLoad );
191 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( imagePropertyMap );
195 bool ToggleImageOnStage( Toolkit::Button button )
197 unsigned int buttonIndex = GetButtonIndex( button );
199 Toolkit::ImageView imageView = mImageViews[ buttonIndex ];
201 if( mImageViewToggleStatus[ buttonIndex ] )
203 imageView.Unparent();
207 mTable.AddChild( imageView, Toolkit::TableView::CellPosition( CellPlacement::IMAGE, GetButtonIndex( button ) ) );
210 mImageViewToggleStatus[ buttonIndex ] = !mImageViewToggleStatus[ buttonIndex ];
215 bool ChangeImageClicked( Toolkit::Button button )
217 unsigned int buttonIndex = GetButtonIndex( button );
219 if( mImageViews[buttonIndex].OnStage() )
221 ++mImageViewImageIndexStatus[buttonIndex];
223 if( mImageViewImageIndexStatus[buttonIndex] == NUMBER_OF_RESOURCES )
225 mImageViewImageIndexStatus[buttonIndex] = 0;
228 // Reset corner radius state value
229 mImageViewRoundedCornerStatus[buttonIndex] = false;
231 Property::Map imagePropertyMap;
232 imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
233 imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL, IMAGE_PATH[ mImageViewImageIndexStatus[buttonIndex] ] );
234 mImageViews[buttonIndex].SetProperty(Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
239 bool RoundedCornerClicked( Toolkit::Button button )
241 unsigned int buttonIndex = GetButtonIndex( button );
243 if( mImageViews[buttonIndex].OnStage() )
245 mImageViewRoundedCornerStatus[ buttonIndex ] = !mImageViewRoundedCornerStatus[ buttonIndex ];
247 Property::Map imagePropertyMap;
248 imagePropertyMap.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
249 imagePropertyMap.Insert( Toolkit::ImageVisual::Property::URL, IMAGE_PATH[ mImageViewImageIndexStatus[buttonIndex] ] );
250 imagePropertyMap.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImageViewRoundedCornerStatus[buttonIndex] ? CORNER_RADIUS_VALUE : 0.0f );
252 mImageViews[buttonIndex].SetProperty( Toolkit::ImageView::Property::IMAGE, imagePropertyMap );
258 * Main key event handler
260 void OnKeyEvent(const KeyEvent& event)
262 if(event.state == KeyEvent::Down)
264 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
272 Application& mApplication;
274 Toolkit::Control mView; ///< The View instance.
275 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
276 Layer mContentLayer; ///< Content layer
277 Toolkit::TableView mTable;
278 Toolkit::ImageView mImageViews[ NUMBER_OF_IMAGES ];
279 bool mImageViewToggleStatus[ NUMBER_OF_IMAGES ];
280 bool mImageViewRoundedCornerStatus[ NUMBER_OF_IMAGES ];
281 unsigned int mImageViewImageIndexStatus[ NUMBER_OF_IMAGES ];
283 Toolkit::TableView::CellPosition mCurrentPositionToggle;
284 Toolkit::TableView::CellPosition mCurrentPositionImage;
288 int DALI_EXPORT_API main( int argc, char **argv )
290 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
291 ImageViewController test( application );
292 application.MainLoop();