2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
12 #include <QApplication>
13 #include <QMouseEvent>
16 #include <core/namer.h>
18 #include "animation.h"
19 #include "animationmanager.h"
20 #include "chessboard.h"
21 #include "components.h"
22 #include "constrainedtext.h"
23 #include "entities/userentity.h"
24 #include "mastersettings.h"
27 using namespace boost
;
29 /** inherit instead of typedef to ease forward declaration :) */
30 class BoardTags
: public std::map
<QString
, std::map
<Point
, boost::shared_ptr
<KGameCanvasPixmap
> > > {
33 ChessBoard::ChessBoard(Components
* components
,
34 AnimationManager
* animManager
,
35 KGameCanvasAbstract
* parent
)
36 : ClickableCanvas(parent
)
41 , m_hinting_pos(Point::invalid())
42 , selection(Point::invalid())
43 , lastSelection(Point::invalid())
46 , m_anim_manager(animManager
) {
48 m_tags
= BoardTagsPtr(new BoardTags
);
50 m_canvas_background
= new KGameCanvasGroup(this);
51 m_canvas_background
->show();
53 m_canvas_border
= new KGameCanvasGroup(this);
54 m_canvas_border
->show();
56 m_canvas_border_text
= new KGameCanvasGroup(this);
57 m_canvas_border_text
->show();
59 m_pieces_group
= new KGameCanvasGroup(this);
60 m_pieces_group
->show();
62 m_namer
= components
->namer();
67 ChessBoard::~ChessBoard() {
68 delete m_pieces_group
;
70 while(!m_canvas_background
->items()->isEmpty())
71 delete m_canvas_background
->items()->first();
72 delete m_canvas_background
;
74 while(!m_canvas_border
->items()->isEmpty())
75 delete m_canvas_border
->items()->first();
76 delete m_canvas_border
;
79 void ChessBoard::settingsChanged() {
80 m_anim_manager
->reload();
81 m_border_text_color
= m_controls_loader
.getStaticValue
<QColor
>("border_color");
82 m_border_font
= m_controls_loader
.getStaticValue
<QFont
>("border_font");
87 void ChessBoard::updateBackground() {
88 while(!m_canvas_background
->items()->isEmpty())
89 delete m_canvas_background
->items()->first();
94 Loader::PixmapOrMap bg
= m_tags_loader
.getValue
<Loader::PixmapOrMap
>("background");
95 if(const QPixmap
* p
= boost::get
<QPixmap
>(&bg
)) {
96 KGameCanvasTiledPixmap
*t
= new KGameCanvasTiledPixmap(*p
, boardRect().size(), QPoint(),
97 true, m_canvas_background
);
100 else if(const Loader::PixmapMap
* p
= boost::get
<Loader::PixmapMap
>(&bg
)) {
101 for(Loader::PixmapMap::const_iterator it
= p
->begin(); it
!= p
->end(); ++it
) {
102 KGameCanvasTiledPixmap
*t
= new KGameCanvasTiledPixmap(it
->second
, it
->first
.size(),
103 QPoint(), true, m_canvas_background
);
104 t
->moveTo(it
->first
.topLeft());
109 kError() << "Unexpected type in boost::variant";
112 void ChessBoard::adjustSprite(const Point
& p
, bool immediate
) {
113 SpritePtr sprite
= m_sprites
[p
].sprite();
118 m_anim_manager
->enqueue(
119 1 /*BROKEN m_anim_movement*/ && !immediate
120 ? AnimationPtr(new MovementAnimation(sprite
, converter()->toReal(p
), 1.0))
121 : AnimationPtr(new InstantAnimation(sprite
, converter()->toReal(p
)))
125 boost::shared_ptr
<KGameCanvasPixmap
> ChessBoard::addTag(const QString
& name
, Point pt
, bool over
) {
126 if(!m_sprites
.valid(pt
))
127 return boost::shared_ptr
<KGameCanvasPixmap
>();
129 QPixmap p
= m_tags_loader
.getPixmap(name
);
130 boost::shared_ptr
<KGameCanvasPixmap
> item
=
131 boost::shared_ptr
<KGameCanvasPixmap
>(new KGameCanvasPixmap(p
, this));
132 item
->moveTo(converter()->toReal(pt
));
134 item
->stackOver(m_pieces_group
);
136 item
->stackUnder(m_pieces_group
);
139 (*m_tags
)[name
][pt
] = item
;
143 void ChessBoard::clearTags(const QString
& name
) {
147 void ChessBoard::clearTags() {
151 void ChessBoard::setTags(const QString
& name
, Point p1
, Point p2
, Point p3
,
152 Point p4
, Point p5
, Point p6
) {
153 //TODO: maybe this could be optimized a bit
163 void ChessBoard::recreateBorder() {
164 m_border_text
.clear();
165 while(!m_canvas_border_text
->items()->isEmpty())
166 delete m_canvas_border_text
->items()->first();
168 if(m_border_coords
.size() == 0)
171 Point s
= m_sprites
.getSize();
172 for(int w
= 0; w
<2; w
++)
173 for(int i
= 0;i
<s
.x
;i
++) {
174 int c
= w
? i
: i
+s
.x
+s
.y
;
175 QString l
= m_border_coords
.size()>c
? m_border_coords
[c
] : QString();
176 ConstrainedText
*item
= new ConstrainedText(m_canvas_border_text
);
177 item
->setColor(m_border_text_color
);
179 item
->setFont(m_border_font
);
180 item
->setColor(m_border_text_color
);
182 m_border_text
.push_back(item
);
185 for(int w
= 0; w
<2; w
++)
186 for(int i
= 0;i
<s
.y
;i
++) {
187 int c
= w
? i
+s
.x
: i
+2*s
.x
+s
.y
;
188 QString n
= m_border_coords
.size()>c
? m_border_coords
[c
] : QString();
189 ConstrainedText
*item
= new ConstrainedText(m_canvas_border_text
);
190 item
->setColor(m_border_text_color
);
192 item
->setFont(m_border_font
);
193 item
->setColor(m_border_text_color
);
195 m_border_text
.push_back(item
);
198 m_pieces_group
->raise();
203 void ChessBoard::updateBorder() {
204 while(!m_canvas_border
->items()->isEmpty())
205 delete m_canvas_border
->items()->first();
211 for(int w
= 0; w
<2; w
++)
212 for(int i
= 0;i
<m_sprites
.getSize().x
;i
++) {
213 int x
= (m_flipped
? (m_sprites
.getSize().x
-1-i
) : i
)*m_square_size
;
214 int y
= w
? -m_border_text_far
: m_square_size
*m_sprites
.getSize().y
+m_border_text_near
;
216 m_border_text
[at
]->setVisible(m_border_text_near
!= m_border_text_far
);
217 m_border_text
[at
]->setConstrainRect(QRect(x
,y
,m_square_size
,m_border_text_far
-m_border_text_near
));
221 for(int w
= 0; w
<2; w
++)
222 for(int i
= 0;i
<m_sprites
.getSize().y
;i
++) {
223 int x
= w
? (-m_border_text_far
-m_border_text_near
)/2
224 : m_square_size
*m_sprites
.getSize().x
+ (m_border_text_far
+m_border_text_near
)/2;
225 int y
= (!m_flipped
? (m_sprites
.getSize().y
-1-i
) : i
)*m_square_size
226 + (m_square_size
-m_border_text_far
-m_border_text_near
)/2;
228 m_border_text
[at
]->setVisible(m_border_text_near
!= m_border_text_far
);
229 m_border_text
[at
]->setConstrainRect(QRect(x
-m_square_size
/2,y
,m_square_size
,m_border_text_far
-m_border_text_near
));
233 ::LuaApi::LuaValueMap params
;
234 params
["width"] = m_square_size
*m_sprites
.getSize().x
;
235 params
["height"] = m_square_size
*m_sprites
.getSize().y
;
236 Loader::PixmapOrMap bord
= m_controls_loader
.getValue
<Loader::PixmapOrMap
>("border", ¶ms
);
237 if(const QPixmap
* p
= boost::get
<QPixmap
>(&bord
)) {
238 KGameCanvasTiledPixmap
*t
= new KGameCanvasTiledPixmap(*p
, boardRect().size(), QPoint(),
239 true, m_canvas_border
);
242 else if(const Loader::PixmapMap
* p
= boost::get
<Loader::PixmapMap
>(&bord
)) {
243 for(Loader::PixmapMap::const_iterator it
= p
->begin(); it
!= p
->end(); ++it
) {
244 KGameCanvasTiledPixmap
*t
= new KGameCanvasTiledPixmap(it
->second
, it
->first
.size(),
245 QPoint(), true, m_canvas_border
);
246 t
->moveTo(it
->first
.topLeft());
251 kError() << "Unexpected type in boost::variant";
254 void ChessBoard::createGrid(Point p
, const QStringList
& border_coords
) {
255 m_border_coords
= border_coords
;
256 m_sprites
= PieceGrid(p
.x
,p
.y
);
260 QPixmap
ChessBoard::loadSprite(const QString
& id
) {
261 return m_loader
.piecePixmap(id
, m_flipped
);
264 QRect
ChessBoard::computeRect(Point p
) const {
265 QPoint realPoint
= converter()->toReal(p
);
266 return squareRect(realPoint
.x(), realPoint
.y());
269 QRect
ChessBoard::squareRect(int x
, int y
) const {
270 return QRect(x
, y
, m_square_size
, m_square_size
);
273 QRegion
ChessBoard::computeRegion(Point p
) const {
274 return QRegion(computeRect(p
));
278 void ChessBoard::setSelection(const Point
& p
) {
279 lastSelection
= selection
;
281 setTags("selection", p
);
284 void ChessBoard::cancelSelection() {
285 lastSelection
= selection
;
286 selection
= Point::invalid();
287 clearTags("selection");
292 void ChessBoard::setPremove(const Move
& premove
) {
293 m_premove_from
= premove
.src();
294 m_premove_to
= premove
.dst();
295 setTags("premove", m_premove_from
, m_premove_to
);
298 void ChessBoard::cancelPremove() {
299 m_premove_from
= Point::invalid();
300 m_premove_to
= Point::invalid();
301 clearTags("premove");
304 void ChessBoard::updateSprites() {
305 // adjust piece positions
306 for (Point i
= m_sprites
.first(); i
<= m_sprites
.last(); i
= m_sprites
.next(i
)) {
307 boost::shared_ptr
<Sprite
> p
= m_sprites
[i
].sprite();
311 p
->setPixmap(loadSprite(m_sprites
[i
].name()));
312 adjustSprite(i
, true);
317 void ChessBoard::updateTags() {
321 for(BoardTags::iterator tit
= m_tags
->begin(); tit
!= m_tags
->end(); ++tit
)
322 for(std::map
<Point
, boost::shared_ptr
<KGameCanvasPixmap
> >::iterator pt
=
323 tit
->second
.begin(); pt
!= tit
->second
.end(); ++pt
) {
324 pt
->second
->moveTo(converter()->toReal(pt
->first
));
325 pt
->second
->setPixmap(m_tags_loader
.getPixmap(tit
->first
));
330 bool ChessBoard::doMove(Move
& m
) {
331 if (m_entity
.lock()->oneClickMoves() || m_entity
.lock()->validTurn(m
.src()) == Moving
) {
332 if (m_entity
.lock()->testMove(m
)) {
333 m_entity
.lock()->executeMove(m
);
338 kDebug() << "invalid move";
345 void ChessBoard::onResize(int new_size
, int border_size
, int border_text_near
,
346 int border_text_far
, bool force_reload
) {
347 if(m_square_size
== new_size
&& !force_reload
)
350 m_square_size
= new_size
;
351 m_border_size
= border_size
;
352 m_border_text_near
= border_text_near
;
353 m_border_text_far
= border_text_far
;
355 // update the size of the piece loader
356 m_loader
.setSize(m_square_size
);
358 // update the size of the tag loader
359 m_tags_loader
.setSize(m_square_size
);
361 // update the size of the controls loader
362 m_controls_loader
.setSize(m_border_size
);
364 // update canvas background
367 // update the sprites
377 void ChessBoard::onMousePress(const QPoint
& pos
, int button
) {
378 Point point
= converter()->toLogical(pos
);
379 if (!m_sprites
.valid(point
))
384 if (button
== Qt::LeftButton
) {
385 if (m_entity
.lock()->oneClickMoves()) {
386 Move
m(Point::invalid(), point
);
390 shared_ptr
<Sprite
> piece
= m_sprites
[point
].sprite();
392 if (piece
&& m_entity
.lock()->movable(point
)) {
394 m_drag_info
= DragInfoPtr(new DragInfo(point
, pos
, piece
,
395 m_entity
.lock()->validTurn(point
)) );
399 // if selection is valid, (pre)move to point
400 else if (selection
!= Point::invalid()) {
401 piece
= m_sprites
[selection
].sprite();
403 Move
m(selection
, point
);
405 switch(m_entity
.lock()->validTurn(selection
)) {
413 if (m_entity
.lock()->testPremove(m
)) {
414 m_entity
.lock()->addPremove(m
);
431 else if (button
== Qt::RightButton
) {
433 if (point
== m_premove_from
|| point
== m_premove_to
)
435 m_entity
.lock()->handleRightClick(point
);
441 void ChessBoard::onMouseRelease(const QPoint
& pos
, int button
) {
442 Point point
= converter()->toLogical(pos
);
446 if (button
== Qt::LeftButton
) {
449 // Q_ASSERT(m_drag_info->piece);
450 Q_ASSERT(m_drag_info
->sprite
);
453 // remove valid move highlighting
454 clearTags("validmove");
456 // toggle selection if the piece didn't move
457 if (m_drag_info
->from
== point
) {
458 if (lastSelection
== point
)
466 if (!m_sprites
.valid(point
))
467 dst
= Point::invalid();
468 Move
m(m_drag_info
->from
, dst
);
470 switch (m_entity
.lock()->validTurn(m
.src())) {
477 if (m_entity
.lock()->testPremove(m
)) {
478 m_entity
.lock()->addPremove(m
);
488 shared_ptr
<Sprite
> s
= m_sprites
[m_drag_info
->from
].sprite();
489 if (!moved
&& s
&& s
->pos() != converter()->toReal(m_drag_info
->from
)) {
491 QPoint real
= converter()->toReal(m_drag_info
->from
);
492 if( (point
== m_drag_info
->from
) ? 0/* !m_anim_movement*/ : 0 /* !m_anim_fade*/) //BROKEN
493 m_anim_manager
->enqueue(shared_ptr
<Animation
>(new InstantAnimation(s
, real
)));
494 else if (point
== m_drag_info
->from
)
495 m_anim_manager
->enqueue(shared_ptr
<Animation
>(new MovementAnimation(s
, real
)));
497 m_anim_manager
->enqueue(shared_ptr
<Animation
>(new TeleportAnimation(s
, s
->pos(), real
)));
500 m_drag_info
= DragInfoPtr();
507 void ChessBoard::onMouseMove(const QPoint
& pos
, int /*button*/) {
508 Point point
= converter()->toLogical(pos
);
509 static Point cachedPoint
= Point();
510 static bool cachedValid
;
513 Q_ASSERT(m_drag_info
->sprite
);
514 // check drag threshold
515 if (!m_drag_info
->dragStarted
) {
516 QPoint delta
= pos
- m_drag_info
->real
;
517 if (delta
.x() * delta
.x() + delta
.y() * delta
.y() > DragInfo::DRAG_THRESHOLD
) {
518 m_drag_info
->dragStarted
= true;
521 if (m_drag_info
->dragStarted
)
522 m_drag_info
->sprite
->moveTo(pos
- QPoint(m_square_size
/ 2, m_square_size
/ 2) );
524 // highlight valid moves
525 Move
move(m_drag_info
->from
, point
);
526 bool valid
= m_sprites
.valid(point
);
528 InteractionType action
= m_entity
.lock()->validTurn(move
.src());
529 if (action
== Moving
)
530 if (point
== cachedPoint
)
533 valid
= m_entity
.lock()->testMove(move
);
540 setTags("validmove", point
);
542 clearTags("validmove");
544 else if (m_entity
.lock()->oneClickMoves()) {
545 if(point
== m_hinting_pos
)
550 if (m_sprites
.valid(point
)) {
551 Move
move(Point::invalid(), point
);
552 if (m_entity
.lock()->testMove(move
)) {
554 hint
= m_entity
.lock()->moveHint(move
);
558 updateHinting(point
, hint
);
562 void ChessBoard::onPositionChanged() {
563 if (m_entity
.lock() && m_entity
.lock()->oneClickMoves() && m_sprites
.valid(m_hinting_pos
)) {
566 Move
move(Point::invalid(), m_hinting_pos
);
567 if (m_entity
.lock()->testMove(move
)) {
569 hint
= m_entity
.lock()->moveHint(move
);
572 updateHinting(move
.dst(), hint
);
576 void ChessBoard::onMouseLeave() {
577 updateHinting(Point::invalid(), Piece());
580 void ChessBoard::updateHinting(Point pt
, Piece piece
) {
581 if(!m_sprites
.valid(pt
))
585 QString piece_name
= m_namer
->name(piece
);
586 if(piece
!= Piece() || !m_sprites
.valid(pt
)) {
587 if(m_hinting
.sprite()) {
588 if(1 /*BROKEN m_anim_fade*/)
589 m_anim_manager
->enqueue( boost::shared_ptr
<Animation
>(new FadeAnimation(m_hinting
.sprite(),
590 m_hinting
.sprite()->pos(), 160, 0)) );
592 m_anim_manager
->enqueue( boost::shared_ptr
<Animation
>(new CaptureAnimation(m_hinting
.sprite())) );
595 m_hinting_pos
= Point::invalid();
596 m_hinting
= NamedSprite();
599 if(pt
== m_hinting_pos
) {
600 if (piece_name
!= m_hinting
.name()) {
601 m_hinting
= NamedSprite(piece_name
, m_hinting
.sprite());
602 m_hinting
.sprite()->setPixmap(loadSprite(piece_name
));
606 if(m_hinting
.sprite()) {
607 if(1 /*BROKEN m_anim_fade*/)
608 m_anim_manager
->enqueue( boost::shared_ptr
<Animation
>(new FadeAnimation(m_hinting
.sprite(),
609 m_hinting
.sprite()->pos(), 160, 0)) );
611 m_anim_manager
->enqueue( boost::shared_ptr
<Animation
>(new CaptureAnimation(m_hinting
.sprite())) );
614 QPixmap pix
= loadSprite(piece_name
);
615 SpritePtr
sprite(new Sprite(pix
, piecesGroup(), converter()->toReal(pt
)));
616 sprite
->setOpacity(160);
621 m_hinting
= NamedSprite(piece_name
, sprite
);
624 m_anim_manager->enqueue( boost::shared_ptr<Animation>(new FadeAnimation(m_hinting.sprite(),
625 m_hinting.sprite()->pos(), 0, 160)) );
627 m_hinting.sprite()->setOpacity(160);
628 m_anim_manager->enqueue(boost::shared_ptr<Animation>(new DropAnimation(m_hinting.sprite())) );
634 void ChessBoard::reset() {
638 m_anim_manager
->stop();
641 void ChessBoard::flip(bool flipped
)
643 if (m_flipped
!= flipped
) {
646 // update sprite positions
647 for (Point i
= m_sprites
.first(); i
<= m_sprites
.last(); i
= m_sprites
.next(i
)) {
648 SpritePtr p
= m_sprites
[i
].sprite();
650 p
->setPixmap(loadSprite(m_sprites
[i
].name()));
651 adjustSprite(i
, true);
660 void ChessBoard::draggingOn(const IColor
* pool
, int index
, const QPoint
& point
) {
661 Point to
= converter()->toLogical(point
);
663 if (m_sprites
.valid(to
))
664 switch (m_entity
.lock()->validTurn(pool
)) {
666 Move
m(pool
, index
, to
);
667 if (m_entity
.lock()->testMove(m
)) {
668 setTags("validmove", to
);
675 setTags("validmove", to
);
682 clearTags("validmove");
685 bool ChessBoard::dropOn(const IColor
* pool
, int index
, const QPoint
& point
) {
687 Point to
= converter()->toLogical(point
);
688 if (!m_sprites
.valid(to
))
691 clearTags("validmove");
693 switch (m_entity
.lock()->validTurn(pool
)) {
696 Move
m(pool
, index
, to
);
697 if (m_entity
.lock()->testMove(m
)) {
698 m_entity
.lock()->executeMove(m
);
705 Move
m(pool
, index
, to
);
706 if (m_entity
.lock()->testPremove(m
)) {
707 m_entity
.lock()->addPremove(m
);
717 kDebug() << "invalid move";