2
0

CIntObjectClasses.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #pragma once
  2. #include "CIntObject.h"
  3. #include "SDL_Extensions.h"
  4. #include "../FunctionList.h"
  5. struct SDL_Surface;
  6. struct Rect;
  7. class CAnimImage;
  8. class CLabel;
  9. class CAnimation;
  10. class CDefHandler;
  11. /*
  12. * CPicture.h, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. // Window GUI class
  21. class CSimpleWindow : public CIntObject
  22. {
  23. public:
  24. SDL_Surface * bitmap; //background
  25. virtual void show(SDL_Surface * to);
  26. CSimpleWindow():bitmap(nullptr){}; //c-tor
  27. virtual ~CSimpleWindow(); //d-tor
  28. };
  29. // Image class
  30. class CPicture : public CIntObject
  31. {
  32. void setSurface(SDL_Surface *to);
  33. public:
  34. SDL_Surface * bg;
  35. Rect * srcRect; //if nullptr then whole surface will be used
  36. bool freeSurf; //whether surface will be freed upon CPicture destruction
  37. bool needRefresh;//Surface needs to be displayed each frame
  38. operator SDL_Surface*()
  39. {
  40. return bg;
  41. }
  42. CPicture(const Rect & r, const SDL_Color & color, bool screenFormat = false); //rect filled with given color
  43. CPicture(const Rect & r, ui32 color, bool screenFormat = false); //rect filled with given color
  44. CPicture(SDL_Surface * BG, int x = 0, int y=0, bool Free = true); //wrap existing SDL_Surface
  45. CPicture(const std::string &bmpname, int x=0, int y=0);
  46. CPicture(SDL_Surface *BG, const Rect &SrcRext, int x = 0, int y = 0, bool free = false); //wrap subrect of given surface
  47. ~CPicture();
  48. void init();
  49. //set alpha value for whole surface. Note: may be messed up if surface is shared
  50. // 0=transparent, 255=opaque
  51. void setAlpha(int value);
  52. void scaleTo(Point size);
  53. void createSimpleRect(const Rect &r, bool screenFormat, ui32 color);
  54. void show(SDL_Surface * to);
  55. void showAll(SDL_Surface * to);
  56. void convertToScreenBPP();
  57. void colorizeAndConvert(PlayerColor player);
  58. void colorize(PlayerColor player);
  59. };
  60. /// area filled with specific texture
  61. class CFilledTexture : CIntObject
  62. {
  63. SDL_Surface * texture;
  64. public:
  65. CFilledTexture(std::string imageName, Rect position);
  66. ~CFilledTexture();
  67. void showAll(SDL_Surface *to);
  68. };
  69. namespace config{struct ButtonInfo;}
  70. /// Base class for buttons.
  71. class CButtonBase : public CKeyShortcut
  72. {
  73. public:
  74. enum ButtonState
  75. {
  76. NORMAL=0,
  77. PRESSED=1,
  78. BLOCKED=2,
  79. HIGHLIGHTED=3
  80. };
  81. private:
  82. int bitmapOffset; // base offset of visible bitmap from animation
  83. ButtonState state;//current state of button from enum
  84. public:
  85. bool swappedImages,//fix for some buttons: normal and pressed image are swapped
  86. keepFrame; // don't change visual representation
  87. void addTextOverlay(const std::string &Text, EFonts font, SDL_Color color = Colors::WHITE);
  88. void update();//to refresh button after image or text change
  89. void setOffset(int newOffset);
  90. void setState(ButtonState newState);
  91. ButtonState getState();
  92. //just to make code clearer
  93. void block(bool on);
  94. bool isBlocked();
  95. bool isHighlighted();
  96. CAnimImage * image; //image for this button
  97. CLabel * text;//text overlay
  98. CButtonBase(); //c-tor
  99. virtual ~CButtonBase(); //d-tor
  100. };
  101. /// Typical Heroes 3 button which can be inactive or active and can
  102. /// hold further information if you right-click it
  103. class CAdventureMapButton : public CButtonBase
  104. {
  105. std::vector<std::string> imageNames;//store list of images that can be used by this button
  106. size_t currentImage;
  107. public:
  108. std::map<int, std::string> hoverTexts; //text for statusbar
  109. std::string helpBox; //for right-click help
  110. CFunctionList<void()> callback;
  111. bool actOnDown,//runs when mouse is pressed down over it, not when up
  112. hoverable,//if true, button will be highlighted when hovered
  113. borderEnabled,
  114. soundDisabled;
  115. SDL_Color borderColor;
  116. void clickRight(tribool down, bool previousState);
  117. virtual void clickLeft(tribool down, bool previousState);
  118. void hover (bool on);
  119. CAdventureMapButton(); //c-tor
  120. CAdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, int x, int y, const std::string &defName, int key=0, std::vector<std::string> * add = nullptr, bool playerColoredButton = false );//c-tor
  121. CAdventureMapButton( const std::pair<std::string, std::string> &help, const CFunctionList<void()> &Callback, int x, int y, const std::string &defName, int key=0, std::vector<std::string> * add = nullptr, bool playerColoredButton = false );//c-tor
  122. CAdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, config::ButtonInfo *info, int key=0);//c-tor
  123. void init(const CFunctionList<void()> &Callback, const std::map<int,std::string> &Name, const std::string &HelpBox, bool playerColoredButton, const std::string &defName, std::vector<std::string> * add, int x, int y, int key );
  124. void setIndex(size_t index, bool playerColoredButton=false);
  125. void setImage(CAnimation* anim, bool playerColoredButton=false, int animFlags=0);
  126. void setPlayerColor(PlayerColor player);
  127. void showAll(SDL_Surface * to);
  128. };
  129. /// A button which can be selected/deselected
  130. class CHighlightableButton
  131. : public CAdventureMapButton
  132. {
  133. public:
  134. CHighlightableButton(const CFunctionList<void()> &onSelect, const CFunctionList<void()> &onDeselect, const std::map<int,std::string> &Name, const std::string &HelpBox, bool playerColoredButton, const std::string &defName, std::vector<std::string> * add, int x, int y, int key=0);
  135. CHighlightableButton(const std::pair<std::string, std::string> &help, const CFunctionList<void()> &onSelect, int x, int y, const std::string &defName, int myid, int key=0, std::vector<std::string> * add = nullptr, bool playerColoredButton = false );//c-tor
  136. CHighlightableButton(const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &onSelect, int x, int y, const std::string &defName, int myid, int key=0, std::vector<std::string> * add = nullptr, bool playerColoredButton = false );//c-tor
  137. bool onlyOn;//button can not be de-selected
  138. bool selected;//state of highlightable button
  139. int ID; //for identification
  140. CFunctionList<void()> callback2; //when de-selecting
  141. void select(bool on);
  142. void clickLeft(tribool down, bool previousState);
  143. };
  144. /// A group of buttons where one button can be selected
  145. class CHighlightableButtonsGroup : public CIntObject
  146. {
  147. public:
  148. CFunctionList2<void(int)> onChange; //called when changing selected button with new button's id
  149. std::vector<CHighlightableButton*> buttons;
  150. bool musicLike; //determines the behaviour of this group
  151. //void addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid);
  152. void addButton(CHighlightableButton* bt);//add existing button, it'll be deleted by CHighlightableButtonsGroup destructor
  153. void addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid, const CFunctionList<void()> &OnSelect=0, int key=0); //creates new button
  154. CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons = false);
  155. ~CHighlightableButtonsGroup();
  156. void select(int id, bool mode); //mode==0: id is serial; mode==1: id is unique button id
  157. void selectionChanged(int to);
  158. void show(SDL_Surface * to);
  159. void showAll(SDL_Surface * to);
  160. void block(ui8 on);
  161. };
  162. /// A typical slider which can be orientated horizontally/vertically.
  163. class CSlider : public CIntObject
  164. {
  165. public:
  166. CAdventureMapButton *left, *right, *slider; //if vertical then left=up
  167. int capacity,//how many elements can be active at same time
  168. amount, //how many elements
  169. positions, //number of highest position (0 if there is only one)
  170. value; //first active element
  171. bool horizontal;
  172. bool wheelScrolling;
  173. bool keyScrolling;
  174. std::function<void(int)> moved;
  175. void redrawSlider();
  176. void sliderClicked();
  177. void moveLeft();
  178. void moveRight();
  179. void moveTo(int to);
  180. void block(bool on);
  181. void setAmount(int to);
  182. void keyPressed(const SDL_KeyboardEvent & key);
  183. void wheelScrolled(bool down, bool in);
  184. void clickLeft(tribool down, bool previousState);
  185. void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  186. void showAll(SDL_Surface * to);
  187. CSlider(int x, int y, int totalw, std::function<void(int)> Moved, int Capacity, int Amount,
  188. int Value=0, bool Horizontal=true, int style = 0); //style 0 - brown, 1 - blue
  189. ~CSlider();
  190. void moveToMax();
  191. };
  192. /// Used as base for Tabs and List classes
  193. class CObjectList : public CIntObject
  194. {
  195. public:
  196. typedef std::function<CIntObject* (size_t)> CreateFunc;
  197. typedef std::function<void(CIntObject *)> DestroyFunc;
  198. private:
  199. CreateFunc createObject;
  200. DestroyFunc destroyObject;
  201. protected:
  202. //Internal methods for safe creation of items (Children capturing and activation/deactivation if needed)
  203. void deleteItem(CIntObject* item);
  204. CIntObject* createItem(size_t index);
  205. CObjectList(CreateFunc create, DestroyFunc destroy = DestroyFunc());//Protected constructor
  206. };
  207. /// Window element with multiple tabs
  208. class CTabbedInt : public CObjectList
  209. {
  210. private:
  211. CIntObject * activeTab;
  212. size_t activeID;
  213. public:
  214. //CreateFunc, DestroyFunc - see CObjectList
  215. //Pos - position of object, all tabs will be moved to this position
  216. //ActiveID - ID of initially active tab
  217. CTabbedInt(CreateFunc create, DestroyFunc destroy = DestroyFunc(), Point position=Point(), size_t ActiveID=0);
  218. void setActive(size_t which);
  219. //recreate active tab
  220. void reset();
  221. //return currently active item
  222. CIntObject * getItem();
  223. };
  224. /// List of IntObjects with optional slider
  225. class CListBox : public CObjectList
  226. {
  227. private:
  228. std::list< CIntObject* > items;
  229. size_t first;
  230. size_t totalSize;
  231. Point itemOffset;
  232. CSlider * slider;
  233. void updatePositions();
  234. public:
  235. //CreateFunc, DestroyFunc - see CObjectList
  236. //Pos - position of first item
  237. //ItemOffset - distance between items in the list
  238. //VisibleSize - maximal number of displayable at once items
  239. //TotalSize
  240. //Slider - slider style, bit field: 1 = present(disabled), 2=horisontal(vertical), 4=blue(brown)
  241. //SliderPos - position of slider, if present
  242. CListBox(CreateFunc create, DestroyFunc destroy, Point Pos, Point ItemOffset, size_t VisibleSize,
  243. size_t TotalSize, size_t InitialPos=0, int Slider=0, Rect SliderPos=Rect() );
  244. //recreate all visible items
  245. void reset();
  246. //change or get total amount of items in the list
  247. void resize(size_t newSize);
  248. size_t size();
  249. //return item with index which or null if not present
  250. CIntObject * getItem(size_t which);
  251. //return currently active items
  252. const std::list< CIntObject * > & getItems();
  253. //get index of this item. -1 if not found
  254. size_t getIndexOf(CIntObject * item);
  255. //scroll list to make item which visible
  256. void scrollTo(size_t which);
  257. //scroll list to specified position
  258. void moveToPos(size_t which);
  259. void moveToNext();
  260. void moveToPrev();
  261. size_t getPos();
  262. };
  263. class CTextContainer : public virtual CIntObject
  264. {
  265. protected:
  266. void blitLine(SDL_Surface * to, Point where, std::string what);
  267. CTextContainer(EAlignment alignment, EFonts font, SDL_Color color);
  268. //CTextContainer() {};
  269. public:
  270. EAlignment alignment;
  271. EFonts font;
  272. SDL_Color color;
  273. };
  274. /// Label which shows text
  275. class CLabel : public CTextContainer
  276. {
  277. protected:
  278. virtual std::string visibleText();
  279. public:
  280. std::string text;
  281. CPicture *bg;
  282. bool autoRedraw; //whether control will redraw itself on setTxt
  283. Point textOffset; //text will be blitted at pos + textOffset with appropriate alignment
  284. bool ignoreLeadingWhitespace;
  285. virtual void setTxt(const std::string &Txt);
  286. void showAll(SDL_Surface * to); //shows statusbar (with current text)
  287. CLabel(int x=0, int y=0, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::WHITE, const std::string &Text = "");
  288. };
  289. class CBoundedLabel : public CLabel
  290. {
  291. public:
  292. int maxW; //longest line of text in px
  293. int maxH; //total height needed to print all lines
  294. std::vector<std::string> lines;
  295. CBoundedLabel(int x=0, int y=0, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::WHITE, const std::string &Text = "")
  296. : CLabel (x, y, Font, Align, Color, Text){};
  297. void setTxt(const std::string &Txt);
  298. void setBounds(int limitW, int limitH);
  299. virtual void recalculateLines(const std::string &Txt);
  300. void showAll(SDL_Surface * to);
  301. };
  302. //Small helper class to manage group of similar labels
  303. class CLabelGroup : public CIntObject
  304. {
  305. std::list<CLabel*> labels;
  306. EFonts font;
  307. EAlignment align;
  308. const SDL_Color &color;
  309. public:
  310. CLabelGroup(EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::WHITE);
  311. void add(int x=0, int y=0, const std::string &text = "");
  312. };
  313. /// a multi-line label that tries to fit text with given available width and height; if not possible, it creates a slider for scrolling text
  314. class CTextBox : public CBoundedLabel
  315. {
  316. public:
  317. int sliderStyle;
  318. std::vector<CAnimImage* > effects;
  319. CSlider *slider;
  320. //CTextBox( std::string Text, const Point &Pos, int w, int h, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::WHITE);
  321. CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::WHITE);
  322. void showAll(SDL_Surface * to); //shows statusbar (with current text)
  323. void recalculateLines(const std::string &Txt);
  324. void sliderMoved(int to);
  325. };
  326. /// Status bar which is shown at the bottom of the in-game screens
  327. class CGStatusBar : public CLabel
  328. {
  329. bool textLock; //Used for blocking changes to the text
  330. void init();
  331. public:
  332. CGStatusBar *oldStatusBar;
  333. //statusbar interface overloads
  334. void print(const std::string & Text); //prints text and refreshes statusbar
  335. void clear();//clears statusbar and refreshes
  336. std::string getCurrent(); //returns currently displayed text
  337. void show(SDL_Surface * to); //shows statusbar (with current text)
  338. //CGStatusBar(int x, int y, EFonts Font = FONT_SMALL, EAlignment Align = CENTER, const SDL_Color &Color = Colors::WHITE, const std::string &Text = "");
  339. CGStatusBar(CPicture *BG, EFonts Font = FONT_SMALL, EAlignment Align = CENTER, const SDL_Color &Color = Colors::WHITE); //given CPicture will be captured by created sbar and it's pos will be used as pos for sbar
  340. CGStatusBar(int x, int y, std::string name, int maxw=-1);
  341. ~CGStatusBar();
  342. void calcOffset();
  343. void lock(bool shouldLock); //If true, current text cannot be changed until lock(false) is called
  344. const static Point edgeOffset; //Amount to move text from side when alignment is left or right
  345. };
  346. /// UIElement which can get input focus
  347. class CFocusable : public virtual CIntObject
  348. {
  349. public:
  350. bool focus; //only one focusable control can have focus at one moment
  351. void giveFocus(); //captures focus
  352. void moveFocus(); //moves focus to next active control (may be used for tab switching)
  353. static std::list<CFocusable*> focusables; //all existing objs
  354. static CFocusable *inputWithFocus; //who has focus now
  355. CFocusable();
  356. ~CFocusable();
  357. };
  358. /// Text input box where players can enter text
  359. class CTextInput : public CLabel, public CFocusable
  360. {
  361. protected:
  362. std::string visibleText();
  363. public:
  364. CFunctionList<void(const std::string &)> cb;
  365. CFunctionList<void(std::string &, const std::string &)> filters;
  366. void setTxt(const std::string &nText, bool callCb = false);
  367. CTextInput(const Rect &Pos, EFonts font, const CFunctionList<void(const std::string &)> &CB);
  368. CTextInput(const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB);
  369. CTextInput(const Rect &Pos, SDL_Surface *srf = nullptr);
  370. void clickLeft(tribool down, bool previousState) override;
  371. void keyPressed(const SDL_KeyboardEvent & key) override;
  372. bool captureThisEvent(const SDL_KeyboardEvent & key) override;
  373. //Filter that will block all characters not allowed in filenames
  374. static void filenameFilter(std::string &text, const std::string & oldText);
  375. //Filter that will allow only input of numbers in range min-max (min-max are allowed)
  376. //min-max should be set via something like std::bind
  377. static void numberFilter(std::string &text, const std::string & oldText, int minValue, int maxValue);
  378. };
  379. /// Shows a text by moving the mouse cursor over the object
  380. class CHoverableArea: public virtual CIntObject
  381. {
  382. public:
  383. std::string hoverText;
  384. virtual void hover (bool on);
  385. CHoverableArea();
  386. virtual ~CHoverableArea();
  387. };
  388. /// Can interact on left and right mouse clicks, plus it shows a text when by hovering over it
  389. class LRClickableAreaWText: public CHoverableArea
  390. {
  391. public:
  392. std::string text;
  393. LRClickableAreaWText();
  394. LRClickableAreaWText(const Rect &Pos, const std::string &HoverText = "", const std::string &ClickText = "");
  395. virtual ~LRClickableAreaWText();
  396. void init();
  397. virtual void clickLeft(tribool down, bool previousState);
  398. virtual void clickRight(tribool down, bool previousState);
  399. };
  400. /// Basic class for windows
  401. class CWindowObject : public CIntObject
  402. {
  403. CPicture * createBg(std::string imageName, bool playerColored);
  404. int getUsedEvents(int options);
  405. CIntObject *shadow;
  406. void setShadow(bool on);
  407. int options;
  408. protected:
  409. CPicture * background;
  410. //Simple function with call to GH.popInt
  411. void close();
  412. //Used only if RCLICK_POPUP was set
  413. void clickRight(tribool down, bool previousState);
  414. //To display border
  415. void showAll(SDL_Surface *to);
  416. //change or set background image
  417. void setBackground(std::string filename);
  418. void updateShadow();
  419. public:
  420. enum EOptions
  421. {
  422. PLAYER_COLORED=1, //background will be player-colored
  423. RCLICK_POPUP=2, // window will behave as right-click popup
  424. BORDERED=4, // window will have border if current resolution is bigger than size of window
  425. SHADOW_DISABLED=8 //this window won't display any shadow
  426. };
  427. /*
  428. * options - EOpions enum
  429. * imageName - name for background image, can be empty
  430. * centerAt - position of window center. Default - center of the screen
  431. */
  432. CWindowObject(int options, std::string imageName, Point centerAt);
  433. CWindowObject(int options, std::string imageName = "");
  434. ~CWindowObject();
  435. };