Initial porting to the new component API.
[tagua/yd.git] / src / chessboard.h
blob7f4084c53e725c2805c14ecded5dda7ccfaa38ed
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 "mainanimation.h"
30 #include "pointconverter.h"
31 #include "pixmaploader.h"
32 #include "clickablecanvas.h"
34 class Animation;
35 class AnimationSettings;
36 class BoardTags;
37 class ConstrainedText;
38 class DragInfo;
39 class IColor;
40 class INamer;
41 class UserEntity;
42 class Variant;
44 typedef boost::shared_ptr<BoardTags> BoardTagsPtr;
46 /**
47 * @class Board <board.h>
48 * @brief Board visualization class
50 * This class is a KGameCanvasItem that displaying a board filled with pieces.
51 * You can set custom tags for each square.
52 * @sa Table, @sa PiecePool, @sa Sprite
54 class ChessBoard : public QObject, public ClickableCanvas, protected PointConverter {
55 Q_OBJECT
57 public:
58 friend class GraphicalSystem;
59 friend class PiecePool;
60 typedef Grid<NamedSprite> PieceGrid;
62 private:
63 class DragInfo {
64 public:
65 static const int DRAG_THRESHOLD = 100; // pixels ^ 2
66 boost::shared_ptr<Sprite> sprite;
67 Point from; // logical coordinates
68 QPoint real; /// real starting point, used to honor drag threshold
69 bool dragging;
70 bool dragStarted;
71 bool droppedOut;
72 InteractionType action;
74 DragInfo(Point from, const QPoint& real,
75 const boost::shared_ptr<Sprite>& sprite,
76 InteractionType action)
77 : sprite(sprite)
78 , from(from)
79 , real(real)
80 , dragging(false)
81 , dragStarted(false)
82 , droppedOut(false)
83 , action(action) {}
86 typedef boost::shared_ptr<DragInfo> DragInfoPtr;
88 /** true if the board is flipped */
89 bool m_flipped;
91 /** size of a square in points */
92 int m_square_size;
94 /** size of the border */
95 int m_border_size;
97 /** margin for text */
98 int m_border_text_near;
100 /** far margin for text */
101 int m_border_text_far;
103 /** loader class, to load pieces */
104 PixmapLoader m_loader;
106 /** main animation structure */
107 MainAnimation* m_main_animation;
109 /** the piece being dragged */
110 DragInfoPtr m_drag_info;
112 /** displayed m_sprites */
113 PieceGrid m_sprites;
115 /** the visual move hint */
116 NamedSprite m_hinting;
117 Point m_hinting_pos;
119 /** the canvas group that holds the pieces */
120 KGameCanvasGroup *m_pieces_group;
122 /** the canvas item that holds the background of the board */
123 KGameCanvasGroup *m_canvas_background;
125 /** the canvas item that holds board border */
126 KGameCanvasGroup *m_canvas_border;
128 /** the canvas item that holds board border text */
129 KGameCanvasGroup *m_canvas_border_text;
131 /** all the tags on the board */
132 BoardTagsPtr m_tags;
134 /** the @a PixmapLoader used to create tags */
135 PixmapLoader m_tags_loader;
137 /** the @a PixmapLoader used for controls */
138 PixmapLoader m_controls_loader;
140 /** the entity responsible of all the interctions with the board */
141 boost::weak_ptr<UserEntity> m_entity;
143 /** The currently selected square */
144 Point selection;
146 /** The reviously selected square */
147 Point lastSelection;
149 /** premove */
150 Point m_premove_from;
151 Point m_premove_to;
153 QColor m_border_text_color;
155 QFont m_border_font;
157 QStringList m_border_coords;
159 const IColor* m_dropped_pool;
160 int m_dropped_index;
162 const AnimationSettings& m_anim_settings;
164 INamer* m_namer;
166 /** the text items for the border */
167 std::vector<ConstrainedText*> m_border_text;
169 /** this internal function updates the background after the board has been resized */
170 void updateBackground();
172 /** this internal function updates the border after the board has been resized */
173 void updateBorder();
175 /** this internal function creates a new border, if needed */
176 void recreateBorder();
178 void updateHinting(Point pt, Piece piece);
180 /** this internal function updates the sprites after the board has been resized */
181 void updateSprites();
183 /** this internal function updates the tags after the board has been resized */
184 void updateTags();
186 void setPremove(const Move& move);
187 void updatePremove();
188 void setSelection(const Point& p);
190 QRect computeRect(Point) const;
191 QRegion computeRegion(Point) const;
192 QRect squareRect(int x, int y) const;
194 /** this function tries to notify the move to the entity */
195 bool doMove(Move&);
197 public:
198 /** constructor, requires the canvas parent */
199 ChessBoard(Variant* variant, const AnimationSettings& animSettings, KGameCanvasAbstract* parent);
200 ~ChessBoard();
202 /** set the board flipping */
203 void flip(bool flipped);
205 /** flips the board */
206 void flip() { flip(!m_flipped); }
208 /** returns the flipped value */
209 bool flipped() const { return m_flipped; }
211 /** returns the size of a square */
212 int squareSize() const { return m_square_size; }
214 /** returns the area covered by the piece grid */
215 QRect boardRect() { return QRect(pos(), QSize(m_square_size*gridSize().x,
216 m_square_size*gridSize().y)); }
218 /** returns the size of the grid */
219 Point gridSize() const { return m_sprites.getSize(); }
221 /** recreates the board underlying grid */
222 void createGrid(Point p, const QStringList& border_coords);
224 /** return the group that contains the pieces */
225 KGameCanvasAbstract* piecesGroup() { return m_pieces_group; }
227 /** returns the point converter class */
228 PointConverter* converter() { return static_cast<PointConverter*>(this); }
230 /** returns the point converter class */
231 const PointConverter* converter() const { return static_cast<const PointConverter*>(this); }
233 /** returns the sprite loader */
234 PixmapLoader* loader() { return &m_loader; }
236 /** Load a sprite using the sprite loader */
237 QPixmap loadSprite(const QString& id);
239 /** returns the sprite loader */
240 const PixmapLoader* loader() const { return &m_loader; }
243 /** adds a tag with name name, the tag will stay over the pieces if over is true */
244 boost::shared_ptr<KGameCanvasPixmap> addTag(const QString& name, Point at, bool over=false);
246 /** clears the tags with name name */
247 void clearTags(const QString& name);
249 /** clears all the tags */
250 void clearTags();
252 /** clear all the tags twith name name, than sets them to the valid given ones */
253 void setTags(const QString& name, Point p1 = Point::invalid(), Point p2 = Point::invalid(),
254 Point p3 = Point::invalid(), Point p4 = Point::invalid(),
255 Point p5 = Point::invalid(), Point p6 = Point::invalid() );
257 /** returns a reference to the loader used to load tag pixmaps */
258 PixmapLoader* tagsLoader() { return &m_tags_loader; }
260 /** returns a reference to the loader used to load controls pixmaps */
261 PixmapLoader* controlsLoader() { return &m_controls_loader; }
264 void cancelPremove();
266 void cancelSelection();
269 /** enqueue an animation */
270 void enqueue(const boost::shared_ptr<Animation>&);
272 /** Brings the sprite to its correct position */
273 void adjustSprite(const Point& p, bool immediate = false);
275 /** sets the controlling entity */
276 inline void setEntity(const boost::shared_ptr<UserEntity>& entity) { m_entity = entity; }
279 /** Notifies to the board that a certain piece is being dragged over the board */
280 void draggingOn(const IColor* pool, int index, const QPoint& p);
282 /** Executes a drop. This function id typically called by by a PiecePool */
283 bool dropOn(const IColor* pool, int index, const QPoint& point);
285 /** mouse release event */
286 virtual void onMouseRelease(const QPoint& pos, int button);
288 /** mouse press event */
289 virtual void onMousePress(const QPoint& pos, int button);
291 /** mouse move event */
292 virtual void onMouseMove(const QPoint& pos, int button);
294 /** mouse leave event */
295 virtual void onMouseLeave();
297 /** resize event (new_size is the new size of a SQUARE) */
298 virtual void onResize(int new_size, int border_size, int border_text_near,
299 int border_text_far, bool force_reload = false);
301 /** the position changed (will update the move highlighting) */
302 virtual void onPositionChanged();
304 /** changed settings handler */
305 virtual void settingsChanged();
307 /** resets all tags and stops all animations */
308 void reset();
310 Q_SIGNALS:
311 void error(ErrorCode code);
314 #endif // CHESSBOARD_H