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.
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());
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
) {
112 Entry
* e
= fetch(at
);
115 if(at
== current
&& e
->position
)
116 m_graphical
->warp(e
->move
, e
->position
);
121 Entry
* e
= fetch(at
);
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
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
) {
150 m_movelist
->remove(i
);
153 void GraphicalGame::onPromoteVariation(const Index
& i
, int v
) {
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
);
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
) {
168 m_movelist
->setComment(i
, s
);
171 void GraphicalGame::onSetVComment(const Index
& i
, int v
, const QString
& s
) {
173 m_movelist
->setVComment(i
, v
, s
);
176 void GraphicalGame::onCurrentIndexChanged(const Index
& old_c
) {
177 if (m_ctrl
) m_ctrl
->forfait();
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
)
189 if(!oe
|| !oe
->position
) {
190 m_graphical
->warp(move(), position());
194 bool can_animate
= (steps
.first
+steps
.second
<= 1) || (m_anim_sequence
195 && (steps
.first
+steps
.second
<= m_anim_sequence_max
));
198 for(int i
=1;i
<=steps
.first
;i
++)
199 if( !move(old_c
.prev(i
-1)) || !position(old_c
.prev(i
))) {
205 for(int i
=steps
.second
-1;i
>=0;i
--)
206 if( !move(current
.prev(i
)) || !position(current
.prev(i
))) {
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
)));
218 m_graphical
->warp( move(), position());
221 void GraphicalGame::onAvailableUndo(bool e
) {
223 m_movelist
->enableUndo(e
);
226 void GraphicalGame::onAvailableRedo(bool e
) {
228 m_movelist
->enableRedo(e
);
231 void GraphicalGame::onUserSelectMove(const Index
& i
) {
232 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
240 void GraphicalGame::onUserSetComment(const Index
& i
, QString s
) {
244 void GraphicalGame::onUserSetVComment(const Index
& i
, int v
, QString s
) {
245 setVComment(i
, v
, s
);
248 void GraphicalGame::onUserClearVariations(const Index
& i
) {
252 void GraphicalGame::onUserTruncate(const Index
& i
) {
254 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
255 if (entity
->truncate())
262 void GraphicalGame::onUserPromoteVariation(const Index
& i
) {
264 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
265 if (entity
->promoteVariation())
272 void GraphicalGame::onUserRemoveVariation(const Index
& i
) {
276 void GraphicalGame::onUserUndo() {
277 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
285 void GraphicalGame::onUserRedo() {
286 if (boost::shared_ptr
<UserEntity
> entity
= m_listener_entity
.lock())
294 void GraphicalGame::onDetachNotifier() {
295 Q_ASSERT(m_movelist
->getNotifier() == static_cast<MoveList::Notifier
*>(this));
300 void GraphicalGame::createCtrlAction() {
301 m_ctrl
= boost::shared_ptr
<CtrlAction
>(new CtrlAction(this));
304 void GraphicalGame::destroyCtrlAction() {