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