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?
29 CtrlAction(Game
* game
)
31 , m_index(game
->index()) {
32 m_done
= m_game
->back();
35 void forfait() { m_done
= false; }
38 if (m_done
) m_game
->goTo(m_index
);
42 GraphicalGame::GraphicalGame(GraphicalSystem
* graphical
,
45 , m_graphical(graphical
)
47 , m_anim_sequence(false) {
50 m_movelist
->setLayoutStyle(graphical
->m_variant
->moveListLayout());
51 m_movelist
->setNotifier( static_cast<MoveList::Notifier
*>(this) );
54 settings().onChange(this, "settingsChanged", "Loader::Theme");
58 GraphicalGame::~GraphicalGame() {
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
) {
76 void GraphicalGame::onAddedInternal(const Index
& ix
, bool confirm_promotion
) {
81 History
*vec
= fetchRef(ix
, &at
);
83 ERROR("invalid index " << ix
);
87 m_movelist
->remove(ix
, confirm_promotion
); //clear existing, if any
90 for(int i
=at
;i
<(int)vec
->size();i
++) {
91 Entry
* e
= &(*vec
)[i
];
92 PositionPtr prev
= position(index
.prev());
95 e
->move
->toString("decorated", prev
) :
96 (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
) {
115 Entry
* e
= fetch(at
);
118 if(at
== current
&& e
->position
)
119 m_graphical
->warp(e
->move
, e
->position
);
124 Entry
* e
= fetch(at
);
128 Entry
* pe
= fetch(at
.prev());
130 AbstractPosition::Ptr last_pos
;
131 if (pe
) last_pos
= pe
->position
;
134 (e
->move
&& last_pos
) ?
135 e
->move
->toString("decorated", last_pos
) :
136 (e
->position
? "(-)" : "???"));
137 int turn
= last_pos
? last_pos
->turn() : (at
.totalNumMoves()+1)%2;
138 m_movelist
->setMove(at
, turn
, mv
, e
->comment
);
139 if(at
== current
&& e
->position
)
140 m_graphical
->warp(e
->move
, e
->position
);
142 // when an entry changes, chances are that we get some more information about the
145 onEntryChanged(at
.next(), propagate
-1);
146 for (Variations::const_iterator it
= e
->variations
.begin();
147 it
!= e
->variations
.end(); ++it
)
148 onEntryChanged(at
.next(it
->first
), propagate
-1);
153 void GraphicalGame::onRemoved(const Index
& i
) {
155 m_movelist
->remove(i
);
158 void GraphicalGame::onPromoteVariation(const Index
& i
, int v
) {
160 m_movelist
->promoteVariation(i
,v
);
161 onAddedInternal(i
.next(), true);
162 onAddedInternal(i
.next(v
), true);
163 VComments vc
= fetch(i
)->vcomments
;
164 VComments::const_iterator it
= vc
.find(v
);
166 m_movelist
->setVComment(i
, v
, it
->second
, true);
167 m_movelist
->select(current
, true);
171 void GraphicalGame::onSetComment(const Index
& i
, const QString
& s
) {
173 m_movelist
->setComment(i
, s
);
176 void GraphicalGame::onSetVComment(const Index
& i
, int v
, const QString
& s
) {
178 m_movelist
->setVComment(i
, v
, s
);
181 void GraphicalGame::onCurrentIndexChanged(const Index
& old_c
) {
182 if (m_ctrl
) m_ctrl
->forfait();
185 m_movelist
->select(current
);
187 Entry
*oe
= fetch(old_c
);
188 Entry
*e
= fetch(current
);
189 std::pair
<int, int> steps
= old_c
.stepsTo(current
);
191 if(!e
|| !e
->position
)
194 if(!oe
|| !oe
->position
) {
195 m_graphical
->warp(move(), position());
199 bool can_animate
= (steps
.first
+steps
.second
<= 1) || (m_anim_sequence
200 && (steps
.first
+steps
.second
<= m_anim_sequence_max
));
203 for(int i
=1;i
<=steps
.first
;i
++)
204 if( !move(old_c
.prev(i
-1)) || !position(old_c
.prev(i
))) {
210 for(int i
=steps
.second
-1;i
>=0;i
--)
211 if( !move(current
.prev(i
)) || !position(current
.prev(i
))) {
217 for(int i
=1;i
<=steps
.first
;i
++)
218 m_graphical
->back( move(old_c
.prev(i
)), move(old_c
.prev(i
-1)), position(old_c
.prev(i
)));
219 for(int i
=steps
.second
-1;i
>=0;i
--)
220 m_graphical
->forward( move(current
.prev(i
)), position(current
.prev(i
)));
223 m_graphical
->warp( move(), position());
226 void GraphicalGame::onAvailableUndo(bool e
) {
228 m_movelist
->enableUndo(e
);
231 void GraphicalGame::onAvailableRedo(bool e
) {
233 m_movelist
->enableRedo(e
);
236 void GraphicalGame::onUserSelectMove(const Index
& i
) {
237 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
245 void GraphicalGame::onUserSetComment(const Index
& i
, QString s
) {
249 void GraphicalGame::onUserSetVComment(const Index
& i
, int v
, QString s
) {
250 setVComment(i
, v
, s
);
253 void GraphicalGame::onUserClearVariations(const Index
& i
) {
257 void GraphicalGame::onUserTruncate(const Index
& i
) {
259 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
260 if (entity
->truncate())
267 void GraphicalGame::onUserPromoteVariation(const Index
& i
) {
269 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
270 if (entity
->promoteVariation())
277 void GraphicalGame::onUserRemoveVariation(const Index
& i
) {
281 void GraphicalGame::onUserUndo() {
282 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
290 void GraphicalGame::onUserRedo() {
291 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
299 void GraphicalGame::onDetachNotifier() {
300 Q_ASSERT(m_movelist
->getNotifier() == static_cast<MoveList::Notifier
*>(this));
305 void GraphicalGame::createCtrlAction() {
306 m_ctrl
= boost::shared_ptr
<CtrlAction
>(new CtrlAction(this));
309 void GraphicalGame::destroyCtrlAction() {