Ported DecoratedMove.
[tagua/yd.git] / src / graphicalgame.cpp
blob909b24b96b87e533afd2b3f2e8c166136abb4628
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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 class CtrlAction {
25 Game* m_game;
26 bool m_done;
27 Index m_index;
28 public:
29 CtrlAction(Game* game)
30 : m_game(game)
31 , m_index(game->index()) {
32 m_done = m_game->back();
35 void forfait() { m_done = false; }
37 ~CtrlAction() {
38 if (m_done) m_game->goTo(m_index);
42 GraphicalGame::GraphicalGame(GraphicalSystem* graphical,
43 MoveList::Table* m)
44 : Game()
45 , m_graphical(graphical)
46 , m_movelist(m)
47 , m_anim_sequence(false) {
48 if(m_movelist) {
49 m_movelist->reset();
50 m_movelist->setLayoutStyle(graphical->m_variant->moveListLayout());
51 m_movelist->setNotifier( static_cast<MoveList::Notifier*>(this) );
52 m_movelist->show();
54 settings().onChange(this, "settingsChanged", "Loader::Theme");
55 settingsChanged();
58 GraphicalGame::~GraphicalGame() {
59 if(m_movelist) {
60 Q_ASSERT(m_movelist->getNotifier() == static_cast<MoveList::Notifier*>(this));
62 m_movelist->setNotifier(NULL, false);
66 void GraphicalGame::settingsChanged() {
67 m_anim_sequence = settings().flag("animations", true)
68 && settings()("animations").flag("sequence", true);
69 m_anim_sequence_max = settings()("animations")("sequence")[QString("max")] | 10;
72 void GraphicalGame::onAdded(const Index& ix) {
73 onAddedInternal(ix);
76 void GraphicalGame::onAddedInternal(const Index& ix, bool confirm_promotion) {
77 if(!m_movelist)
78 return;
80 int at;
81 History *vec = fetchRef(ix, &at);
82 if(!vec) {
83 ERROR("invalid index " << ix);
84 return;
87 m_movelist->remove(ix, confirm_promotion); //clear existing, if any
89 Index index = ix;
90 for(int i=at;i<(int)vec->size();i++) {
91 Entry* e = &(*vec)[i];
92 PositionPtr prev = position(index.prev());
93 DecoratedMove mv = (e->move && prev) ? e->move->toDecoratedMove(prev) :
94 DecoratedMove(e->position ? "(-)" : "???");
95 int turn = prev ? prev->turn() : (index.totalNumMoves()+1)%2;
96 //mv += " " + QString::number(turn);
97 m_movelist->setMove(index, turn, mv, e->comment, confirm_promotion);
99 for (Variations::const_iterator it = e->variations.begin();
100 it != e->variations.end(); ++it)
101 onAddedInternal(index.next(it->first), confirm_promotion);
102 for (VComments::const_iterator it = e->vcomments.begin();
103 it != e->vcomments.end(); ++it)
104 m_movelist->setVComment(index, it->first, it->second, confirm_promotion);
106 index = index.next();
110 void GraphicalGame::onEntryChanged(const Index& at, int propagate) {
111 if(at <= Index(0)) {
112 Entry* e = fetch(at);
113 if(!e)
114 return;
115 if(at == current && e->position)
116 m_graphical->warp(e->move, e->position);
117 return;
120 if(m_movelist) {
121 Entry* e = fetch(at);
122 if(!e)
123 return;
125 Entry* pe = fetch(at.prev());
127 AbstractPosition::Ptr last_pos;
128 if (pe) last_pos = pe->position;
130 DecoratedMove mv = (e->move && last_pos) ? e->move->toDecoratedMove(last_pos) :
131 DecoratedMove(e->position ? "(-)" : "???");
132 int turn = last_pos ? last_pos->turn() : (at.totalNumMoves()+1)%2;
133 m_movelist->setMove(at, turn, mv, e->comment);
134 if(at == current && e->position)
135 m_graphical->warp(e->move, e->position);
137 // when an entry changes, chances are that we get some more information about the
138 // next ones as well
139 if(propagate) {
140 onEntryChanged(at.next(), propagate-1);
141 for (Variations::const_iterator it = e->variations.begin();
142 it != e->variations.end(); ++it)
143 onEntryChanged(at.next(it->first), propagate-1);
148 void GraphicalGame::onRemoved(const Index& i) {
149 if(m_movelist)
150 m_movelist->remove(i);
153 void GraphicalGame::onPromoteVariation(const Index& i, int v) {
154 if(m_movelist) {
155 m_movelist->promoteVariation(i,v);
156 onAddedInternal(i.next(), true);
157 onAddedInternal(i.next(v), true);
158 VComments vc = fetch(i)->vcomments;
159 VComments::const_iterator it = vc.find(v);
160 if(it != vc.end())
161 m_movelist->setVComment(i, v, it->second, true);
162 m_movelist->select(current, true);
166 void GraphicalGame::onSetComment(const Index& i, const QString& s) {
167 if(m_movelist)
168 m_movelist->setComment(i, s);
171 void GraphicalGame::onSetVComment(const Index& i, int v, const QString& s) {
172 if(m_movelist)
173 m_movelist->setVComment(i, v, s);
176 void GraphicalGame::onCurrentIndexChanged(const Index& old_c) {
177 if (m_ctrl) m_ctrl->forfait();
179 if(m_movelist)
180 m_movelist->select(current);
182 Entry *oe = fetch(old_c);
183 Entry *e = fetch(current);
184 std::pair<int, int> steps = old_c.stepsTo(current);
186 if(!e || !e->position)
187 return;
189 if(!oe || !oe->position) {
190 m_graphical->warp(move(), position());
191 return;
194 bool can_animate = (steps.first+steps.second <= 1) || (m_anim_sequence
195 && (steps.first+steps.second <= m_anim_sequence_max));
197 if(can_animate)
198 for(int i=1;i<=steps.first;i++)
199 if( !move(old_c.prev(i-1)) || !position(old_c.prev(i))) {
200 can_animate = false;
201 break;
204 if(can_animate)
205 for(int i=steps.second-1;i>=0;i--)
206 if( !move(current.prev(i)) || !position(current.prev(i))) {
207 can_animate = false;
208 break;
211 if(can_animate) {
212 for(int i=1;i<=steps.first;i++)
213 m_graphical->back( move(old_c.prev(i)), move(old_c.prev(i-1)), position(old_c.prev(i)));
214 for(int i=steps.second-1;i>=0;i--)
215 m_graphical->forward( move(current.prev(i)), position(current.prev(i)));
217 else
218 m_graphical->warp( move(), position());
221 void GraphicalGame::onAvailableUndo(bool e) {
222 if(m_movelist)
223 m_movelist->enableUndo(e);
226 void GraphicalGame::onAvailableRedo(bool e) {
227 if(m_movelist)
228 m_movelist->enableRedo(e);
231 void GraphicalGame::onUserSelectMove(const Index& i) {
232 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
233 if (entity->goTo(i))
234 return;
236 // fallback
237 goTo(i);
240 void GraphicalGame::onUserSetComment(const Index& i, QString s) {
241 setComment(i, s);
244 void GraphicalGame::onUserSetVComment(const Index& i, int v, QString s) {
245 setVComment(i, v, s);
248 void GraphicalGame::onUserClearVariations(const Index& i) {
249 clearVariations(i);
252 void GraphicalGame::onUserTruncate(const Index& i) {
253 if (i == index())
254 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
255 if (entity->truncate())
256 return;
258 // fallback
259 truncate(i);
262 void GraphicalGame::onUserPromoteVariation(const Index& i) {
263 if (i == index())
264 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
265 if (entity->promoteVariation())
266 return;
268 // fallback
269 promoteVariation(i);
272 void GraphicalGame::onUserRemoveVariation(const Index& i) {
273 removeVariation(i);
276 void GraphicalGame::onUserUndo() {
277 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
278 if (entity->undo())
279 return;
281 // fallback
282 undo();
285 void GraphicalGame::onUserRedo() {
286 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
287 if (entity->redo())
288 return;
290 // fallback
291 redo();
294 void GraphicalGame::onDetachNotifier() {
295 Q_ASSERT(m_movelist->getNotifier() == static_cast<MoveList::Notifier*>(this));
297 m_movelist = NULL;
300 void GraphicalGame::createCtrlAction() {
301 m_ctrl = boost::shared_ptr<CtrlAction>(new CtrlAction(this));
304 void GraphicalGame::destroyCtrlAction() {
305 m_ctrl.reset();