Cleaned up CrazyhouseMove.
[tagua/yd.git] / src / board.h
blob741c84edf0ef07ee76f26d7842e0b41facd1427e
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 "piecegroup.h"
26 #include "namedsprite.h"
28 class DragInfo;
29 class Animation;
30 class UserEntity;
32 class BoardTags;
33 typedef boost::shared_ptr<BoardTags> BoardTagsPtr;
35 /**
36 * @class Board <board.h>
37 * @brief Board visualization class
39 * This class is a KGameCanvasItem that displaying a board filled with pieces.
40 * You can set custom tags for each square.
41 * @sa Table, @sa PiecePool, @sa Sprite
43 class Board : public QObject, public PieceGroup {
44 Q_OBJECT
46 public:
47 friend class GraphicalSystem;
48 friend class PiecePool;
49 typedef Grid<NamedSprite> PieceGrid;
51 private:
52 class DragInfo {
53 public:
54 static const int DRAG_THRESHOLD = 100; // pixels ^ 2
55 boost::shared_ptr<Sprite> sprite;
56 Point from; // logical coordinates
57 QPoint real; /// real starting point, used to honor drag threshold
58 bool dragging;
59 bool dragStarted;
60 bool droppedOut;
61 InteractionType action;
63 DragInfo(Point from, const QPoint& real,
64 const boost::shared_ptr<Sprite>& sprite,
65 InteractionType action)
66 : sprite(sprite)
67 , from(from)
68 , real(real)
69 , dragging(false)
70 , dragStarted(false)
71 , droppedOut(false)
72 , action(action) {}
75 typedef boost::shared_ptr<DragInfo> DragInfoPtr;
78 /** the piece being dragged */
79 DragInfoPtr m_drag_info;
81 /** displayed m_sprites */
82 PieceGrid m_sprites;
84 #if 0
85 /** used by a PiecePool to make available the piece that is being dropped
86 on the board to the GraphicalInfo and the variant-specific animator */
87 NamedSprite m_drop_sprite;
88 #endif
90 /** the visual move hint */
91 NamedSprite m_hinting;
92 Point m_hinting_pos;
94 /** the canvas group that holds the pieces */
95 KGameCanvasGroup *m_pieces_group;
97 /** the canvas item that is used as background of the board */
98 KGameCanvasGroup *m_canvas_background;
100 /** all the tags on the board */
101 BoardTagsPtr m_tags;
103 /** the @a PixmapLoader used to create tags */
104 PixmapLoader m_tags_loader;
106 /** the entity responsible of all the interctions with the board */
107 boost::weak_ptr<UserEntity> m_entity;
109 /** The currently selected square */
110 Point selection;
112 /** The reviously selected square */
113 Point lastSelection;
115 /** premove */
116 Point m_premove_from;
117 Point m_premove_to;
119 /** size of the border */
120 int m_border_size;
122 /** ascent of the font used for the border */
123 int m_border_asc;
125 /** show the border if true */
126 bool m_show_border;
128 QColor m_border_color;
130 QColor m_border_text_color;
132 QFont m_border_font;
134 QStringList m_border_coords;
136 int m_dropped_pool;
137 int m_dropped_index;
139 /** the rectangles used to draw the border */
140 std::vector<boost::shared_ptr<KGameCanvasRectangle> > m_border_margins;
142 /** the text items for the border */
143 std::vector<boost::shared_ptr<KGameCanvasItem> > m_border_items;
145 /** this internal function updates the background after the board has been resized */
146 void updateBackground();
148 /** this internal function updates the border after the board has been resized */
149 void updateBorder();
151 /** this internal function creates a new border, if needed */
152 void recreateBorder();
154 void updateHinting(Point pt, AbstractPiece::Ptr piece);
156 /** this internal function updates the sprites after the board has been resized */
157 void updateSprites();
159 /** this internal function updates the tags after the board has been resized */
160 void updateTags();
162 void mySettingsChanged();
164 void setPremove(const NormalUserMove& move);
165 void setPremove(const DropUserMove& move);
166 void setPremove(const class Premove& move);
167 void updatePremove();
168 void setSelection(const Point& p);
170 QRect computeRect(Point) const;
171 QRegion computeRegion(Point) const;
172 QRect squareRect(int x, int y) const;
174 /** this function tries to notify the move to the entity */
175 bool doMove(const NormalUserMove&);
177 /** fetch the sprite */
178 boost::shared_ptr<Sprite> spriteAt(const Point& p) { return m_sprites[p].sprite(); }
180 public:
181 /** constructor, requires the canvas parent */
182 Board(KGameCanvasAbstract* parent);
183 ~Board();
185 /** set the board flipping */
186 void flip(bool flipped);
188 /** flips the board */
189 inline void flip() { flip(!m_flipped); }
191 /** adds a tag with name name, the tag will stay over the pieces if over is true */
192 boost::shared_ptr<KGameCanvasPixmap> addTag(const QString& name, Point at, bool over=false);
194 /** clears the tags with name name */
195 void clearTags(const QString& name);
197 /** clears all the tags */
198 void clearTags();
200 /** clear all the tags twith name name, than sets them to the valid given ones */
201 void setTags(const QString& name, Point p1 = Point::invalid(), Point p2 = Point::invalid(),
202 Point p3 = Point::invalid(), Point p4 = Point::invalid(),
203 Point p5 = Point::invalid(), Point p6 = Point::invalid() );
205 /** returns a reference to the loader used to load tag pixmaps */
206 PixmapLoader* tagsLoader() { return &m_tags_loader; }
209 void cancelPremove();
210 void cancelSelection();
212 /** recreates the board underlying grid */
213 void createGrid(Point p, const QStringList& border_coords);
215 /** returns the size of the border */
216 int marginSize(){ return m_border_size+1; };
219 /** sets the controlling entity */
220 inline void setEntity(const boost::shared_ptr<UserEntity>& entity) { m_entity = entity; }
223 /** Notifies to the board that a certain piece is being dragged over the board */
224 void draggingOn(int pool, int index, const QPoint& p);
226 /** Executes a drop. This function id typically called by by a PiecePool */
227 bool dropOn(int pool, int index, const QPoint& point);
229 /** returns the size of the grid */
230 virtual Point gridSize() const { return m_sprites.getSize(); }
232 /** return the group that contains the pieces */
233 virtual KGameCanvasAbstract* piecesGroup() { return m_pieces_group; }
235 /** mouse release event */
236 virtual void onMouseRelease(const QPoint& pos, int button);
238 /** mouse press event */
239 virtual void onMousePress(const QPoint& pos, int button);
241 /** mouse move event */
242 virtual void onMouseMove(const QPoint& pos, int button);
244 /** mouse leave event */
245 virtual void onMouseLeave();
247 /** resize event (new_size is the new size of a SQUARE) */
248 virtual void onResize(int new_size, bool force_reload = false);
250 /** the position changed (will update the move highlighting) */
251 virtual void onPositionChanged();
253 /** changed settings handler */
254 virtual void settingsChanged();
256 /** resets all tags and stops all animations */
257 void reset();
259 signals:
260 void error(ErrorCode code);
263 #endif //BOARD_H