Fix use of singleton pattern.
[tagua/yd.git] / src / chessboard.h
blob9da00f5717a508c9b21efefbf5883d5b41dfd2e7
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 #ifndef CHESSBOARD_H
12 #define CHESSBOARD_H
14 #include <boost/shared_ptr.hpp>
15 #include <boost/weak_ptr.hpp>
16 #include <QObject>
17 #include <QFont>
18 #include <KGameCanvas>
19 #include <boost/shared_ptr.hpp>
21 #include <core/move.h>
22 #include <core/namedsprite.h>
23 #include <core/piece.h>
25 #include "entities/userentity.h"
26 #include "common.h"
27 #include "grid.h"
28 #include "usermove.h"
29 #include "pointconverter.h"
30 #include "pixmaploader.h"
31 #include "clickablecanvas.h"
33 class Animation;
34 class AnimationManager;
35 class BoardTags;
36 class Components;
37 class ConstrainedText;
38 class DragInfo;
39 class IColor;
40 class INamer;
41 class UserEntity;
43 typedef boost::shared_ptr<BoardTags> BoardTagsPtr;
45 /**
46 * @brief Board visualization class
48 * This class is a KGameCanvasItem that displaying a board filled with pieces.
49 * You can set custom tags for each square.
50 * @sa Table, @sa PiecePool, @sa Sprite
52 class ChessBoard : public QObject, public ClickableCanvas, protected PointConverter {
53 Q_OBJECT
55 public:
56 friend class GraphicalSystem;
57 friend class PiecePool;
58 typedef Grid<NamedSprite> PieceGrid;
60 private:
61 class DragInfo {
62 public:
63 static const int DRAG_THRESHOLD = 100; // pixels ^ 2
64 boost::shared_ptr<Sprite> sprite;
65 Point from; // logical coordinates
66 QPoint real; /// real starting point, used to honor drag threshold
67 bool dragging;
68 bool dragStarted;
69 bool droppedOut;
70 InteractionType action;
72 DragInfo(Point from, const QPoint& real,
73 const boost::shared_ptr<Sprite>& sprite,
74 InteractionType action)
75 : sprite(sprite)
76 , from(from)
77 , real(real)
78 , dragging(false)
79 , dragStarted(false)
80 , droppedOut(false)
81 , action(action) {}
84 typedef boost::shared_ptr<DragInfo> DragInfoPtr;
86 /** true if the board is flipped */
87 bool m_flipped;
89 /** size of a square in points */
90 int m_square_size;
92 /** size of the border */
93 int m_border_size;
95 /** margin for text */
96 int m_border_text_near;
98 /** far margin for text */
99 int m_border_text_far;
101 /** loader class, to load pieces */
102 PixmapLoader m_loader;
104 /** the piece being dragged */
105 DragInfoPtr m_drag_info;
107 /** displayed m_sprites */
108 PieceGrid m_sprites;
110 /** the visual move hint */
111 NamedSprite m_hinting;
112 Point m_hinting_pos;
114 /** the canvas group that holds the pieces */
115 KGameCanvasGroup *m_pieces_group;
117 /** the canvas item that holds the background of the board */
118 KGameCanvasGroup *m_canvas_background;
120 /** the canvas item that holds board border */
121 KGameCanvasGroup *m_canvas_border;
123 /** the canvas item that holds board border text */
124 KGameCanvasGroup *m_canvas_border_text;
126 /** all the tags on the board */
127 BoardTagsPtr m_tags;
129 /** the @a PixmapLoader used to create tags */
130 PixmapLoader m_tags_loader;
132 /** the @a PixmapLoader used for controls */
133 PixmapLoader m_controls_loader;
135 /** the entity responsible of all the interctions with the board */
136 boost::weak_ptr<UserEntity> m_entity;
138 /** The currently selected square */
139 Point selection;
141 /** The reviously selected square */
142 Point lastSelection;
144 /** premove */
145 Point m_premove_from;
146 Point m_premove_to;
148 QColor m_border_text_color;
150 QFont m_border_font;
152 QStringList m_border_coords;
154 const IColor* m_dropped_pool;
155 int m_dropped_index;
157 AnimationManager* m_anim_manager;
159 INamer* m_namer;
161 /** the text items for the border */
162 std::vector<ConstrainedText*> m_border_text;
164 /** this internal function updates the background after the board has been resized */
165 void updateBackground();
167 /** this internal function updates the border after the board has been resized */
168 void updateBorder();
170 /** this internal function creates a new border, if needed */
171 void recreateBorder();
173 void updateHinting(Point pt, Piece piece);
175 /** this internal function updates the sprites after the board has been resized */
176 void updateSprites();
178 /** this internal function updates the tags after the board has been resized */
179 void updateTags();
181 void setPremove(const Move& move);
182 void updatePremove();
183 void setSelection(const Point& p);
185 QRect computeRect(Point) const;
186 QRegion computeRegion(Point) const;
187 QRect squareRect(int x, int y) const;
189 /** this function tries to notify the move to the entity */
190 bool doMove(Move&);
192 public:
193 /** constructor, requires the canvas parent */
194 ChessBoard(Components* components,
195 AnimationManager* animManager,
196 KGameCanvasAbstract* parent);
197 ~ChessBoard();
199 /** set the board flipping */
200 void flip(bool flipped);
202 /** flips the board */
203 void flip() { flip(!m_flipped); }
205 /** returns the flipped value */
206 bool flipped() const { return m_flipped; }
208 /** returns the size of a square */
209 int squareSize() const { return m_square_size; }
211 /** returns the area covered by the piece grid */
212 QRect boardRect() { return QRect(pos(), QSize(m_square_size*gridSize().x,
213 m_square_size*gridSize().y)); }
215 /** returns the size of the grid */
216 Point gridSize() const { return m_sprites.getSize(); }
218 /** recreates the board underlying grid */
219 void createGrid(Point p, const QStringList& border_coords);
221 /** return the group that contains the pieces */
222 KGameCanvasAbstract* piecesGroup() { return m_pieces_group; }
224 /** returns the point converter class */
225 PointConverter* converter() { return static_cast<PointConverter*>(this); }
227 /** returns the point converter class */
228 const PointConverter* converter() const { return static_cast<const PointConverter*>(this); }
230 /** returns the sprite loader */
231 PixmapLoader* loader() { return &m_loader; }
233 /** Load a sprite using the sprite loader */
234 QPixmap loadSprite(const QString& id);
236 /** returns the sprite loader */
237 const PixmapLoader* loader() const { return &m_loader; }
240 /** adds a tag with name name, the tag will stay over the pieces if over is true */
241 boost::shared_ptr<KGameCanvasPixmap> addTag(const QString& name, Point at, bool over=false);
243 /** clears the tags with name name */
244 void clearTags(const QString& name);
246 /** clears all the tags */
247 void clearTags();
249 /** clear all the tags twith name name, than sets them to the valid given ones */
250 void setTags(const QString& name, Point p1 = Point::invalid(), Point p2 = Point::invalid(),
251 Point p3 = Point::invalid(), Point p4 = Point::invalid(),
252 Point p5 = Point::invalid(), Point p6 = Point::invalid() );
254 /** returns a reference to the loader used to load tag pixmaps */
255 PixmapLoader* tagsLoader() { return &m_tags_loader; }
257 /** returns a reference to the loader used to load controls pixmaps */
258 PixmapLoader* controlsLoader() { return &m_controls_loader; }
261 void cancelPremove();
263 void cancelSelection();
265 /** Brings the sprite to its correct position */
266 void adjustSprite(const Point& p, bool immediate = false);
268 /** sets the controlling entity */
269 inline void setEntity(const boost::shared_ptr<UserEntity>& entity) { m_entity = entity; }
272 /** Notifies to the board that a certain piece is being dragged over the board */
273 void draggingOn(const IColor* pool, int index, const QPoint& p);
275 /** Executes a drop. This function id typically called by by a PiecePool */
276 bool dropOn(const IColor* pool, int index, const QPoint& point);
278 /** mouse release event */
279 virtual void onMouseRelease(const QPoint& pos, int button);
281 /** mouse press event */
282 virtual void onMousePress(const QPoint& pos, int button);
284 /** mouse move event */
285 virtual void onMouseMove(const QPoint& pos, int button);
287 /** mouse leave event */
288 virtual void onMouseLeave();
290 /** resize event (new_size is the new size of a SQUARE) */
291 virtual void onResize(int new_size, int border_size, int border_text_near,
292 int border_text_far, bool force_reload = false);
294 /** the position changed (will update the move highlighting) */
295 virtual void onPositionChanged();
297 /** changed settings handler */
298 virtual void settingsChanged();
300 /** resets all tags and stops all animations */
301 void reset();
303 Q_SIGNALS:
304 void error(ErrorCode code);
307 #endif // CHESSBOARD_H