3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.tizenopensource.org/license
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.
19 #include "StopwatchPanel.h"
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;
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;
36 StopwatchPanel::StopwatchPanel(void)
41 , __stopwatchMode(RESET)
45 StopwatchPanel::~StopwatchPanel(void)
50 StopwatchPanel::Initialize(void)
52 result r = Construct(Rectangle(0, 0, 10, 10)); // Should be set proper area at OnInitializing().
53 TryReturn(!IsFailed(r), false, "%s", GetErrorMessage(r));
59 StopwatchPanel::OnInitializing(void)
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;
71 const Form* pForm = dynamic_cast<Form*>(GetParent());
74 clientRect = pForm->GetClientAreaBounds();
75 SetBounds(Rectangle(0, 0, clientRect.width, clientRect.height));
78 Button* pButton = new (std::nothrow) Button();
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);
87 // Start, Reset button
88 pButton = new (std::nothrow) Button();
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);
96 pButton = new (std::nothrow) Button();
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);
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);
116 __pTimer = new (std::nothrow) Timer();
118 __pTimer->Construct(*this);
126 StopwatchPanel::OnTerminating(void)
128 result r = E_SUCCESS;
136 StopwatchPanel::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
138 SceneManager* pSceneManager = SceneManager::GetInstance();
139 AppAssert(pSceneManager);
144 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT));
147 case ID_BUTTON_START:
148 StartPauseStopwatch();
151 case ID_BUTTON_RESET:
157 // ISceneEventListener
159 StopwatchPanel::OnSceneActivatedN(const Osp::Ui::Scenes::SceneId& previousSceneId,
160 const Osp::Ui::Scenes::SceneId& currentSceneId, Osp::Base::Collection::IList* pArgs)
162 const Form* pForm = dynamic_cast<Form*>(GetParent());
164 Header* pHeader = pForm->GetHeader();
166 pHeader->SetTitleText(L"Stopwatch");
168 __pTimer->StartAsRepeatable(TIMER_INTERVAL);
172 StopwatchPanel::OnSceneDeactivated(const Osp::Ui::Scenes::SceneId& currentSceneId,
173 const Osp::Ui::Scenes::SceneId& nextSceneId)
177 // ITimerEventListener
179 StopwatchPanel::OnTimerExpired(Timer &timer)
185 StopwatchPanel::StartPauseStopwatch(void)
187 switch (__stopwatchMode)
190 SystemTime::GetTicks(__startTicks);
191 __stopwatchMode = START;
195 SystemTime::GetTicks(__stopTicks);
196 __stopwatchMode = STOP;
201 long long currentTicks;
202 SystemTime::GetTicks(currentTicks);
203 __startTicks = currentTicks - (__stopTicks - __startTicks);
204 __stopwatchMode = START;
211 StopwatchPanel::ResetStopwatch(void)
213 switch (__stopwatchMode)
219 __stopwatchMode = RESET;
224 __stopwatchMode = RESET;
231 StopwatchPanel::UpdateTime(void)
233 long long currentTicks;
236 switch (__stopwatchMode)
239 UpdateAndDisplayTime(0);
243 SystemTime::GetTicks(currentTicks);
244 term = currentTicks - __startTicks;
245 UpdateAndDisplayTime(term);
249 term = __stopTicks - __startTicks;
250 UpdateAndDisplayTime(term);
256 StopwatchPanel::UpdateAndDisplayTime(long long tickTime)
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;
265 timeString.Format(64, L"%02lld:%02lld:%02lld.%02lld", modHour, modMin % 60, modSec % 60, modMili % 100);
266 __pLabelTime->SetText(timeString);
267 __pLabelTime->Draw();