Skip to content

Commit

Permalink
feats: add skins and draw indication. 🎨
Browse files Browse the repository at this point in the history
  • Loading branch information
Joker2770 committed Apr 5, 2023
1 parent 878e2f2 commit f5c45fb
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 23 deletions.
11 changes: 11 additions & 0 deletions res.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,16 @@
<qresource prefix="/">
<file>skins/HGarden2.bmp</file>
<file>icon/icon.jpg</file>
<file>skins/bold.bmp</file>
<file>skins/fontanGomo.bmp</file>
<file>skins/gems.bmp</file>
<file>skins/gomoku.bmp</file>
<file>skins/HGarden.bmp</file>
<file>skins/light.bmp</file>
<file>skins/pisq.bmp</file>
<file>skins/rain.bmp</file>
<file>skins/star.bmp</file>
<file>skins/wood.bmp</file>
<file>skins/yellow.bmp</file>
</qresource>
</RCC>
Binary file modified skins/HGarden.bmp
Binary file not shown.
Binary file modified skins/HGarden2.bmp
Binary file not shown.
Binary file modified skins/bold.bmp
Binary file not shown.
Binary file modified skins/fontanGomo.bmp
Binary file not shown.
Binary file modified skins/gems.bmp
Binary file not shown.
Binary file modified skins/gomoku.bmp
Binary file not shown.
Binary file modified skins/light.bmp
Binary file not shown.
Binary file modified skins/pisq.bmp
Binary file not shown.
Binary file modified skins/rain.bmp
Binary file not shown.
Binary file modified skins/star.bmp
Binary file not shown.
Binary file modified skins/wood.bmp
Binary file not shown.
Binary file modified skins/yellow.bmp
Binary file not shown.
155 changes: 133 additions & 22 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ MainWindow::MainWindow(QWidget *parent)
this->pActionTimeoutMatch = new QAction("Match Timeout", this);
this->pActionTimeoutTurn = new QAction("Turn Timeout", this);
this->pActionMaxMemory = new QAction("Max Memory", this);
this->pActionSkin = new QAction("Skin", this);
this->pActionStart = new QAction("Start", this);
this->pActionPause = new QAction("Pause", this);
this->pActionContinue = new QAction("Continue", this);
Expand All @@ -65,6 +66,7 @@ MainWindow::MainWindow(QWidget *parent)
this->pActionTimeoutMatch->setShortcut(QKeySequence(Qt::Key_M));
this->pActionTimeoutTurn->setShortcut(QKeySequence(Qt::Key_T));
this->pActionMaxMemory->setShortcut(QKeySequence(Qt::Key_O));
this->pActionSkin->setShortcut(QKeySequence(Qt::Key_K));
this->pActionFreeStyleGomoku->setShortcut(QKeySequence(Qt::Key_F));
this->pActionStandardGomoku->setShortcut(QKeySequence(Qt::Key_S));
this->pActionRenju->setShortcut(QKeySequence(Qt::Key_R));
Expand All @@ -73,6 +75,7 @@ MainWindow::MainWindow(QWidget *parent)
this->pMenuSetting->addAction(this->pActionTimeoutMatch);
this->pMenuSetting->addAction(this->pActionTimeoutTurn);
this->pMenuSetting->addAction(this->pActionMaxMemory);
this->pMenuSetting->addAction(this->pActionSkin);
this->pMenuGame->addAction(this->pActionStart);
this->pMenuGame->addAction(this->pActionPause);
this->pMenuGame->addAction(this->pActionContinue);
Expand Down Expand Up @@ -112,10 +115,18 @@ MainWindow::MainWindow(QWidget *parent)
this->m_p1_name.clear();
this->m_p2_name.clear();

this->resize(this->mBoard->getBSize().first * RECT_WIDTH, (this->mBoard->getBSize().second + 1) * RECT_HEIGHT + this->pMenuBar->height());
this->resize((this->mBoard->getBSize().first + 2) * RECT_WIDTH, (this->mBoard->getBSize().second + 3) * RECT_HEIGHT + this->pMenuBar->height());
this->setWindowFlags(this->windowFlags()&~Qt::WindowMaximizeButtonHint);

this->m_bBoard = false;
this->m_bSkin = true;

QPixmap pm;
pm.load(":/skins/HGarden2.bmp");
for (size_t i = 0; i < 5; i++)
{
this->m_images.push_back(pm.copy(i * 20, 0, 20, 20).scaled(RECT_WIDTH, RECT_HEIGHT));
}

