AdventureMapButton.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef __ADVENTUREMAPBUTTON_H__
  2. #define __ADVENTUREMAPBUTTON_H__
  3. #include "FunctionList.h"
  4. #include <boost/bind.hpp>
  5. #include "GUIBase.h"
  6. /*
  7. * AdventureMapButton.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. extern SDL_Color tytulowy, tlo, zwykly ;
  16. class CAnimation;
  17. class CAnimImage;
  18. class CLabel;
  19. namespace config{struct ButtonInfo;}
  20. /// Base class for buttons.
  21. class CButtonBase : public KeyShortcut
  22. {
  23. public:
  24. enum ButtonState
  25. {
  26. NORMAL=0,
  27. PRESSED=1,
  28. BLOCKED=2,
  29. HIGHLIGHTED=3
  30. };
  31. private:
  32. int bitmapOffset; // base offset of visible bitmap from animation
  33. ButtonState state;//current state of button from enum
  34. public:
  35. bool swappedImages,//fix for some buttons: normal and pressed image are swapped
  36. keepFrame; // don't change visual representation
  37. void addTextOverlay(const std::string &Text, EFonts font, SDL_Color color = zwykly);
  38. void update();//to refresh button after image or text change
  39. void setOffset(int newOffset);
  40. void setState(ButtonState newState);
  41. ButtonState getState();
  42. //just to make code clearer
  43. void block(bool on);
  44. bool isBlocked();
  45. bool isHighlighted();
  46. CAnimImage * image; //image for this button
  47. CLabel * text;//text overlay
  48. CButtonBase(); //c-tor
  49. virtual ~CButtonBase(); //d-tor
  50. };
  51. /// Typical Heroes 3 button which can be inactive or active and can
  52. /// hold further information if you right-click it
  53. class AdventureMapButton : public CButtonBase
  54. {
  55. std::vector<std::string> imageNames;//store list of images that can be used by this button
  56. size_t currentImage;
  57. public:
  58. std::map<int, std::string> hoverTexts; //text for statusbar
  59. std::string helpBox; //for right-click help
  60. CFunctionList<void()> callback;
  61. bool actOnDown,//runs when mouse is pressed down over it, not when up
  62. hoverable,//if true, button will be highlighted when hovered
  63. borderEnabled,
  64. soundDisabled;
  65. SDL_Color borderColor;
  66. void clickRight(tribool down, bool previousState);
  67. virtual void clickLeft(tribool down, bool previousState);
  68. void hover (bool on);
  69. AdventureMapButton(); //c-tor
  70. AdventureMapButton( 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
  71. AdventureMapButton( 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
  72. AdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, config::ButtonInfo *info, int key=0);//c-tor
  73. 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 );
  74. void setIndex(size_t index, bool playerColoredButton=false);
  75. void setImage(CAnimation* anim, bool playerColoredButton=false);
  76. void setPlayerColor(int player);
  77. void show(SDL_Surface *to);
  78. void showAll(SDL_Surface *to);
  79. };
  80. /// A button which can be selected/deselected
  81. class CHighlightableButton
  82. : public AdventureMapButton
  83. {
  84. public:
  85. 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);
  86. 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
  87. 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
  88. bool onlyOn;//button can not be de-selected
  89. bool selected;//state of highlightable button
  90. int ID; //for identification
  91. CFunctionList<void()> callback2; //when de-selecting
  92. void select(bool on);
  93. void clickLeft(tribool down, bool previousState);
  94. };
  95. /// A group of buttons where one button can be selected
  96. class CHighlightableButtonsGroup : public CIntObject
  97. {
  98. public:
  99. CFunctionList2<void(int)> onChange; //called when changing selected button with new button's id
  100. std::vector<CHighlightableButton*> buttons;
  101. bool musicLike; //determines the behaviour of this group
  102. //void addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid);
  103. void addButton(CHighlightableButton* bt);//add existing button, it'll be deleted by CHighlightableButtonsGroup destructor
  104. 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
  105. CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons = false);
  106. ~CHighlightableButtonsGroup();
  107. void select(int id, bool mode); //mode==0: id is serial; mode==1: id is unique button id
  108. void selectionChanged(int to);
  109. void show(SDL_Surface * to);
  110. void showAll(SDL_Surface * to);
  111. void block(ui8 on);
  112. };
  113. /// A typical slider which can be orientated horizontally/vertically.
  114. class CSlider : public CIntObject
  115. {
  116. public:
  117. AdventureMapButton *left, *right, *slider; //if vertical then left=up
  118. int capacity,//how many elements can be active at same time
  119. amount, //how many elements
  120. positions, //number of highest position (0 if there is only one)
  121. value; //first active element
  122. bool horizontal;
  123. bool wheelScrolling;
  124. bool keyScrolling;
  125. boost::function<void(int)> moved;
  126. void redrawSlider();
  127. void sliderClicked();
  128. void moveLeft();
  129. void moveRight();
  130. void moveTo(int to);
  131. void block(bool on);
  132. void setAmount(int to);
  133. void keyPressed(const SDL_KeyboardEvent & key);
  134. void wheelScrolled(bool down, bool in);
  135. void clickLeft(tribool down, bool previousState);
  136. void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  137. void showAll(SDL_Surface * to);
  138. CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount,
  139. int Value=0, bool Horizontal=true, int style = 0); //style 0 - brown, 1 - blue
  140. ~CSlider();
  141. void moveToMax();
  142. };
  143. #endif // __ADVENTUREMAPBUTTON_H__