| 1 | // version: $Id$ |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2004-2007 Artur Wiebe * |
| 4 | * [email protected] * |
| 5 | * * |
| 6 | * This program 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, write to the * |
| 18 | * Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | #include <QLayout> |
| 22 | #include <QtGui> |
| 23 | #include <QDebug> |
| 24 | |
| 25 | #include "board.h" |
| 26 | #include "common.h" |
| 27 | #include "pdn.h" |
| 28 | #include "echeckers.h" |
| 29 | #include "rcheckers.h" |
| 30 | /* |
| 31 | #include "newgamedlg.h" |
| 32 | |
| 33 | #include "player.h" |
| 34 | #include "humanplayer.h" |
| 35 | #include "computerplayer.h" |
| 36 | */ |
| 37 | |
| 38 | QColor myColorInterpolator(const QColor &start, const QColor &end, qreal progress) { |
| 39 | qreal r = (1 - progress) * start.red() + progress * end.red(); |
| 40 | qreal g = (1 - progress) * start.green() + progress * end.green(); |
| 41 | qreal b = (1 - progress) * start.blue() + progress * end.blue(); |
| 42 | //qDebug("from %s to %s, progress %f", qUtf8Printable(start.name()), qUtf8Printable(end.name()), progress); |
| 43 | return QColor(r, g, b); |
| 44 | } |
| 45 | |
| 46 | myBoard::myBoard(QWidget* parent) |
| 47 | : QFrame(parent) |
| 48 | { |
| 49 | /* |
| 50 | * board & info |
| 51 | */ |
| 52 | setFrameStyle(QFrame::Box|QFrame::Plain); |
| 53 | for(int i=0; i<64; i++) { |
| 54 | m_fields[i] = new Field(this, i); |
| 55 | } |
| 56 | |
| 57 | pixmap_valid = false; |
| 58 | m_src_field = NULL; |
| 59 | m_dst_field = NULL; |
| 60 | animation_steps = 10; |
| 61 | current_step = 0; |
| 62 | animation_timer = startTimer(100); |
| 63 | |
| 64 | // for(int i=0; i<32; i++) { |
| 65 | // connect(m_fields[i], SIGNAL(click(int)), |
| 66 | // this, SIGNAL(fieldClicked(int))); |
| 67 | // } |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | * game init |
| 72 | */ |
| 73 | m_game = 0; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | |
| 78 | myBoard::~myBoard() |
| 79 | { |
| 80 | for (int i = 0; i < 64; i++) { |
| 81 | delete m_fields[i]; |
| 82 | } |
| 83 | if(m_game) { |
| 84 | delete m_game; |
| 85 | } |
| 86 | if (pixmap) { |
| 87 | delete pixmap; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void myBoard::beginSetup() { |
| 92 | for (int i = 0; i < 64; i++) { |
| 93 | m_fields[i]->beginSetup(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void myBoard::endSetup() { |
| 98 | for (int i = 0; i < 64; i++) { |
| 99 | m_fields[i]->endSetup(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void myBoard::paintEvent(QPaintEvent*) |
| 104 | { |
| 105 | QPainter p(this); |
| 106 | |
| 107 | draw(); |
| 108 | Q_ASSERT(pixmap_valid); |
| 109 | p.drawPixmap(0, 0, *pixmap); |
| 110 | |
| 111 | p.end(); |
| 112 | } |
| 113 | |
| 114 | void myBoard::setTheme(const QString& path, bool set_white) |
| 115 | { |
| 116 | int fieldSize = getTargetFieldSize(size()); |
| 117 | m_theme = new Theme(this, path, 0); |
| 118 | if (fieldSize > 3) { |
| 119 | if (m_theme->getIsResizeable()) { |
| 120 | m_theme->setTargetSize(fieldSize); |
| 121 | } |
| 122 | } |
| 123 | for (int i = 0; i < 64; i++) { |
| 124 | m_fields[i]->setTheme(m_theme); |
| 125 | } |
| 126 | |
| 127 | beginSetup(); |
| 128 | |
| 129 | setColorWhite(set_white); |
| 130 | |
| 131 | for(int i=0; i<32; i++) { |
| 132 | m_fields[i]->setPattern(2); |
| 133 | } |
| 134 | for(int i=32; i<64; i++) { |
| 135 | m_fields[i]->setPattern(1); |
| 136 | } |
| 137 | |
| 138 | //setFixedSize(sizeHint()); |
| 139 | if (! m_theme->getIsResizeable()) { |
| 140 | int width = m_theme->getFieldWidth()*8 + 2*frameWidth(); |
| 141 | int height = m_theme->getFieldHeight()*8 + 2*frameWidth(); |
| 142 | setFixedSize(width, height); |
| 143 | } else { |
| 144 | setFixedSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX); |
| 145 | } |
| 146 | |
| 147 | if(m_game) { |
| 148 | fieldsSetup(); |
| 149 | } |
| 150 | |
| 151 | endSetup(); |
| 152 | repaint(); |
| 153 | } |
| 154 | |
| 155 | QSize myBoard::sizeHint() const { |
| 156 | if (! m_theme) { |
| 157 | return QSize(); |
| 158 | } else { |
| 159 | int width = m_theme->getFieldWidth()*8 + 2*frameWidth(); |
| 160 | int height = m_theme->getFieldHeight()*8 + 2*frameWidth(); |
| 161 | return QSize(width, height); |
| 162 | //qInfo("Size is fixed: %d x %d", width, height); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void myBoard::reset() |
| 167 | { |
| 168 | int new_board[32]; |
| 169 | |
| 170 | for(int i=0; i<12; i++) { |
| 171 | new_board[i]=MAN2; |
| 172 | } |
| 173 | for(int i=12; i<20; i++) { |
| 174 | new_board[i]=FREE; |
| 175 | } |
| 176 | for(int i=20; i<32; i++) { |
| 177 | new_board[i]=MAN1; |
| 178 | } |
| 179 | |
| 180 | // reset frames. |
| 181 | for(int i=0; i<32; i++) { |
| 182 | m_fields[i]->showFrame(false); |
| 183 | } |
| 184 | |
| 185 | if(m_game) { |
| 186 | m_game->setup(new_board); |
| 187 | } |
| 188 | |
| 189 | fieldsSetup(); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void myBoard::adjustNotation(bool bottom_is_white) |
| 194 | { |
| 195 | if(!m_game) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | for(int i=0; i<32; i++) { |
| 200 | m_fields[i]->setLabel(m_game->getFieldNotation(i, bottom_is_white)); |
| 201 | } |
| 202 | invalidate(); |
| 203 | repaint(); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | void myBoard::fieldsSetup() |
| 208 | { |
| 209 | for(int i=0; i<32; i++) { |
| 210 | m_fields[i]->set(m_game->item(i), bottom_is_white); |
| 211 | } |
| 212 | invalidate(); |
| 213 | } |
| 214 | |
| 215 | void myBoard::invalidate() { |
| 216 | pixmap_valid = false; |
| 217 | for (int i = 0; i < 64; i++) { |
| 218 | m_fields[i]->invalidate(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void myBoard::drawField(QPainter& painter, Field* field, int row, int column) { |
| 223 | int width = size().width(); |
| 224 | int height = size().height(); |
| 225 | |
| 226 | int rowHeight = height / 8; |
| 227 | int colWidth = width / 8; |
| 228 | int size = rowHeight < colWidth ? rowHeight : colWidth; |
| 229 | |
| 230 | QRect rect(column * size, row * size, size, size); |
| 231 | field->draw(painter, rect); |
| 232 | } |
| 233 | |
| 234 | void myBoard::draw() { |
| 235 | if (pixmap_valid) { |
| 236 | return; |
| 237 | } |
| 238 | QPixmap* prev = pixmap; |
| 239 | pixmap = new QPixmap(size()); |
| 240 | pixmap->fill(Qt::white); |
| 241 | QPainter painter(pixmap); |
| 242 | for(int i=0; i<4; i++) { |
| 243 | for(int k=0; k<4; k++) { |
| 244 | drawField(painter, m_fields[i*8+k+32], i*2, k*2 ); |
| 245 | drawField(painter, m_fields[i*8+k ], i*2, k*2+1); |
| 246 | drawField(painter, m_fields[i*8+k+4 ], i*2+1,k*2 ); |
| 247 | drawField(painter, m_fields[i*8+k+36], i*2+1,k*2+1); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if (animation_in_progress && m_src_field && m_dst_field) { |
| 252 | painter.setPen(QPen(m_src_color, 5)); |
| 253 | painter.drawRect(m_src_field->rect()); |
| 254 | painter.setPen(Qt::black); |
| 255 | |
| 256 | painter.setPen(QPen(m_dst_color, 5)); |
| 257 | painter.drawRect(m_dst_field->rect()); |
| 258 | painter.setPen(Qt::black); |
| 259 | } |
| 260 | painter.end(); |
| 261 | update(); |
| 262 | pixmap_valid = true; |
| 263 | delete prev; |
| 264 | } |
| 265 | |
| 266 | void myBoard::setColorWhite(bool b) |
| 267 | { |
| 268 | bottom_is_white = b; |
| 269 | } |
| 270 | |
| 271 | void myBoard::setNotation(bool s, bool above) |
| 272 | { |
| 273 | for(int i=0; i<32; i++) { |
| 274 | m_fields[i]->showLabel(s, above); |
| 275 | } |
| 276 | invalidate(); |
| 277 | repaint(); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | void myBoard::do_move(const QString& move) |
| 282 | { |
| 283 | qDebug() << __PRETTY_FUNCTION__; |
| 284 | if(!m_current->isHuman()) { |
| 285 | add_log(myBoard::Warning, tr("It's not your turn.")); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | int from_num, to_num; |
| 290 | if(extract_move(move, &from_num, &to_num)) { |
| 291 | slot_click(from_num); |
| 292 | slot_click(to_num); |
| 293 | } else |
| 294 | add_log(myBoard::Warning, tr("Syntax error. Usage: /from-to")); |
| 295 | } |
| 296 | */ |
| 297 | |
| 298 | |
| 299 | |
| 300 | bool myBoard::convert_move(const QString& move_orig, int* from_num, int* to_num) |
| 301 | { |
| 302 | QString move = move_orig.toUpper().replace('X', '-'); |
| 303 | QString from; |
| 304 | QString to; |
| 305 | int sect = move.count('-'); |
| 306 | |
| 307 | *from_num = *to_num = -1; |
| 308 | |
| 309 | from = move.section('-', 0, 0); |
| 310 | to = move.section('-', sect, sect); |
| 311 | |
| 312 | if(from!=QString::null && to!=QString::null) { |
| 313 | for(int i=0; i<32; i++) { |
| 314 | if(m_fields[i]->label()==from) { |
| 315 | *from_num = m_fields[i]->number(); |
| 316 | } |
| 317 | if(m_fields[i]->label()==to) { |
| 318 | *to_num = m_fields[i]->number(); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if(*from_num>=0 && *to_num>=0) { |
| 323 | return true; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | void myBoard::setNotationFont(const QFont& f) |
| 332 | { |
| 333 | setFont(f); |
| 334 | for(int i=0; i<32; i++) { |
| 335 | m_fields[i]->fontUpdate(); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | |
| 340 | void myBoard::setGame(int rules) |
| 341 | { |
| 342 | if(m_game) { |
| 343 | delete m_game; |
| 344 | } |
| 345 | |
| 346 | if(rules==ENGLISH) { |
| 347 | m_game = new ECheckers(); |
| 348 | } else { |
| 349 | m_game = new RCheckers(); |
| 350 | } |
| 351 | |
| 352 | reset(); |
| 353 | } |
| 354 | |
| 355 | |
| 356 | void myBoard::selectField(int field_num, bool is_on) |
| 357 | { |
| 358 | for(int i=0; i<32; i++) { |
| 359 | if(i==field_num) { |
| 360 | m_fields[i]->showFrame(is_on); |
| 361 | } else { |
| 362 | m_fields[i]->showFrame(false); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | |
| 368 | QString myBoard::doMove(int from_num, int to_num, bool white_player) |
| 369 | { |
| 370 | bool bottom_player = (white_player && bottom_is_white) |
| 371 | || (!white_player && !bottom_is_white); |
| 372 | |
| 373 | int from_pos = from_num; |
| 374 | int to_pos = to_num; |
| 375 | |
| 376 | if(!bottom_player) { |
| 377 | from_pos = 31-from_pos; |
| 378 | to_pos = 31-to_pos; |
| 379 | m_game->fromString(m_game->toString(true)); |
| 380 | } |
| 381 | if(!m_game->go1(from_pos, to_pos)) { |
| 382 | return QString::null; |
| 383 | /* |
| 384 | qDebug() << __PRETTY_FUNCTION__ |
| 385 | << from_pos << "," << to_pos |
| 386 | << " could not move."; |
| 387 | */ |
| 388 | } |
| 389 | if(!bottom_player) { |
| 390 | m_game->fromString(m_game->toString(true)); |
| 391 | } |
| 392 | |
| 393 | fieldsSetup(); |
| 394 | highlight(from_num, to_num, bottom_player); |
| 395 | |
| 396 | return QString("%1?%3") |
| 397 | .arg(m_fields[from_num]->label()) |
| 398 | .arg(m_fields[to_num]->label()); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | bool myBoard::doMove(const QString& move, bool white_player) |
| 403 | { |
| 404 | int from_pos, to_pos; |
| 405 | if(convert_move(move, &from_pos, &to_pos)) { |
| 406 | doMove(from_pos, to_pos, white_player); |
| 407 | return true; |
| 408 | } |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | void myBoard::doFreeMove(int from, int to) |
| 414 | { |
| 415 | int old_to = m_game->item(to); |
| 416 | int old_from = m_game->item(from); |
| 417 | m_game->setItem(to, old_from); |
| 418 | m_game->setItem(from, old_to); |
| 419 | fieldsSetup(); |
| 420 | } |
| 421 | |
| 422 | int myBoard::getTargetFieldSize(QSize size) { |
| 423 | int w_max = size.width(); |
| 424 | int h_max = size.height(); |
| 425 | int max_size = w_max < h_max ? w_max : h_max; |
| 426 | return (max_size - 2*frameWidth()) / 8; |
| 427 | } |
| 428 | |
| 429 | void myBoard::resizeEvent(QResizeEvent* e) { |
| 430 | if (m_theme) { |
| 431 | int size = getTargetFieldSize(e->size()); |
| 432 | m_theme->setTargetSize(size); |
| 433 | fieldsSetup(); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | void myBoard::mousePressEvent(QMouseEvent* me) |
| 438 | { |
| 439 | if(me->button() != Qt::LeftButton) |
| 440 | return; |
| 441 | if (animation_in_progress) |
| 442 | return; |
| 443 | for (int i = 0; i < 32; i++) { |
| 444 | if (m_fields[i]->rect().contains(me->pos())) { |
| 445 | invalidate(); |
| 446 | update(); |
| 447 | emit fieldClicked(i); |
| 448 | return; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void myBoard::highlight(int from, int to, bool bottom_player) { |
| 454 | animation_in_progress = true; |
| 455 | m_src_field = m_fields[from]; |
| 456 | m_dst_field = m_fields[to]; |
| 457 | animating_bottom_player = bottom_player; |
| 458 | } |
| 459 | |
| 460 | void myBoard::timerEvent(QTimerEvent* e) { |
| 461 | if (e->timerId() != animation_timer) { |
| 462 | return; |
| 463 | } |
| 464 | if (animation_in_progress) { |
| 465 | qreal progress = ((qreal)current_step) / ((qreal)animation_steps); |
| 466 | QColor begin; |
| 467 | QColor end; |
| 468 | if (animating_bottom_player) { |
| 469 | begin = QColor(0, 0, 0); |
| 470 | end = QColor(0, 255, 0); |
| 471 | } else { |
| 472 | begin = QColor(0, 0, 0); |
| 473 | end = QColor(255, 0, 0); |
| 474 | } |
| 475 | m_src_color = myColorInterpolator(begin, end, progress); |
| 476 | m_dst_color = myColorInterpolator(begin, end, 1-progress); |
| 477 | current_step ++; |
| 478 | if (current_step >= animation_steps) { |
| 479 | current_step = 0; |
| 480 | animation_in_progress = false; |
| 481 | m_src_field = NULL; |
| 482 | m_dst_field = NULL; |
| 483 | invalidate(); |
| 484 | repaint(); |
| 485 | } else { |
| 486 | invalidate(); |
| 487 | repaint(); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 |