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 "graphicalgame.h"
15 #include "mastersettings.h"
16 #include "graphicalsystem.h"
17 #include "movelist_table.h"
18 #include "decoratedmove.h"
19 #include "entities/userentity.h"
22 using namespace GamePrivate
; // is this ok?
24 template <typename Enum
>
25 inline void setFlag(QFlags
<Enum
>& flag
, Enum e
, bool value
) {
38 CtrlAction(Game
* game
)
40 , m_index(game
->index()) {
41 m_done
= m_game
->back();
44 void forfait() { m_done
= false; }
47 if (m_done
) m_game
->goTo(m_index
);
51 GraphicalGame::GraphicalGame(GraphicalSystem
* graphical
,
54 , m_graphical(graphical
)
56 , m_anim_sequence(false) {
60 m_movelist
->setLayoutStyle(graphical
->m_variant
->moveListLayout());
61 m_movelist
->setNotifier( static_cast<MoveList::Notifier
*>(this) );
64 settings().onChange(this, "settingsChanged", "Loader::Theme");
68 GraphicalGame::~GraphicalGame() {
70 Q_ASSERT(m_movelist
->getNotifier() == static_cast<MoveList::Notifier
*>(this));
72 m_movelist
->setNotifier(NULL
, false);
76 void GraphicalGame::settingsChanged() {
77 m_anim_sequence
= settings().flag("animations", true)
78 && settings()("animations").flag("sequence", true);
79 m_anim_sequence_max
= settings()("animations")("sequence")[QString("max")] | 10;
82 void GraphicalGame::onAdded(const Index
& ix
) {
87 void GraphicalGame::onAddedInternal(const Index
& ix
, bool confirm_promotion
) {
92 History
*vec
= fetchRef(ix
, &at
);
94 ERROR("invalid index " << ix
);
98 m_movelist
->remove(ix
, confirm_promotion
); //clear existing, if any
101 for(int i
=at
;i
<(int)vec
->size();i
++) {
102 Entry
* e
= &(*vec
)[i
];
103 PositionPtr prev
= position(index
.prev());
106 e
->move
->toString("decorated", prev
) :
107 (e
->position
? "(-)" : "???"));
109 int turn
= prev
? prev
->turn() : (index
.totalNumMoves()+1)%2;
110 //mv += " " + QString::number(turn);
111 m_movelist
->setMove(index
, turn
, mv
, e
->comment
, confirm_promotion
);
113 for (Variations::const_iterator it
= e
->variations
.begin();
114 it
!= e
->variations
.end(); ++it
)
115 onAddedInternal(index
.next(it
->first
), confirm_promotion
);
116 for (VComments::const_iterator it
= e
->vcomments
.begin();
117 it
!= e
->vcomments
.end(); ++it
)
118 m_movelist
->setVComment(index
, it
->first
, it
->second
, confirm_promotion
);
120 index
= index
.next();
124 void GraphicalGame::onEntryChanged(const Index
& at
, int propagate
) {
126 Entry
* e
= fetch(at
);
129 if(at
== current
&& e
->position
)
130 m_graphical
->warp(e
->move
, e
->position
);
135 Entry
* e
= fetch(at
);
139 Entry
* pe
= fetch(at
.prev());
141 AbstractPosition::Ptr last_pos
;
142 if (pe
) last_pos
= pe
->position
;
145 (e
->move
&& last_pos
) ?
146 e
->move
->toString("decorated", last_pos
) :
147 (e
->position
? "(-)" : "???"));
148 int turn
= last_pos
? last_pos
->turn() : (at
.totalNumMoves()+1)%2;
149 m_movelist
->setMove(at
, turn
, mv
, e
->comment
);
150 if(at
== current
&& e
->position
)
151 m_graphical
->warp(e
->move
, e
->position
);
153 // when an entry changes, chances are that we get some more information about the
156 onEntryChanged(at
.next(), propagate
-1);
157 for (Variations::const_iterator it
= e
->variations
.begin();
158 it
!= e
->variations
.end(); ++it
)
159 onEntryChanged(at
.next(it
->first
), propagate
-1);
164 void GraphicalGame::onRemoved(const Index
& i
) {
166 m_movelist
->remove(i
);
170 void GraphicalGame::onPromoteVariation(const Index
& i
, int v
) {
172 m_movelist
->promoteVariation(i
,v
);
173 onAddedInternal(i
.next(), true);
174 onAddedInternal(i
.next(v
), true);
175 VComments vc
= fetch(i
)->vcomments
;
176 VComments::const_iterator it
= vc
.find(v
);
178 m_movelist
->setVComment(i
, v
, it
->second
, true);
179 m_movelist
->select(current
, true);
184 void GraphicalGame::onSetComment(const Index
& i
, const QString
& s
) {
186 m_movelist
->setComment(i
, s
);
189 void GraphicalGame::onSetVComment(const Index
& i
, int v
, const QString
& s
) {
191 m_movelist
->setVComment(i
, v
, s
);
194 void GraphicalGame::updateActionState() {
195 ActionState old_state
= m_action_state
;
196 setFlag(m_action_state
, BACK
, current
!= 0);
197 setFlag(m_action_state
, BEGIN
, current
!= 0);
198 Entry
* next_entry
= fetch(current
.next());
199 setFlag(m_action_state
, FORWARD
, next_entry
);
200 setFlag(m_action_state
, END
, next_entry
);
201 if (old_state
!= m_action_state
)
202 onActionStateChange();
205 void GraphicalGame::onCurrentIndexChanged(const Index
& old_c
) {
206 if (m_ctrl
) m_ctrl
->forfait();
209 m_movelist
->select(current
);
213 Entry
*oe
= fetch(old_c
);
214 Entry
*e
= fetch(current
);
215 std::pair
<int, int> steps
= old_c
.stepsTo(current
);
217 if(!e
|| !e
->position
)
220 if(!oe
|| !oe
->position
) {
221 m_graphical
->warp(move(), position());
225 bool can_animate
= (steps
.first
+steps
.second
<= 1) || (m_anim_sequence
226 && (steps
.first
+steps
.second
<= m_anim_sequence_max
));
229 for(int i
=1;i
<=steps
.first
;i
++)
230 if( !move(old_c
.prev(i
-1)) || !position(old_c
.prev(i
))) {
236 for(int i
=steps
.second
-1;i
>=0;i
--)
237 if( !move(current
.prev(i
)) || !position(current
.prev(i
))) {
243 for(int i
=1;i
<=steps
.first
;i
++)
244 m_graphical
->back( move(old_c
.prev(i
)), move(old_c
.prev(i
-1)), position(old_c
.prev(i
)));
245 for(int i
=steps
.second
-1;i
>=0;i
--)
246 m_graphical
->forward( move(current
.prev(i
)), position(current
.prev(i
)));
249 m_graphical
->warp( move(), position());
251 // set m_action_state
255 void GraphicalGame::onAvailableUndo(bool e
) {
256 setFlag(m_action_state
, UNDO
, e
);
257 onActionStateChange();
260 void GraphicalGame::onAvailableRedo(bool e
) {
261 setFlag(m_action_state
, REDO
, e
);
262 onActionStateChange();
265 void GraphicalGame::onUserSelectMove(const Index
& i
) {
266 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
274 void GraphicalGame::onUserSetComment(const Index
& i
, QString s
) {
278 void GraphicalGame::onUserSetVComment(const Index
& i
, int v
, QString s
) {
279 setVComment(i
, v
, s
);
282 void GraphicalGame::onUserClearVariations(const Index
& i
) {
286 void GraphicalGame::onUserTruncate(const Index
& i
) {
288 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
289 if (entity
->truncate())
296 void GraphicalGame::onUserPromoteVariation(const Index
& i
) {
298 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
299 if (entity
->promoteVariation())
306 void GraphicalGame::onUserRemoveVariation(const Index
& i
) {
310 void GraphicalGame::onUserUndo() {
311 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
319 void GraphicalGame::onUserRedo() {
320 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
328 void GraphicalGame::onDetachNotifier() {
329 Q_ASSERT(m_movelist
->getNotifier() == static_cast<MoveList::Notifier
*>(this));
334 void GraphicalGame::createCtrlAction() {
335 m_ctrl
= boost::shared_ptr
<CtrlAction
>(new CtrlAction(this));
338 void GraphicalGame::destroyCtrlAction() {
342 void GraphicalGame::setActionStateObserver(
343 const boost::shared_ptr
<ActionStateObserver
>& obs
) {
344 m_action_state_observer
= obs
;
347 void GraphicalGame::onActionStateChange() {
348 m_action_state_observer
->notifyActionStateChange(m_action_state
);
351 ActionStateObserver::~ActionStateObserver() { }