this->m_freeStyleGomoku = new FreeStyleGomoku();
this->m_standardGomoku = new StandardGomoku();
Expand Down Expand Up @@ -146,6 +157,7 @@ MainWindow::MainWindow(QWidget *parent)
connect(this->pActionTimeoutMatch, SIGNAL(triggered()), this, SLOT(OnActionTimeoutMatch()));
connect(this->pActionTimeoutTurn, SIGNAL(triggered()), this, SLOT(OnActionTimeoutTurn()));
connect(this->pActionMaxMemory, SIGNAL(triggered()), this, SLOT(OnActionMaxMemory()));
connect(this->pActionSkin, SIGNAL(triggered()), this, SLOT(OnActionSkin()));
connect(this->pRuleActionGroup,SIGNAL(triggered(QAction *)),this,SLOT(On_ClickedRuleActionGroup(QAction*)));
connect(this->pActionPlayerSetting, SIGNAL(triggered()), this, SLOT(OnActionPlayerSetting()));
connect(this->pActionVer, SIGNAL(triggered()), this, SLOT(OnActionVer()));
Expand Down Expand Up @@ -240,6 +252,11 @@ MainWindow::~MainWindow()
delete this->pActionMaxMemory;
this->pActionMaxMemory = nullptr;
}
if (nullptr != this->pActionSkin)
{
delete this->pActionSkin;
this->pActionSkin = nullptr;
}
if (nullptr != this->pActionFreeStyleGomoku)
{
delete this->pActionFreeStyleGomoku;
Expand Down Expand Up @@ -315,6 +332,7 @@ void MainWindow::paintEvent(QPaintEvent *e)
DrawPlayerState();
DrawPlayerName();
DrawChessboard();
DrawIndication();
DrawItems();
DrawMark();

Expand All @@ -326,23 +344,20 @@ void MainWindow::DrawChessboard()
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setBrush(Qt::darkYellow);
painter.setPen(QPen(QColor(Qt::black),2));
painter.setPen(QPen(QColor(Qt::black), 2));

for(int i = 0; i < this->mBoard->getBSize().first - 1; ++i)
for (int i = 0; i < this->mBoard->getBSize().first; ++i)
{
for (int j = 1; j < this->mBoard->getBSize().second; ++j)
painter.drawRect( (i+0.5)*RECT_WIDTH,(j+0.5)*RECT_HEIGHT,RECT_WIDTH,RECT_HEIGHT);
for (int j = 0; j < this->mBoard->getBSize().second; ++j)
{
if (this->m_bSkin && !this->m_images.at(0).isNull())
painter.drawPixmap((i + 1) * RECT_WIDTH, (j + 2) * RECT_HEIGHT, RECT_WIDTH, RECT_HEIGHT, this->m_images.at(0));
else
painter.drawRect((i + 1) * RECT_WIDTH, (j + 2) * RECT_HEIGHT, RECT_WIDTH, RECT_HEIGHT);
}
}
}

void MainWindow::DrawChessAtPoint(QPainter &painter, QPoint &pt)
{
// painter.drawRect( (pt.x()+0.5)*RECT_WIDTH,(pt.y()+0.5)*RECT_HEIGHT,RECT_WIDTH,RECT_HEIGHT);

QPoint ptCenter((pt.x() + 0.5) * RECT_WIDTH, (pt.y() + 0.5) * RECT_HEIGHT);
painter.drawEllipse(ptCenter, (int)(RECT_WIDTH * 0.5), (int)(RECT_HEIGHT * 0.5));
}

void MainWindow::DrawMark()
{
if (this->mBoard->getVRecord().size() > 0)
Expand All @@ -351,11 +366,22 @@ void MainWindow::DrawMark()
QPoint pMark;
painter.setBrush(Qt::red);
pair<int, int> last_move_pos = this->mBoard->coord2idx(this->mBoard->getVRecord().back().first);
pMark.setX(last_move_pos.first);
pMark.setY(last_move_pos.second + 1);
pMark.setX(last_move_pos.first + 1);
pMark.setY(last_move_pos.second + 2);

QPoint ptCenter((pMark.x() + 0.5) * RECT_WIDTH, (pMark.y() + 0.5) * RECT_HEIGHT);
painter.drawEllipse(ptCenter, (int)(RECT_WIDTH * 0.25), (int)(RECT_HEIGHT * 0.25));

int idx = 3;
if (this->mBoard->getVRecord().back().second == STONECOLOR::BLACK)
idx = 3;
else
idx = 4;


if (this->m_bSkin && !this->m_images.at(idx).isNull())
painter.drawPixmap(pMark.x() * RECT_WIDTH, pMark.y() * RECT_HEIGHT, RECT_WIDTH, RECT_HEIGHT, this->m_images.at(idx));
else
painter.drawEllipse(ptCenter, (int)(RECT_WIDTH * 0.25), (int)(RECT_HEIGHT * 0.25));
}
}

