AdventureMapButton.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 showAll(SDL_Surface *to);
  78. };
  79. /// A button which can be selected/deselected
  80. class CHighlightableButton
  81. : public AdventureMapButton
  82. {
  83. public:
  84. 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);
  85. 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
  86. 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
  87. bool onlyOn;//button can not be de-selected
  88. bool selected;//state of highlightable button
  89. int ID; //for identification
  90. CFunctionList<void()> callback2; //when de-selecting
  91. void select(bool on);
  92. void clickLeft(tribool down, bool previousState);
  93. };
  94. /// A group of buttons where one button can be selected
  95. class CHighlightableButtonsGroup : public CIntObject
  96. {
  97. public:
  98. CFunctionList2<void(int)> onChange; //called when changing selected button with new button's id
  99. std::vector<CHighlightableButton*> buttons;
  100. bool musicLike; //determines the behaviour of this group
  101. //void addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid);
  102. void addButton(CHighlightableButton* bt);//add existing button, it'll be deleted by CHighlightableButtonsGroup destructor
  103. 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
  104. CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons = false);
  105. ~CHighlightableButtonsGroup();
  106. void select(int id, bool mode); //mode==0: id is serial; mode==1: id is unique button id
  107. void selectionChanged(int to);
  108. void show(SDL_Surface * to);
  109. void showAll(SDL_Surface * to);
  110. void block(ui8 on);
  111. };
  112. /// A typical slider which can be orientated horizontally/vertically.
  113. class CSlider : public CIntObject
  114. {
  115. public:
  116. AdventureMapButton *left, *right, *slider; //if vertical then left=up
  117. int capacity,//how many elements can be active at same time
  118. amount, //how many elements
  119. positions, //number of highest position (0 if there is only one)
  120. value; //first active element
  121. bool horizontal;
  122. bool wheelScrolling;
  123. bool keyScrolling;
  124. boost::function<void(int)> moved;
  125. void redrawSlider();
  126. void sliderClicked();
  127. void moveLeft();
  128. void moveRight();
  129. void moveTo(int to);
  130. void block(bool on);
  131. void setAmount(int to);
  132. void keyPressed(const SDL_KeyboardEvent & key);
  133. void wheelScrolled(bool down, bool in);
  134. void clickLeft(tribool down, bool previousState);
  135. void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  136. void showAll(SDL_Surface * to);
  137. CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount,
  138. int Value=0, bool Horizontal=true, int style = 0); //style 0 - brown, 1 - blue
  139. ~CSlider();
  140. void moveToMax();
  141. };
  142. #endif // __ADVENTUREMAPBUTTON_H__