AdventureMapButton.h 6.2 KB

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