Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / SceneManagement / SceneManagement / project / src / StopwatchPanel.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.tizenopensource.org/license
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <new>
19 #include "StopwatchPanel.h"
20 #include <FUi.h>
21 #include <FSystem.h>
22
23 using namespace Osp::Graphics;
24 using namespace Osp::Ui::Controls;
25 using namespace Osp::Ui::Scenes;
26 using namespace Osp::Base;
27 using namespace Osp::System;
28
29
30 static const int ID_BUTTON_BACK = 120;
31 static const int ID_BUTTON_START = 121;
32 static const int ID_BUTTON_RESET = 122;
33 static const int TIMER_INTERVAL = 20;
34
35
36 StopwatchPanel::StopwatchPanel(void)
37         : __pLabelTime(null)
38         , __pTimer(null)
39         , __startTicks(0)
40         , __stopTicks(0)
41         , __stopwatchMode(RESET)
42 {
43 }
44
45 StopwatchPanel::~StopwatchPanel(void)
46 {
47 }
48
49 bool
50 StopwatchPanel::Initialize(void)
51 {
52         result r = Construct(Rectangle(0, 0, 10, 10));  // Should be set proper area at OnInitializing().
53         TryReturn(!IsFailed(r), false, "%s", GetErrorMessage(r));
54
55         return true;
56 }
57
58 result
59 StopwatchPanel::OnInitializing(void)
60 {
61         const int buttonWidth = 100;
62         const int buttonHeight = 60;
63         const int margin = 40;
64         const int labelWidth = 470;
65         const int labelHeight = 80;
66         const int textSize = 82;
67         result r = E_SUCCESS;
68         Rectangle clientRect;
69
70         // Resize
71         const Form* pForm = dynamic_cast<Form*>(GetParent());
72         AppAssert(pForm);
73
74         clientRect = pForm->GetClientAreaBounds();
75         SetBounds(Rectangle(0, 0, clientRect.width, clientRect.height));
76
77         // Setup button
78         Button* pButton = new (std::nothrow) Button();
79         AppAssert(pButton);
80         pButton->Construct(Rectangle(clientRect.width - buttonWidth - margin, clientRect.height - buttonHeight - margin,
81                                                                  buttonWidth, buttonHeight));
82         pButton->SetText(L"Back");
83         pButton->SetActionId(ID_BUTTON_BACK);
84         pButton->AddActionEventListener(*this);
85         AddControl(*pButton);
86
87         // Start, Reset button
88         pButton = new (std::nothrow) Button();
89         AppAssert(pButton);
90         pButton->Construct(Rectangle((clientRect.width-buttonWidth * 1.5 * 2) / 2 - margin,
91                                                                  (clientRect.height-labelHeight) / 2 + buttonHeight, buttonWidth * 1.5, buttonHeight * 1.5));
92         pButton->SetText(L"Start / Stop");
93         pButton->SetActionId(ID_BUTTON_START);
94         pButton->AddActionEventListener(*this);
95         AddControl(*pButton);
96         pButton = new (std::nothrow) Button();
97         AppAssert(pButton);
98         pButton->Construct(Rectangle(clientRect.width-buttonWidth * 1.5 - margin,
99                                                                  (clientRect.height-labelHeight) / 2 + buttonHeight, buttonWidth * 1.5, buttonHeight * 1.5));
100         pButton->SetText(L"Reset");
101         pButton->SetActionId(ID_BUTTON_RESET);
102         pButton->AddActionEventListener(*this);
103         AddControl(*pButton);
104
105         // Setup stopwatch display control
106         __pLabelTime = new (std::nothrow) Label();
107         AppAssert(__pLabelTime);
108         __pLabelTime->Construct(Rectangle((clientRect.width-labelWidth) / 2, (clientRect.height-labelHeight) / 2 - buttonHeight,
109                                                         labelWidth, labelHeight), L"");
110         __pLabelTime->SetTextConfig(textSize, LABEL_TEXT_STYLE_BOLD);
111         __pLabelTime->SetTextHorizontalAlignment(ALIGNMENT_CENTER);
112         __pLabelTime->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
113         AddControl(*__pLabelTime);
114
115         // Setup timer
116         __pTimer = new (std::nothrow) Timer();
117         AppAssert(__pTimer);
118         __pTimer->Construct(*this);
119
120         UpdateTime();
121
122         return r;
123 }
124
125 result
126 StopwatchPanel::OnTerminating(void)
127 {
128         result r = E_SUCCESS;
129         delete __pTimer;
130         __pTimer = null;
131
132         return r;
133 }
134
135 void
136 StopwatchPanel::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
137 {
138         SceneManager* pSceneManager = SceneManager::GetInstance();
139         AppAssert(pSceneManager);
140
141         switch (actionId)
142         {
143         case ID_BUTTON_BACK:
144                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT));
145                 break;
146
147         case ID_BUTTON_START:
148                 StartPauseStopwatch();
149                 break;
150
151         case ID_BUTTON_RESET:
152                 ResetStopwatch();
153                 break;
154         }
155 }
156
157 // ISceneEventListener
158 void
159 StopwatchPanel::OnSceneActivatedN(const Osp::Ui::Scenes::SceneId& previousSceneId,
160                                                           const Osp::Ui::Scenes::SceneId& currentSceneId, Osp::Base::Collection::IList* pArgs)
161 {
162         const Form* pForm = dynamic_cast<Form*>(GetParent());
163         AppAssert(pForm);
164         Header* pHeader = pForm->GetHeader();
165         AppAssert(pHeader);
166         pHeader->SetTitleText(L"Stopwatch");
167
168         __pTimer->StartAsRepeatable(TIMER_INTERVAL);
169 }
170
171 void
172 StopwatchPanel::OnSceneDeactivated(const Osp::Ui::Scenes::SceneId& currentSceneId,
173                                                                 const Osp::Ui::Scenes::SceneId& nextSceneId)
174 {
175         __pTimer->Cancel();
176 }
177 // ITimerEventListener
178 void
179 StopwatchPanel::OnTimerExpired(Timer &timer)
180 {
181         UpdateTime();
182 }
183
184 void
185 StopwatchPanel::StartPauseStopwatch(void)
186 {
187         switch (__stopwatchMode)
188         {
189         case RESET:
190                 SystemTime::GetTicks(__startTicks);
191                 __stopwatchMode = START;
192                 break;
193
194         case START:
195                 SystemTime::GetTicks(__stopTicks);
196                 __stopwatchMode = STOP;
197                 break;
198
199         case STOP:
200         {
201                 long long currentTicks;
202                 SystemTime::GetTicks(currentTicks);
203                 __startTicks = currentTicks - (__stopTicks - __startTicks);
204                 __stopwatchMode = START;
205         }
206                 break;
207         }
208 }
209
210 void
211 StopwatchPanel::ResetStopwatch(void)
212 {
213         switch (__stopwatchMode)
214         {
215         case RESET:
216                 break;
217
218         case START:
219                 __stopwatchMode = RESET;
220                 UpdateTime();
221                 break;
222
223         case STOP:
224                 __stopwatchMode = RESET;
225                 UpdateTime();
226                 break;
227         }
228 }
229
230 void
231 StopwatchPanel::UpdateTime(void)
232 {
233         long long currentTicks;
234         long long term;
235
236         switch (__stopwatchMode)
237         {
238         case RESET:
239                 UpdateAndDisplayTime(0);
240                 break;
241
242         case START:
243                 SystemTime::GetTicks(currentTicks);
244                 term = currentTicks - __startTicks;
245                 UpdateAndDisplayTime(term);
246                 break;
247
248         case STOP:
249                 term = __stopTicks - __startTicks;
250                 UpdateAndDisplayTime(term);
251                 break;
252         }
253 }
254
255 void
256 StopwatchPanel::UpdateAndDisplayTime(long long tickTime)
257 {
258         String timeString;
259
260         long long modMili = tickTime / 10;      // delete last one digit
261         long long modSec = tickTime / 1000;
262         long long modMin = modSec / 60;
263         long long modHour = modMin / 60;
264
265         timeString.Format(64, L"%02lld:%02lld:%02lld.%02lld", modHour, modMin % 60, modSec % 60, modMili % 100);
266         __pLabelTime->SetText(timeString);
267         __pLabelTime->Draw();
268 }