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 "graphicalgame.h"
17 #include "mastersettings.h"
18 #include "graphicalsystem.h"
19 #include "movelist_table.h"
20 #include "decoratedmove.h"
21 #include "entities/userentity.h"
23 using namespace GamePrivate
; // is this ok?
25 template <typename Enum
>
26 inline void setFlag(QFlags
<Enum
>& flag
, Enum e
, bool value
) {
39 CtrlAction(Game
* game
)
41 , m_index(game
->index()) {
42 m_done
= m_game
->back();
45 void forfait() { m_done
= false; }
48 if (m_done
) m_game
->goTo(m_index
);
52 GraphicalGame::GraphicalGame(GraphicalSystem
* graphical
,
55 , m_graphical(graphical
)
57 , m_anim_sequence(false) {
61 m_movelist
->setLayoutStyle(graphical
->m_variant
->moveListLayout());
62 m_movelist
->setNotifier( static_cast<MoveList::Notifier
*>(this) );
65 settings().onChange(this, "settingsChanged", "Loader::Theme");
69 GraphicalGame::~GraphicalGame() {
71 Q_ASSERT(m_movelist
->getNotifier() == static_cast<MoveList::Notifier
*>(this));
73 m_movelist
->setNotifier(NULL
, false);
77 void GraphicalGame::settingsChanged() {
78 m_anim_sequence
= settings().flag("animations", true)
79 && settings()("animations").flag("sequence", true);
80 m_anim_sequence_max
= settings()("animations")("sequence")[QString("max")] | 10;
83 void GraphicalGame::onAdded(const Index
& ix
) {
88 void GraphicalGame::onAddedInternal(const Index
& ix
, bool confirm_promotion
) {
93 History
*vec
= fetchRef(ix
, &at
);
95 kError() << "invalid index " << ix
;
99 m_movelist
->remove(ix
, confirm_promotion
); //clear existing, if any
102 for(int i
=at
;i
<(int)vec
->size();i
++) {
103 Entry
* e
= &(*vec
)[i
];
104 PositionPtr prev
= position(index
.prev());
107 e
->move
->toString("decorated", prev
) :
108 (e
->position
? "(-)" : "???"));
110 int turn
= prev
? prev
->turn() : (index
.totalNumMoves()+1)%2;
111 //mv += " " + QString::number(turn);
112 m_movelist
->setMove(index
, turn
, mv
, e
->comment
, confirm_promotion
);
114 for (Variations::const_iterator it
= e
->variations
.begin();
115 it
!= e
->variations
.end(); ++it
)
116 onAddedInternal(index
.next(it
->first
), confirm_promotion
);
117 for (VComments::const_iterator it
= e
->vcomments
.begin();
118 it
!= e
->vcomments
.end(); ++it
)
119 m_movelist
->setVComment(index
, it
->first
, it
->second
, confirm_promotion
);
121 index
= index
.next();
125 void GraphicalGame::onEntryChanged(const Index
& at
, int propagate
) {
127 Entry
* e
= fetch(at
);
130 if(at
== current
&& e
->position
)
131 m_graphical
->warp(e
->move
, e
->position
);
136 Entry
* e
= fetch(at
);
140 Entry
* pe
= fetch(at
.prev());
142 AbstractPosition::Ptr last_pos
;
143 if (pe
) last_pos
= pe
->position
;
146 (e
->move
&& last_pos
) ?
147 e
->move
->toString("decorated", last_pos
) :
148 (e
->position
? "(-)" : "???"));
149 int turn
= last_pos
? last_pos
->turn() : (at
.totalNumMoves()+1)%2;
150 m_movelist
->setMove(at
, turn
, mv
, e
->comment
);
151 if(at
== current
&& e
->position
)
152 m_graphical
->warp(e
->move
, e
->position
);
154 // when an entry changes, chances are that we get some more information about the
157 onEntryChanged(at
.next(), propagate
-1);
158 for (Variations::const_iterator it
= e
->variations
.begin();
159 it
!= e
->variations
.end(); ++it
)
160 onEntryChanged(at
.next(it
->first
), propagate
-1);
165 void GraphicalGame::onRemoved(const Index
& i
) {
167 m_movelist
->remove(i
);
171 void GraphicalGame::onPromoteVariation(const Index
& i
, int v
) {
173 m_movelist
->promoteVariation(i
,v
);
174 onAddedInternal(i
.next(), true);
175 onAddedInternal(i
.next(v
), true);
176 VComments vc
= fetch(i
)->vcomments
;
177 VComments::const_iterator it
= vc
.find(v
);
179 m_movelist
->setVComment(i
, v
, it
->second
, true);
180 m_movelist
->select(current
, true);
185 void GraphicalGame::onSetComment(const Index
& i
, const QString
& s
) {
187 m_movelist
->setComment(i
, s
);
190 void GraphicalGame::onSetVComment(const Index
& i
, int v
, const QString
& s
) {
192 m_movelist
->setVComment(i
, v
, s
);
195 void GraphicalGame::updateActionState() {
196 ActionState old_state
= m_action_state
;
197 setFlag(m_action_state
, BACK
, current
!= 0);
198 setFlag(m_action_state
, BEGIN
, current
!= 0);
199 Entry
* next_entry
= fetch(current
.next());
200 setFlag(m_action_state
, FORWARD
, next_entry
);
201 setFlag(m_action_state
, END
, next_entry
);
202 if (old_state
!= m_action_state
)
203 onActionStateChange();
206 void GraphicalGame::onCurrentIndexChanged(const Index
& old_c
) {
207 if (m_ctrl
) m_ctrl
->forfait();
210 m_movelist
->select(current
);
214 Entry
*oe
= fetch(old_c
);
215 Entry
*e
= fetch(current
);
216 std::pair
<int, int> steps
= old_c
.stepsTo(current
);
218 if(!e
|| !e
->position
)
221 if(!oe
|| !oe
->position
) {
222 m_graphical
->warp(move(), position());
226 bool can_animate
= (steps
.first
+steps
.second
<= 1) || (m_anim_sequence
227 && (steps
.first
+steps
.second
<= m_anim_sequence_max
));
230 for(int i
=1;i
<=steps
.first
;i
++)
231 if( !move(old_c
.prev(i
-1)) || !position(old_c
.prev(i
))) {
237 for(int i
=steps
.second
-1;i
>=0;i
--)
238 if( !move(current
.prev(i
)) || !position(current
.prev(i
))) {
244 for(int i
=1;i
<=steps
.first
;i
++)
245 m_graphical
->back( move(old_c
.prev(i
)), move(old_c
.prev(i
-1)), position(old_c
.prev(i
)));
246 for(int i
=steps
.second
-1;i
>=0;i
--)
247 m_graphical
->forward( move(current
.prev(i
)), position(current
.prev(i
)));
250 m_graphical
->warp( move(), position());
252 // set m_action_state
256 void GraphicalGame::onAvailableUndo(bool e
) {
257 setFlag(m_action_state
, UNDO
, e
);
258 onActionStateChange();
261 void GraphicalGame::onAvailableRedo(bool e
) {
262 setFlag(m_action_state
, REDO
, e
);
263 onActionStateChange();
266 void GraphicalGame::onUserSelectMove(const Index
& i
) {
267 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
275 void GraphicalGame::onUserSetComment(const Index
& i
, QString s
) {
279 void GraphicalGame::onUserSetVComment(const Index
& i
, int v
, QString s
) {
280 setVComment(i
, v
, s
);
283 void GraphicalGame::onUserClearVariations(const Index
& i
) {
287 void GraphicalGame::onUserTruncate(const Index
& i
) {
289 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
290 if (entity
->truncate())
297 void GraphicalGame::onUserPromoteVariation(const Index
& i
) {
299 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
300 if (entity
->promoteVariation())
307 void GraphicalGame::onUserRemoveVariation(const Index
& i
) {
311 void GraphicalGame::onUserUndo() {
312 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
320 void GraphicalGame::onUserRedo() {
321 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
329 void GraphicalGame::onDetachNotifier() {
330 Q_ASSERT(m_movelist
->getNotifier() == static_cast<MoveList::Notifier
*>(this));
335 void GraphicalGame::createCtrlAction() {
336 m_ctrl
= boost::shared_ptr
<CtrlAction
>(new CtrlAction(this));
339 void GraphicalGame::destroyCtrlAction() {
343 void GraphicalGame::setActionStateObserver(
344 const boost::shared_ptr
<ActionStateObserver
>& obs
) {
345 m_action_state_observer
= obs
;
348 void GraphicalGame::onActionStateChange() {
349 m_action_state_observer
->notifyActionStateChange(m_action_state
);
352 ActionStateObserver::~ActionStateObserver() { }