1 /***************************************************************************
2  *      Mechanized Assault and Exploration Reloaded Projectfile            *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 
20 #ifndef ui_graphical_game_control_mousemode_mousemodeH
21 #define ui_graphical_game_control_mousemode_mousemodeH
22 
23 #include <memory>
24 
25 #include "ui/graphical/game/control/mousemode/mousemodetype.h"
26 #include "utility/signal/signal.h"
27 #include "utility/signal/signalconnectionmanager.h"
28 
29 class cMouse;
30 class cMap;
31 class cPosition;
32 class cGameMapWidget;
33 class cUnitSelection;
34 class cMouseAction;
35 class cPlayer;
36 class cMapField;
37 
38 /**
39  * Abstract base class for mouse modes.
40  *
41  * A mouse mode handles the mouse cursor and the action that gets performed on a click onto the map.
42  */
43 class cMouseMode
44 {
45 public:
46 	/**
47 	 * Creates the instance of the new mouse mode.
48 	 *
49 	 * @param map The map on which the mouse mode should operate. May be null.
50 	 * @param unitSelection The current unit selection.
51 	 * @param player The player that is currently active in the GUI. May be null.
52 	 */
53 	cMouseMode (const cMap* map, const cUnitSelection& unitSelection, const cPlayer* player);
54 	virtual ~cMouseMode();
55 
56 	/**
57 	 * Set an other map to the mouse mode.
58 	 * @param map The new map. May be null.
59 	 */
60 	void setMap (const cMap* map);
61 	/**
62 	 *
63 	 * @param player
64 	 */
65 	void setPlayer (const cPlayer* player);
66 
67 	/**
68 	 * Should be called whenever the mouse moved from one map position to another one.
69 	 *
70 	 * @param mapPosition The new position on the map the mouse is now over.
71 	 *                    Can be an invalid position (e.g. (-1, -1)) if the mouse is currently not over
72 	 *                    any map field.
73 	 */
74 	void handleMapTilePositionChanged (const cPosition& mapPosition);
75 
76 	/**
77 	 * Should return the type of the mouse modes implementation. This can be used
78 	 * for quick comparison of mouse modes (maybe even without the need to construct
79 	 * a mouse mode object).
80 	 */
81 	virtual eMouseModeType getType() const = 0;
82 
83 	/**
84 	 * Should set the cursor on the passed mouse object according to the current
85 	 * position of the mouse on the map and the underlying mouse mode implementation.
86 	 *
87 	 * @param mouse The mouse to set the cursor to.
88 	 * @param mapPosition The position on the map where the mouse is currently located on.
89 	 */
90 	virtual void setCursor (cMouse& mouse, const cPosition& mapPosition) const = 0;
91 
92 	/**
93 	 * Gets the action that should be performed on a click at the given map position.
94 	 *
95 	 * @param mapPosition The position on the map for which the action should be returned.
96 	 * @return The action to be performed. Can be null if the mouse modes implementation
97 	 *         can not execute any action at the given position.
98 	 */
99 	virtual std::unique_ptr<cMouseAction> getMouseAction (const cPosition& mapPosition) const = 0;
100 
101 	/**
102 	 * Gets called when the mouse mode needs to be refreshed (E.g. because the active map
103 	 * or player has been changed).
104 	 */
105 	mutable cSignal<void ()> needRefresh;
106 protected:
107 	/**
108 	 * Signal connection manager for connections to signals from selected units.
109 	 * All connected connections will be disconnected whenever the selection changes.
110 	 */
111 	cSignalConnectionManager selectedUnitSignalConnectionManager;
112 	/**
113 	 * Signal connection manager for connections to signals from the map field that
114 	 * is currently below the mouse.
115 	 * All connected connections will be disconnected whenever the mouse leaves the current field.
116 	 */
117 	cSignalConnectionManager mapFieldSignalConnectionManager;
118 	/**
119 	 * Signal connection manager for connections to signals from units on the map field
120 	 * that is currently below the mouse.
121 	 * All connected connections will be disconnected whenever the mouse leaves the current field.
122 	 */
123 	cSignalConnectionManager mapFieldUnitsSignalConnectionManager;
124 
125 	const cMap* map;
126 	const cUnitSelection& unitSelection;
127 	const cPlayer* player;
128 
129 	/**
130 	 * Gets called whenever the unit selection has changed.
131 	 * Allows the mouse mode implementations to react on selection changes and
132 	 * establish new connections to signals triggered by the selected units.
133 	 */
134 	virtual void establishUnitSelectionConnections();
135 	/**
136 	 * Gets called whenever the map field below the mouse has changed.
137 	 * Allows the mouse mode implementations to react on mouse movements and
138 	 * establish new connections to signals triggered by the the units that are
139 	 * below the mouse.
140 	 *
141 	 * @param field The new field below the mouse.
142 	 */
143 	virtual void establishMapFieldConnections (const cMapField& field);
144 
145 private:
146 	cSignalConnectionManager signalConnectionManager;
147 
148 	void updateSelectedUnitConnections();
149 };
150 
151 #endif // ui_graphical_game_control_mousemode_mousemodeH
152