/*************************************************************************** * Copyright (C) 2009 by Stefan Bühler * * stbuehler@web.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "testgame.h" #include #include #include #include namespace toruschess { PieceLibrary::PieceLibrary(QObject *parent) : QObject(parent), m_pieces(0), m_buffers(0) { setSize(45, 45); } PieceLibrary::~PieceLibrary() { delete [] m_pieces; delete [] m_buffers; } void PieceLibrary::setSize(int width, int height) { m_width = qMax(10, width); m_height = qMax(10, height); if (m_buffers) { for (int i = 0; i < 12; i++) { delete m_buffers[i]; } } else { m_pieces = new QSvgRenderer* [12]; m_buffers = new QImage* [12]; for (int i = 0; i < 12; i++) { m_pieces[i] = new QSvgRenderer(QString(":/media/chess_%1.svg").arg(i+1), this); } } for (int i = 0; i < 12; i++) { m_buffers[i] = new QImage(QSize(m_width, m_height), QImage::Format_ARGB32_Premultiplied); m_buffers[i]->fill(0x0); QPainter p(m_buffers[i]); m_pieces[i]->render(&p); } } void PieceLibrary::paint(QPainter &pt, int piece) const { if (piece == 0) return; if (piece < 0) piece = 6 - piece; if (piece <= 0 || piece > 12) return; pt.drawImage(0, 0, *m_buffers[piece-1]); } TestPlace::TestPlace(QWidget *parent, const PieceLibrary *lib, const Field *field, const Pos &p) : QWidget(parent), m_field(field), m_lib(lib), m_pos(p), m_mark(false) { setMinimumSize(m_lib->minPieceSize()); } QSize TestPlace::sizeHint() const { return m_lib->pieceSize(); } void TestPlace::paintEvent(QPaintEvent *) { QPainter pt(this); int pl = m_field->place(m_pos); if ((m_pos.x() + m_pos.y()) % 2) { // #D18B47 pt.fillRect(rect(), QBrush(QColor(0xD1, 0x8B, 0x47))); } else { // #FFCE9E pt.fillRect(rect(), QBrush(QColor(0xFF, 0xCE, 0x9E))); } m_lib->paint(pt, pl); if (m_mark) { int r = qMax(1, (rect().width() + rect().height()) / 36); pt.setBrush(pl != 0 ? Qt::green : Qt::white); pt.setPen(Qt::red); pt.drawEllipse(QPoint(rect().width() / 2, rect().height() / 2), r, r); } } TestField::TestField(QWidget *parent, PieceLibrary *lib, Game *game) : QWidget(parent), m_lib(lib), m_game(game) { QGridLayout *layout = new QGridLayout(); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { m_places[x][y] = new TestPlace(this, m_lib, m_game->field(), Pos(x, y)); layout->addWidget(m_places[x][y], y, x); } } layout->setSpacing(0); setLayout(layout); show(); } void TestField::markMoves(const QList &moves) { foreach(Move m, m_marked) m_places[m.to().x()][m.to().y()]->hideMark(); m_marked = moves; foreach(Move m, m_marked) m_places[m.to().x()][m.to().y()]->showMark(); update(); } void TestField::resizeEvent(QResizeEvent *event) { m_lib->setSize(event->size().width()/8, event->size().height()/8); } void TestField::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { QList moves; int x = (event->x() * 8) / width(); int y = (event->y() * 8) / height(); if (x < 0 || x >= 8 || y < 0 || y >= 8) { markMoves(moves); } else { moves = m_game->field()->validMoves(Pos(x, y)); markMoves(moves); } } } void TestField::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { if (m_marked.size() == 0) return; Pos from = m_marked[0].from(); QList moves; markMoves(QList()); int x = (event->x() * 8) / width(); int y = (event->y() * 8) / height(); if (x < 0 || x >= 8 || y < 0 || y >= 8) { return; } Move m(m_game->field(), from, Pos(x, y)); m_game->move(m); } } TestGame::TestGame(QWidget *parent) : QMainWindow(parent), m_game(new Game()) { setWindowTitle("Torus Chess"); PieceLibrary *lib = new PieceLibrary(this); setCentralWidget(new TestField(this, lib, m_game)); } }