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 <core/color.h>
12 #include <core/moveserializer.h>
13 #include <core/state.h>
14 #include <core/validator.h>
17 #include "components.h"
18 #include "graphicalgame.h"
24 #include "mastersettings.h"
25 #include "graphicalsystem.h"
26 #include "movelist_table.h"
27 #include "decoratedmove.h"
28 #include "entities/userentity.h"
30 using namespace GamePrivate
; // is this ok?
32 template <typename Enum
>
33 inline void setFlag(QFlags
<Enum
>& flag
, Enum e
, bool value
) {
46 CtrlAction(Game
* game
)
48 , m_index(game
->index()) {
49 m_done
= m_game
->back();
52 void forfait() { m_done
= false; }
55 if (m_done
) m_game
->goTo(m_index
);
59 GraphicalGame::GraphicalGame(GraphicalSystem
* graphical
,
61 : Game(graphical
->components())
62 , m_graphical(graphical
)
64 , m_anim_sequence(false) {
66 m_decorator
= graphical
->components()->moveSerializer("decorated");
70 // FIXME restore move list layout
71 m_movelist
->setLayoutStyle(0 /*graphical->m_variant->moveListLayout()*/);
72 m_movelist
->setNotifier( static_cast<MoveList::Notifier
*>(this) );
75 settings().onChange(this, "settingsChanged", "Loader::Theme");
79 GraphicalGame::~GraphicalGame() {
81 Q_ASSERT(m_movelist
->getNotifier() == static_cast<MoveList::Notifier
*>(this));
83 m_movelist
->setNotifier(NULL
, false);
87 void GraphicalGame::settingsChanged() {
88 m_anim_sequence
= settings().flag("animations", true)
89 && settings()("animations").flag("sequence", true);
90 m_anim_sequence_max
= settings()("animations")("sequence")[QString("max")] | 10;
93 void GraphicalGame::onAdded(const Index
& ix
) {
98 void GraphicalGame::onAddedInternal(const Index
& ix
, bool confirm_promotion
) {
103 History
*vec
= fetchRef(ix
, &at
);
105 kError() << "invalid index " << ix
;
109 m_movelist
->remove(ix
, confirm_promotion
); //clear existing, if any
112 for(int i
=at
;i
<(int)vec
->size();i
++) {
113 Entry
* e
= &(*vec
)[i
];
114 StatePtr prev
= position(index
.prev());
116 (e
->move
!= Move() && prev
) ?
117 m_decorator
->serialize(e
->move
, prev
.get()) :
118 (e
->position
? "(-)" : "???"));
120 int turn
= prev
? prev
->turn()->index() : (index
.totalNumMoves()+1)%2;
122 //mv += " " + QString::number(turn);
123 m_movelist
->setMove(index
, turn
, mv
, e
->comment
, confirm_promotion
);
125 for (Variations::const_iterator it
= e
->variations
.begin();
126 it
!= e
->variations
.end(); ++it
)
127 onAddedInternal(index
.next(it
->first
), confirm_promotion
);
128 for (VComments::const_iterator it
= e
->vcomments
.begin();
129 it
!= e
->vcomments
.end(); ++it
)
130 m_movelist
->setVComment(index
, it
->first
, it
->second
, confirm_promotion
);
132 index
= index
.next();
136 void GraphicalGame::onEntryChanged(const Index
& at
, int propagate
) {
138 Entry
* e
= fetch(at
);
141 if(at
== current
&& e
->position
)
142 m_graphical
->warp(e
->move
, e
->position
);
147 Entry
* e
= fetch(at
);
151 Entry
* pe
= fetch(at
.prev());
154 if (pe
) last_pos
= pe
->position
;
157 (e
->move
!= Move() && last_pos
) ?
158 m_decorator
->serialize(e
->move
, last_pos
.get()) :
159 (e
->position
? "(-)" : "???"));
161 int turn
= last_pos
?
162 last_pos
->turn()->index() :
163 (at
.totalNumMoves()+1)%2;
165 m_movelist
->setMove(at
, turn
, mv
, e
->comment
);
166 if(at
== current
&& e
->position
)
167 m_graphical
->warp(e
->move
, e
->position
);
169 // when an entry changes, chances are that we get some more information about the
172 onEntryChanged(at
.next(), propagate
-1);
173 for (Variations::const_iterator it
= e
->variations
.begin();
174 it
!= e
->variations
.end(); ++it
)
175 onEntryChanged(at
.next(it
->first
), propagate
-1);
180 void GraphicalGame::onRemoved(const Index
& i
) {
182 m_movelist
->remove(i
);
186 void GraphicalGame::onPromoteVariation(const Index
& i
, int v
) {
188 m_movelist
->promoteVariation(i
,v
);
189 onAddedInternal(i
.next(), true);
190 onAddedInternal(i
.next(v
), true);
191 VComments vc
= fetch(i
)->vcomments
;
192 VComments::const_iterator it
= vc
.find(v
);
194 m_movelist
->setVComment(i
, v
, it
->second
, true);
195 m_movelist
->select(current
, true);
200 void GraphicalGame::onSetComment(const Index
& i
, const QString
& s
) {
202 m_movelist
->setComment(i
, s
);
205 void GraphicalGame::onSetVComment(const Index
& i
, int v
, const QString
& s
) {
207 m_movelist
->setVComment(i
, v
, s
);
210 void GraphicalGame::updateActionState() {
211 ActionState old_state
= m_action_state
;
212 setFlag(m_action_state
, BACK
, current
!= 0);
213 setFlag(m_action_state
, BEGIN
, current
!= 0);
214 Entry
* next_entry
= fetch(current
.next());
215 setFlag(m_action_state
, FORWARD
, next_entry
);
216 setFlag(m_action_state
, END
, next_entry
);
217 if (old_state
!= m_action_state
)
218 onActionStateChange();
221 void GraphicalGame::onCurrentIndexChanged(const Index
& old_c
) {
222 if (m_ctrl
) m_ctrl
->forfait();
225 m_movelist
->select(current
);
229 Entry
*oe
= fetch(old_c
);
230 Entry
*e
= fetch(current
);
231 std::pair
<int, int> steps
= old_c
.stepsTo(current
);
233 if(!e
|| !e
->position
)
236 if(!oe
|| !oe
->position
) {
237 m_graphical
->warp(move(), position());
241 bool can_animate
= (steps
.first
+steps
.second
<= 1) || (m_anim_sequence
242 && (steps
.first
+steps
.second
<= m_anim_sequence_max
));
245 for(int i
=1;i
<=steps
.first
;i
++)
246 if( move(old_c
.prev(i
-1)) == Move() ||
247 !position(old_c
.prev(i
))) {
253 for(int i
=steps
.second
-1;i
>=0;i
--)
254 if( move(current
.prev(i
)) == Move() ||
255 !position(current
.prev(i
))) {
261 for(int i
=1;i
<=steps
.first
;i
++)
262 m_graphical
->back( move(old_c
.prev(i
)), move(old_c
.prev(i
-1)), position(old_c
.prev(i
)));
263 for(int i
=steps
.second
-1;i
>=0;i
--)
264 m_graphical
->forward( move(current
.prev(i
)), position(current
.prev(i
)));
267 m_graphical
->warp( move(), position());
269 // set m_action_state
273 void GraphicalGame::onAvailableUndo(bool e
) {
274 setFlag(m_action_state
, UNDO
, e
);
275 onActionStateChange();
278 void GraphicalGame::onAvailableRedo(bool e
) {
279 setFlag(m_action_state
, REDO
, e
);
280 onActionStateChange();
283 void GraphicalGame::onUserSelectMove(const Index
& i
) {
284 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
292 void GraphicalGame::onUserSetComment(const Index
& i
, QString s
) {
296 void GraphicalGame::onUserSetVComment(const Index
& i
, int v
, QString s
) {
297 setVComment(i
, v
, s
);
300 void GraphicalGame::onUserClearVariations(const Index
& i
) {
304 void GraphicalGame::onUserTruncate(const Index
& i
) {
306 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
307 if (entity
->truncate())
314 void GraphicalGame::onUserPromoteVariation(const Index
& i
) {
316 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
317 if (entity
->promoteVariation())
324 void GraphicalGame::onUserRemoveVariation(const Index
& i
) {
328 void GraphicalGame::onUserUndo() {
329 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
337 void GraphicalGame::onUserRedo() {
338 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
346 void GraphicalGame::onDetachNotifier() {
347 Q_ASSERT(m_movelist
->getNotifier() == static_cast<MoveList::Notifier
*>(this));
352 void GraphicalGame::createCtrlAction() {
353 m_ctrl
= boost::shared_ptr
<CtrlAction
>(new CtrlAction(this));
356 void GraphicalGame::destroyCtrlAction() {
360 void GraphicalGame::setActionStateObserver(
361 const boost::shared_ptr
<ActionStateObserver
>& obs
) {
362 m_action_state_observer
= obs
;
365 void GraphicalGame::onActionStateChange() {
366 m_action_state_observer
->notifyActionStateChange(m_action_state
);
369 ActionStateObserver::~ActionStateObserver() { }