| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2009 by The qGo Project * |
| 3 | * * |
| 4 | * This file is part of qGo. * |
| 5 | * * |
| 6 | * qGo is free software: you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/> * |
| 18 | * or write to the Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | /* |
| 24 | * stone.cpp |
| 25 | * This class handles the graphical appearance of the stones on the board |
| 26 | * Is is used only by the 'board' class |
| 27 | */ |
| 28 | |
| 29 | #include "../defines.h" |
| 30 | #include "stone.h" |
| 31 | #include "imagehandler.h" |
| 32 | |
| 33 | #include <QGraphicsScene> |
| 34 | |
| 35 | /* |
| 36 | * Creates the stone as a GraphicsScene item |
| 37 | */ |
| 38 | Stone::Stone(QList<QPixmap> *a, QGraphicsScene *canvas, StoneColor c, int x, int y , bool has_shadow) |
| 39 | : QGraphicsPixmapItem(0), color(c), myX(x), myY(y) |
| 40 | { |
| 41 | canvas->addItem(this); |
| 42 | |
| 43 | pixmapList = a; |
| 44 | |
| 45 | // We use a trick here with the count of the pixmap list |
| 46 | // because the alternate ghosts list has only 2 images |
| 47 | if (pixmapList->count() <= 2) |
| 48 | setPixmap(pixmapList->at( color == stoneBlack ? 0 : 1)); |
| 49 | else |
| 50 | setPixmap(pixmapList->at( color == stoneBlack ? 0 : (rand() % (pixmapList->count() -2) ) + 1)); |
| 51 | |
| 52 | shadow = NULL; |
| 53 | |
| 54 | if (has_shadow) |
| 55 | { |
| 56 | shadow = new QGraphicsPixmapItem(0); |
| 57 | shadow->setPixmap(pixmapList->last()); |
| 58 | shadow->setZValue(4); |
| 59 | canvas->addItem(shadow); |
| 60 | } |
| 61 | |
| 62 | moveNum = new QGraphicsSimpleTextItem("",this); |
| 63 | moveNum->setPen(QPen(( color == stoneBlack ? Qt::white : Qt::black))); |
| 64 | |
| 65 | setZValue(5); |
| 66 | show(); |
| 67 | |
| 68 | dead = false; |
| 69 | seki = false; |
| 70 | checked = false; |
| 71 | |
| 72 | } |
| 73 | |
| 74 | Stone::~Stone() |
| 75 | { |
| 76 | if (shadow) |
| 77 | delete shadow; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Resets the stone image with the color (used when resizing the board) |
| 82 | */ |
| 83 | void Stone::setColor(StoneColor c) |
| 84 | { |
| 85 | if (pixmapList->count() <= 2) |
| 86 | setPixmap(pixmapList->at( c == stoneBlack ? 0 : 1)); |
| 87 | else |
| 88 | setPixmap(pixmapList->at( c == stoneBlack ? 0 : (rand() % (pixmapList->count() -2) ) + 1)); |
| 89 | |
| 90 | color = c; |
| 91 | |
| 92 | if (shadow) |
| 93 | shadow->setPixmap(pixmapList->last()); |
| 94 | |
| 95 | // int w= pixmap().width(); |
| 96 | |
| 97 | // QFont f("",w); |
| 98 | // QString xx = moveNum->text(); |
| 99 | // if (num) |
| 100 | // num->font().setPointSize(12); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * self expl. |
| 105 | */ |
| 106 | void Stone::toggleOneColorGo(bool oneColor) |
| 107 | { |
| 108 | // if we play one color go, the black stones are repainted white, else normal black |
| 109 | if (color == stoneBlack) |
| 110 | setPixmap(pixmapList->at( oneColor ? (rand() % WHITE_STONES_NB) + 1 : 0 )); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Moves the stone to position (in pixels) |
| 115 | */ |
| 116 | void Stone::setPos(double x, double y) |
| 117 | { |
| 118 | |
| 119 | QGraphicsPixmapItem::setPos((qreal)x,(qreal)y); |
| 120 | qreal offset; |
| 121 | if (shadow) { |
| 122 | offset = shadow->boundingRect().height(); |
| 123 | shadow->setPos((qreal)(x-offset / 8 ), (qreal)(y+ offset / 8)); |
| 124 | } |
| 125 | |
| 126 | moveNum->setFont(QFont("",pixmap().width()/3, 1)); |
| 127 | |
| 128 | // qreal h = moveNum->boundingRect().height(); |
| 129 | // qreal w = 0; |
| 130 | qreal h = (boundingRect().height() - moveNum->boundingRect().height())/2 ; |
| 131 | qreal w = (boundingRect().width() - moveNum->boundingRect().width())/2 ; |
| 132 | |
| 133 | moveNum->setPos(w,h); |
| 134 | |
| 135 | } |
| 136 | |
| 137 | void Stone::hide() |
| 138 | { |
| 139 | QGraphicsPixmapItem::hide(); |
| 140 | if (shadow) shadow->hide(); |
| 141 | |
| 142 | } |
| 143 | |
| 144 | void Stone::show() |
| 145 | { |
| 146 | QGraphicsPixmapItem::show(); |
| 147 | if (shadow) shadow->show(); |
| 148 | |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * This allocates a new pixmap list to the stone (used for toggling dead stones when counting) |
| 153 | */ |
| 154 | void Stone::togglePixmap(QList<QPixmap> *a, bool showShadow) |
| 155 | { |
| 156 | //if the list is only B + W , or if the list is B + several white |
| 157 | if (a->count() <= 2) |
| 158 | setPixmap(a->at( color == stoneBlack ? 0 : 1)); |
| 159 | else |
| 160 | setPixmap(a->at( color == stoneBlack ? 0 : (rand() % (a->count() -2) ) + 1)); |
| 161 | |
| 162 | if (shadow) |
| 163 | { |
| 164 | if (showShadow) |
| 165 | shadow->show(); |
| 166 | else |
| 167 | shadow->hide(); |
| 168 | } |
| 169 | } |