CIntObjectClasses.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 (e.g. hero list = 5)
  168. int amount; //total amount of elements (e.g. hero list = 0-8)
  169. int positions; //number of highest position (0 if there is only one)
  170. int value; //first active element
  171. int scrollStep; // how many elements will be scrolled via one click, default = 1
  172. bool horizontal;
  173. bool wheelScrolling;
  174. bool keyScrolling;
  175. std::function<void(int)> moved;
  176. void redrawSlider();
  177. void sliderClicked();
  178. void moveLeft();
  179. void moveRight();
  180. void moveTo(int to);
  181. void block(bool on);
  182. void setAmount(int to);
  183. void keyPressed(const SDL_KeyboardEvent & key);
  184. void wheelScrolled(bool down, bool in);
  185. void clickLeft(tribool down, bool previousState);
  186. void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  187. void showAll(SDL_Surface * to);
  188. CSlider(int x, int y, int totalw, std::function<void(int)> Moved, int Capacity, int Amount,
  189. int Value=0, bool Horizontal=true, int style = 0); //style 0 - brown, 1 - blue
  190. ~CSlider();
  191. void moveToMax();
  192. };
  193. /// Used as base for Tabs and List classes
  194. class CObjectList : public CIntObject
  195. {
  196. public:
  197. typedef std::function<CIntObject* (size_t)> CreateFunc;
  198. typedef std::function<void(CIntObject *)> DestroyFunc;
  199. private:
  200. CreateFunc createObject;
  201. DestroyFunc destroyObject;
  202. protected:
  203. //Internal methods for safe creation of items (Children capturing and activation/deactivation if needed)
  204. void deleteItem(CIntObject* item);
  205. CIntObject* createItem(size_t index);
  206. CObjectList(CreateFunc create, DestroyFunc destroy = DestroyFunc());//Protected constructor
  207. };
  208. /// Window element with multiple tabs
  209. class CTabbedInt : public CObjectList
  210. {
  211. private:
  212. CIntObject * activeTab;
  213. size_t activeID;
  214. public:
  215. //CreateFunc, DestroyFunc - see CObjectList
  216. //Pos - position of object, all tabs will be moved to this position
  217. //ActiveID - ID of initially active tab
  218. CTabbedInt(CreateFunc create, DestroyFunc destroy = DestroyFunc(), Point position=Point(), size_t ActiveID=0);
  219. void setActive(size_t which);
  220. //recreate active tab
  221. void reset();
  222. //return currently active item
  223. CIntObject * getItem();
  224. };
  225. /// List of IntObjects with optional slider
  226. class CListBox : public CObjectList
  227. {
  228. private:
  229. std::list< CIntObject* > items;
  230. size_t first;
  231. size_t totalSize;
  232. Point itemOffset;
  233. CSlider * slider;
  234. void updatePositions();
  235. public:
  236. //CreateFunc, DestroyFunc - see CObjectList
  237. //Pos - position of first item
  238. //ItemOffset - distance between items in the list
  239. //VisibleSize - maximal number of displayable at once items
  240. //TotalSize
  241. //Slider - slider style, bit field: 1 = present(disabled), 2=horisontal(vertical), 4=blue(brown)
  242. //SliderPos - position of slider, if present
  243. CListBox(CreateFunc create, DestroyFunc destroy, Point Pos, Point ItemOffset, size_t VisibleSize,
  244. size_t TotalSize, size_t InitialPos=0, int Slider=0, Rect SliderPos=Rect() );
  245. //recreate all visible items
  246. void reset();
  247. //change or get total amount of items in the list
  248. void resize(size_t newSize);
  249. size_t size();
  250. //return item with index which or null if not present
  251. CIntObject * getItem(size_t which);
  252. //return currently active items
  253. const std::list< CIntObject * > & getItems();
  254. //get index of this item. -1 if not found
  255. size_t getIndexOf(CIntObject * item);
  256. //scroll list to make item which visible
  257. void scrollTo(size_t which);
  258. //scroll list to specified position
  259. void moveToPos(size_t which);
  260. void moveToNext();
  261. void moveToPrev();
  262. size_t getPos();
  263. };
  264. /// Small helper class to manage group of similar labels
  265. class CLabelGroup : public CIntObject
  266. {
  267. std::list<CLabel*> labels;
  268. EFonts font;
  269. EAlignment align;
  270. SDL_Color color;
  271. public:
  272. CLabelGroup(EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::WHITE);
  273. void add(int x=0, int y=0, const std::string &text = "");
  274. };
  275. /// Base class for all text-related widgets.
  276. /// Controls text blitting-related options
  277. class CTextContainer : public virtual CIntObject
  278. {
  279. protected:
  280. /// returns size of border, for left- or right-aligned text
  281. virtual Point getBorderSize() = 0;
  282. /// do actual blitting of line. Text "what" will be placed at "where" and aligned according to alignment
  283. void blitLine(SDL_Surface * to, Rect where, std::string what);
  284. CTextContainer(EAlignment alignment, EFonts font, SDL_Color color);
  285. public:
  286. EAlignment alignment;
  287. EFonts font;
  288. SDL_Color color; // default font color. Can be overriden by placing "{}" into the string
  289. };
  290. /// Label which shows text
  291. class CLabel : public CTextContainer
  292. {
  293. protected:
  294. Point getBorderSize() override;
  295. CPicture *bg;
  296. public:
  297. std::string text;
  298. bool autoRedraw; //whether control will redraw itself on setTxt
  299. std::string getText();
  300. virtual void setText(const std::string &Txt);
  301. CLabel(int x=0, int y=0, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT,
  302. const SDL_Color &Color = Colors::WHITE, const std::string &Text = "");
  303. void showAll(SDL_Surface * to); //shows statusbar (with current text)
  304. };
  305. /// Multi-line label that can display multiple lines of text
  306. /// If text is too big to fit into requested area remaining part will not be visible
  307. class CMultiLineLabel : public CLabel
  308. {
  309. // text to blit, split into lines that are no longer than widget width
  310. std::vector<std::string> lines;
  311. // area of text that actually will be printed, default is widget size
  312. Rect visibleSize;
  313. void splitText(const std::string &Txt);
  314. Rect getTextLocation();
  315. public:
  316. // total size of text, x = longest line of text, y = total height of lines
  317. Point textSize;
  318. CMultiLineLabel(int x=0, int y=0, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::WHITE, const std::string &Text = "");
  319. void setText(const std::string &Txt);
  320. void showAll(SDL_Surface * to);
  321. void setVisibleSize(Rect visibleSize);
  322. // scrolls text visible in widget. Positive value will move text up
  323. void scrollTextTo(int distance);
  324. void scrollTextBy(int distance);
  325. };
  326. /// a multi-line label that tries to fit text with given available width and height;
  327. /// if not possible, it creates a slider for scrolling text
  328. class CTextBox : public CIntObject
  329. {
  330. int sliderStyle;
  331. public:
  332. CMultiLineLabel * label;
  333. CSlider *slider;
  334. CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::WHITE);
  335. void resize(Point newSize);
  336. void setText(const std::string &Txt);
  337. void sliderMoved(int to);
  338. };
  339. /// Status bar which is shown at the bottom of the in-game screens
  340. class CGStatusBar : public CLabel
  341. {
  342. bool textLock; //Used for blocking changes to the text
  343. void init();
  344. CGStatusBar *oldStatusBar;
  345. protected:
  346. Point getBorderSize() override;
  347. public:
  348. void clear();//clears statusbar and refreshes
  349. void setText(const std::string & Text) override; //prints text and refreshes statusbar
  350. void show(SDL_Surface * to); //shows statusbar (with current text)
  351. 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
  352. CGStatusBar(int x, int y, std::string name, int maxw=-1);
  353. ~CGStatusBar();
  354. void lock(bool shouldLock); //If true, current text cannot be changed until lock(false) is called
  355. };
  356. /// UIElement which can get input focus
  357. class CFocusable : public virtual CIntObject
  358. {
  359. public:
  360. bool focus; //only one focusable control can have focus at one moment
  361. void giveFocus(); //captures focus
  362. void moveFocus(); //moves focus to next active control (may be used for tab switching)
  363. static std::list<CFocusable*> focusables; //all existing objs
  364. static CFocusable *inputWithFocus; //who has focus now
  365. CFocusable();
  366. ~CFocusable();
  367. };
  368. /// Text input box where players can enter text
  369. class CTextInput : public CLabel, public CFocusable
  370. {
  371. protected:
  372. std::string visibleText();
  373. public:
  374. CFunctionList<void(const std::string &)> cb;
  375. CFunctionList<void(std::string &, const std::string &)> filters;
  376. void setText(const std::string &nText, bool callCb = false);
  377. CTextInput(const Rect &Pos, EFonts font, const CFunctionList<void(const std::string &)> &CB);
  378. CTextInput(const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB);
  379. CTextInput(const Rect &Pos, SDL_Surface *srf = nullptr);
  380. void clickLeft(tribool down, bool previousState) override;
  381. void keyPressed(const SDL_KeyboardEvent & key) override;
  382. bool captureThisEvent(const SDL_KeyboardEvent & key) override;
  383. //Filter that will block all characters not allowed in filenames
  384. static void filenameFilter(std::string &text, const std::string & oldText);
  385. //Filter that will allow only input of numbers in range min-max (min-max are allowed)
  386. //min-max should be set via something like boost::bind
  387. static void numberFilter(std::string &text, const std::string & oldText, int minValue, int maxValue);
  388. };
  389. /// Shows a text by moving the mouse cursor over the object
  390. class CHoverableArea: public virtual CIntObject
  391. {
  392. public:
  393. std::string hoverText;
  394. virtual void hover (bool on);
  395. CHoverableArea();
  396. virtual ~CHoverableArea();
  397. };
  398. /// Can interact on left and right mouse clicks, plus it shows a text when by hovering over it
  399. class LRClickableAreaWText: public CHoverableArea
  400. {
  401. public:
  402. std::string text;
  403. LRClickableAreaWText();
  404. LRClickableAreaWText(const Rect &Pos, const std::string &HoverText = "", const std::string &ClickText = "");
  405. virtual ~LRClickableAreaWText();
  406. void init();
  407. virtual void clickLeft(tribool down, bool previousState);
  408. virtual void clickRight(tribool down, bool previousState);
  409. };
  410. /// Basic class for windows
  411. class CWindowObject : public CIntObject
  412. {
  413. CPicture * createBg(std::string imageName, bool playerColored);
  414. int getUsedEvents(int options);
  415. CIntObject *shadow;
  416. void setShadow(bool on);
  417. int options;
  418. protected:
  419. CPicture * background;
  420. //Simple function with call to GH.popInt
  421. void close();
  422. //Used only if RCLICK_POPUP was set
  423. void clickRight(tribool down, bool previousState);
  424. //To display border
  425. void showAll(SDL_Surface *to);
  426. //change or set background image
  427. void setBackground(std::string filename);
  428. void updateShadow();
  429. public:
  430. enum EOptions
  431. {
  432. PLAYER_COLORED=1, //background will be player-colored
  433. RCLICK_POPUP=2, // window will behave as right-click popup
  434. BORDERED=4, // window will have border if current resolution is bigger than size of window
  435. SHADOW_DISABLED=8 //this window won't display any shadow
  436. };
  437. /*
  438. * options - EOpions enum
  439. * imageName - name for background image, can be empty
  440. * centerAt - position of window center. Default - center of the screen
  441. */
  442. CWindowObject(int options, std::string imageName, Point centerAt);
  443. CWindowObject(int options, std::string imageName = "");
  444. ~CWindowObject();
  445. };