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.
11 #include "graphicalsystem.h"
12 #include "chesstable.h"
15 #include "piecepool.h"
16 #include "pointconverter.h"
18 #include "animation.h"
19 #include "pref_theme.h"
20 #include "movelist_table.h"
21 #include "mastersettings.h"
23 using namespace boost
;
25 //BEGIN GraphicalSystem
27 GraphicalSystem::GraphicalSystem(ChessTable
* view
,
28 AbstractPosition::Ptr startingPosition
,
29 const VariantPtr
& variant
)
31 , m_variant(variant
) {
33 m_pos
= startingPosition
->clone();
34 Point s
= m_pos
->size();
35 for(int i
=0;i
<s
.x
;i
++)
36 for(int j
=0;j
<s
.y
;j
++)
37 m_pos
->set(Point(i
,j
), AbstractPiece::Ptr());
39 m_board
= view
->board();
41 m_board
->createGrid(m_pos
->size(), m_pos
->borderCoords());
44 m_view
->pool(0)->clear();
45 m_view
->pool(1)->clear();
47 m_animator
= m_variant
->createAnimator(this);
49 // settings().onChange(this, "settingsChanged", "Loader::Theme");
53 warp(AbstractMove::Ptr(), startingPosition
);
56 GraphicalSystem::~GraphicalSystem() {
59 const PointConverter
* GraphicalSystem::converter() const {
60 return m_board
->converter();
63 AbstractPosition::Ptr
GraphicalSystem::position() const {
67 void GraphicalSystem::settingsChanged() {
68 /* recreate the animator to reload its settings */
69 m_animator
= m_variant
->createAnimator(this);
71 ThemeInfo theme
= PrefTheme::getBestTheme(m_variant
, "pieces");
72 ThemeInfo sqtheme
= PrefTheme::getBestTheme(m_variant
, "squares");
73 ThemeInfo figtheme
= PrefTheme::getBestTheme(m_variant
, "figurines");
74 ThemeInfo ctrltheme
= PrefTheme::getBestTheme(m_variant
, "controls");
76 m_board
->loader()->setTheme(theme
);
77 m_board
->tagsLoader()->setTheme(sqtheme
);
78 m_board
->controlsLoader()->setTheme(ctrltheme
);
81 m_view
->pool(i
)->loader()->setTheme(theme
);
84 m_view
->clock(i
)->controlsLoader()->setTheme(ctrltheme
);
86 m_view
->moveListTable()->setLoaderTheme(figtheme
);
87 m_view
->moveListTable()->settingsChanged();
89 //clear board and pool, forcing reload
90 m_view
->settingsChanged();
93 void GraphicalSystem::setup(const shared_ptr
<UserEntity
>& entity
) {
94 m_view
->setEntity(entity
);
97 NamedSprite
GraphicalSystem::getSprite(const Point
& p
) {
98 if (!m_board
->m_sprites
.valid(p
))
101 return m_board
->m_sprites
[p
];
104 NamedSprite
GraphicalSystem::takeSprite(const Point
& p
) {
105 if (!m_board
->m_sprites
.valid(p
))
106 return NamedSprite();
108 NamedSprite retv
= m_board
->m_sprites
[p
];
109 m_board
->m_sprites
[p
] = NamedSprite();
113 NamedSprite
GraphicalSystem::setPiece(const Point
& p
, const AbstractPiece
* piece
, bool show
) {
114 return m_board
->m_sprites
[p
] = createPiece(p
, piece
, show
);
117 NamedSprite
GraphicalSystem::createPiece(const Point
& p
, const AbstractPiece
* piece
, bool show
) {
119 if (!m_board
->m_sprites
.valid(p
))
120 return NamedSprite();
122 NamedSprite
s(piece
->name(), SpritePtr(new Sprite(m_board
->loadSprite(piece
->name()),
123 m_board
->piecesGroup(), m_board
->converter()->toReal(p
))) );
129 void GraphicalSystem::setSprite(const Point
& p
, const NamedSprite
& sprite
) {
130 if (!m_board
->m_sprites
.valid(p
))
133 m_board
->m_sprites
[p
] = sprite
;
136 int GraphicalSystem::poolSize(int pool
) {
137 return m_view
->pool(pool
)->fill();
140 NamedSprite
GraphicalSystem::getPoolSprite(int pool
, int index
) {
141 return m_view
->pool(pool
)->getSprite(index
);
144 void GraphicalSystem::removePoolSprite(int pool
, int index
) {
145 m_view
->pool(pool
)->removeSprite(index
);
148 NamedSprite
GraphicalSystem::takePoolSprite(int pool
, int index
) {
149 return m_view
->pool(pool
)->takeSprite(index
);
152 NamedSprite
GraphicalSystem::insertPoolPiece(int pool
, int index
, const AbstractPiece
* piece
) {
153 PiecePool
*pl
= m_view
->pool(pool
);
154 QPixmap px
= pl
->loadSprite(piece
->name());
155 // QPixmap px = pl->m_loader(piece->name());
157 NamedSprite
s( piece
->name(), SpritePtr( new Sprite( px
, pl
, QPoint() ) ) );
158 pl
->insertSprite(index
, s
);
162 std::pair
<int, int> GraphicalSystem::droppedPoolPiece() {
163 return std::pair
<int, int>(m_board
->m_dropped_pool
, m_board
->m_dropped_index
);
166 AnimationPtr
GraphicalSystem::animate(const Animate::Scheme
& scheme
, Animate::AnimationType type
) {
167 return scheme
.run(m_view
->animationSettings(), converter(), type
);
172 void GraphicalSystem::updatePool(AbstractPosition::PoolPtr pool
) {
174 AbstractPosition::PoolPtr curr
= m_pos
->pool();
176 AbstractPosition::AbstractPool::iterator oldit
= curr
->begin();
177 AbstractPosition::AbstractPool::iterator newit
= pool
->begin();
179 while(oldit
!= curr
->end() || newit
!= pool
->end()) {
180 if(newit
== pool
->end() || (oldit
!= curr
->end()
181 && oldit
->first
->less(newit
->first
) )) {
182 removeFromPool(oldit
->first
, oldit
->second
);
185 else if (oldit
== curr
->end() || (newit
!= pool
->end()
186 && newit
->first
->less(oldit
->first
) )) {
187 addToPool(newit
->first
, newit
->second
);
191 Q_ASSERT(newit
->first
->equals(oldit
->first
));
192 if(oldit
->second
< newit
->second
)
193 addToPool(newit
->first
, newit
->second
- oldit
->second
);
194 else if(oldit
->second
> newit
->second
)
195 removeFromPool(newit
->first
, oldit
->second
- newit
->second
);
202 void GraphicalSystem::addToPool(AbstractPiece::Ptr piece
, int n
) {
203 PiecePool
*pool
= m_view
->pool(!piece
->color());
204 QPixmap px
= pool
->m_loader(piece
->name());
206 for(int i
=0;i
<n
;i
++) {
207 SpritePtr s
= SpritePtr( new Sprite( px
, pool
->piecesGroup(), QPoint() ) );
208 pool
->addPiece(Element(piece
, s
));
211 m_pos
->addToPool(piece
, n
);
214 void GraphicalSystem::removeFromPool(AbstractPiece::Ptr piece
, int n
) {
215 PiecePool
*pool
= m_view
->pool(!piece
->color());
218 pool
->takePiece(piece
);
219 m_pos
->removeFromPool(piece
, n
);
224 void GraphicalSystem::addTag(const QString
& name
, Point pt
, bool over
) {
225 m_board
->addTag(name
, pt
, over
);
228 void GraphicalSystem::clearTags(const QString
& name
) {
229 m_board
->clearTags(name
);
232 void GraphicalSystem::setTags(const QString
& name
, Point p1
, Point p2
,
233 Point p3
, Point p4
, Point p5
, Point p6
) {
234 m_board
->setTags(name
, p1
, p2
, p3
, p4
, p5
, p6
);
239 bool GraphicalSystem::consistent() const {
240 for (Point i
= first(); i
<= last(); i
= next(i
)) {
241 Element e
= getElement(i
);
242 if (static_cast<bool>(e
.piece()) ^
243 static_cast<bool>(e
.sprite())) return false;
250 Point
GraphicalSystem::first() const { return m_board
->m_sprites
.first(); }
251 Point
GraphicalSystem::last() const { return m_board
->m_sprites
.last(); }
252 Point
GraphicalSystem::next(const Point
& p
) const { return m_board
->m_sprites
.next(p
); }
253 bool GraphicalSystem::valid(const Point
& p
) const { return m_board
->m_sprites
.valid(p
); }
256 void GraphicalSystem::forward(const AbstractMove::Ptr
& move
,
257 const AbstractPosition::Ptr
& pos
,
258 const SpritePtr
& /*movingSprite*/) {
259 AbstractPiece::Ptr sel1
= m_pos
->get(m_board
->selection
);
262 AnimationPtr animation
= m_animator
->forward(pos
, move
);
263 //??? animation->setChainAbortions(false);
264 m_board
->enqueue(animation
);
265 m_board
->setTags("highlighting", move
->toUserMove().from
, move
->toUserMove().to
);
267 m_pos
->copyFrom(pos
);
270 warp(AbstractMove::Ptr(), pos
);
272 AbstractPiece::Ptr sel2
= m_pos
->get(m_board
->selection
);
273 if(!(sel1
&& sel2
&& sel1
->equals(sel2
)))
274 m_board
->cancelSelection();
276 m_board
->cancelPremove();
277 m_view
->updateTurn(pos
->turn());
278 m_board
->onPositionChanged();
281 void GraphicalSystem::back(const AbstractMove::Ptr
& lastMove
,
282 const AbstractMove::Ptr
& move
,
283 const AbstractPosition::Ptr
& pos
) {
284 AbstractPiece::Ptr sel1
= m_pos
->get(m_board
->selection
);
287 AnimationPtr animation
= m_animator
->back(pos
, move
);
288 //??? animation->setChainAbortions(false);
289 m_board
->enqueue(animation
);
291 m_pos
->copyFrom(pos
);
297 m_board
->setTags("highlighting", lastMove
->toUserMove().from
, lastMove
->toUserMove().to
);
299 m_board
->clearTags("highlighting");
301 AbstractPiece::Ptr sel2
= m_pos
->get(m_board
->selection
);
302 if(!(sel1
&& sel2
&& sel1
->equals(sel2
)))
303 m_board
->cancelSelection();
304 m_board
->cancelPremove();
305 m_view
->updateTurn(pos
->turn());
306 m_board
->onPositionChanged();
309 void GraphicalSystem::warp(const AbstractMove::Ptr
& lastMove
,
310 const AbstractPosition::Ptr
& pos
) {
312 AbstractPiece::Ptr sel1
= m_pos
->get(m_board
->selection
);
314 AnimationPtr animation
= m_animator
->warp(pos
);
315 //??? animation->setChainAbortions(false);
317 m_board
->enqueue(animation
);
320 m_pos
->copyFrom(pos
);
323 m_board
->setTags("highlighting", lastMove
->toUserMove().from
, lastMove
->toUserMove().to
);
325 m_board
->clearTags("highlighting");
327 AbstractPiece::Ptr sel2
= m_pos
->get(m_board
->selection
);
328 if(!(sel1
&& sel2
&& sel1
->equals(sel2
)))
329 m_board
->cancelSelection();
330 m_view
->updateTurn(pos
->turn());
331 m_board
->onPositionChanged();
334 void GraphicalSystem::adjustSprite(const Point
& p
) {
335 SpritePtr sprite
= m_board
->m_sprites
[p
].sprite();
337 QPoint destination
= m_board
->converter()->toReal(p
);
338 shared_ptr
<Animation
> animation(new MovementAnimation(sprite
, destination
));
339 m_board
->enqueue(animation
);
342 void GraphicalSystem::setTurn(int turn
) {
343 m_pos
->setTurn(turn
);
344 m_view
->updateTurn(m_pos
->turn());