Continued refactoring.
[tagua/yd.git] / src / graphicalgame.cpp
blob24fc2a5414ed922771801c9422307fad70fd0340
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 "global.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, SLOT(settingsChanged()));
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 //BROKEN
68 #if 0
69 m_anim_sequence = m_graphical->getBoolSetting("animations", true)
70 && m_graphical->getBoolSetting("animations.sequence", true);
71 m_anim_sequence_max = m_graphical->getIntSetting("animations.sequence.max", 10);
72 #endif
75 void GraphicalGame::onAdded(const Index& ix) {
76 onAddedInternal(ix);
79 void GraphicalGame::onAddedInternal(const Index& ix, bool confirm_promotion) {
80 if(!m_movelist)
81 return;
83 int at;
84 History *vec = fetchRef(ix, &at);
85 if(!vec) {
86 std::cout << "--> Error in GraphicalGame::onAdded, invalid index "<< ix << std::endl;
87 return;
90 m_movelist->remove(ix, confirm_promotion); //clear existing, if any
92 Index index = ix;
93 for(int i=at;i<(int)vec->size();i++) {
94 Entry* e = &(*vec)[i];
95 PositionPtr prev = position(index.prev());
96 DecoratedMove mv = (e->move && prev) ? e->move->toDecoratedMove(prev) :
97 DecoratedMove() << MovePart(e->position ? "(-)" : "???");
98 int turn = prev ? prev->turn() : (index.totalNumMoves()+1)%2;
99 //mv += " " + QString::number(turn);
100 m_movelist->setMove(index, turn, mv, e->comment, confirm_promotion);
102 for (Variations::const_iterator it = e->variations.begin();
103 it != e->variations.end(); ++it)
104 onAddedInternal(index.next(it->first), confirm_promotion);
105 for (VComments::const_iterator it = e->vcomments.begin();
106 it != e->vcomments.end(); ++it)
107 m_movelist->setVComment(index, it->first, it->second, confirm_promotion);
109 index = index.next();
113 void GraphicalGame::onEntryChanged(const Index& at, int propagate) {
114 if(at <= Index(0)) {
115 Entry* e = fetch(at);
116 if(!e)
117 return;
118 if(at == current && e->position)
119 m_graphical->warp(e->move, e->position);
120 return;
123 if(m_movelist) {
124 Entry* e = fetch(at);
125 if(!e)
126 return;
128 Entry* pe = fetch(at.prev());
130 AbstractPosition::Ptr last_pos;
131 if (pe) last_pos = pe->position;
133 DecoratedMove mv = (e->move && last_pos) ? e->move->toDecoratedMove(last_pos) :
134 DecoratedMove() << MovePart(e->position ? "(-)" : "???");
135 int turn = last_pos ? last_pos->turn() : (at.totalNumMoves()+1)%2;
136 m_movelist->setMove(at, turn, mv, e->comment);
137 if(at == current && e->position)
138 m_graphical->warp(e->move, e->position);
140 // when an entry changes, chances are that we get some more information about the
141 // next ones as well
142 if(propagate) {
143 onEntryChanged(at.next(), propagate-1);
144 for (Variations::const_iterator it = e->variations.begin();
145 it != e->variations.end(); ++it)
146 onEntryChanged(at.next(it->first), propagate-1);
151 void GraphicalGame::onRemoved(const Index& i) {
152 if(m_movelist)
153 m_movelist->remove(i);
156 void GraphicalGame::onPromoteVariation(const Index& i, int v) {
157 if(m_movelist) {
158 m_movelist->promoteVariation(i,v);
159 onAddedInternal(i.next(), true);
160 onAddedInternal(i.next(v), true);
161 VComments vc = fetch(i)->vcomments;
162 VComments::const_iterator it = vc.find(v);
163 if(it != vc.end())
164 m_movelist->setVComment(i, v, it->second, true);
165 m_movelist->select(current, true);
169 void GraphicalGame::onSetComment(const Index& i, const QString& s) {
170 if(m_movelist)
171 m_movelist->setComment(i, s);
174 void GraphicalGame::onSetVComment(const Index& i, int v, const QString& s) {
175 if(m_movelist)
176 m_movelist->setVComment(i, v, s);
179 void GraphicalGame::onCurrentIndexChanged(const Index& old_c) {
180 if (m_ctrl) m_ctrl->forfait();
182 if(m_movelist)
183 m_movelist->select(current);
185 Entry *oe = fetch(old_c);
186 Entry *e = fetch(current);
187 std::pair<int, int> steps = old_c.stepsTo(current);
189 if(!e || !e->position)
190 return;
192 if(!oe || !oe->position) {
193 m_graphical->warp(move(), position());
194 return;
197 bool can_animate = (steps.first+steps.second <= 1) || (m_anim_sequence
198 && (steps.first+steps.second <= m_anim_sequence_max));
200 if(can_animate)
201 for(int i=1;i<=steps.first;i++)
202 if( !move(old_c.prev(i-1)) || !position(old_c.prev(i))) {
203 can_animate = false;
204 break;
207 if(can_animate)
208 for(int i=steps.second-1;i>=0;i--)
209 if( !move(current.prev(i)) || !position(current.prev(i))) {
210 can_animate = false;
211 break;
214 if(can_animate) {
215 for(int i=1;i<=steps.first;i++)
216 m_graphical->back( move(old_c.prev(i)), move(old_c.prev(i-1)), position(old_c.prev(i)));
217 for(int i=steps.second-1;i>=0;i--)
218 m_graphical->forward( move(current.prev(i)), position(current.prev(i)));
220 else
221 m_graphical->warp( move(), position());
224 void GraphicalGame::onAvailableUndo(bool e) {
225 if(m_movelist)
226 m_movelist->enableUndo(e);
229 void GraphicalGame::onAvailableRedo(bool e) {
230 if(m_movelist)
231 m_movelist->enableRedo(e);
234 void GraphicalGame::onUserSelectMove(const Index& i) {
235 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
236 if (entity->goTo(i))
237 return;
239 // fallback
240 goTo(i);
243 void GraphicalGame::onUserSetComment(const Index& i, QString s) {
244 setComment(i, s);
247 void GraphicalGame::onUserSetVComment(const Index& i, int v, QString s) {
248 setVComment(i, v, s);
251 void GraphicalGame::onUserClearVariations(const Index& i) {
252 clearVariations(i);
255 void GraphicalGame::onUserTruncate(const Index& i) {
256 if (i == index())
257 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
258 if (entity->truncate())
259 return;
261 // fallback
262 truncate(i);
265 void GraphicalGame::onUserPromoteVariation(const Index& i) {
266 if (i == index())
267 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
268 if (entity->promoteVariation())
269 return;
271 // fallback
272 promoteVariation(i);
275 void GraphicalGame::onUserRemoveVariation(const Index& i) {
276 removeVariation(i);
279 void GraphicalGame::onUserUndo() {
280 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
281 if (entity->undo())
282 return;
284 // fallback
285 undo();
288 void GraphicalGame::onUserRedo() {
289 if (boost::shared_ptr<UserEntity> entity = m_listener_entity.lock())
290 if (entity->redo())
291 return;
293 // fallback
294 redo();
297 void GraphicalGame::onDetachNotifier() {
298 Q_ASSERT(m_movelist->getNotifier() == static_cast<MoveList::Notifier*>(this));
300 m_movelist = NULL;
303 void GraphicalGame::createCtrlAction() {
304 m_ctrl = boost::shared_ptr<CtrlAction>(new CtrlAction(this));
307 void GraphicalGame::destroyCtrlAction() {
308 m_ctrl.reset();
311 #include "graphicalgame.moc"