$treeview $search $mathjax
Stratagus
2.2.6
$projectbrief
|
$projectbrief
|
$searchbox |
_________ __ __ / _____// |_____________ _/ |______ ____ __ __ ______ \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ \ /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > \/ \/ \//_____/ \/ ______________________ ______________________ T H E W A R B E G I N S Stratagus - A free fantasy real time strategy game engine
00001 // _________ __ __ 00002 // / _____// |_____________ _/ |______ ____ __ __ ______ 00003 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ 00004 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ | 00005 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > 00006 // \/ \/ \//_____/ \/ 00007 // ______________________ ______________________ 00008 // T H E W A R B E G I N S 00009 // Stratagus - A free fantasy real time strategy game engine 00010 // 00012 // 00013 // (c) Copyright 1999-2007 by Lutz Sammer and Jimmy Salmon 00014 // 00015 // This program is free software; you can redistribute it and/or modify 00016 // it under the terms of the GNU General Public License as published by 00017 // the Free Software Foundation; only version 2 of the License. 00018 // 00019 // This program is distributed in the hope that it will be useful, 00020 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 // GNU General Public License for more details. 00023 // 00024 // You should have received a copy of the GNU General Public License 00025 // along with this program; if not, write to the Free Software 00026 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00027 // 02111-1307, USA. 00028 // 00029 00030 #ifndef __UI_H__ 00031 #define __UI_H__ 00032 00034 00037 00038 /*---------------------------------------------------------------------------- 00039 -- Includes 00040 ----------------------------------------------------------------------------*/ 00041 00042 #include <vector> 00043 #include <string> 00044 #include <map> 00045 00046 #include "SDL.h" 00047 00048 #ifndef __UPGRADE_STRUCTS_H__ 00049 #include "upgrade_structs.h" 00050 #endif 00051 #ifndef __CURSOR_H__ 00052 #include "cursor.h" 00053 #endif 00054 #ifndef __INTERFACE_H__ 00055 #include "interface.h" 00056 #endif 00057 #ifndef __SCRIPT_H__ 00058 #include "script.h" 00059 #endif 00060 #ifndef __MINIMAP_H__ 00061 #include "minimap.h" 00062 #endif 00063 /*---------------------------------------------------------------------------- 00064 -- Declarations 00065 ----------------------------------------------------------------------------*/ 00066 00067 class CUnit; 00068 class CFile; 00069 class CFont; 00070 class LuaActionListener; 00071 class CDrawProxy; 00072 00073 /*---------------------------------------------------------------------------- 00074 -- Definitions 00075 ----------------------------------------------------------------------------*/ 00076 00077 enum TextAlignment { 00078 TextAlignUndefined, 00079 TextAlignCenter, 00080 TextAlignLeft, 00081 TextAlignRight 00082 }; 00083 00084 class ButtonStyleProperties { 00085 public: 00086 ButtonStyleProperties() : Sprite(NULL), Frame(0), BorderColor(0), 00087 BorderSize(0), TextAlign(TextAlignUndefined), 00088 TextX(0), TextY(0) 00089 { 00090 BorderColorRGB.r = BorderColorRGB.g = BorderColorRGB.b = 0; 00091 } 00092 00093 CGraphic *Sprite; 00094 int Frame; 00095 SDL_Color BorderColorRGB; 00096 Uint32 BorderColor; 00097 int BorderSize; 00098 TextAlignment TextAlign; 00099 int TextX; 00100 int TextY; 00101 std::string TextNormalColor; 00102 std::string TextReverseColor; 00103 }; 00104 00105 class ButtonStyle { 00106 public: 00107 ButtonStyle() : Width(0), Height(0), Font(0), 00108 TextAlign(TextAlignUndefined), TextX(0), TextY(0) {} 00109 00110 int Width; 00111 int Height; 00112 CFont *Font; 00113 std::string TextNormalColor; 00114 std::string TextReverseColor; 00115 TextAlignment TextAlign; 00116 int TextX; 00117 int TextY; 00118 ButtonStyleProperties Default; 00119 ButtonStyleProperties Hover; 00120 ButtonStyleProperties Clicked; 00121 }; 00122 00124 class CUIButton { 00125 public: 00126 CUIButton() : X(0), Y(0), Style(NULL), Callback(NULL) {} 00127 ~CUIButton() {} 00128 00129 int X; 00130 int Y; 00131 std::string Text; 00132 ButtonStyle *Style; 00133 LuaActionListener *Callback; 00134 }; 00135 00136 #define MAX_NUM_VIEWPORTS 8 00137 00138 00164 class CViewport { 00165 public: 00166 CViewport() : X(0), Y(0), EndX(0), EndY(0), MapX(0), MapY(0), 00167 OffsetX(0), OffsetY(0), MapWidth(0), MapHeight(0), Unit(NULL), 00168 Proxy(NULL) {} 00169 ~CViewport(); 00170 00171 00173 bool IsInsideMapArea(int x, int y) const; 00175 int Viewport2MapX(int x) const; 00177 int Viewport2MapY(int y) const; 00179 int Map2ViewportX(int x) const; 00181 int Map2ViewportY(int y) const; 00183 void MapPixel2Viewport(int &x, int &y) const; 00184 00186 void Set(int x, int y, int offsetx, int offsety); 00188 void Center(const Vec2i& pos, const PixelPos &offset); 00189 protected: 00191 void DrawMapBackgroundInViewport() const; 00193 void DrawMapFogOfWar() const; 00194 public: 00195 void UpdateUnits(); 00196 00198 void Draw() const; 00199 void DrawBorder() const; 00201 bool AnyMapAreaVisibleInViewport(int sx, int sy, int ex, int ey) const; 00202 00203 int X; 00204 int Y; 00205 int EndX; 00206 int EndY; 00207 00208 int MapX; 00209 int MapY; 00210 int OffsetX; 00211 int OffsetY; 00212 int MapWidth; 00213 int MapHeight; 00214 00215 CUnit *Unit; 00216 CDrawProxy *Proxy; 00217 }; 00218 00224 enum ViewportModeType { 00225 VIEWPORT_SINGLE = 0, 00226 VIEWPORT_SPLIT_HORIZ, 00227 VIEWPORT_SPLIT_HORIZ3, 00228 VIEWPORT_SPLIT_VERT, 00229 VIEWPORT_QUAD, 00230 NUM_VIEWPORT_MODES 00231 }; 00232 00233 class CMapArea 00234 { 00235 public: 00236 CMapArea() : X(0), Y(0), EndX(0), EndY(0), 00237 ScrollPaddingLeft(0), ScrollPaddingRight(0), 00238 ScrollPaddingTop(0), ScrollPaddingBottom(0) {} 00239 00240 int X; 00241 int Y; 00242 int EndX; 00243 int EndY; 00244 int ScrollPaddingLeft; 00245 int ScrollPaddingRight; 00246 int ScrollPaddingTop; 00247 int ScrollPaddingBottom; 00248 }; 00249 00253 class ConditionPanel { 00254 public: 00255 ConditionPanel() : ShowOnlySelected(false), HideNeutral(false), 00256 HideAllied(false), ShowOpponent(false), BoolFlags(NULL), 00257 Variables(NULL) {} 00258 ~ConditionPanel() { 00259 delete[] BoolFlags; 00260 delete[] Variables; 00261 } 00262 00263 bool ShowOnlySelected; 00264 00265 bool HideNeutral; 00266 bool HideAllied; 00267 bool ShowOpponent; 00268 00269 char *BoolFlags; 00270 char *Variables; 00271 }; 00272 00276 class CContentType { 00277 public: 00278 CContentType() : PosX(0), PosY(0), Condition(NULL) {} 00279 virtual ~CContentType() { delete Condition; } 00280 00282 virtual void Draw(const CUnit &unit, CFont *defaultfont) const = 0; 00283 00284 int PosX; 00285 int PosY; 00286 00287 ConditionPanel *Condition; 00288 }; 00289 00293 class CContentTypeText : public CContentType { 00294 public: 00295 CContentTypeText() : Text(NULL), Font(NULL), Centered(0), Index(-1), 00296 Component(VariableValue), ShowName(0), Stat(0) {} 00297 virtual ~CContentTypeText() { 00298 FreeStringDesc(Text); 00299 delete Text; 00300 } 00301 00302 virtual void Draw(const CUnit &unit, CFont *defaultfont) const; 00303 00304 StringDesc *Text; 00305 CFont *Font; 00306 char Centered; 00307 int Index; 00308 EnumVariable Component; 00309 char ShowName; 00310 char Stat; 00311 }; 00312 00316 class CContentTypeFormattedText : public CContentType { 00317 public: 00318 CContentTypeFormattedText() : Font(NULL), Centered(false), 00319 Index(-1), Component(VariableValue) {} 00320 virtual ~CContentTypeFormattedText() {} 00321 00322 virtual void Draw(const CUnit &unit, CFont *defaultfont) const; 00323 00324 std::string Format; 00325 CFont *Font; 00326 bool Centered; 00327 int Index; 00328 EnumVariable Component; 00329 }; 00330 00334 class CContentTypeFormattedText2 : public CContentType { 00335 public: 00336 CContentTypeFormattedText2() : Font(NULL), Centered(false), 00337 Index1(-1), Component1(VariableValue), Index2(-1), Component2(VariableValue) {} 00338 virtual ~CContentTypeFormattedText2() {} 00339 00340 virtual void Draw(const CUnit &unit, CFont *defaultfont) const; 00341 00342 std::string Format; 00343 CFont *Font; 00344 bool Centered; 00345 int Index1; 00346 EnumVariable Component1; 00347 int Index2; 00348 EnumVariable Component2; 00349 }; 00350 00354 class CContentTypeIcon : public CContentType { 00355 public: 00356 virtual void Draw(const CUnit &unit, CFont *defaultfont) const; 00357 00358 EnumUnit UnitRef; 00359 }; 00360 00364 class CContentTypeLifeBar : public CContentType { 00365 public: 00366 CContentTypeLifeBar() : Index(-1), Width(0), Height(0) {} 00367 00368 virtual void Draw(const CUnit &unit, CFont *defaultfont) const; 00369 00370 int Index; 00371 int Width; 00372 int Height; 00373 #if 0 // FIXME : something for color and value parametrisation (not implemented) 00374 Color *colors; 00375 int *values; 00376 #endif 00377 }; 00378 00382 class CContentTypeCompleteBar : public CContentType { 00383 public: 00384 CContentTypeCompleteBar() : Index(-1), Width(0), Height(0), Border(0), Color(0) {} 00385 00386 virtual void Draw(const CUnit &unit, CFont *defaultfont) const; 00387 00388 int Index; 00389 int Width; 00390 int Height; 00391 char Border; 00392 char Color; 00393 }; 00394 00398 class CUnitInfoPanel { 00399 public: 00400 CUnitInfoPanel() : PosX(0), PosY(0), DefaultFont(0), 00401 Contents(), Condition(NULL) {} 00402 ~CUnitInfoPanel() { 00403 for (std::vector<CContentType *>::iterator content = Contents.begin(); 00404 content != Contents.end(); ++content) { 00405 delete *content; 00406 } 00407 delete Condition; 00408 } 00409 00410 00411 std::string Name; 00412 int PosX; 00413 int PosY; 00414 CFont *DefaultFont; 00415 00416 std::vector<CContentType *>Contents; 00417 00418 ConditionPanel *Condition; 00419 }; 00420 00421 00422 class CFiller 00423 { 00424 struct bits_map { 00425 bits_map() : Width(0), Height(0), bstore(NULL) {} 00426 ~bits_map(); 00427 00428 void Init(CGraphic *g); 00429 00430 bool TransparentPixel(int x, int y) 00431 { 00432 if(bstore) { 00433 const unsigned int x_index = x / 32; 00434 y *= Width; 00435 y /= 32; 00436 x -= (x_index * 32); 00437 return ((bstore[y + x_index] & (1 << x)) == 0); 00438 } 00439 return false; 00440 }; 00441 00442 int Width; 00443 int Height; 00444 unsigned int *bstore; 00445 }; 00446 00447 bits_map map; 00448 public: 00449 CFiller() : G(NULL), X(0), Y(0) {} 00450 00451 void Load(); 00452 00453 bool OnGraphic(int x, int y) { 00454 x -= X; 00455 y -= Y; 00456 if (x >= 0 && y >= 0 && x < map.Width && y < map.Height) { 00457 return !map.TransparentPixel(x, y); 00458 } 00459 return false; 00460 } 00461 CGraphic *G; 00462 int X; 00463 int Y; 00464 }; 00465 00466 class CButtonPanel 00467 { 00468 public: 00469 CButtonPanel() : G(NULL), X(0), Y(0), ShowCommandKey(true) 00470 { 00471 AutoCastBorderColorRGB.r = 0; 00472 AutoCastBorderColorRGB.g = 0; 00473 AutoCastBorderColorRGB.b = 0; 00474 } 00475 00476 void Draw(); 00477 void Update(); 00478 void DoClicked(int button); 00479 int DoKey(int key); 00480 00481 CGraphic *G; 00482 int X; 00483 int Y; 00484 std::vector<CUIButton> Buttons; 00485 SDL_Color AutoCastBorderColorRGB; 00486 bool ShowCommandKey; 00487 }; 00488 00489 class CPieMenu { 00490 public: 00491 CPieMenu() : G(NULL), MouseButton(NoButton) 00492 { 00493 memset(this->X, 0, sizeof(this->X)); 00494 memset(this->Y, 0, sizeof(this->Y)); 00495 } 00496 00497 CGraphic *G; 00498 int MouseButton; 00499 int X[8]; 00500 int Y[8]; 00501 00502 void SetRadius(int radius) { 00503 const int coeffX[] = { 0, 193, 256, 193, 0, -193, -256, -193}; 00504 const int coeffY[] = { -256, -193, 0, 193, 256, 193, 0, -193}; 00505 for (int i = 0; i < 8; ++i) { 00506 this->X[i] = (coeffX[i] * radius) >> 8; 00507 this->Y[i] = (coeffY[i] * radius) >> 8; 00508 } 00509 } 00510 }; 00511 00512 class CResourceInfo { 00513 public: 00514 CResourceInfo() : G(NULL), IconFrame(0), IconX(0), IconY(0), IconWidth(-1), 00515 TextX(-1), TextY(-1) {} 00516 00517 CGraphic *G; 00518 int IconFrame; 00519 int IconX; 00520 int IconY; 00521 int IconWidth; 00522 int TextX; 00523 int TextY; 00524 }; 00525 #define MaxResourceInfo MaxCosts + 2 00526 00527 class CInfoPanel 00528 { 00529 public: 00530 CInfoPanel() : G(NULL), X(0), Y(0) {} 00531 00532 void Draw(); 00533 00534 CGraphic *G; 00535 int X; 00536 int Y; 00537 }; 00538 00539 class CStatusLine 00540 { 00541 public: 00542 CStatusLine() : Width(0), TextX(0), TextY(0), Font(0) {} 00543 00544 void Draw(); 00545 void Set(const std::string &status); 00546 inline const std::string &Get() { return this->StatusLine; } 00547 void Clear(); 00548 00549 int Width; 00550 int TextX; 00551 int TextY; 00552 CFont *Font; 00553 00554 private: 00555 std::string StatusLine; 00556 }; 00557 00558 class CUITimer 00559 { 00560 public: 00561 CUITimer() : X(0), Y(0), Font(NULL) {} 00562 00563 int X; 00564 int Y; 00565 CFont *Font; 00566 }; 00567 00568 00572 class CUserInterface 00573 { 00574 public: 00575 CUserInterface(); 00576 ~CUserInterface(); 00577 00578 void Load(); 00579 00580 00581 bool MouseScroll; 00582 bool KeyScroll; 00583 00584 int MouseScrollSpeed; 00586 int MouseScrollSpeedDefault; 00588 int MouseScrollSpeedControl; 00589 00590 int MouseWarpX; 00591 int MouseWarpY; 00592 00593 std::string NormalFontColor; 00594 std::string ReverseFontColor; 00595 00596 std::vector<CFiller> Fillers; 00597 00598 CResourceInfo Resources[MaxResourceInfo]; 00599 00600 CInfoPanel InfoPanel; 00601 std::vector<CUnitInfoPanel *> InfoPanelContents; 00602 00603 CUIButton *SingleSelectedButton; 00604 00605 std::vector<CUIButton> SelectedButtons; 00606 CFont *MaxSelectedFont; 00607 int MaxSelectedTextX; 00608 int MaxSelectedTextY; 00609 00610 CUIButton *SingleTrainingButton; 00611 std::string SingleTrainingText; 00612 CFont *SingleTrainingFont; 00613 int SingleTrainingTextX; 00614 int SingleTrainingTextY; 00615 00616 std::vector<CUIButton> TrainingButtons; 00617 std::string TrainingText; 00618 CFont *TrainingFont; 00619 int TrainingTextX; 00620 int TrainingTextY; 00621 00622 CUIButton *UpgradingButton; 00623 00624 CUIButton *ResearchingButton; 00625 00626 std::vector<CUIButton> TransportingButtons; 00627 00628 // Completed bar 00629 SDL_Color CompletedBarColorRGB; 00630 Uint32 CompletedBarColor; 00631 bool CompletedBarShadow; 00632 00633 // Button panel 00634 CButtonPanel ButtonPanel; 00635 00636 // Pie Menu 00637 CPieMenu PieMenu; 00638 00639 // Map area 00640 ViewportModeType ViewportMode; 00641 CViewport *MouseViewport; 00642 CViewport *SelectedViewport; 00643 int NumViewports; 00644 CViewport Viewports[MAX_NUM_VIEWPORTS]; 00645 CMapArea MapArea; 00646 CFont *MessageFont; 00647 int MessageScrollSpeed; 00648 00649 // Menu buttons 00650 CUIButton MenuButton; 00651 CUIButton NetworkMenuButton; 00652 CUIButton NetworkDiplomacyButton; 00653 00654 // The minimap 00655 CMinimap Minimap; 00656 Uint32 ViewportCursorColor; 00657 00658 // The status line 00659 CStatusLine StatusLine; 00660 00661 // Game timer 00662 CUITimer Timer; 00663 00664 // Offsets for 640x480 center used by menus 00665 int Offset640X; 00666 int Offset480Y; 00667 00668 // 00669 // Cursors used. 00670 // 00671 CursorConfig Point; 00672 CursorConfig Glass; 00673 CursorConfig Cross; 00674 CursorConfig YellowHair; 00675 CursorConfig GreenHair; 00676 CursorConfig RedHair; 00677 CursorConfig Scroll; 00678 00679 CursorConfig ArrowE; 00680 CursorConfig ArrowNE; 00681 CursorConfig ArrowN; 00682 CursorConfig ArrowNW; 00683 CursorConfig ArrowW; 00684 CursorConfig ArrowSW; 00685 CursorConfig ArrowS; 00686 CursorConfig ArrowSE; 00687 00693 00694 CGraphic *VictoryBackgroundG; 00695 CGraphic *DefeatBackgroundG; 00696 }; 00697 00698 /* 00699 * Basic Shared Pointer for Current Selected Buttons 00700 * parallel drawing problems. 00701 */ 00702 class ButtonActionProxy { 00703 ButtonAction *ptr; // pointer to the ButtonAction array 00704 int* count; // shared number of owners 00705 00706 void dispose() { 00707 if (count == NULL || --*count == 0) { 00708 delete count; 00709 delete[] ptr; 00710 } 00711 } 00712 00713 ButtonActionProxy& operator= (ButtonAction *p) { 00714 if (this->ptr != p) { 00715 if (count == NULL || --*count == 0) { 00716 delete[] ptr; 00717 if (count) { 00718 *count = 1; 00719 } 00720 } 00721 ptr = p; 00722 } 00723 return *this; 00724 } 00725 00726 friend void CButtonPanel::Update(); 00727 public: 00728 00729 ButtonActionProxy (): ptr(0), count(0) {} 00730 00731 ButtonActionProxy (const ButtonActionProxy& p) 00732 : ptr(p.ptr), count(p.count) 00733 { 00734 if (!count) { 00735 count = new int(1); 00736 *count = 1; 00737 } 00738 ++*count; 00739 } 00740 00741 ~ButtonActionProxy () { 00742 dispose(); 00743 } 00744 00745 void Reset() { 00746 dispose(); 00747 count = NULL; 00748 ptr = NULL; 00749 } 00750 00751 ButtonAction &operator[](unsigned int index) { 00752 return ptr[index]; 00753 } 00754 00755 bool IsValid() 00756 { 00757 return ptr != NULL; 00758 } 00759 00760 }; 00761 00762 extern ButtonActionProxy CurrentButtons; 00763 00764 /*---------------------------------------------------------------------------- 00765 -- Variables 00766 ----------------------------------------------------------------------------*/ 00767 00768 extern CUserInterface UI; 00769 00771 extern std::map<std::string, ButtonStyle *> ButtonStyleHash; 00772 00773 extern bool RightButtonAttacks; 00774 00775 extern const char DefaultGroupKeys[]; 00776 extern const char *UiGroupKeys; 00777 00778 extern bool FancyBuildings; 00779 00780 // only exported to save them 00781 00782 /*---------------------------------------------------------------------------- 00783 -- Functions 00784 ----------------------------------------------------------------------------*/ 00785 00787 extern void InitUserInterface(); 00789 extern void SaveUserInterface(CFile *file); 00791 extern void CleanUserInterface(); 00792 #ifdef DEBUG 00793 extern void FreeButtonStyles(); 00794 #endif 00796 extern void UserInterfaceCclRegister(); 00797 00799 extern ButtonStyle *FindButtonStyle(const std::string &style); 00800 00802 extern void UIHandleMouseMove(int x, int y); 00804 extern void UIHandleButtonDown(unsigned button); 00806 extern void UIHandleButtonUp(unsigned button); 00807 00809 extern void RestrictCursorToViewport(); 00811 extern void RestrictCursorToMinimap(); 00812 00814 extern CViewport *GetViewport(int x, int y); 00816 extern void CycleViewportMode(int); 00818 extern void SetViewportMode(ViewportModeType mode); 00819 extern void CheckViewportMode(); 00820 extern void UpdateViewports(); 00821 00823 extern void MouseScrollMap(int x, int y); 00825 extern bool GetMouseScroll(); 00827 extern void SetMouseScroll(bool enabled); 00829 extern bool GetKeyScroll(); 00831 extern void SetKeyScroll(bool enabled); 00833 extern bool GetGrabMouse(); 00835 extern void SetGrabMouse(bool enabled); 00837 extern bool GetLeaveStops(); 00839 extern void SetLeaveStops(bool enabled); 00840 00841 extern int AddHandler(lua_State *l); 00842 extern void CallHandler(unsigned int handle, int value); 00843 00844 00846 00847 #endif // !__UI_H__