2
0

CIntObject.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #pragma once
  2. #include <SDL_events.h>
  3. #include "IShowActivatable.h"
  4. #include "SRect.h"
  5. #include "../FontBase.h"
  6. struct SDL_Surface;
  7. /*
  8. * CIntObject.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. using boost::logic::tribool;
  17. // Base UI element
  18. class CIntObject : public IShowActivatable //interface object
  19. {
  20. public:
  21. CIntObject *parent; //parent object
  22. std::vector<CIntObject *> children;
  23. SRect pos, //position of object on the screen
  24. posRelative; //position of object in the parent (not used if no parent)
  25. CIntObject();
  26. virtual ~CIntObject(); //d-tor
  27. //l-clicks handling
  28. bool pressedL; //for determining if object is L-pressed
  29. void activateLClick();
  30. void deactivateLClick();
  31. virtual void clickLeft(tribool down, bool previousState);
  32. //r-clicks handling
  33. bool pressedR; //for determining if object is R-pressed
  34. void activateRClick();
  35. void deactivateRClick();
  36. virtual void clickRight(tribool down, bool previousState);
  37. //hover handling
  38. bool hovered; //for determining if object is hovered
  39. void activateHover();
  40. void deactivateHover();
  41. virtual void hover (bool on);
  42. //keyboard handling
  43. bool captureAllKeys; //if true, only this object should get info about pressed keys
  44. void activateKeys();
  45. void deactivateKeys();
  46. virtual void keyPressed(const SDL_KeyboardEvent & key);
  47. //mouse movement handling
  48. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  49. void activateMouseMove();
  50. void deactivateMouseMove();
  51. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  52. //time handling
  53. int toNextTick;
  54. void activateTimer();
  55. void deactivateTimer();
  56. virtual void tick();
  57. //mouse wheel
  58. void activateWheel();
  59. void deactivateWheel();
  60. virtual void wheelScrolled(bool down, bool in);
  61. //double click
  62. void activateDClick();
  63. void deactivateDClick();
  64. virtual void onDoubleClick();
  65. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, ALL=0xffff};
  66. ui16 active;
  67. ui16 used;
  68. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  69. ui8 defActions; //which calls will be tried to be redirected to children
  70. ui8 recActions; //which calls we allow te receive from parent
  71. enum EAlignment {TOPLEFT, CENTER, BOTTOMRIGHT};
  72. void disable(); //deactivates if needed, blocks all automatic activity, allows only disposal
  73. void enable(bool activation = true); //activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  74. void defActivate();
  75. void defDeactivate();
  76. void activate();
  77. void deactivate();
  78. void activate(ui16 what);
  79. void deactivate(ui16 what);
  80. void show(SDL_Surface * to);
  81. void showAll(SDL_Surface * to);
  82. void redraw();
  83. void drawBorderLoc(SDL_Surface * sur, const SRect &r, const int3 &color);
  84. void printAtLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst);
  85. void printToLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst);
  86. void printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst);
  87. void printAtMiddleLoc(const std::string & text, const SPoint &p, EFonts font, SDL_Color kolor, SDL_Surface * dst);
  88. void printAtMiddleWBLoc(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst);
  89. void blitAtLoc(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  90. void blitAtLoc(SDL_Surface * src, const SPoint &p, SDL_Surface * dst);
  91. bool isItInLoc(const SDL_Rect &rect, int x, int y);
  92. bool isItInLoc(const SDL_Rect &rect, const SPoint &p);
  93. const SRect & center(const SRect &r, bool propagate = true); //sets pos so that r will be in the center of screen, assigns sizes of r to pos, returns new position
  94. const SRect & center(const SPoint &p, bool propagate = true); //moves object so that point p will be in its center
  95. const SRect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  96. void fitToScreen(int borderWidth, bool propagate = true); //moves window to fit into screen
  97. void moveBy(const SPoint &p, bool propagate = true);
  98. void moveTo(const SPoint &p, bool propagate = true);
  99. void changeUsedEvents(ui16 what, bool enable, bool adjust = true);
  100. void addChild(CIntObject *child, bool adjustPosition = false);
  101. void removeChild(CIntObject *child, bool adjustPosition = false);
  102. void delChild(CIntObject *child); //removes from children list, deletes
  103. template <typename T> void delChildNUll(T *&child, bool deactivateIfNeeded = false) //removes from children list, deletes and sets pointer to NULL
  104. {
  105. if(!child)
  106. return;
  107. if(deactivateIfNeeded && child->active)
  108. child->deactivate();
  109. delChild(child);
  110. child = NULL;
  111. }
  112. };