AdventureMapButton.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. void addTextOverlay(const std::string &Text, EFonts font, SDL_Color color = zwykly);
  37. void update();//to refresh button after image or text change
  38. void setOffset(int newOffset);
  39. void setState(ButtonState newState);
  40. ButtonState getState();
  41. //just to make code clearer
  42. void block(bool on);
  43. bool isBlocked();
  44. bool isHighlighted();
  45. CAnimImage * image; //image for this button
  46. CLabel * text;//text overlay
  47. CButtonBase(); //c-tor
  48. virtual ~CButtonBase(); //d-tor
  49. };
  50. /// Typical Heroes 3 button which can be inactive or active and can
  51. /// hold further information if you right-click it
  52. class AdventureMapButton : public CButtonBase
  53. {
  54. std::vector<std::string> imageNames;//store list of images that can be used by this button
  55. size_t currentImage;
  56. public:
  57. std::map<int, std::string> hoverTexts; //text for statusbar
  58. std::string helpBox; //for right-click help
  59. CFunctionList<void()> callback;
  60. bool actOnDown,//runs when mouse is pressed down over it, not when up
  61. hoverable;//if true, button will be highlighted when hovered
  62. void clickRight(tribool down, bool previousState);
  63. virtual void clickLeft(tribool down, bool previousState);
  64. void hover (bool on);
  65. AdventureMapButton(); //c-tor
  66. 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
  67. 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
  68. AdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, config::ButtonInfo *info, int key=0);//c-tor
  69. 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 );
  70. void setIndex(size_t index, bool playerColoredButton=false);
  71. void setImage(CAnimation* anim, bool playerColoredButton=false);
  72. void setPlayerColor(int player);
  73. };
  74. /// A button which can be selected/deselected
  75. class CHighlightableButton
  76. : public AdventureMapButton
  77. {
  78. public:
  79. 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);
  80. 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
  81. 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
  82. bool onlyOn;//button can not be de-selected
  83. int ID; //for identification
  84. CFunctionList<void()> callback2; //when de-selecting
  85. void select(bool on);
  86. void clickLeft(tribool down, bool previousState);
  87. };
  88. /// A group of buttons where one button can be selected
  89. class CHighlightableButtonsGroup : public CIntObject
  90. {
  91. public:
  92. CFunctionList2<void(int)> onChange; //called when changing selected button with new button's id
  93. std::vector<CHighlightableButton*> buttons;
  94. bool musicLike; //determines the behaviour of this group
  95. //void addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid);
  96. void addButton(CHighlightableButton* bt);//add existing button, it'll be deleted by CHighlightableButtonsGroup destructor
  97. 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
  98. CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons = false);
  99. ~CHighlightableButtonsGroup();
  100. void select(int id, bool mode); //mode==0: id is serial; mode==1: id is unique button id
  101. void selectionChanged(int to);
  102. void show(SDL_Surface * to);
  103. void showAll(SDL_Surface * to);
  104. void block(ui8 on);
  105. };
  106. /// A typical slider which can be orientated horizontally/vertically.
  107. class CSlider : public CIntObject
  108. {
  109. public:
  110. AdventureMapButton *left, *right, *slider; //if vertical then left=up
  111. int capacity,//how many elements can be active at same time
  112. amount, //how many elements
  113. positions, //number of highest position (0 if there is only one)
  114. value; //first active element
  115. bool horizontal;
  116. bool wheelScrolling;
  117. bool keyScrolling;
  118. boost::function<void(int)> moved;
  119. void redrawSlider();
  120. void sliderClicked();
  121. void moveLeft();
  122. void moveRight();
  123. void moveTo(int to);
  124. void block(bool on);
  125. void setAmount(int to);
  126. void keyPressed(const SDL_KeyboardEvent & key);
  127. void wheelScrolled(bool down, bool in);
  128. void clickLeft(tribool down, bool previousState);
  129. void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  130. void showAll(SDL_Surface * to);
  131. CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount,
  132. int Value=0, bool Horizontal=true, int style = 0); //style 0 - brown, 1 - blue
  133. ~CSlider();
  134. void moveToMax();
  135. };
  136. #endif // __ADVENTUREMAPBUTTON_H__