AdventureMapButton.h 6.6 KB

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