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 #include "ui/graphical/game/control/mousemode/mousemodesteal.h"
21 #include "ui/graphical/game/control/mouseaction/mouseactionsteal.h"
22 #include "ui/graphical/game/unitselection.h"
23 #include "game/data/map/map.h"
24 #include "game/data/units/vehicle.h"
25 #include "game/data/units/building.h"
26 #include "input/mouse/mouse.h"
27 #include "input/mouse/cursor/mousecursorsimple.h"
28 #include "input/mouse/cursor/mousecursoramount.h"
29 
30 //------------------------------------------------------------------------------
cMouseModeSteal(const cMap * map_,const cUnitSelection & unitSelection_,const cPlayer * player_)31 cMouseModeSteal::cMouseModeSteal (const cMap* map_, const cUnitSelection& unitSelection_, const cPlayer* player_) :
32 	cMouseMode (map_, unitSelection_, player_)
33 {
34 	establishUnitSelectionConnections();
35 }
36 
37 //------------------------------------------------------------------------------
getType() const38 eMouseModeType cMouseModeSteal::getType() const
39 {
40 	return eMouseModeType::Steal;
41 }
42 
43 //------------------------------------------------------------------------------
setCursor(cMouse & mouse,const cPosition & mapPosition) const44 void cMouseModeSteal::setCursor (cMouse& mouse, const cPosition& mapPosition) const
45 {
46 	if (canExecuteAction (mapPosition))
47 	{
48 		const auto selectedVehicle = unitSelection.getSelectedVehicle();
49 
50 		if (selectedVehicle && map)
51 		{
52 			const auto& field = map->getField (mapPosition);
53 			const cUnit* unit = field.getVehicle();
54 
55 			mouse.setCursor (std::make_unique<cMouseCursorAmount> (eMouseCursorAmountType::Steal, selectedVehicle->calcCommandoChance (unit, true)));
56 		}
57 		else mouse.setCursor (std::make_unique<cMouseCursorAmount> (eMouseCursorAmountType::Steal));
58 	}
59 	else
60 	{
61 		mouse.setCursor (std::make_unique<cMouseCursorSimple> (eMouseCursorSimpleType::No));
62 	}
63 }
64 
65 //------------------------------------------------------------------------------
getMouseAction(const cPosition & mapPosition) const66 std::unique_ptr<cMouseAction> cMouseModeSteal::getMouseAction (const cPosition& mapPosition) const
67 {
68 	if (canExecuteAction (mapPosition))
69 	{
70 		return std::make_unique<cMouseActionSteal> ();
71 	}
72 	else return nullptr;
73 }
74 
75 //------------------------------------------------------------------------------
canExecuteAction(const cPosition & mapPosition) const76 bool cMouseModeSteal::canExecuteAction (const cPosition& mapPosition) const
77 {
78 	if (!map) return false;
79 
80 	const auto selectedVehicle = unitSelection.getSelectedVehicle();
81 
82 	return selectedVehicle && selectedVehicle->canDoCommandoAction (mapPosition, *map, true);
83 }
84 
85 //------------------------------------------------------------------------------
establishUnitSelectionConnections()86 void cMouseModeSteal::establishUnitSelectionConnections()
87 {
88 	const auto selectedUnit = unitSelection.getSelectedUnit();
89 
90 	if (selectedUnit)
91 	{
92 		selectedUnitSignalConnectionManager.connect (selectedUnit->data.shotsChanged, [this]() { needRefresh(); });
93 		selectedUnitSignalConnectionManager.connect (selectedUnit->positionChanged, [this]() { needRefresh(); });
94 	}
95 }
96 
97 //------------------------------------------------------------------------------
establishMapFieldConnections(const cMapField & field)98 void cMouseModeSteal::establishMapFieldConnections (const cMapField& field)
99 {
100 	mapFieldSignalConnectionManager.connect (field.unitsChanged, [this, &field]() { updateFieldUnitConnections (field); needRefresh(); });
101 
102 	updateFieldUnitConnections (field);
103 }
104 
105 //------------------------------------------------------------------------------
updateFieldUnitConnections(const cMapField & field)106 void cMouseModeSteal::updateFieldUnitConnections (const cMapField& field)
107 {
108 	mapFieldUnitsSignalConnectionManager.disconnectAll();
109 
110 	auto plane = field.getPlane();
111 	if (plane)
112 	{
113 		mapFieldUnitsSignalConnectionManager.connect (plane->flightHeightChanged, [this]() { needRefresh(); });
114 		mapFieldUnitsSignalConnectionManager.connect (plane->disabledChanged, [this]() { needRefresh(); });
115 		mapFieldUnitsSignalConnectionManager.connect (plane->data.storedUnitsChanged, [this]() { needRefresh(); });
116 	}
117 	auto vehicle = field.getVehicle();
118 	if (vehicle)
119 	{
120 		mapFieldUnitsSignalConnectionManager.connect (vehicle->data.storedUnitsChanged, [this]() { needRefresh(); });
121 		mapFieldUnitsSignalConnectionManager.connect (vehicle->disabledChanged, [this]() { needRefresh(); });
122 	}
123 	auto building = field.getBuilding();
124 	if (building)
125 	{
126 		mapFieldUnitsSignalConnectionManager.connect (building->data.storedUnitsChanged, [this]() { needRefresh(); });
127 		mapFieldUnitsSignalConnectionManager.connect (building->disabledChanged, [this]() { needRefresh(); });
128 	}
129 }
130