X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Feditorwidget.cpp;fp=src%2Feditorwidget.cpp;h=0c6ab283239781c0c5f3966281a3326c179cdcae;hb=058aca46f433b3a36a341ca7272c39d2bef55c09;hp=0000000000000000000000000000000000000000;hpb=cee848899590199081c8ade4736662a8a5dc4a2f;p=warehouse-man-deluxe diff --git a/src/editorwidget.cpp b/src/editorwidget.cpp new file mode 100644 index 0000000..0c6ab28 --- /dev/null +++ b/src/editorwidget.cpp @@ -0,0 +1,501 @@ +// +// editorwidget.cpp: Game editor window widget +// +// by James Hammons +// (C) 2014 Underground Software +// +// JLH = James Hammons +// +// Who When What +// --- ---------- ------------------------------------------------------------ +// JLH 07/01/2014 Created this file +// + +#include "editorwidget.h" +#include // for usleep() +#include +#include "boards.h" +#include "gameboard.h" + + +#define GRIDSIZE 40 + + +EditorWidget::EditorWidget(QWidget * parent/*= 0*/): QWidget(parent), + currentLevel(0) +{ +// memset(¤tLevel, 0, sizeof(currentLevel)); +// currentLevel.corner.x = -1; +// currentLevel.corner.y = -1; +// levelStorage.push_back(currentLevel); +// Level * b1 = &level[0]; +// memset(board, 0, sizeof(board)); +// CreateBackground(); +// score->setTextFormat(Qt::PlainText); + + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + setFocusPolicy(Qt::StrongFocus); // Without this, it gets no keys + Load(); +} + + +// This never gets called...! +EditorWidget::~EditorWidget(void) +{ + Save(); +} + + +QSize EditorWidget::sizeHint() const +{ + return QSize(400, 400); +} + + +void EditorWidget::paintEvent(QPaintEvent * /*event*/) +{ + QPainter painter(this); + QFont font; + Level & level = levelStorage[currentLevel]; + + for(int y=0; y<=clientArea.height()/GRIDSIZE; y++) + { + for(int x=0; x<=clientArea.width()/GRIDSIZE; x++) + { + Point current(level.corner.x + x, level.corner.y + y); + + if ((current.x < 0) && (current.y < 0)) + { + painter.setBrush(Qt::black); + painter.drawRect(x + (GRIDSIZE / 2), y + (GRIDSIZE / 2), GRIDSIZE / 2, GRIDSIZE / 2); + } + else if (current.x < 0) + { + painter.setBrush(Qt::black); + painter.drawRect(x + (GRIDSIZE / 2), y * GRIDSIZE, GRIDSIZE / 2, GRIDSIZE); + } + else if (current.y < 0) + { + painter.setBrush(Qt::black); + painter.drawRect(x * GRIDSIZE, y + (GRIDSIZE / 2), GRIDSIZE, GRIDSIZE / 2); + } + else + { + uint8_t tile = level.board[current.x][current.y]; + + painter.setBrush(Qt::transparent); + painter.setPen(Qt::black); + painter.drawRect(x * GRIDSIZE, y * GRIDSIZE, GRIDSIZE, GRIDSIZE); + + if (tile & (GTWall | GTSpace | GTBox)) + { + if (tile & GTWall) + { + painter.setBrush(Qt::white); + painter.drawRect(x * GRIDSIZE, y * GRIDSIZE, GRIDSIZE, GRIDSIZE); + } + else if (tile & GTBox) + { + painter.setBrush(Qt::red); + painter.drawRect((x * GRIDSIZE) + (GRIDSIZE * 0.1), + (y * GRIDSIZE) + (GRIDSIZE * 0.1), GRIDSIZE * 0.8, GRIDSIZE * 0.8); + } + } + else if (tile & GTNull) + { + painter.setBrush(Qt::black); +// painter.setPen(Qt::black); + painter.drawRect(x * GRIDSIZE, y * GRIDSIZE, GRIDSIZE, GRIDSIZE); + } + else if (tile & GTMan) + { + painter.setBrush(Qt::yellow); + painter.drawEllipse((x * GRIDSIZE) + (GRIDSIZE * 0.1), + (y * GRIDSIZE) + (GRIDSIZE * 0.1), GRIDSIZE * 0.8, GRIDSIZE * 0.8); + } + + if ((tile & GTBoxSpot) && !(tile & (GTWall | GTNull))) + { + painter.setBrush(Qt::magenta); + painter.drawRect((x * GRIDSIZE) + (GRIDSIZE / 3), + (y * GRIDSIZE) + (GRIDSIZE / 3), GRIDSIZE / 3, GRIDSIZE / 3); + } + } + } + } + + int x = level.cursor.x - level.corner.x; + int y = level.cursor.y - level.corner.y; + + painter.setBrush(Qt::transparent); + painter.setPen(Qt::darkGreen); + painter.drawEllipse((x * GRIDSIZE) + 4, (y * GRIDSIZE) + 4, GRIDSIZE - 8, GRIDSIZE - 8); +} + + +void EditorWidget::mousePressEvent(QMouseEvent * event) +{ + if (event->button() == Qt::LeftButton) + { + event->accept(); + } +} + + +void EditorWidget::mouseMoveEvent(QMouseEvent * event) +{ + if (event->buttons() & Qt::LeftButton) + { + event->accept(); + } +} + + +void EditorWidget::mouseReleaseEvent(QMouseEvent * event) +{ + if (event->button() == Qt::LeftButton) + { + event->accept(); + } +} + + +void EditorWidget::mouseDoubleClickEvent(QMouseEvent * event) +{ + if (event->button() == Qt::LeftButton) + { + event->accept(); + } +} + + +void EditorWidget::keyPressEvent(QKeyEvent * event) +{ + int key = event->key(); + Level & level = levelStorage[currentLevel]; + Point & cursor = level.cursor; + Point & corner = level.corner; + + if (key == Qt::Key_Up) + { + if (cursor.y > 0) + cursor.y--; + + if (((cursor.y - corner.y) == 0) && (corner.y > -1)) + corner.y--; + } + else if (key == Qt::Key_Down) + { + if (cursor.y < (BOARDSIZE - 1)) + cursor.y++; + + if (cursor.y >= (range.y + corner.y - 1)) + { + if (corner.y < (BOARDSIZE - range.y)) + corner.y++; + } + } + else if (key == Qt::Key_Left) + { + if (cursor.x > 0) + cursor.x--; + + if (((cursor.x - corner.x) == 0) && (corner.x > -1)) + corner.x--; + } + else if (key == Qt::Key_Right) + { + if (cursor.x < (BOARDSIZE - 1)) + cursor.x++; + + if (cursor.x >= (range.x + corner.x - 1)) + { + if (corner.x < (BOARDSIZE - range.x)) + corner.x++; + } + } + else if (key == Qt::Key_Period) + { + level.board[cursor.x][cursor.y] ^= GTBoxSpot; + } + else if (key == Qt::Key_B) + { + level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTBox); + level.board[cursor.x][cursor.y] |= GTBox; + } + else if (key == Qt::Key_W) + { + level.board[cursor.x][cursor.y] = GTWall; + } + else if (key == Qt::Key_Space) + { + level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTBox); + level.board[cursor.x][cursor.y] |= GTSpace; + } + else if (key == Qt::Key_O) + { + // There can be only one! + for(int x=0; xsize(); + +//printf("Size of window is: %i x %i\n", s.width(), s.height()); +//printf("Size of game grid is: %i x %i\n", gameBoard->width, gameBoard->height); + ResizeGrid(); +} + + +void EditorWidget::CreateBackground(void) +{ +#if 0 + char BGRes[27][64] = { + ":/res/grfttile.bmp", + ":/res/cloth_6.bmp", + ":/res/bg_tech_3.bmp", + ":/res/bg_tech_2.bmp", + ":/res/bg_tech_1.bmp", + ":/res/bg_weave_3.bmp", + ":/res/bg_weave_2.bmp", + ":/res/bg_clouds_2.bmp", + ":/res/bg_floor_plate.bmp", + ":/res/bg_marble_b.bmp", + ":/res/bg_marble_g.bmp", + ":/res/bg_marble_p.bmp", + ":/res/bg_marble_r.bmp", + ":/res/bg_marble_rb.bmp", + ":/res/bg_money_1.bmp", + ":/res/bg_pinstripe2.bmp", + ":/res/bg_pinstripe7.bmp", + ":/res/bg_raindrops_large.bmp", + ":/res/bg_raindrops_small.bmp", + ":/res/bg_stucco.bmp", + ":/res/bg_wood_w.bmp", + ":/res/bg_wood_b1.bmp", + ":/res/bg_wood_d.bmp", + ":/res/bg_wood_f.bmp", + ":/res/bg_wood_mh.bmp", + ":/res/bg_wood_mv.bmp", + ":/res/bg_wood_ro.bmp" + }; + + QPalette pal = palette(); + pal.setBrush(backgroundRole(), QBrush(QPixmap(BGRes[m_nBackground]))); + setAutoFillBackground(true); + setPalette(pal); + + return true; // Ignore errors for now... +#else +// QPalette pal = palette(); +// pal.setBrush(backgroundRole(), QBrush(QPixmap(":/bg_marble_g.bmp"))); +// setAutoFillBackground(true); +// setPalette(pal); +#endif +} + + +void EditorWidget::ClearLevel(void) +{ +// gameBoard->ResetGame(); +// memset(board, 0, sizeof(board)); + update(); +} + + +bool EditorWidget::Load(void) +{ + FILE * f = fopen("./wmd-level.data", "rb"); + + if (f) + { + int numberOfLevels; + fscanf(f, "%i\n", &numberOfLevels); +//printf("Load: Looking for %i levels...\n", numberOfLevels); + + for(int i=0; iboard[x][y] == 0) + continue; + + if (min.x > x) + min.x = x; + + if (min.y > y) + min.y = y; + + if (max.x < x) + max.x = x; + + if (max.y < y) + max.y = y; + } + } + + size.x = max.x - min.x + 1; + size.y = max.y - min.y + 1; + corner.x = min.x; + corner.y = min.y; +} + + +void EditorWidget::ResizeGrid(void) +{ + clientArea = size(); + range.x = clientArea.width() / GRIDSIZE; + range.y = clientArea.height() / GRIDSIZE; +#if 0 + QSize s = size(); + + // Set some room for the board title (7.5%) + float titleHeight = s.height() * 0.075; + + // Find the constraints + float boxSizeX = s.width() / gameBoard->width; + float boxSizeY = (s.height() - titleHeight) / gameBoard->height; + + maxLength = (int)(boxSizeX > boxSizeY ? boxSizeY : boxSizeX); + + offsetX = (s.width() - (maxLength * gameBoard->width)) / 2; + offsetY = ((s.height() - titleHeight) - (maxLength * gameBoard->height)) / 2; + titleBox.setRect(0, offsetY, s.width(), titleHeight); + offsetY += titleHeight; // Add in the title's height +#endif +} + + +// +// Halt processing for 'count' milliseconds +// +void EditorWidget::Pause(int count) +{ +// DWORD endCount = GetTickCount() + count; +// while (GetTickCount() < endCount) {} // Still crude, but better control +#if 1 + usleep(count * 1000); +#else + // This causes it to lock up randomly. :-/ + QTime time; + time.start(); + + while (time.msec() < count) + ; // Do nothing... +#endif +} +