您最多能選擇 25 個主題
主題必須以字母或數字為開頭,可包含連接號「-」且最長為 35 個字元。
109 行
3.0 KiB
109 行
3.0 KiB
/* VierGewinnt - A simple 4-in-a-row network game
|
|
*
|
|
* Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
|
|
* http://www.seba-geek.de
|
|
*
|
|
* This file is part of VierGewinnt.
|
|
*
|
|
* VierGewinnt 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.
|
|
*
|
|
* VierGewinnt 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 VierGewinnt. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "renderarea.h"
|
|
|
|
#include <QPainter>
|
|
|
|
RenderArea::RenderArea(QWidget *parent) : QWidget(parent) {
|
|
QSizePolicy tmppol;
|
|
tmppol.setHorizontalPolicy(QSizePolicy::Maximum);
|
|
tmppol.setVerticalPolicy(QSizePolicy::Maximum);
|
|
setSizePolicy(tmppol);
|
|
|
|
// this->antialiased = true;
|
|
|
|
// pens
|
|
xpen.setColor(Qt::green);
|
|
xpen.setWidth(10);
|
|
xpen.setJoinStyle(Qt::RoundJoin);
|
|
xpen.setCapStyle(Qt::RoundCap);
|
|
|
|
ypen.setColor(Qt::red);
|
|
ypen.setWidth(10);
|
|
|
|
mfact = 0.75;
|
|
feld = 0; // just to clarify, also set in next statement
|
|
setFeldInfo(7, 6, 0);
|
|
}
|
|
|
|
void RenderArea::setFeldInfo(int _x, int _y, VierGewinntWidget::Stein **_feld) {
|
|
fx = _x;
|
|
fy = _y;
|
|
feld = _feld;
|
|
}
|
|
|
|
QSize RenderArea::minimumSizeHint() const {
|
|
return QSize(320, 240);
|
|
}
|
|
|
|
QSize RenderArea::sizeHint() const {
|
|
return QSize(640, 480);
|
|
}
|
|
|
|
void RenderArea::paintEvent(QPaintEvent * /*event*/) {
|
|
QPainter painter(this);
|
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
|
// Start with grid
|
|
int fsize = std::min(width()/fx, height()/fy);
|
|
// painter.drawLine(0, 0, width(), height());
|
|
painter.translate((width()-fsize*fx)/2, (height()-fsize*fy)/2);
|
|
for(int i=0; i<=fx; i++)
|
|
painter.drawLine(i*fsize, 0, i*fsize, fsize*fy);
|
|
for(int i=0; i<=fy; i++)
|
|
painter.drawLine(0, i*fsize, fsize*fx, i*fsize);
|
|
|
|
|
|
if(feld==0)
|
|
return;
|
|
|
|
// paint x and o
|
|
for(int i=0; i<fx; i++) {
|
|
for(int j=0; j<fy; j++) {
|
|
if(feld[i][j]!=VierGewinntWidget::n) {
|
|
painter.save();
|
|
painter.translate(i*fsize+fsize/2, j*fsize+fsize/2);
|
|
if(feld[i][j]==VierGewinntWidget::x) {
|
|
painter.setPen(xpen);
|
|
painter.drawLine(-fsize/2*mfact, -fsize/2*mfact, fsize/2*mfact, fsize/2*mfact);
|
|
painter.drawLine(-fsize/2*mfact, fsize/2*mfact, fsize/2*mfact,-fsize/2*mfact);
|
|
} else {
|
|
painter.setPen(ypen);
|
|
painter.drawEllipse(-fsize/2*mfact, -fsize/2*mfact, fsize*mfact, fsize*mfact);
|
|
}
|
|
painter.restore();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void RenderArea::mouseReleaseEvent(QMouseEvent *event) {
|
|
if(event->button()!=Qt::LeftButton)
|
|
return;
|
|
|
|
int fsize = std::min(width()/fx, height()/fy);
|
|
int myrow = (event->x()-((width()-fsize*fx)/2))/fsize;
|
|
if(myrow<0||myrow>=fx||event->x()-((width()-fsize*fx)/2)<0)
|
|
return;
|
|
|
|
addStein(myrow);
|
|
}
|