CIntObjectClasses.h 19 KB

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