Expand All @@ -367,17 +393,26 @@ void MainWindow::DrawItems()
for (size_t i = 0; i < this->mBoard->getVRecord().size(); i++)
{
QPoint p;
p.setX(this->mBoard->coord2idx(this->mBoard->getVRecord().at(i).first).first + 1);
p.setY(this->mBoard->coord2idx(this->mBoard->getVRecord().at(i).first).second + 2);
QPoint ptCenter((p.x() + 0.5) * RECT_WIDTH, (p.y() + 0.5) * RECT_HEIGHT);

int idx = 1;
if (this->mBoard->getVRecord().at(i).second == STONECOLOR::BLACK)
{
painter.setBrush(Qt::black);
idx = 1;
}
else
{
painter.setBrush(Qt::white);
idx = 2;
}
p.setX(this->mBoard->coord2idx(this->mBoard->getVRecord().at(i).first).first);
p.setY(this->mBoard->coord2idx(this->mBoard->getVRecord().at(i).first).second + 1);
DrawChessAtPoint(painter, p);

if (this->m_bSkin && !this->m_images.at(idx).isNull())
painter.drawPixmap(p.x() * RECT_WIDTH, p.y() * RECT_HEIGHT, RECT_WIDTH, RECT_HEIGHT, this->m_images.at(idx));
else
painter.drawEllipse(ptCenter, (int)(RECT_WIDTH * 0.5), (int)(RECT_HEIGHT * 0.5));
}
}

Expand Down Expand Up @@ -511,6 +546,27 @@ void MainWindow::DrawPlayerName()
painter.drawText(this->geometry().width() - 200, this->geometry().height() - 30, 150, 50, Qt::AlignRight, "Human");
}

void MainWindow::DrawIndication()
{
if (nullptr != this->mBoard)
{
QFont font;
font.setPixelSize(10);
font.setItalic(true);
// font.setBold(true);

QPainter painter(this);
painter.setFont(font);
painter.setPen(QPen(QColor(Qt::black), 2));

for (size_t i = 0; i < this->mBoard->getBSize().first; i++)
painter.drawText((i + 1) * RECT_WIDTH + 5, 60, 20, 20, Qt::AlignRight, QString::fromStdString(to_string(i)));

for (size_t i = 0; i < this->mBoard->getBSize().second; i++)
painter.drawText(15, (i + 2) * RECT_HEIGHT + 10, 20, 20, Qt::AlignBottom, QString::fromStdString(to_string(i)));
}
}

vector<pair<pair<int,int>, int>> MainWindow::record_expend(const vector<pair<int, int>> vRecord)
{
vector<pair<pair<int,int>, int>> vRecExpendTmp;
Expand Down Expand Up @@ -542,10 +598,10 @@ void MainWindow::mousePressEvent(QMouseEvent * e)
pt.setX( (e->pos().x() ) / RECT_WIDTH);
pt.setY( (e->pos().y() ) / RECT_HEIGHT);

if (pt.y() < 1)
if (pt.y() < 2 || pt.x() < 1)
return;

pair<int, int> p_idx(pt.x(), pt.y() - 1);
pair<int, int> p_idx(pt.x() - 1, pt.y() - 2);

if (this->mBoard->GetState() != BOARDSTATUS::BOARDFULL)
{
Expand Down Expand Up @@ -1088,7 +1144,7 @@ void MainWindow::OnActionBoardSize()
int iTmp = i_get;
pair<int, int> pTmp(iTmp, iTmp);
if (this->mBoard->setBSize(pTmp))
resize(this->mBoard->getBSize().first * RECT_WIDTH, (this->mBoard->getBSize().second + 1) * RECT_HEIGHT + this->pMenuBar->height());
resize((this->mBoard->getBSize().first + 2) * RECT_WIDTH, (this->mBoard->getBSize().second + 3) * RECT_HEIGHT + this->pMenuBar->height());

this->mBoard->Notify();

Expand Down Expand Up @@ -1161,6 +1217,61 @@ void MainWindow::OnActionMaxMemory()
}
}

