Add finer engine tracing.
[tagua/yd.git] / src / graphicalgame.cpp
blobfe76aed008b026b0db86bcac157d65eb59cab263
1 /*
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.
9 */
11 #include <iostream>
12 #include "graphicalgame.h"
13 #include "game.h"
14 #include "game_p.h"
15 #include "mastersettings.h"
16 #include "graphicalsystem.h"
17 #include "movelist_table.h"
18 #include "decoratedmove.h"
19 #include "entities/userentity.h"
20 #include <iostream>
22 using namespace GamePrivate; // is this ok?
24 template <typename Enum>
25 inline void setFlag(QFlags<Enum>& flag, Enum e, bool value) {
26 if (value)
27 flag |= e;
28 else
29 flag &= ~e;
33 class CtrlAction {
34 Game* m_game;
35 bool m_done;
36 Index m_index;
37 public:
38 CtrlAction(Game* game)
39 : m_game(game)
40 , m_index(game->index()) {
41 m_done = m_game->back();
44 void forfait() { m_done = false; }
46 ~CtrlAction() {
47 if (m_done) m_game->goTo(m_index);
51 GraphicalGame::GraphicalGame(GraphicalSystem* graphical,
52 MoveList::Table* m)
53 : Game()
54 , m_graphical(graphical)
55 , m_movelist(m)
56 , m_anim_sequence(false) {
57 m_action_state = 0;
58 if(m_movelist) {
59 m_movelist->reset();
60 m_movelist->setLayoutStyle(graphical->m_variant->moveListLayout());
61 m_movelist->setNotifier( static_cast<MoveList::Notifier*>(this) );
62 m_movelist->show();
64 settings().onChange(this, "settingsChanged", "Loader::Theme");
65 settingsChanged();
68 GraphicalGame::~GraphicalGame() {
69 if(m_movelist) {
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) {
83 onAddedInternal(ix);
84 updateActionState();
87 void GraphicalGame::onAddedInternal(const Index& ix, bool confirm_promotion) {
88 if(!m_movelist)
89 return;
91 int at;
92 History *vec = fetchRef(ix, &at);
93 if(!vec) {
94 ERROR("invalid index " << ix);
95 return;
98 m_movelist->remove(ix, confirm_promotion); //clear existing, if any
100 Index index = ix;
101 for(int i=at;i<(int)vec->size();i++) {
102 Entry* e = &(*vec)[i];
103 PositionPtr prev = position(index.prev());
104 DecoratedMove mv(
105 (e->move && 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) {
125 if(at <= Index(0)) {
126 Entry* e = fetch(at);
127 if(!e)
128 return;
129 if(at == current && e->position)
130 m_graphical->warp(e->move, e->position);
131 return;
134 if(m_movelist) {
135 Entry* e = fetch(at);
136 if(!e)
137 return;
139 Entry* pe = fetch(at.prev());
141 AbstractPosition::Ptr last_pos;
142 if (pe) last_pos = pe->position;
144 DecoratedMove mv(
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
154 // next ones as well
155 if(propagate) {
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) {
165 if(m_movelist)
166 m_movelist->remove(i);
167 updateActionState();
170 void GraphicalGame::onPromoteVariation(const Index& i, int v) {
171 if(m_movelist) {
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);
177 if(it != vc.end())
178 m_movelist->setVComment(i, v, it->second, true);
179 m_movelist->select(current, true);
181 updateActionState();
184 void GraphicalGame::onSetComment(const Index& i, const QString& s) {
185 if(m_movelist)
186 m_movelist->setComment(i, s);
189 void GraphicalGame::onSetVComment(const Index& i, int v, const QString& s) {
190 if(m_movelist)
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();
208 if(m_movelist)
209 m_movelist->select(current);
211 updateActionState();
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)
218 return;
220 if(!oe || !oe->position) {
221 m_graphical->warp(move(), position());
222 return;
225 bool can_animate = (steps.first+steps.second <= 1) || (m_anim_sequence
226 && (steps.first+steps.second <= m_anim_sequence_max));
228 if(can_animate)
229 for(int i=1;i<=steps.first;i++)
230 if( !move(old_c.prev(i-1)) || !position(old_c.prev(i))) {
231 can_animate = false;
232 break;
235 if(can_animate)
236 for(int i=steps.second-1;i>=0;i--)
237 if( !move(current.prev(i)) || !position(current.prev(i))) {
238 can_animate = false;
239 break;
242 if(can_animate) {
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)));
248 else
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())
267 if (entity->goTo(i))
268 return;
270 // fallback
271 goTo(i);
274 void GraphicalGame::onUserSetComment(const Index& i, QString s) {
275 setComment(i, s);
278 void GraphicalGame::onUserSetVComment(const Index& i, int v, QString s) {
279 setVComment(i, v, s);
282 void GraphicalGame::onUserClearVariations(const Index& i) {
283 clearVariations(i);
286 void GraphicalGame::onUserTruncate(const Index& i) {
287 if (i == index())
288 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
289 if (entity->truncate())
290 return;
292 // fallback
293 truncate(i);
296 void GraphicalGame::onUserPromoteVariation(const Index& i) {
297 if (i == index())
298 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
299 if (entity->promoteVariation())
300 return;
302 // fallback
303 promoteVariation(i);
306 void GraphicalGame::onUserRemoveVariation(const Index& i) {
307 removeVariation(i);
310 void GraphicalGame::onUserUndo() {
311 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
312 if (entity->undo())
313 return;
315 // fallback
316 undo();
319 void GraphicalGame::onUserRedo() {
320 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
321 if (entity->redo())
322 return;
324 // fallback
325 redo();
328 void GraphicalGame::onDetachNotifier() {
329 Q_ASSERT(m_movelist->getNotifier() == static_cast<MoveList::Notifier*>(this));
331 m_movelist = NULL;
334 void GraphicalGame::createCtrlAction() {
335 m_ctrl = boost::shared_ptr<CtrlAction>(new CtrlAction(this));
338 void GraphicalGame::destroyCtrlAction() {
339 m_ctrl.reset();
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() { }