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 "piecepool.h"
14 #include <core/color.h>
15 #include "chessboard.h"
17 PiecePool::PiecePool(const IColor
* pool
, ChessBoard
* b
,
18 const AnimationManager
& animManager
,
19 KGameCanvasAbstract
* parent
)
20 : ClickableCanvas(parent
)
27 , m_anim_manager(animManager
) {
32 PiecePool::~PiecePool() { }
35 QPoint
PiecePool::toReal(int i
) const {
45 return QPoint(x
, y
) * m_square_size
;
49 int PiecePool::toLogical(const QPoint
& p
) const {
50 int x
= p
.x() / m_square_size
;
51 int y
= p
.y() / m_square_size
;
53 if (x
<0 || x
>= m_width
)
62 int retv
= y
* m_width
+ x
;
63 if (retv
< 0 || static_cast<unsigned int>(retv
) >= m_sprites
.size())
70 void PiecePool::settingsChanged() {
75 void PiecePool::setGridWidth(int w
) {
76 m_width
= w
> 0 ? w
: 1;
79 QRect
PiecePool::boardRect() {
80 Q_ASSERT(m_width
> 0);
81 int height
= (m_sprites
.size() + m_width
- 1) / m_width
;
82 QSize
sz(m_width
, m_flipped
? -height
: height
);
83 return QRect(pos(), sz
* m_square_size
);
87 int PiecePool::fill() {
88 return m_sprites
.size();
92 void PiecePool::clear() {
96 void PiecePool::insertSprite(int index
, const NamedSprite
& nsprite
) {
97 if(m_dragged
&& index
> m_dragged_index
)
100 if(index
< 0 || index
> fill() ) {
101 ERROR("invalid index " << index
);
105 m_sprites
.resize(m_sprites
.size()+1);
107 for(int i
= m_sprites
.size()-1; i
> index
; i
--) {
108 // double speed = (1.0 + 1.0 / (i - index + 1)) * 0.4;
109 m_sprites
[i
] = m_sprites
[i
-1];
110 // FIXME restore pool animations
111 // animate(Animate::Pool::move(m_sprites[i], i));
114 m_sprites
[index
] = nsprite
;
116 // FIXME restore pool animations
117 // animate(Animate::Pool::move(m_sprites[index], index), Animate::Instant);
118 // animate(Animate::Pool::appear(m_sprites[index]));
122 NamedSprite
PiecePool::getSprite(int index
) {
123 if(m_dragged
&& index
== m_dragged_index
)
126 if(m_dragged
&& index
> m_dragged_index
)
129 if(index
< 0 || index
>= (int)m_sprites
.size() ) {
130 ERROR("invalid index " << index
);
131 return NamedSprite();
134 return m_sprites
[index
];
138 void PiecePool::removeSprite(int index
) {
139 if(m_dragged
&& index
== m_dragged_index
) {
140 m_dragged
= NamedSprite();
141 m_dragged_index
= -1;
145 if(m_dragged
&& index
> m_dragged_index
)
148 if(index
< 0 || index
>= (int)m_sprites
.size() ) {
149 ERROR("invalid index " << index
);
157 NamedSprite
PiecePool::takeSprite(int index
) {
158 if(m_dragged
&& index
== m_dragged_index
) {
159 NamedSprite retv
= m_dragged
;
160 m_dragged
= NamedSprite();
161 m_dragged_index
= -1;
166 ERROR("Only the sprite being dropped can be taken from the pool.");
167 return NamedSprite();
169 if(m_dragged
&& index
> m_dragged_index
)
172 if(index
< 0 || index
>= (int)m_sprites
.size() ) {
173 ERROR("invalid index " << index
);
174 return NamedSprite();
177 return takeSpriteAt(index
);
182 NamedSprite
PiecePool::takeSpriteAt(int index
) {
183 if(index
< 0 || index
>= (int)m_sprites
.size() ) {
184 ERROR("invalid index " << index
);
185 return NamedSprite();
188 NamedSprite retv
= m_sprites
[index
];
190 return NamedSprite();
191 // FIXME restore pool animations
192 // animate(Animate::Pool::disappear(retv));
194 for(int i
= index
; i
< (int)m_sprites
.size()-1; i
++) {
195 // double speed = (1.0 + 1.0 / (i - index + 1)) * 0.4;
196 m_sprites
[i
] = m_sprites
[i
+1];
197 // FIXME restore pool animations
198 // animate(Animate::Pool::move(m_sprites[i], i));
200 m_sprites
.resize(m_sprites
.size()-1);
206 void PiecePool::cancelDragging(bool fadeOff
) {
211 NamedSprite phantom
= duplicate(m_dragged
);
212 // FIXME restore pool animations
213 // animate(Animate::Pool::disappear(phantom));
216 m_dragged
.sprite()->putInCanvas(this);
217 m_dragged
.sprite()->setPixmap(loadSprite(m_dragged
.name()));
218 insertSprite(m_dragged_index
, m_dragged
);
220 m_dragged
= NamedSprite();
221 m_dragged_index
= -1;
225 void PiecePool::flipAndMoveBy(QPoint p
) {
226 QPoint deltapos
= m_flipped
? -p
: p
;
227 moveTo(pos() + deltapos
);
228 m_flipped
= !m_flipped
;
230 for(int i
=0;i
<(int)m_sprites
.size(); i
++) {
231 SpritePtr p
= m_sprites
[i
].sprite();
232 p
->setPixmap(loadSprite(m_sprites
[i
].name()));
233 // FIXME restore pool animations
234 // animate(Animate::Pool::move(m_sprites[i], i), Animate::Instant);
239 void PiecePool::onResize(int new_size
, bool force_reload
) {
240 if (m_square_size
== new_size
&& !force_reload
)
243 m_square_size
= new_size
;
244 m_loader
.setSize(m_square_size
);
246 // update the sprites
247 for (int i
=0;i
<(int)m_sprites
.size(); i
++) {
248 m_sprites
[i
].sprite()->setPixmap(loadSprite(m_sprites
[i
].name()));
249 // FIXME restore pool animation
250 // m_main_animation->addAnimation(AnimationPtr(new InstantAnimation(m_sprites[i].sprite(), toReal(i))));
255 void PiecePool::onMouseRelease(const QPoint
& pos
, int button
) {
256 if (button
!= Qt::LeftButton
|| !m_dragged
)
259 m_board
->m_dropped_pool
= m_pool
;
260 m_board
->m_dropped_index
= m_dragged_index
;
262 /* did the board take this sprite? */
263 m_board
->dropOn( m_pool
, m_dragged_index
, pos
+ this->pos() - m_board
->pos() );
265 m_board
->m_dropped_pool
= 0;
266 m_board
->m_dropped_index
= -1;
268 cancelDragging(true);
272 void PiecePool::onMousePress(const QPoint
& pos
, int button
) {
273 if (button
!= Qt::LeftButton
)
277 ERROR("Eh? We are already dragging?");
278 cancelDragging(); //never remove implicitly a piece from the pool
281 int index
= toLogical(pos
);
284 NamedSprite got
= takeSpriteAt(index
);
288 got
.sprite()->hide();
290 /* recreate the sprite, as "got" may be being animated */
291 QPixmap px
= m_board
->loadSprite(got
.name());
292 QPoint at
= pos
+ this->pos() - m_board
->pos() - QPoint(px
.width(), px
.height())/2;
293 m_dragged
= NamedSprite( got
.name(), SpritePtr(new Sprite(px
, m_board
->piecesGroup(), at
) ) );
294 m_dragged
.sprite()->raise();
295 m_dragged
.sprite()->show();
296 m_dragged_index
= index
;
300 void PiecePool::onMouseMove(const QPoint
& pos
, int /*button*/) {
302 m_dragged
.sprite()->moveTo(pos
+ this->pos() - m_board
->pos()
303 - QPoint(m_dragged
.sprite()->pixmap().width(),
304 m_dragged
.sprite()->pixmap().height() ) / 2 );
305 m_board
->draggingOn( m_pool
, m_dragged_index
, pos
+ this->pos() - m_board
->pos() );
309 QPixmap
PiecePool::loadSprite(const QString
& id
) {
310 // use board flipped state here, because the pool
311 // flipping only refers to the displaying of pieces
312 // and should not affect their orientation (which should
313 // stay coherent with that of the pieces on the board).
314 return m_loader
.piecePixmap(id
, m_board
->flipped());
317 const IndexConverter
* PiecePool::converter() const { return this; }