CIntObjectClasses.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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(NULL){}; //c-tor
  27. virtual ~CSimpleWindow(); //d-tor
  28. };
  29. // Image class
  30. class CPicture : public CIntObject
  31. {
  32. public:
  33. SDL_Surface * bg;
  34. Rect * srcRect; //if NULL then whole surface will be used
  35. bool freeSurf; //whether surface will be freed upon CPicture destruction
  36. bool needRefresh;//Surface needs to be displayed each frame
  37. operator SDL_Surface*()
  38. {
  39. return bg;
  40. }
  41. CPicture(const Rect & r, const SDL_Color & color, bool screenFormat = false); //rect filled with given color
  42. CPicture(const Rect & r, ui32 color, bool screenFormat = false); //rect filled with given color
  43. CPicture(SDL_Surface * BG, int x = 0, int y=0, bool Free = true); //wrap existing SDL_Surface
  44. CPicture(const std::string &bmpname, int x=0, int y=0);
  45. CPicture(SDL_Surface *BG, const Rect &SrcRext, int x = 0, int y = 0, bool free = false); //wrap subrect of given surface
  46. ~CPicture();
  47. void init();
  48. void createSimpleRect(const Rect &r, bool screenFormat, ui32 color);
  49. void show(SDL_Surface * to);
  50. void showAll(SDL_Surface * to);
  51. void convertToScreenBPP();
  52. void colorizeAndConvert(int player);
  53. void colorize(int player);
  54. };
  55. namespace config{struct ButtonInfo;}
  56. /// Base class for buttons.
  57. class CButtonBase : public CKeyShortcut
  58. {
  59. public:
  60. enum ButtonState
  61. {
  62. NORMAL=0,
  63. PRESSED=1,
  64. BLOCKED=2,
  65. HIGHLIGHTED=3
  66. };
  67. private:
  68. int bitmapOffset; // base offset of visible bitmap from animation
  69. ButtonState state;//current state of button from enum
  70. public:
  71. bool swappedImages,//fix for some buttons: normal and pressed image are swapped
  72. keepFrame; // don't change visual representation
  73. void addTextOverlay(const std::string &Text, EFonts font, SDL_Color color = Colors::Cornsilk);
  74. void update();//to refresh button after image or text change
  75. void setOffset(int newOffset);
  76. void setState(ButtonState newState);
  77. ButtonState getState();
  78. //just to make code clearer
  79. void block(bool on);
  80. bool isBlocked();
  81. bool isHighlighted();
  82. CAnimImage * image; //image for this button
  83. CLabel * text;//text overlay
  84. CButtonBase(); //c-tor
  85. virtual ~CButtonBase(); //d-tor
  86. };
  87. /// Typical Heroes 3 button which can be inactive or active and can
  88. /// hold further information if you right-click it
  89. class CAdventureMapButton : public CButtonBase
  90. {
  91. std::vector<std::string> imageNames;//store list of images that can be used by this button
  92. size_t currentImage;
  93. public:
  94. std::map<int, std::string> hoverTexts; //text for statusbar
  95. std::string helpBox; //for right-click help
  96. CFunctionList<void()> callback;
  97. bool actOnDown,//runs when mouse is pressed down over it, not when up
  98. hoverable,//if true, button will be highlighted when hovered
  99. borderEnabled,
  100. soundDisabled;
  101. SDL_Color borderColor;
  102. void clickRight(tribool down, bool previousState);
  103. virtual void clickLeft(tribool down, bool previousState);
  104. void hover (bool on);
  105. CAdventureMapButton(); //c-tor
  106. 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 = NULL, bool playerColoredButton = false );//c-tor
  107. 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 = NULL, bool playerColoredButton = false );//c-tor
  108. CAdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, config::ButtonInfo *info, int key=0);//c-tor
  109. 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 );
  110. void setIndex(size_t index, bool playerColoredButton=false);
  111. void setImage(CAnimation* anim, bool playerColoredButton=false);
  112. void setPlayerColor(int player);
  113. void showAll(SDL_Surface * to);
  114. };
  115. /// A button which can be selected/deselected
  116. class CHighlightableButton
  117. : public CAdventureMapButton
  118. {
  119. public:
  120. 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);
  121. 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 = NULL, bool playerColoredButton = false );//c-tor
  122. 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 = NULL, bool playerColoredButton = false );//c-tor
  123. bool onlyOn;//button can not be de-selected
  124. bool selected;//state of highlightable button
  125. int ID; //for identification
  126. CFunctionList<void()> callback2; //when de-selecting
  127. void select(bool on);
  128. void clickLeft(tribool down, bool previousState);
  129. };
  130. /// A group of buttons where one button can be selected
  131. class CHighlightableButtonsGroup : public CIntObject
  132. {
  133. public:
  134. CFunctionList2<void(int)> onChange; //called when changing selected button with new button's id
  135. std::vector<CHighlightableButton*> buttons;
  136. bool musicLike; //determines the behaviour of this group
  137. //void addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid);
  138. void addButton(CHighlightableButton* bt);//add existing button, it'll be deleted by CHighlightableButtonsGroup destructor
  139. 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
  140. CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons = false);
  141. ~CHighlightableButtonsGroup();
  142. void select(int id, bool mode); //mode==0: id is serial; mode==1: id is unique button id
  143. void selectionChanged(int to);
  144. void show(SDL_Surface * to);
  145. void showAll(SDL_Surface * to);
  146. void block(ui8 on);
  147. };
  148. /// A typical slider which can be orientated horizontally/vertically.
  149. class CSlider : public CIntObject
  150. {
  151. public:
  152. CAdventureMapButton *left, *right, *slider; //if vertical then left=up
  153. int capacity,//how many elements can be active at same time
  154. amount, //how many elements
  155. positions, //number of highest position (0 if there is only one)
  156. value; //first active element
  157. bool horizontal;
  158. bool wheelScrolling;
  159. bool keyScrolling;
  160. boost::function<void(int)> moved;
  161. void redrawSlider();
  162. void sliderClicked();
  163. void moveLeft();
  164. void moveRight();
  165. void moveTo(int to);
  166. void block(bool on);
  167. void setAmount(int to);
  168. void keyPressed(const SDL_KeyboardEvent & key);
  169. void wheelScrolled(bool down, bool in);
  170. void clickLeft(tribool down, bool previousState);
  171. void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  172. void showAll(SDL_Surface * to);
  173. CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount,
  174. int Value=0, bool Horizontal=true, int style = 0); //style 0 - brown, 1 - blue
  175. ~CSlider();
  176. void moveToMax();
  177. };
  178. /// Used as base for Tabs and List classes
  179. class CObjectList : public CIntObject
  180. {
  181. public:
  182. typedef boost::function<CIntObject* (size_t)> CreateFunc;
  183. typedef boost::function<void(CIntObject *)> DestroyFunc;
  184. private:
  185. CreateFunc createObject;
  186. DestroyFunc destroyObject;
  187. protected:
  188. //Internal methods for safe creation of items (Children capturing and activation/deactivation if needed)
  189. void deleteItem(CIntObject* item);
  190. CIntObject* createItem(size_t index);
  191. CObjectList(CreateFunc create, DestroyFunc destroy = DestroyFunc());//Protected constructor
  192. };
  193. /// Window element with multiple tabs
  194. class CTabbedInt : public CObjectList
  195. {
  196. private:
  197. CIntObject * activeTab;
  198. size_t activeID;
  199. public:
  200. //CreateFunc, DestroyFunc - see CObjectList
  201. //Pos - position of object, all tabs will be moved to this position
  202. //ActiveID - ID of initially active tab
  203. CTabbedInt(CreateFunc create, DestroyFunc destroy = DestroyFunc(), Point position=Point(), size_t ActiveID=0);
  204. void setActive(size_t which);
  205. //recreate active tab
  206. void reset();
  207. //return currently active item
  208. CIntObject * getItem();
  209. };
  210. /// List of IntObjects with optional slider
  211. class CListBox : public CObjectList
  212. {
  213. private:
  214. std::list< CIntObject* > items;
  215. size_t first;
  216. size_t totalSize;
  217. Point itemOffset;
  218. CSlider * slider;
  219. void updatePositions();
  220. public:
  221. //CreateFunc, DestroyFunc - see CObjectList
  222. //Pos - position of first item
  223. //ItemOffset - distance between items in the list
  224. //VisibleSize - maximal number of displayable at once items
  225. //TotalSize
  226. //Slider - slider style, bit field: 1 = present(disabled), 2=horisontal(vertical), 4=blue(brown)
  227. //SliderPos - position of slider, if present
  228. CListBox(CreateFunc create, DestroyFunc destroy, Point Pos, Point ItemOffset, size_t VisibleSize,
  229. size_t TotalSize, size_t InitialPos=0, int Slider=0, Rect SliderPos=Rect() );
  230. //recreate all visible items
  231. void reset();
  232. //return currently active items
  233. std::list< CIntObject * > getItems();
  234. //scroll list
  235. void moveToPos(size_t which);
  236. void moveToNext();
  237. void moveToPrev();
  238. };
  239. /// Status bar which is shown at the bottom of the in-game screens
  240. class CStatusBar
  241. : public CIntObject, public IStatusBar
  242. {
  243. public:
  244. SDL_Surface * bg; //background
  245. int middlex, middley; //middle of statusbar
  246. std::string current; //text currently printed
  247. CStatusBar(int x, int y, std::string name="ADROLLVR.bmp", int maxw=-1); //c-tor
  248. ~CStatusBar(); //d-tor
  249. void print(const std::string & text); //prints text and refreshes statusbar
  250. void clear();//clears statusbar and refreshes
  251. void show(SDL_Surface * to); //shows statusbar (with current text)
  252. std::string getCurrent(); //getter for current
  253. };
  254. /// Label which shows text
  255. class CLabel
  256. : public virtual CIntObject
  257. {
  258. public:
  259. EAlignment alignment;
  260. EFonts font;
  261. SDL_Color color;
  262. std::string text;
  263. CPicture *bg;
  264. bool autoRedraw; //whether control will redraw itself on setTxt
  265. Point textOffset; //text will be blitted at pos + textOffset with appropriate alignment
  266. bool ignoreLeadingWhitespace;
  267. virtual void setTxt(const std::string &Txt);
  268. void showAll(SDL_Surface * to); //shows statusbar (with current text)
  269. CLabel(int x=0, int y=0, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::Cornsilk, const std::string &Text = "");
  270. };
  271. /// 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
  272. class CTextBox
  273. : public CLabel
  274. {
  275. public:
  276. int maxW; //longest line of text in px
  277. int maxH; //total height needed to print all lines
  278. int sliderStyle;
  279. bool redrawParentOnScrolling;
  280. std::vector<std::string> lines;
  281. std::vector<CAnimImage* > effects;
  282. CSlider *slider;
  283. //CTextBox( std::string Text, const Point &Pos, int w, int h, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::Cornsilk);
  284. CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::Cornsilk);
  285. void showAll(SDL_Surface * to); //shows statusbar (with current text)
  286. void setTxt(const std::string &Txt);
  287. void setBounds(int limitW, int limitH);
  288. void recalculateLines(const std::string &Txt);
  289. void sliderMoved(int to);
  290. };
  291. /// Status bar which is shown at the bottom of the in-game screens
  292. class CGStatusBar
  293. : public CLabel, public IStatusBar
  294. {
  295. void init();
  296. public:
  297. IStatusBar *oldStatusBar;
  298. //statusbar interface overloads
  299. void print(const std::string & Text); //prints text and refreshes statusbar
  300. void clear();//clears statusbar and refreshes
  301. std::string getCurrent(); //returns currently displayed text
  302. void show(SDL_Surface * to); //shows statusbar (with current text)
  303. CGStatusBar(int x, int y, EFonts Font = FONT_SMALL, EAlignment Align = CENTER, const SDL_Color &Color = Colors::Cornsilk, const std::string &Text = "");
  304. CGStatusBar(CPicture *BG, EFonts Font = FONT_SMALL, EAlignment Align = CENTER, const SDL_Color &Color = Colors::Cornsilk); //given CPicture will be captured by created sbar and it's pos will be used as pos for sbar
  305. CGStatusBar(int x, int y, std::string name, int maxw=-1);
  306. ~CGStatusBar();
  307. void calcOffset();
  308. };
  309. /// UIElement which can get input focus
  310. class CFocusable
  311. : public virtual CIntObject
  312. {
  313. public:
  314. bool focus; //only one focusable control can have focus at one moment
  315. void giveFocus(); //captures focus
  316. void moveFocus(); //moves focus to next active control (may be used for tab switching)
  317. static std::list<CFocusable*> focusables; //all existing objs
  318. static CFocusable *inputWithFocus; //who has focus now
  319. CFocusable();
  320. ~CFocusable();
  321. };
  322. /// Text input box where players can enter text
  323. class CTextInput
  324. : public CLabel, public CFocusable
  325. {
  326. public:
  327. CFunctionList<void(const std::string &)> cb;
  328. void setText(const std::string &nText, bool callCb = false);
  329. CTextInput(const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB);
  330. CTextInput(const Rect &Pos, SDL_Surface *srf = NULL);
  331. ~CTextInput();
  332. void showAll(SDL_Surface * to);
  333. void clickLeft(tribool down, bool previousState);
  334. void keyPressed(const SDL_KeyboardEvent & key);
  335. };
  336. /// Listbox UI Element
  337. class CList : public CIntObject
  338. {
  339. public:
  340. SDL_Surface * bg; //background bitmap
  341. CDefHandler *arrup, *arrdo; //button arrows for scrolling list
  342. SDL_Surface *empty, *selection;
  343. SDL_Rect arrupp, arrdop; //positions of arrows
  344. int posw, posh; //position width/height
  345. int selected, //id of selected position, <0 if none
  346. from;
  347. const int SIZE; //size of list
  348. tribool pressed; //true=up; false=down; indeterminate=none
  349. CList(int Size = 5); //c-tor
  350. void clickLeft(tribool down, bool previousState);
  351. void activate();
  352. void deactivate();
  353. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent)=0; //call-in
  354. virtual void genList()=0;
  355. virtual void select(int which)=0;
  356. virtual void draw(SDL_Surface * to)=0;
  357. virtual int size() = 0; //how many elements do we have
  358. void fixPos(); //scrolls list, so the selection will be visible
  359. };
  360. /// Shows a text by moving the mouse cursor over the object
  361. class CHoverableArea: public virtual CIntObject
  362. {
  363. public:
  364. std::string hoverText;
  365. virtual void hover (bool on);
  366. CHoverableArea();
  367. virtual ~CHoverableArea();
  368. };
  369. /// Can interact on left and right mouse clicks, plus it shows a text when by hovering over it
  370. class LRClickableAreaWText: public CHoverableArea
  371. {
  372. public:
  373. std::string text;
  374. LRClickableAreaWText();
  375. LRClickableAreaWText(const Rect &Pos, const std::string &HoverText = "", const std::string &ClickText = "");
  376. virtual ~LRClickableAreaWText();
  377. void init();
  378. virtual void clickLeft(tribool down, bool previousState);
  379. virtual void clickRight(tribool down, bool previousState);
  380. };