Debian Codegraph @

engine/include/ui.h

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name ui.h - The user interface header file. */
11 //
12 // (c) Copyright 1999-2007 by Lutz Sammer and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27
28 #ifndef __UI_H__
29 #define __UI_H__
30
31 //@{
32
33 /*----------------------------------------------------------------------------
34 -- Includes
35 ----------------------------------------------------------------------------*/
36
37 #include <vector>
38 #include <string>
39 #include <map>
40
41 #include "SDL.h"
42 #include "upgrade_structs.h"
43 #include "cursor.h"
44 #include "interface.h"
45 #include "minimap.h"
46
47 /*----------------------------------------------------------------------------
48 -- Declarations
49 ----------------------------------------------------------------------------*/
50
51 class CUnit;
52 class CFile;
53 class CFont;
54 class LuaActionListener;
55 struct lua_State;
56
57 /*----------------------------------------------------------------------------
58 -- Definitions
59 ----------------------------------------------------------------------------*/
60
61 enum TextAlignment {
62 TextAlignUndefined,
63 TextAlignCenter,
64 TextAlignLeft,
65 TextAlignRight,
66 };
67
68 class ButtonStyleProperties {
69 public:
70 ButtonStyleProperties() : Sprite(NULL), Frame(0), BorderColor(0),
71 BorderSize(0), TextAlign(TextAlignUndefined),
72 TextX(0), TextY(0)
73 {
74 BorderColorRGB.r = BorderColorRGB.g = BorderColorRGB.b = 0;
75 }
76
77 CGraphic *Sprite;
78 int Frame;
79 SDL_Color BorderColorRGB;
80 Uint32 BorderColor;
81 int BorderSize;
82 TextAlignment TextAlign; /// Text alignment
83 int TextX; /// Text X location
84 int TextY; /// Text Y location
85 std::string TextNormalColor; /// Normal text color
86 std::string TextReverseColor; /// Reverse text color
87 } ;
88
89 class ButtonStyle {
90 public:
91 ButtonStyle() : Width(0), Height(0), Font(0),
92 TextAlign(TextAlignUndefined), TextX(0), TextY(0) {}
93
94 int Width; /// Button width
95 int Height; /// Button height
96 CFont *Font; /// Font
97 std::string TextNormalColor; /// Normal text color
98 std::string TextReverseColor; /// Reverse text color
99 TextAlignment TextAlign; /// Text alignment
100 int TextX; /// Text X location
101 int TextY; /// Text Y location
102 ButtonStyleProperties Default; /// Default button properties
103 ButtonStyleProperties Hover; /// Hover button properties
104 ButtonStyleProperties Clicked; /// Clicked button properties
105 };
106
107 /// buttons on screen themselves
108 class CUIButton {
109 public:
110 CUIButton() : X(0), Y(0), Style(NULL), Callback(NULL) {}
111 ~CUIButton() {}
112
113 int X; /// x coordinate on the screen
114 int Y; /// y coordinate on the screen
115 std::string Text; /// button text
116 ButtonStyle *Style; /// button style
117 LuaActionListener *Callback; /// callback function
118 };
119
120 #define MAX_NUM_VIEWPORTS 8 /// Number of supported viewports
121 #define MAX_SAVED_MAP_POSITIONS 3 /// Number of supported saved map positions
122
123 /**
124 ** A map viewport.
125 **
126 ** A part of the map displayed on sceen.
127 **
128 ** CViewport::X CViewport::Y
129 ** CViewport::EndX CViewport::EndY
130 **
131 ** upper left corner of this viewport is located at pixel
132 ** coordinates (X, Y) with respect to upper left corner of
133 ** stratagus's window, similarly lower right corner of this
134 ** viewport is (EndX, EndY) pixels away from the UL corner of
135 ** stratagus's window.
136 **
137 ** CViewport::MapX CViewport::MapY
138 ** CViewport::MapWidth CViewport::MapHeight
139 **
140 ** Tile coordinates of UL corner of this viewport with respect to
141 ** UL corner of the whole map.
142 **
143 ** CViewport::Unit
144 **
145 ** Viewport is bound to a unit. If the unit moves the viewport
146 ** changes the position together with the unit.
147 ** @todo binding to a group.
148 */
149 class CViewport {
150 public:
151 CViewport() : X(0), Y(0), EndX(0), EndY(0), MapX(0), MapY(0),
152 OffsetX(0), OffsetY(0), MapWidth(0), MapHeight(0), Unit(NULL) {};
153
154 /// Check if X and Y pixels are within map area
155 bool IsInsideMapArea(int x, int y) const;
156 /// Convert screen X pixel to map tile
157 int Viewport2MapX(int x) const;
158 /// Convert screen Y pixel to map tile
159 int Viewport2MapY(int y) const;
160 /// Convert map tile to screen X pixel
161 int Map2ViewportX(int x) const;
162 /// Convert map tile to screen Y pixel
163 int Map2ViewportY(int y) const;
164 /// Convert map pixel coordinates into viewport coordinates
165 void MapPixel2Viewport(int &x, int &y) const;
166
167 /// Set the current map view to x,y(upper,left corner)
168 void Set(int x, int y, int offsetx, int offsety);
169 /// Center map on point in viewport
170 void Center(int x, int y, int offsetx, int offsety);
171 protected:
172 /// Draw the map background
173 void DrawMapBackgroundInViewport() const;
174 /// Draw the map fog of war
175 void DrawMapFogOfWar() const;
176 public:
177 /// Draw the full Viewport.
178 void Draw() const;
179 void DrawBorder() const;
180 /// Check if any part of an area is visible in viewport
181 bool AnyMapAreaVisibleInViewport(int sx, int sy, int ex, int ey) const;
182
183 int X; /// Screen pixel left corner x coordinate
184 int Y; /// Screen pixel upper corner y coordinate
185 int EndX; /// Screen pixel right x coordinate
186 int EndY; /// Screen pixel bottom y coordinate
187
188 int MapX; /// Map tile left corner x coordinate
189 int MapY; /// Map tile upper corner y coordinate
190 int OffsetX; /// X Offset within MapX
191 int OffsetY; /// Y Offset within MapY
192 int MapWidth; /// Width in map tiles
193 int MapHeight; /// Height in map tiles
194
195 CUnit *Unit; /// Bound to this unit
196 };
197
198 /**
199 ** Enumeration of the different predefined viewport configurations.
200 **
201 ** Add new types of viewports at the end of the enumeration for
202 ** backward compatibility of save games.
203 */
204 enum ViewportModeType {
205 VIEWPORT_SINGLE, /// Old single viewport
206 VIEWPORT_SPLIT_HORIZ, /// Two viewports split horizontal
207 VIEWPORT_SPLIT_HORIZ3, /// Three viewports split horiontal
208 VIEWPORT_SPLIT_VERT, /// Two viewports split vertical
209 VIEWPORT_QUAD, /// Four viewports split symmetric
210 NUM_VIEWPORT_MODES, /// Number of different viewports.
211 };
212
213 class CMapArea
214 {
215 public:
216 CMapArea() : X(0), Y(0), EndX(0), EndY(0),
217 ScrollPaddingLeft(0), ScrollPaddingRight(0),
218 ScrollPaddingTop(0), ScrollPaddingBottom(0) {}
219
220 int X; /// Screen pixel left corner x coordinate
221 int Y; /// Screen pixel upper corner y coordinate
222 int EndX; /// Screen pixel right x coordinate
223 int EndY; /// Screen pixel bottom y coordinate
224 int ScrollPaddingLeft; /// Scrollable area past the left of map
225 int ScrollPaddingRight; /// Scrollable area past the right of map
226 int ScrollPaddingTop; /// Scrollable area past the top of map
227 int ScrollPaddingBottom; /// Scrollable area past the bottom of map
228 };
229
230 class CFiller
231 {
232 public:
233 CFiller() : G(NULL), X(0), Y(0) {}
234
235 CGraphic *G; /// Graphic
236 int X; /// X coordinate
237 int Y; /// Y coordinate
238 };
239
240 class CButtonPanel
241 {
242 public:
243 CButtonPanel() : G(NULL), X(0), Y(0), ShowCommandKey(true)
244 {
245 AutoCastBorderColorRGB.r = 0;
246 AutoCastBorderColorRGB.g = 0;
247 AutoCastBorderColorRGB.b = 0;
248 }
249
250 void Draw();
251 void Update();
252 void DoClicked(int button);
253 int DoKey(int key);
254
255 CGraphic *G;
256 int X;
257 int Y;
258 std::vector<CUIButton> Buttons;
259 SDL_Color AutoCastBorderColorRGB;
260 bool ShowCommandKey;
261 };
262
263 class CPieMenu {
264 public:
265 CPieMenu() : G(NULL), MouseButton(NoButton)
266 {
267 memset(this->X, 0, sizeof(this->X));
268 memset(this->Y, 0, sizeof(this->Y));
269 }
270
271 CGraphic *G; /// Optional background image
272 int MouseButton; /// Which mouse button pops up the piemenu, deactivate with NoButton
273 int X[8]; /// X position of the pies
274 int Y[8]; /// Y position of the pies
275
276 void SetRadius(int radius) {
277 const int coeffX[] = { 0, 193, 256, 193, 0, -193, -256, -193};
278 const int coeffY[] = { -256, -193, 0, 193, 256, 193, 0, -193};
279 for (int i = 0; i < 8; ++i) {
280 this->X[i] = (coeffX[i] * radius) >> 8;
281 this->Y[i] = (coeffY[i] * radius) >> 8;
282 }
283 }
284 };
285
286 class CResourceInfo {
287 public:
288 CResourceInfo() : G(NULL), IconFrame(0), IconX(0), IconY(0),
289 TextX(-1), TextY(-1) {}
290
291 CGraphic *G; /// icon graphic
292 int IconFrame; /// icon frame
293 int IconX; /// icon X position
294 int IconY; /// icon Y position
295 int TextX; /// text X position
296 int TextY; /// text Y position
297 };
298
299 class CInfoPanel
300 {
301 public:
302 CInfoPanel() : X(0), Y(0) {}
303
304 void Draw();
305
306 int X;
307 int Y;
308 };
309
310 class CStatusLine
311 {
312 public:
313 CStatusLine() : Width(0), TextX(0), TextY(0), Font(0) {}
314
315 void Draw();
316 void Set(const std::string &status);
317 inline const std::string &Get() { return this->StatusLine; }
318 void Clear();
319
320 int Width;
321 int TextX;
322 int TextY;
323 CFont *Font;
324
325 private:
326 std::string StatusLine;
327 };
328
329 class CUITimer
330 {
331 public:
332 CUITimer() : X(0), Y(0), Font(NULL) {}
333
334 int X;
335 int Y;
336 CFont *Font;
337 };
338
339
340 /**
341 ** Defines the user interface.
342 */
343 class CUserInterface
344 {
345 public:
346 CUserInterface();
347 ~CUserInterface();
348
349 void Load();
350
351
352 bool MouseScroll; /// Enable mouse scrolling
353 bool KeyScroll; /// Enable keyboard scrolling
354 /// Middle-Mouse Scroll Speed (screenpixels per mousepixel)
355 int MouseScrollSpeedDefault;
356 /// Middle-Mouse Scroll Speed with Control pressed
357 int MouseScrollSpeedControl;
358
359 int MouseWarpX; /// Cursor warp X position
360 int MouseWarpY; /// Cursor warp Y position
361
362 std::string NormalFontColor; /// Color for normal text displayed
363 std::string ReverseFontColor; /// Color for reverse text displayed
364
365 std::vector<CFiller> Fillers; /// Filler graphics
366
367 CResourceInfo Resources[MaxCosts];/// Icon+Text of all resources
368
369 CInfoPanel InfoPanel; /// Info panel
370
371 CUIButton *SingleSelectedButton; /// Button for single selected unit
372
373 std::vector<CUIButton> SelectedButtons;/// Selected buttons
374 CFont *MaxSelectedFont; /// Font type to use
375 int MaxSelectedTextX; /// position to place '+#' text
376 int MaxSelectedTextY; /// if > maximum units selected
377
378 CUIButton *SingleTrainingButton; /// Button for single training
379 std::string SingleTrainingText; /// Text for single training
380 CFont *SingleTrainingFont; /// Font for single traning
381 int SingleTrainingTextX; /// X text position single training
382 int SingleTrainingTextY; /// Y text position single training
383
384 std::vector<CUIButton> TrainingButtons;/// Training buttons
385 std::string TrainingText; /// Multiple Training Text
386 CFont *TrainingFont; /// Multiple Training Font
387 int TrainingTextX; /// Multiple Training X Text position
388 int TrainingTextY; /// Multiple Training Y Text position
389
390 std::vector<CUIButton> TransportingButtons;/// Button info for transporting
391
392 // Completed bar
393 SDL_Color CompletedBarColorRGB; /// color for completed bar
394 Uint32 CompletedBarColor; /// color for completed bar
395 bool CompletedBarShadow; /// should complete bar have shadow
396
397 // Button panel
398 CButtonPanel ButtonPanel;
399
400 // Pie Menu
401 CPieMenu PieMenu;
402
403 // Map area
404 ViewportModeType ViewportMode; /// Current viewport mode
405 CViewport *MouseViewport; /// Viewport containing mouse
406 CViewport *SelectedViewport; /// Current selected active viewport
407 int NumViewports; /// # Viewports currently used
408 CViewport Viewports[MAX_NUM_VIEWPORTS]; /// Parameters of all viewports
409 CMapArea MapArea; /// geometry of the whole map area
410 CFont *MessageFont; /// Font used for messages
411 int MessageScrollSpeed; /// Scroll speed in seconds for messages
412
413 // Saved map positions
414 int SavedMapPositionX[MAX_SAVED_MAP_POSITIONS]; /// Saved map position X
415 int SavedMapPositionY[MAX_SAVED_MAP_POSITIONS]; /// Saved map position Y
416
417 // Menu buttons
418 CUIButton MenuButton; /// menu button
419 CUIButton NetworkMenuButton; /// network menu button
420 CUIButton NetworkDiplomacyButton; /// network diplomacy button
421
422 // The minimap
423 CMinimap Minimap; /// minimap
424 Uint32 ViewportCursorColor; /// minimap cursor color
425
426 // The status line
427 CStatusLine StatusLine; /// status line
428
429 // Game timer
430 CUITimer Timer; /// game timer
431
432 // Offsets for 640x480 center used by menus
433 int Offset640X; /// Offset for 640x480 X position
434 int Offset480Y; /// Offset for 640x480 Y position
435
436 //
437 // Cursors used.
438 //
439 CursorConfig Point; /// General pointing cursor
440 CursorConfig Glass; /// HourGlass, system is waiting
441 CursorConfig Cross; /// Multi-select cursor.
442 CursorConfig YellowHair; /// Yellow action,attack cursor.
443 CursorConfig GreenHair; /// Green action,attack cursor.
444 CursorConfig RedHair; /// Red action,attack cursor.
445 CursorConfig Scroll; /// Cursor for scrolling map around.
446
447 CursorConfig ArrowE; /// Cursor pointing east
448 CursorConfig ArrowNE; /// Cursor pointing north east
449 CursorConfig ArrowN; /// Cursor pointing north
450 CursorConfig ArrowNW; /// Cursor pointing north west
451 CursorConfig ArrowW; /// Cursor pointing west
452 CursorConfig ArrowSW; /// Cursor pointing south west
453 CursorConfig ArrowS; /// Cursor pointing south
454 CursorConfig ArrowSE; /// Cursor pointing south east
455
456 /// @todo could use different sounds/speech for the errors
457 /// Is in gamesounds?
458 /// SoundConfig PlacementError; /// played on placements errors
459 /// SoundConfig PlacementSuccess; /// played on placements success
460 /// SoundConfig Click; /// click noice used often
461 };
462
463 /*----------------------------------------------------------------------------
464 -- Variables
465 ----------------------------------------------------------------------------*/
466
467 extern CUserInterface UI; /// The user interface
468
469 /// Hash table of all the button styles
470 extern std::map<std::string, ButtonStyle *> ButtonStyleHash;
471
472 extern bool RightButtonAttacks; /// right button attacks
473 extern ButtonAction *CurrentButtons; /// Current Selected Buttons
474
475 extern std::string UiGroupKeys; /// Up to 11 keys used for group selection
476
477 /*----------------------------------------------------------------------------
478 -- Functions
479 ----------------------------------------------------------------------------*/
480
481 /// Initialize the ui
482 extern void InitUserInterface(void);
483 /// Save the ui state
484 extern void SaveUserInterface(CFile *file);
485 /// Clean up the ui module
486 extern void CleanUserInterface(void);
487 #ifdef DEBUG
488 extern void FreeButtonStyles();
489 #endif
490 /// Register ccl features
491 extern void UserInterfaceCclRegister(void);
492
493 /// Find a button style
494 extern ButtonStyle *FindButtonStyle(const std::string &style);
495
496 /// Called if the mouse is moved in Normal interface state
497 extern void UIHandleMouseMove(int x, int y);
498 /// Called if any mouse button is pressed down
499 extern void UIHandleButtonDown(unsigned button);
500 /// Called if any mouse button is released up
501 extern void UIHandleButtonUp(unsigned button);
502
503 /// Restrict mouse cursor to viewport
504 extern void RestrictCursorToViewport(void);
505 /// Restrict mouse cursor to minimap
506 extern void RestrictCursorToMinimap(void);
507
508 /// Get viewport for screen pixel position
509 extern CViewport *GetViewport(int x, int y);
510 /// Cycle through all available viewport modes
511 extern void CycleViewportMode(int);
512 /// Select viewport mode
513 extern void SetViewportMode(ViewportModeType mode);
514
515 /// Use the mouse to scroll the map
516 extern void MouseScrollMap(int x, int y);
517 /// Check if mouse scrolling is enabled
518 extern bool GetMouseScroll(void);
519 /// Enable/disable scrolling with the mouse
520 extern void SetMouseScroll(bool enabled);
521 /// Check if keyboard scrolling is enabled
522 extern bool GetKeyScroll(void);
523 /// Enable/disable scrolling with the keyboard
524 extern void SetKeyScroll(bool enabled);
525 /// Check if mouse grabbing is enabled
526 extern bool GetGrabMouse(void);
527 /// Enable/disable grabbing the mouse
528 extern void SetGrabMouse(bool enabled);
529 /// Check if scrolling stops when leaving the window
530 extern bool GetLeaveStops(void);
531 /// Enable/disable leaving the window stops scrolling
532 extern void SetLeaveStops(bool enabled);
533 /// Set saved map position
534 extern void SetSavedMapPosition(int index, int x, int y);
535 /// Recall map position
536 extern void RecallSavedMapPosition(int index);
537
538 extern int AddHandler(lua_State *l);
539 extern void CallHandler(unsigned int handle, int value);
540
541
542 //@}
543
544 #endif // !__UI_H__