Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / partner / cpp / Sample / Tizen C++ / RockPaperScissors / RockPaperScissors / project / src / MainForm.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 "MainForm.h"
20 #include "FormMgr.h"
21
22 using namespace Osp::Base;
23 using namespace Osp::Ui;
24 using namespace Osp::Ui::Controls;
25 using namespace Osp::App;
26 using namespace Osp::Graphics;
27
28 MainForm::MainForm(void)
29 {
30 }
31
32 MainForm::~MainForm(void)
33 {
34 }
35
36 bool
37 MainForm::Initialize(void)
38 {
39         Form::Construct(FORM_STYLE_NORMAL|FORM_STYLE_HEADER|FORM_STYLE_INDICATOR);
40         SetName(L"MainForm");
41
42         // Set Header title
43         Header* pHeader = this->GetHeader();
44         pHeader->SetTitleText(L"RockPaperScissors");
45
46         return true;
47 }
48
49 result
50 MainForm::OnInitializing(void)
51 {
52         result r = E_SUCCESS;
53
54         Button *pBtnStart = new (std::nothrow) Button();
55         TryReturn(pBtnStart != null, E_FAILURE, "[Fail]Button Create");
56
57         Rectangle clientAreaBound = GetClientAreaBounds();
58         int buttonX = static_cast<int>(clientAreaBound.width * 0.33); // 1/3 Down
59         int buttonY = static_cast<int>(clientAreaBound.height * 0.75); // 3/4
60         int buttonWidth = static_cast<int>(clientAreaBound.width * 0.34); // 1/3 Up
61         int buttonHeight = static_cast<int>(clientAreaBound.height * 0.14); // 1/7
62
63         pBtnStart->Construct(Rectangle(buttonX, buttonY, buttonWidth, buttonHeight));
64         pBtnStart->SetText(L"Start");
65         pBtnStart->SetActionId(IDC_BTN_START);
66         pBtnStart->AddActionEventListener(*this);
67         AddControl(*pBtnStart);
68
69         return r;
70 }
71
72 result
73 MainForm::OnTerminating(void)
74 {
75         return E_SUCCESS;
76 }
77
78 void
79 MainForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
80 {
81         Frame *pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
82
83         switch(actionId)
84         {
85         case IDC_BTN_START:
86                 {
87                         FormMgr *pFormMgr = static_cast<FormMgr *>(pFrame->GetControl("FormMgr"));
88
89                         if (pFormMgr != null)
90                         {
91                                 pFormMgr->SendUserEvent(FormMgr::REQUEST_ROCKPAPERSCISSORSFORM , null);
92                         }
93                 }
94                 break;
95
96         default:
97                 break;
98         }
99 }
100
101 result
102 MainForm::OnDraw(void)
103 {
104         result r = E_SUCCESS;
105
106         Osp::Graphics::Canvas* __pCanvas = GetCanvasN();
107
108         if(__pCanvas != null)
109         {
110                 Font font;
111                 // Rectangle
112                 static Rectangle clientAreaBound = GetClientAreaBounds();
113                 static int rectMarginX = static_cast<int>(clientAreaBound.width * 0.03); // 1/32 Down
114                 static int rectMarginY = static_cast<int>(clientAreaBound.height * 0.25); // 1/3 Down
115                 static int rectX = clientAreaBound.x + rectMarginX;
116                 static int rectY = clientAreaBound.y + rectMarginY;
117                 static int rectWidth = clientAreaBound.width - (rectMarginX * 2);
118                 static int rectHeight = static_cast<int>(clientAreaBound.height * 0.45); // 1/2
119                 static int titleHeight = static_cast<int>(rectHeight * 0.167); // rectHeight/6
120                 // Font size
121                 static int HEAD_FONT_SIZE = static_cast<int>(titleHeight * 0.7);
122                 static int BODY_FONT_SIZE = static_cast<int>(titleHeight * 0.45);
123                 // Line
124                 static int lineStartX = rectX;
125                 static int lineEndX = lineStartX + rectWidth;
126                 static int lineY = rectY + titleHeight;
127                 // Headline
128                 static int headX = rectX;
129                 static int headY = rectY;
130                 // Text
131                 static int textX = headX + rectMarginX;
132                 static int textY = lineY + rectMarginX;
133                 static int textGapY = BODY_FONT_SIZE + rectMarginX;
134                 static String text0(L"<How to play this game>");
135                 static String text1(L"This game can join up to 4 people.");
136                 static String text2(L"1. Choose the number.");
137                 static String text3(L"2. Press the start button.");
138                 static String text4(L"3. To stop, place a hand");
139                 static String text5(L"across & near the top of the phone.");
140
141                 __pCanvas->SetBackgroundColor(Color::GetColor(COLOR_ID_BLACK));
142                 __pCanvas->SetForegroundColor(Color::GetColor(COLOR_ID_YELLOW));
143                 __pCanvas->Clear();
144
145                 font.Construct(FONT_STYLE_PLAIN || FONT_STYLE_BOLD, HEAD_FONT_SIZE);
146                 __pCanvas->SetFont(font);
147                 __pCanvas->DrawRectangle(Rectangle(rectX, rectY, rectWidth, rectHeight));
148                 __pCanvas->DrawText(Point(headX, headY), text0);
149                 __pCanvas->DrawLine(Point(lineStartX, lineY), Point(lineEndX, lineY));
150
151                 font.Construct(FONT_STYLE_PLAIN, BODY_FONT_SIZE);
152                 __pCanvas->SetFont(font);
153                 __pCanvas->DrawText(Point(headX, textY), text1);
154                 __pCanvas->DrawText(Point(textX, textY + textGapY), text2);
155                 __pCanvas->DrawText(Point(textX, textY + (textGapY * 2)), text3);
156                 __pCanvas->DrawText(Point(textX, textY + (textGapY * 3)), text4);
157                 __pCanvas->DrawText(Point(textX, textY + (textGapY * 4)), text5);
158
159                 delete __pCanvas;
160         }
161
162         return r;
163 }