Removed old commented out code.
[tagua/yd.git] / src / board.h
blob5e78a526ff966eb4395c8a32a9a8c0853bce18f0
1 /*
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.
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;
36 class BoardTags;
37 typedef boost::shared_ptr<BoardTags> BoardTagsPtr;
39 /**
40 * @class Board <board.h>
41 * @brief Board visualization class
43 * This class is a KGameCanvasItem that displaying a board filled with pieces.
44 * You can set custom tags for each square.
45 * @sa Table, @sa PiecePool, @sa Sprite
47 class Board : public QObject, public ClickableCanvas, protected PointConverter {
48 Q_OBJECT
50 public:
51 friend class GraphicalSystem;
52 friend class PiecePool;
53 typedef Grid<NamedSprite> PieceGrid;
55 private:
56 class DragInfo {
57 public:
58 static const int DRAG_THRESHOLD = 100; // pixels ^ 2
59 boost::shared_ptr<Sprite> sprite;
60 Point from; // logical coordinates
61 QPoint real; /// real starting point, used to honor drag threshold
62 bool dragging;
63 bool dragStarted;
64 bool droppedOut;
65 InteractionType action;
67 DragInfo(Point from, const QPoint& real,
68 const boost::shared_ptr<Sprite>& sprite,
69 InteractionType action)
70 : sprite(sprite)
71 , from(from)
72 , real(real)
73 , dragging(false)
74 , dragStarted(false)
75 , droppedOut(false)
76 , action(action) {}
79 typedef boost::shared_ptr<DragInfo> DragInfoPtr;
81 /** true if the board is flipped */
82 bool m_flipped;
84 /** size of a square in points */
85 int m_square_size;
87 /** size of the border */
88 int m_border_size;
90 /** margin for text */
91 int m_border_text_near;
93 /** far margin for text */
94 int m_border_text_far;
96 /** loader class, to load pieces */
97 PixmapLoader m_loader;
99 /** main animation structure */
100 MainAnimation* m_main_animation;
102 /** the piece being dragged */
103 DragInfoPtr m_drag_info;
105 /** displayed m_sprites */
106 PieceGrid m_sprites;
108 /** the visual move hint */
109 NamedSprite m_hinting;
110 Point m_hinting_pos;
112 /** the canvas group that holds the pieces */
113 KGameCanvasGroup *m_pieces_group;
115 /** the canvas item that holds the background of the board */
116 KGameCanvasGroup *m_canvas_background;
118 /** the canvas item that holds board border */
119 KGameCanvasGroup *m_canvas_border;
121 /** the canvas item that holds board border text */
122 KGameCanvasGroup *m_canvas_border_text;
124 /** all the tags on the board */
125 BoardTagsPtr m_tags;
127 /** the @a PixmapLoader used to create tags */
128 PixmapLoader m_tags_loader;
130 /** the @a PixmapLoader used for controls */
131 PixmapLoader m_controls_loader;
133 /** the entity responsible of all the interctions with the board */
134 boost::weak_ptr<UserEntity> m_entity;
136 /** The currently selected square */
137 Point selection;
139 /** The reviously selected square */
140 Point lastSelection;
142 /** premove */
143 Point m_premove_from;
144 Point m_premove_to;
146 QColor m_border_text_color;
148 QFont m_border_font;
150 QStringList m_border_coords;
152 int m_dropped_pool;
153 int m_dropped_index;
155 /** the text items for the border */
156 std::vector<ConstrainedText*> m_border_text;
158 /** this internal function updates the background after the board has been resized */
159 void updateBackground();
161 /** this internal function updates the border after the board has been resized */
162 void updateBorder();
164 /** this internal function creates a new border, if needed */
165 void recreateBorder();
167 void updateHinting(Point pt, AbstractPiece::Ptr piece);
169 /** this internal function updates the sprites after the board has been resized */
170 void updateSprites();
172 /** this internal function updates the tags after the board has been resized */
173 void updateTags();
175 void setPremove(const NormalUserMove& move);
176 void setPremove(const DropUserMove& move);
177 void setPremove(const class Premove& move);
178 void updatePremove();
179 void setSelection(const Point& p);
181 QRect computeRect(Point) const;
182 QRegion computeRegion(Point) const;
183 QRect squareRect(int x, int y) const;
185 /** this function tries to notify the move to the entity */
186 bool doMove(const NormalUserMove&);
188 public:
189 /** constructor, requires the canvas parent */
190 Board(KGameCanvasAbstract* parent);
191 ~Board();
193 /** set the board flipping */
194 void flip(bool flipped);
196 /** flips the board */
197 void flip() { flip(!m_flipped); }
199 /** returns the flipped value */
200 bool flipped() const { return m_flipped; }
202 /** returns the size of a square */
203 int squareSize() const { return m_square_size; }
205 /** returns the area covered by the piece grid */
206 QRect boardRect() { return QRect(pos(), QSize(m_square_size*gridSize().x,
207 m_square_size*gridSize().y)); }
209 /** returns the size of the grid */
210 Point gridSize() const { return m_sprites.getSize(); }
212 /** recreates the board underlying grid */
213 void createGrid(Point p, const QStringList& border_coords);
215 /** return the group that contains the pieces */
216 KGameCanvasAbstract* piecesGroup() { return m_pieces_group; }
218 /** returns the point converter class */
219 PointConverter* converter() { return static_cast<PointConverter*>(this); }
221 /** returns the point converter class */
222 const PointConverter* converter() const { return static_cast<const PointConverter*>(this); }
224 /** returns the sprite loader */
225 PixmapLoader* loader() { return &m_loader; }
227 /** returns the sprite loader */
228 const PixmapLoader* loader() const { return &m_loader; }
231 /** adds a tag with name name, the tag will stay over the pieces if over is true */
232 boost::shared_ptr<KGameCanvasPixmap> addTag(const QString& name, Point at, bool over=false);
234 /** clears the tags with name name */
235 void clearTags(const QString& name);
237 /** clears all the tags */
238 void clearTags();
240 /** clear all the tags twith name name, than sets them to the valid given ones */
241 void setTags(const QString& name, Point p1 = Point::invalid(), Point p2 = Point::invalid(),
242 Point p3 = Point::invalid(), Point p4 = Point::invalid(),
243 Point p5 = Point::invalid(), Point p6 = Point::invalid() );
245 /** returns a reference to the loader used to load tag pixmaps */
246 PixmapLoader* tagsLoader() { return &m_tags_loader; }
248 /** returns a reference to the loader used to load controls pixmaps */
249 PixmapLoader* controlsLoader() { return &m_controls_loader; }
252 void cancelPremove();
254 void cancelSelection();
257 /** enqueue an animation */
258 void enqueue(const boost::shared_ptr<Animation>&);
260 /** Brings the sprite to its correct position */
261 void adjustSprite(const Point& p, bool immediate = false);
263 /** sets the controlling entity */
264 inline void setEntity(const boost::shared_ptr<UserEntity>& entity) { m_entity = entity; }
267 /** Notifies to the board that a certain piece is being dragged over the board */
268 void draggingOn(int pool, int index, const QPoint& p);
270 /** Executes a drop. This function id typically called by by a PiecePool */
271 bool dropOn(int pool, int index, const QPoint& point);
273 /** mouse release event */
274 virtual void onMouseRelease(const QPoint& pos, int button);
276 /** mouse press event */
277 virtual void onMousePress(const QPoint& pos, int button);
279 /** mouse move event */
280 virtual void onMouseMove(const QPoint& pos, int button);
282 /** mouse leave event */
283 virtual void onMouseLeave();
285 /** resize event (new_size is the new size of a SQUARE) */
286 virtual void onResize(int new_size, int border_size, int border_text_near,
287 int border_text_far, bool force_reload = false);
289 /** the position changed (will update the move highlighting) */
290 virtual void onPositionChanged();
292 /** changed settings handler */
293 virtual void settingsChanged();
295 /** resets all tags and stops all animations */
296 void reset();
298 signals:
299 void error(ErrorCode code);
302 #endif //BOARD_H