2 * Copyright (c) 2016 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-toolkit/dali-toolkit.h>
20 #include <dali-toolkit/devel-api/controls/progress-bar/progress-bar.h>
23 using namespace Dali::Toolkit;
24 using Dali::Toolkit::ProgressBar;
28 const char * const THEME_PATH( DEMO_STYLE_DIR "progress-bar-example-theme.json" ); ///< The theme used for this example
29 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "background-gradient.jpg";
30 const char* const TOOLBAR_IMAGE = DEMO_IMAGE_DIR "top-bar.png";
31 const char* const TOOLBAR_TITLE = "Progress Bar";
33 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
36 const int MARGIN_SIZE = 10;
37 const int TOP_MARGIN = 85;
39 const unsigned int TIMER_TIMEOUT_TIME = 50;
40 const float PROGRESS_INCREMENT_VALUE = 0.01f;
45 * @brief Shows how to create a default progress bar and custom styled progress bars.
47 class ProgressBarExample: public ConnectionTracker
51 ProgressBarExample( Application& application )
52 : mApplication( application )
54 // Connect to the Application's Init signal
55 mProgressValue = 0.0f;
56 mApplication.InitSignal().Connect( this, &ProgressBarExample::Create );
61 void Create( Application& application )
63 // The Init signal is received once (only) during the Application lifetime
65 // Respond to key events
66 Stage::GetCurrent().KeyEventSignal().Connect( this, &ProgressBarExample::OnKeyEvent );
68 // Creates a default view with a default tool bar.
69 // The view is added to the stage.
71 mContentLayer = DemoHelper::CreateView( application,
78 mProgressBarDefault = ProgressBar::New();
79 mProgressBarDefault.SetParentOrigin(ParentOrigin::TOP_CENTER);
80 mProgressBarDefault.SetAnchorPoint(AnchorPoint::TOP_CENTER);
81 mProgressBarDefault.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
82 mProgressBarDefault.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
84 mProgressBarCustomStyle1 = ProgressBar::New();
85 mProgressBarCustomStyle1.SetStyleName( "ProgressBarCustomStyle1" );
86 mProgressBarCustomStyle1.SetParentOrigin(ParentOrigin::TOP_CENTER);
87 mProgressBarCustomStyle1.SetAnchorPoint(AnchorPoint::TOP_CENTER);
88 mProgressBarCustomStyle1.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
89 mProgressBarCustomStyle1.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
91 mProgressBarCustomStyle2 = ProgressBar::New();
92 mProgressBarCustomStyle2.SetStyleName( "ProgressBarCustomStyle2" );
93 mProgressBarCustomStyle2.SetParentOrigin(ParentOrigin::TOP_CENTER);
94 mProgressBarCustomStyle2.SetAnchorPoint(AnchorPoint::TOP_CENTER);
95 mProgressBarCustomStyle2.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
96 mProgressBarCustomStyle2.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
98 Toolkit::TableView contentTable = Toolkit::TableView::New(2, 1);
99 contentTable.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
100 contentTable.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
101 contentTable.SetAnchorPoint(AnchorPoint::TOP_LEFT);
102 contentTable.SetParentOrigin(ParentOrigin::TOP_LEFT);
103 contentTable.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE * 0.5f));
105 for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
107 contentTable.SetFitHeight( i );
110 contentTable.SetPosition( 0.0f, TOP_MARGIN );
111 mContentLayer.Add( contentTable );
113 // Image selector for progress bar
114 Toolkit::TableView progressBackground = Toolkit::TableView::New( 3, 1 );
115 progressBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
116 progressBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
117 progressBackground.SetBackgroundColor( BACKGROUND_COLOUR );
118 progressBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
119 progressBackground.SetRelativeWidth( 0, 1.0f );
121 for( unsigned int i = 0; i < progressBackground.GetRows(); ++i )
123 progressBackground.SetFitHeight( i );
126 contentTable.Add( progressBackground );
127 progressBackground.Add( mProgressBarDefault );
128 progressBackground.Add( mProgressBarCustomStyle1 );
129 progressBackground.Add( mProgressBarCustomStyle2 );
132 Toolkit::TableView buttonBackground = Toolkit::TableView::New( 1, 1 );
133 buttonBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
134 buttonBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
135 buttonBackground.SetBackgroundColor( BACKGROUND_COLOUR );
136 buttonBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
138 for( unsigned int i = 0; i < buttonBackground.GetRows(); ++i )
140 buttonBackground.SetFitHeight( i );
143 contentTable.Add( buttonBackground );
145 mResetProgressButton = Toolkit::PushButton::New();
146 mResetProgressButton.SetProperty( Toolkit::Button::Property::LABEL, "Reset" );
147 mResetProgressButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
148 mResetProgressButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
149 mResetProgressButton.ClickedSignal().Connect( this, &ProgressBarExample::OnResetProgressButtonSelected );
151 buttonBackground.Add( mResetProgressButton );
153 // Create a timer to update the progress of all progress bars
154 mTimer = Timer::New( TIMER_TIMEOUT_TIME );
155 mTimer.TickSignal().Connect( this, &ProgressBarExample::OnTimerTick );
161 mProgressValue += PROGRESS_INCREMENT_VALUE;
162 mProgressBarDefault.SetProperty(ProgressBar::Property::PROGRESS_VALUE, mProgressValue);
163 mProgressBarCustomStyle1.SetProperty(ProgressBar::Property::PROGRESS_VALUE, mProgressValue);
164 mProgressBarCustomStyle2.SetProperty(ProgressBar::Property::PROGRESS_VALUE, mProgressValue);
166 return ( mProgressValue < 1.0f ); // Only call again if progress has NOT got to the end
169 bool OnResetProgressButtonSelected( Toolkit::Button button )
171 mProgressValue = 0.0f;
172 mProgressBarDefault.SetProperty(ProgressBar::Property::PROGRESS_VALUE, 0.0f);
173 mProgressBarCustomStyle1.SetProperty(ProgressBar::Property::PROGRESS_VALUE, 0.0f);
174 mProgressBarCustomStyle2.SetProperty(ProgressBar::Property::PROGRESS_VALUE, 0.0f);
179 void OnKeyEvent( const KeyEvent& event )
181 if( event.state == KeyEvent::Down )
183 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
185 // Exit application when click back or escape.
193 Application& mApplication;
195 Toolkit::Control mView; ///< The View instance.
196 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
197 Layer mContentLayer; ///< Content layer.
198 ProgressBar mProgressBarDefault;
199 ProgressBar mProgressBarCustomStyle1;
200 ProgressBar mProgressBarCustomStyle2;
201 Toolkit::PushButton mResetProgressButton;
202 float mProgressValue;
205 int DALI_EXPORT_API main( int argc, char **argv )
207 Application application = Application::New( &argc, &argv, THEME_PATH );
208 ProgressBarExample test( application );
209 application.MainLoop();