File: gui.h

package info (click to toggle)
megaglest 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,904 kB
  • ctags: 18,215
  • sloc: cpp: 144,232; ansic: 11,860; sh: 2,949; perl: 1,899; python: 1,751; objc: 142; asm: 42; makefile: 24
file content (238 lines) | stat: -rw-r--r-- 6,072 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// ==============================================================
//	This file is part of Glest (www.glest.org)
//
//	Copyright (C) 2001-2008 MartiƱo Figueroa
//
//	You can redistribute this code and/or modify it under
//	the terms of the GNU General Public License as published
//	by the Free Software Foundation; either version 2 of the
//	License, or (at your option) any later version
// ==============================================================

#ifndef _GLEST_GAME_GUI_H_
#define _GLEST_GAME_GUI_H_

#include "resource.h"
#include "command_type.h"
#include "display.h"
#include "commander.h"
#include "console.h"
#include "selection.h"
#include "randomgen.h"
#include <map>
#include "object.h"
#include "platform_common.h"
#include "leak_dumper.h"

using Shared::Util::RandomGen;

namespace Glest{ namespace Game{

class Unit;
class World;
class CommandType;
class GameCamera;
class Game;

enum DisplayState{
	dsEmpty,
	dsUnitSkills,
	dsUnitBuild,
	dsEnemy
};

// =====================================================
//	class Mouse3d
// =====================================================

class Mouse3d{
public:
	static const float fadeSpeed;

private:
	bool enabled;
	int rot;
	float fade;

public:
	Mouse3d();

	void enable();
	void update();

	bool isEnabled() const	{return enabled;}
	float getFade() const	{return fade;}
	int getRot() const		{return rot;}
};

// =====================================================
//	class SelectionQuad
// =====================================================

class SelectionQuad{
private:
	Vec2i posDown;
	Vec2i posUp;
	bool enabled;

public:
	SelectionQuad();

	bool isEnabled() const		{return enabled;}
	Vec2i getPosDown() const	{return posDown;}
	Vec2i getPosUp() const		{return posUp;}

	void setPosDown(const Vec2i &posDown);
	void setPosUp(const Vec2i &posUp);
	void disable();
};

// =====================================================
// 	class Gui
//
///	In game GUI
// =====================================================

class Gui {
public:
	static const int maxSelBuff= 128*5;
	static const int upgradeDisplayIndex= 8;

	static const int meetingPointPos= 14;
	static const int cancelPos= 15;
	static const int imageCount= 16;

	static const int invalidPos= -1;
	static const int doubleClickSelectionRadius= 20;

private:
	//External objects
	RandomGen random;
	const Commander *commander;
	const World *world;
	const Game *game;
	GameCamera *gameCamera;
	Console *console;

	//Positions
	Vec2i posObjWorld;		//world coords
	bool validPosObjWorld;

	//display
	const UnitType *choosenBuildingType;
	const CommandType *activeCommandType;
	CommandClass activeCommandClass;
	int activePos;
	int lastPosDisplay;

	//composite
	Display display;
	Mouse3d mouse3d;
	Selection selection;
	SelectionQuad selectionQuad;
	int lastQuadCalcFrame;
	int selectionCalculationFrameSkip;
	int minQuadSize;

	Chrono lastGroupRecallTime;
	int lastGroupRecall;

	//states
	bool selectingBuilding;
	bool selectingPos;
	bool selectingMeetingPoint;

	CardinalDir selectedBuildingFacing;
	Vec2i selectedResourceObjectPos;
	Vec2i highlightedResourceObjectPos;
	int highlightedUnitId;

	Texture2D* hudTexture;

public:
	Gui();
	void init(Game *game);
	void end();

	//get
	Vec2i getPosObjWorld() const			{return posObjWorld;}
	const UnitType *getBuilding() const;

	Texture2D *getHudTexture() const {return hudTexture;}
	void setHudTexture(Texture2D* value) { hudTexture = value;}

	const Mouse3d *getMouse3d() const				{return &mouse3d;}
	const Display *getDisplay()	const				{return &display;}
	const Selection *getSelection()	const			{return &selection;}
	Selection *getSelectionPtr()	{return &selection;}
	const Object *getSelectedResourceObject()	const;
	Object *getHighlightedResourceObject()	const;
	Unit *getHighlightedUnit() const;

	const SelectionQuad *getSelectionQuad() const	{return &selectionQuad;}
	CardinalDir getSelectedFacing() const			{return selectedBuildingFacing;}
	bool isSelected(const Unit *unit) const			{return selection.hasUnit(unit);}

	bool isValidPosObjWorld() const		{return validPosObjWorld;}
	bool isSelecting() const			{return selectionQuad.isEnabled();}
	bool isSelectingPos() const			{return selectingPos;}
	bool isSelectingBuilding() const	{return selectingBuilding;}
	bool isPlacingBuilding() const;

	//set
	void invalidatePosObjWorld();

	//events
	void update();
	void tick();
	bool mouseValid(int x, int y);
	void mouseDownLeftDisplay(int x, int y);
	void mouseMoveDisplay(int x, int y);
	void mouseMoveOutsideDisplay();
	void mouseDownLeftGraphics(int x, int y, bool prepared);
	void mouseDownRightGraphics(int x, int y, bool prepared);
	void mouseUpLeftGraphics(int x, int y);
	void mouseMoveGraphics(int x, int y);
	void mouseDoubleClickLeftGraphics(int x, int y);
	void groupKey(int groupIndex);
	void hotKey(SDL_KeyboardEvent key);

	//misc
	void switchToNextDisplayColor();
	void onSelectionChanged();

	void saveGame(XmlNode *rootNode) const;
	void loadGame(const XmlNode *rootNode, World *world);

private:

	//orders
	void giveDefaultOrders(int x, int y);
	void giveDefaultOrders(int x, int y, const Unit *targetUnit, bool paintMouse3d);
	void givePreparedDefaultOrders(int x, int y);
	void giveOneClickOrders();
	void giveTwoClickOrders(int x, int y, bool prepared);

	//hotkeys
	void centerCameraOnSelection();
	void selectInterestingUnit(InterestingUnitType iut);
	void clickCommonCommand(CommandClass commandClass);

	//misc
	int computePosDisplay(int x, int y);
	void computeDisplay();
	void resetState();
	void mouseDownDisplayUnitSkills(int posDisplay);
	void mouseDownDisplayUnitBuild(int posDisplay);
	void computeInfoString(int posDisplay);
	string computeDefaultInfoString();
	void addOrdersResultToConsole(CommandClass cc, std::pair<CommandResult,string> result);
	bool isSharedCommandClass(CommandClass commandClass);
	void computeSelected(bool doubleCkick,bool force);
	bool computeTarget(const Vec2i &screenPos, Vec2i &targetPos, const Unit *&targetUnit);
	Unit* getRelevantObjectFromSelection(Selection::UnitContainer *uc);
};

}} //end namespace

#endif