2 // editorwidget.cpp: Game editor window widget
5 // (C) 2014 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 07/01/2014 Created this file
14 #include "editorwidget.h"
15 #include <unistd.h> // for usleep()
18 #include "gameboard.h"
24 EditorWidget::EditorWidget(QWidget * parent/*= 0*/): QWidget(parent),
27 // memset(¤tLevel, 0, sizeof(currentLevel));
28 // currentLevel.corner.x = -1;
29 // currentLevel.corner.y = -1;
30 // levelStorage.push_back(currentLevel);
31 // Level * b1 = &level[0];
32 // memset(board, 0, sizeof(board));
33 // CreateBackground();
34 // score->setTextFormat(Qt::PlainText);
36 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
37 setFocusPolicy(Qt::StrongFocus); // Without this, it gets no keys
42 // This never gets called...!
43 EditorWidget::~EditorWidget(void)
49 QSize EditorWidget::sizeHint() const
51 return QSize(400, 400);
55 void EditorWidget::paintEvent(QPaintEvent * /*event*/)
57 QPainter painter(this);
59 Level & level = levelStorage[currentLevel];
61 for(int y=0; y<=clientArea.height()/GRIDSIZE; y++)
63 for(int x=0; x<=clientArea.width()/GRIDSIZE; x++)
65 Point current(level.corner.x + x, level.corner.y + y);
67 if ((current.x < 0) && (current.y < 0))
69 painter.setBrush(Qt::black);
70 painter.drawRect(x + (GRIDSIZE / 2), y + (GRIDSIZE / 2), GRIDSIZE / 2, GRIDSIZE / 2);
72 else if (current.x < 0)
74 painter.setBrush(Qt::black);
75 painter.drawRect(x + (GRIDSIZE / 2), y * GRIDSIZE, GRIDSIZE / 2, GRIDSIZE);
77 else if (current.y < 0)
79 painter.setBrush(Qt::black);
80 painter.drawRect(x * GRIDSIZE, y + (GRIDSIZE / 2), GRIDSIZE, GRIDSIZE / 2);
84 uint8_t tile = level.board[current.x][current.y];
86 painter.setBrush(Qt::transparent);
87 painter.setPen(Qt::black);
88 painter.drawRect(x * GRIDSIZE, y * GRIDSIZE, GRIDSIZE, GRIDSIZE);
90 if (tile & (GTWall | GTSpace | GTBox))
94 painter.setBrush(Qt::white);
95 painter.drawRect(x * GRIDSIZE, y * GRIDSIZE, GRIDSIZE, GRIDSIZE);
97 else if (tile & GTBox)
99 painter.setBrush(Qt::red);
100 painter.drawRect((x * GRIDSIZE) + (GRIDSIZE * 0.1),
101 (y * GRIDSIZE) + (GRIDSIZE * 0.1), GRIDSIZE * 0.8, GRIDSIZE * 0.8);
104 else if (tile & GTNull)
106 painter.setBrush(Qt::black);
107 // painter.setPen(Qt::black);
108 painter.drawRect(x * GRIDSIZE, y * GRIDSIZE, GRIDSIZE, GRIDSIZE);
110 else if (tile & GTMan)
112 painter.setBrush(Qt::yellow);
113 painter.drawEllipse((x * GRIDSIZE) + (GRIDSIZE * 0.1),
114 (y * GRIDSIZE) + (GRIDSIZE * 0.1), GRIDSIZE * 0.8, GRIDSIZE * 0.8);
117 if ((tile & GTBoxSpot) && !(tile & (GTWall | GTNull)))
119 painter.setBrush(Qt::magenta);
120 painter.drawRect((x * GRIDSIZE) + (GRIDSIZE / 3),
121 (y * GRIDSIZE) + (GRIDSIZE / 3), GRIDSIZE / 3, GRIDSIZE / 3);
127 int x = level.cursor.x - level.corner.x;
128 int y = level.cursor.y - level.corner.y;
130 painter.setBrush(Qt::transparent);
131 painter.setPen(Qt::darkGreen);
132 painter.drawEllipse((x * GRIDSIZE) + 4, (y * GRIDSIZE) + 4, GRIDSIZE - 8, GRIDSIZE - 8);
136 void EditorWidget::mousePressEvent(QMouseEvent * event)
138 if (event->button() == Qt::LeftButton)
145 void EditorWidget::mouseMoveEvent(QMouseEvent * event)
147 if (event->buttons() & Qt::LeftButton)
154 void EditorWidget::mouseReleaseEvent(QMouseEvent * event)
156 if (event->button() == Qt::LeftButton)
163 void EditorWidget::mouseDoubleClickEvent(QMouseEvent * event)
165 if (event->button() == Qt::LeftButton)
172 void EditorWidget::keyPressEvent(QKeyEvent * event)
174 int key = event->key();
175 Level & level = levelStorage[currentLevel];
176 Point & cursor = level.cursor;
177 Point & corner = level.corner;
179 if (key == Qt::Key_Up)
184 if (((cursor.y - corner.y) == 0) && (corner.y > -1))
187 else if (key == Qt::Key_Down)
189 if (cursor.y < (BOARDSIZE - 1))
192 if (cursor.y >= (range.y + corner.y - 1))
194 if (corner.y < (BOARDSIZE - range.y))
198 else if (key == Qt::Key_Left)
203 if (((cursor.x - corner.x) == 0) && (corner.x > -1))
206 else if (key == Qt::Key_Right)
208 if (cursor.x < (BOARDSIZE - 1))
211 if (cursor.x >= (range.x + corner.x - 1))
213 if (corner.x < (BOARDSIZE - range.x))
217 else if (key == Qt::Key_Period)
219 level.board[cursor.x][cursor.y] ^= GTBoxSpot;
221 else if (key == Qt::Key_B)
223 level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTBox);
224 level.board[cursor.x][cursor.y] |= GTBox;
226 else if (key == Qt::Key_W)
228 level.board[cursor.x][cursor.y] = GTWall;
230 else if (key == Qt::Key_Space)
232 level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTBox);
233 level.board[cursor.x][cursor.y] |= GTSpace;
235 else if (key == Qt::Key_O)
237 // There can be only one!
238 for(int x=0; x<BOARDSIZE; x++)
239 for(int y=0; y<BOARDSIZE; y++)
240 level.board[x][y] &= ~GTMan;
242 // Also, he can only be on a space!
243 level.board[cursor.x][cursor.y] &= GTBoxSpot;
244 level.board[cursor.x][cursor.y] |= GTMan;
246 else if (key == Qt::Key_V)
248 level.board[cursor.x][cursor.y] = GTNull;
251 return; // Only update screen if keypress was recognized
257 void EditorWidget::keyReleaseEvent(QKeyEvent * /*event*/)
262 void EditorWidget::resizeEvent(QResizeEvent * /*event*/)
264 // QSize s = event->size();
266 //printf("Size of window is: %i x %i\n", s.width(), s.height());
267 //printf("Size of game grid is: %i x %i\n", gameBoard->width, gameBoard->height);
272 void EditorWidget::CreateBackground(void)
275 char BGRes[27][64] = {
276 ":/res/grfttile.bmp",
278 ":/res/bg_tech_3.bmp",
279 ":/res/bg_tech_2.bmp",
280 ":/res/bg_tech_1.bmp",
281 ":/res/bg_weave_3.bmp",
282 ":/res/bg_weave_2.bmp",
283 ":/res/bg_clouds_2.bmp",
284 ":/res/bg_floor_plate.bmp",
285 ":/res/bg_marble_b.bmp",
286 ":/res/bg_marble_g.bmp",
287 ":/res/bg_marble_p.bmp",
288 ":/res/bg_marble_r.bmp",
289 ":/res/bg_marble_rb.bmp",
290 ":/res/bg_money_1.bmp",
291 ":/res/bg_pinstripe2.bmp",
292 ":/res/bg_pinstripe7.bmp",
293 ":/res/bg_raindrops_large.bmp",
294 ":/res/bg_raindrops_small.bmp",
295 ":/res/bg_stucco.bmp",
296 ":/res/bg_wood_w.bmp",
297 ":/res/bg_wood_b1.bmp",
298 ":/res/bg_wood_d.bmp",
299 ":/res/bg_wood_f.bmp",
300 ":/res/bg_wood_mh.bmp",
301 ":/res/bg_wood_mv.bmp",
302 ":/res/bg_wood_ro.bmp"
305 QPalette pal = palette();
306 pal.setBrush(backgroundRole(), QBrush(QPixmap(BGRes[m_nBackground])));
307 setAutoFillBackground(true);
310 return true; // Ignore errors for now...
312 // QPalette pal = palette();
313 // pal.setBrush(backgroundRole(), QBrush(QPixmap(":/bg_marble_g.bmp")));
314 // setAutoFillBackground(true);
320 void EditorWidget::ClearLevel(void)
322 // gameBoard->ResetGame();
323 // memset(board, 0, sizeof(board));
328 bool EditorWidget::Load(void)
330 FILE * f = fopen("./wmd-level.data", "rb");
335 fscanf(f, "%i\n", &numberOfLevels);
336 //printf("Load: Looking for %i levels...\n", numberOfLevels);
338 for(int i=0; i<numberOfLevels; i++)
342 memset(&level, 0, sizeof(Level));
344 fscanf(f, "%i\n%i\n%i\n%i\n", &size.x, &size.y, &corner.x, &corner.y);
345 fscanf(f, "%i\n%i\n%i\n%i\n", &level.corner.x, &level.corner.y, &level.cursor.x, &level.cursor.y);
346 fread(level.name, 1, 41, f);
347 //printf("Load: name=\"%s\"\n", level.name);
348 //printf("Load: size=%i, %i; corner=%i, %i; level corner=%i, %i; cursor=%i, %i\n", size.x, size.y, corner.x, corner.y, level.corner.x, level.corner.y, level.cursor.x, level.cursor.y);
350 for(int k=0; k<size.y; k++)
351 for(int j=0; j<size.x; j++)
352 level.board[corner.x + j][corner.y + k] = fgetc(f);
354 levelStorage.push_back(level);
361 // There is no save file, so use the internal levels
362 for(uint32_t i=0; i<NUMBER_OF_BOARDS; i++)
365 memset(&l, 0, sizeof(Level));
366 l.corner.x = l.corner.y = -1;
368 // Create a GameBoard object for level #i (1-based)...
371 for(int32_t x=0; x<b.width; x++)
372 for(int32_t y=0; y<b.height; y++)
373 l.board[x][y] = b.board[(y * b.width) + x];
375 l.board[b.playerX][b.playerY] |= GTMan;
376 strncpy(l.name, b.name, 40); // NULL is provided by the memset() above
377 levelStorage.push_back(l);
384 bool EditorWidget::Save(void)
386 FILE * f = fopen("./wmd-level.data", "wb");
390 printf("Could not open 'wmd-level.data' for writing!\n");
394 fprintf(f, "%lu\n", levelStorage.size());
396 for(uint32_t i=0; i<levelStorage.size(); i++)
398 Level & level = levelStorage[i];
400 GetSizeAndCorner(&level, size, corner);
402 fprintf(f, "%i\n%i\n%i\n%i\n", size.x, size.y, corner.x, corner.y);
403 fprintf(f, "%i\n%i\n%i\n%i\n", level.corner.x, level.corner.y, level.cursor.x, level.cursor.y);
404 fwrite(level.name, 1, 41, f);
406 for(int k=0; k<size.y; k++)
407 for(int j=0; j<size.x; j++)
408 fputc(level.board[corner.x + j][corner.y + k], f);
416 void EditorWidget::SetNameOnCurrentLevel(const char * newName)
418 Level & level = levelStorage[currentLevel];
419 memset(level.name, 0, 41);
420 strncpy(level.name, newName, 40);
424 void EditorWidget::GetSizeAndCorner(Level * level, Point & size, Point & corner)
426 size.x = size.y = corner.x = corner.y = 0;
428 min.x = min.y = BOARDSIZE;
430 for(int x=0; x<BOARDSIZE; x++)
432 for(int y=0; y<BOARDSIZE; y++)
434 if (level->board[x][y] == 0)
451 size.x = max.x - min.x + 1;
452 size.y = max.y - min.y + 1;
458 void EditorWidget::ResizeGrid(void)
461 range.x = clientArea.width() / GRIDSIZE;
462 range.y = clientArea.height() / GRIDSIZE;
466 // Set some room for the board title (7.5%)
467 float titleHeight = s.height() * 0.075;
469 // Find the constraints
470 float boxSizeX = s.width() / gameBoard->width;
471 float boxSizeY = (s.height() - titleHeight) / gameBoard->height;
473 maxLength = (int)(boxSizeX > boxSizeY ? boxSizeY : boxSizeX);
475 offsetX = (s.width() - (maxLength * gameBoard->width)) / 2;
476 offsetY = ((s.height() - titleHeight) - (maxLength * gameBoard->height)) / 2;
477 titleBox.setRect(0, offsetY, s.width(), titleHeight);
478 offsetY += titleHeight; // Add in the title's height
484 // Halt processing for 'count' milliseconds
486 void EditorWidget::Pause(int count)
488 // DWORD endCount = GetTickCount() + count;
489 // while (GetTickCount() < endCount) {} // Still crude, but better control
491 usleep(count * 1000);
493 // This causes it to lock up randomly. :-/
497 while (time.msec() < count)