void MainWindow::OnActionSkin()
{
if (this->mState != GAME_STATE::PLAYING)
{
bool ok = false;
QStringList s_items;
s_items << "none" << "bold" << "fontanGomo" << "gems" << "gomoku" << "HGarden" << "HGarden2" << "light" << "pisq" << "rain" << "star" << "wood" << "yellow";
QString s_get = QInputDialog::getItem(this, "Skin", "Please choose skin:", s_items, 0, false,
&ok, Qt::MSWindowsFixedSizeDialogHint);
if (ok)
{
if (QString::compare(s_get, "none") == 0)
this->m_bSkin = false;
else
{
QPixmap pm;
this->m_images.clear();

if (QString::compare(s_get, "bold") == 0)
pm.load(":/skins/bold.bmp");
else if (QString::compare(s_get, "fontanGomo") == 0)
pm.load(":/skins/fontanGomo.bmp");
else if (QString::compare(s_get, "gems") == 0)
pm.load(":/skins/gems.bmp");
else if (QString::compare(s_get, "gomoku") == 0)
pm.load(":/skins/gomoku.bmp");
else if (QString::compare(s_get, "HGarden") == 0)
pm.load(":/skins/HGarden.bmp");
else if (QString::compare(s_get, "HGarden2") == 0)
pm.load(":/skins/HGarden2.bmp");
else if (QString::compare(s_get, "light") == 0)
pm.load(":/skins/light.bmp");
else if (QString::compare(s_get, "pisq") == 0)
pm.load(":/skins/pisq.bmp");
else if (QString::compare(s_get, "rain") == 0)
pm.load(":/skins/rain.bmp");
else if (QString::compare(s_get, "star") == 0)
pm.load(":/skins/star.bmp");
else if (QString::compare(s_get, "wood") == 0)
pm.load(":/skins/wood.bmp");
else if (QString::compare(s_get, "yellow") == 0)
pm.load(":/skins/yellow.bmp");

if (!pm.isNull())
{
for (size_t i = 0; i < 5; i++)
this->m_images.push_back(pm.copy(i * 20, 0, 20, 20).scaled(RECT_WIDTH, RECT_HEIGHT));

this->m_bSkin = true;
}
}
}
}
}

void MainWindow::On_ClickedRuleActionGroup(QAction *pAction)
{
if (this->mState == GAME_STATE::OVER || this->mState == GAME_STATE::IDLE)
Expand Down
7 changes: 6 additions & 1 deletion src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <QActionGroup>
#include <QDialog>
#include <QSharedPointer>
#include <QPixmap>

#include "board.h"
#include "FreeStyleGomoku.h"
Expand Down Expand Up @@ -74,6 +75,7 @@ class MainWindow : public QMainWindow
QAction *pActionTimeoutMatch;
QAction *pActionTimeoutTurn;
QAction *pActionMaxMemory;
QAction *pActionSkin;
QAction *pActionStart;
QAction *pActionPause;
QAction *pActionContinue;
Expand All @@ -100,6 +102,7 @@ public slots:
void OnActionTimeoutMatch();
void OnActionTimeoutTurn();
void OnActionMaxMemory();
void OnActionSkin();
void On_ClickedRuleActionGroup(QAction *);
void OnActionPlayerSetting();
void OnActionVer();
Expand Down Expand Up @@ -128,7 +131,7 @@ public slots:
void DrawTimeLeft();
void DrawPlayerState();
void DrawPlayerName();
void DrawChessAtPoint(QPainter& painter,QPoint& pt);
void DrawIndication();

vector<pair<pair<int, int>, int>> record_expend(const vector<pair<int, int>> vRecord);

Expand All @@ -140,6 +143,7 @@ public slots:
Renju *m_renju;
Timer *m_T1;
Timer *m_T2;
vector<QPixmap> m_images;
QString m_p1_name;
QString m_p2_name;
long long m_timeout_match;
Expand All @@ -150,6 +154,7 @@ public slots:
GAME_STATE mState;
GAME_RULE mRule;
bool m_bBoard;
bool m_bSkin;
bool m_bOK_P1;
bool m_bOK_P2;
};
Expand Down

0 comments on commit f5c45fb

Please sign in to comment.