forked from HaikuArchives/Haiku2048
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowBoard.cpp
326 lines (286 loc) · 7.06 KB
/
WindowBoard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (c) 2015 Markus Himmel
* This file is distributed under the terms of the MIT license
*/
#include "WindowBoard.h"
#include "Game.h"
#include "NumberView.cpp"
#include <Alert.h>
#include <Application.h>
#include <Box.h>
#include <Button.h>
#include <LayoutBuilder.h>
#include <Messenger.h>
#include <Rect.h>
#include <String.h>
#include <StringView.h>
#include <IconUtils.h>
#include <Resources.h>
GameWindow::GameWindow(WindowBoard *master)
:
BWindow(BRect(100, 100, 500, 400), "Haiku2048", B_TITLED_WINDOW, 0),
fMaster(master)
{
fIconUndo = initIcon("icon_undo.hvif");
fIconNew = initIcon("icon_new.hvif");
BButton *newGameButton = new BButton("newgame", "",
new BMessage(H2048_NEW_GAME));
newGameButton->SetIcon(fIconNew);
undoButton = new BButton("undomove", "",
new BMessage(H2048_UNDO_MOVE));
undoButton->SetIcon(fIconUndo);
undoButton->SetEnabled(false);
fScore_Highest = new BStringView("score_highest", "High Score: 0");
fScore = new BStringView("score", "Score: 0");
fHighscoreName = new BStringView("highscore_name","");
fBoard = new BGridLayout();
BLayoutBuilder::Group<>(this, B_VERTICAL)
.SetInsets(B_USE_WINDOW_INSETS)
.AddGroup(B_HORIZONTAL)
.Add(newGameButton)
.Add(undoButton)
.AddStrut(5)
.AddGroup(B_VERTICAL, -15)
.Add(fHighscoreName)
.AddGroup(B_HORIZONTAL)
.AddGlue()
.Add(fScore_Highest)
.End()
.End()
.AddGlue()
.Add(fScore)
.End()
.Add(fBoard);
uint32 sizeX = fMaster->fTarget->SizeX();
uint32 sizeY = fMaster->fTarget->SizeY();
fViews = new NumberView *[sizeX * sizeY];
for (uint32 x = 0; x < sizeX; x++)
{
for (uint32 y = 0; y < sizeY; y++)
{
NumberView *num = new NumberView(0);
fViews[x * sizeY + y] = num;
fBoard->AddView(num, x, y);
}
}
ResizeToPreferred();
BRect rect = Bounds();
prevWidth = rect.Width() - 20; // due to changes in font size, some of the boxes at
// the bottom would be concealed. This is to expand window size.
prevHeight = rect.Height();
defaultWidth = rect.Width();
defaultHeight = rect.Height();
}
BBitmap*
GameWindow::initIcon(const char* iconName) {
BBitmap* icon = NULL;
BResources* resources = BApplication::AppResources();
size_t size;
const void* rawIcon = resources->LoadResource('VICN', iconName, &size);
icon = new BBitmap(BRect(0, 0, 31, 31), B_RGBA32);
BIconUtils::GetVectorIcon((const uint8*)rawIcon, size, icon);
return icon;
}
GameWindow::~GameWindow()
{
fMaster->fWindow = NULL;
delete [] fViews;
}
bool
GameWindow::QuitRequested()
{
be_app_messenger.SendMessage(B_QUIT_REQUESTED);
return true;
}
void
GameWindow::MessageReceived(BMessage *message)
{
switch (message->what)
{
case H2048_NEW_GAME:
{
BMessenger game(NULL, fMaster->fTarget);
game.SendMessage(message);
break;
}
case H2048_WINDOW_SHOW:
{
bool canUndo = false;
message->FindBool("canUndo", &canUndo);
showBoard(canUndo);
break;
}
case H2048_UNDO_MOVE:
{
if (!fMaster->fSending) {
// fMaster->fSending is false when:
// * The game hasn't started (unlikely, and the button is disabled anyway)
// * Game over
// In case of game over, we have to make it true, so that
// subsequent keypressed are acknowledged
fMaster->fSending = true;
}
BMessenger game(NULL, fMaster->fTarget);
game.SendMessage(message);
break;
}
case B_KEY_DOWN:
{
const char *data;
ssize_t length;
if (fMaster->fSending
&& message->FindData("bytes", B_STRING_TYPE, (const void **)&data, &length) == B_OK
&& (data[0] == 'u' || (data[0] >= 28 && data[0] <= 31)))
{
if (data[0] == 'u') {
PostMessage(H2048_UNDO_MOVE);
break;
}
GameMove m;
switch (data[0])
{
case 30: //Up
m = Up;
break;
case 28: // Left
m = Left;
break;
case 31: // Down
m = Down;
break;
case 29: // Right
m = Right;
break;
}
BMessage move(H2048_MAKE_MOVE);
move.AddInt32("direction", m);
BMessenger messenger(NULL, fMaster->fTarget);
messenger.SendMessage(&move);
}
BWindow::MessageReceived(message);
break;
}
case H2048_REQUEST_NAME:
{
BView *RequestBox = new BView(BRect(), "reqbox", B_FOLLOW_LEFT, B_WILL_DRAW);
RequestBox->SetViewColor(200,200,230);
AddChild(RequestBox);
fInputBox = new BTextControl(BRect(BPoint(0,0), BSize(200, 30)), "name", "Your name:", "", new BMessage(H2048_SET_NAME));
RequestBox->AddChild(fInputBox);
ResizeBy(0.0, 35.0);
fInputBox->MakeFocus();
break;
}
case H2048_SET_NAME:
{
BMessage req(H2048_NAME_REQUESTED);
req.AddString("playername", fInputBox->Text());
BMessenger messenger(NULL, fMaster->fTarget);
messenger.SendMessage(&req);
ResizeBy(0.0, -35.0);
RemoveChild(FindView("reqbox"));
delete fInputBox;
fInputBox = NULL;
}
default:
BWindow::MessageReceived(message);
break;
}
}
void
GameWindow::showBoard(bool canUndo)
{
undoButton->SetEnabled(canUndo);
Game *target = fMaster->fTarget;
uint32 sizeX = target->SizeX();
uint32 sizeY = target->SizeY();
for (uint32 x = 0; x < sizeX; x++)
{
for (uint32 y = 0; y < sizeY; y++)
{
fViews[x * sizeY + y]->SetNumber(1 << target->BoardAt(x, y));
fViews[x * sizeY + y]->Invalidate();
}
}
BString highscore_name;
highscore_name << "Highscore";
if(fMaster->fTarget->Username()[0])
{
highscore_name << " by " << fMaster->fTarget->Username();
}
highscore_name << ":";
fHighscoreName->SetText(highscore_name.String());
BString score_highest;
score_highest << fMaster->fTarget->Score_Highest();
fScore_Highest->SetText(score_highest.String());
fScore_Highest->SetFont(be_bold_font);
fScore_Highest->SetFontSize(15);
BString score;
score << fMaster->fTarget->Score();
fScore->SetText(score.String());
fScore->SetFont(be_bold_font);
fScore->SetFontSize(35);
}
void
GameWindow::FrameResized(float width,
float height)
{
// We don't want the user to scale the window so small that
// there's no space for the buttons.
if (width < defaultWidth) {
ResizeTo(defaultWidth, defaultHeight);
width=defaultWidth;
}
// Maintain the same width:height ratio
else {
float ratio = width/prevWidth;
ResizeTo(width, height*ratio);
}
//Don't scale on first resize
if(prevWidth >= defaultWidth){
ScaleBy(width/prevWidth);
}
prevWidth = width;
prevHeight = height;
}
WindowBoard::WindowBoard(Game *target)
:
GameBoard(target),
fSending(false)
{
fWindow = new GameWindow(this);
fWindow->Show();
}
WindowBoard::~WindowBoard()
{
delete fWindow;
}
void
WindowBoard::gameStarted()
{
BMessage redraw(H2048_WINDOW_SHOW);
redraw.AddBool("canUndo", false);
BMessenger messenger(NULL, fWindow);
messenger.SendMessage(&redraw);
fSending = true;
}
void
WindowBoard::gameEnded()
{
fSending = false;
(new BAlert("Title", "Game Ended", "OK"))->Go();
}
void
WindowBoard::boardChanged(bool canUndo)
{
BMessage redraw(H2048_WINDOW_SHOW);
redraw.AddBool("canUndo", canUndo);
BMessenger messenger(NULL, fWindow);
messenger.SendMessage(&redraw);
}
void
WindowBoard::nameRequest()
{
BMessenger messenger(NULL, fWindow);
messenger.SendMessage(H2048_REQUEST_NAME);
}