PolyScreenEntity.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyVector2.h"
  22. #include "PolyMatrix4.h"
  23. #include "PolyRectangle.h"
  24. #include "PolyInputKeys.h"
  25. #include "PolyEntity.h"
  26. #include "PolyEventDispatcher.h"
  27. namespace Polycode {
  28. /**
  29. * 2D Entity base. The ScreenEntity is the base class for all 2D elements in Polycode. They can be added to a screen or to other ScreenEntities and are rendered automatically. If you want to create custom screen objects, subclass this. ScreenEntity subclasses Entity, which use 3d positioning and tranformation, but provides some 2d-only versions of the transformation functions for convenience.
  30. */
  31. class _PolyExport ScreenEntity : public Entity, public EventDispatcher {
  32. public:
  33. using Entity::setPosition;
  34. using Entity::setScale;
  35. ScreenEntity();
  36. virtual ~ScreenEntity();
  37. /**
  38. * Set 2d position.
  39. * @param x Horizontal position.
  40. * @param y Vertical position.
  41. */
  42. void setPosition(Number x, Number y);
  43. /**
  44. * Set 2d position.
  45. * @param v New 2D position vector.
  46. */
  47. void setPosition(const Vector2 &v);
  48. /**
  49. * Set 2d scale.
  50. * @param x Horizontal scale.
  51. * @param y Vertical scale.
  52. */
  53. void setScale(Number x, Number y);
  54. /**
  55. * Set 2d scale.
  56. * @param v New 2D scale vector.
  57. */
  58. void setScale(const Vector2 &v);
  59. /**
  60. * Set 2d rotation.
  61. * @param rotation New rotation value in degrees.
  62. */
  63. void setRotation(Number rotation);
  64. /**
  65. * Returns current rotation.
  66. * @return Current rotation value.
  67. */
  68. Number getRotation() const;
  69. bool _onMouseDown(Number x, Number y, int mouseButton, int timestamp, Vector2 parentAdjust = Vector2(0,0));
  70. bool _onMouseUp(Number x, Number y, int mouseButton, int timestamp, Vector2 parentAdjust = Vector2(0,0));
  71. void _onMouseMove(Number x, Number y, int timestamp, Vector2 parentAdjust = Vector2(0,0));
  72. void _onMouseWheelUp(Number x, Number y, int timestamp, Vector2 parentAdjust = Vector2(0,0));
  73. void _onMouseWheelDown(Number x, Number y, int timestamp, Vector2 parentAdjust = Vector2(0,0));
  74. virtual void onMouseDown(Number x, Number y){}
  75. virtual void onMouseUp(Number x, Number y){}
  76. virtual void onMouseMove(Number x, Number y){}
  77. virtual void onMouseWheelUp(Number x, Number y) {}
  78. virtual void onMouseWheelDown(Number x, Number y) {}
  79. void _onKeyDown(PolyKEY key, wchar_t charCode);
  80. void _onKeyUp(PolyKEY key, wchar_t charCode);
  81. Matrix4 getScreenConcatenatedMatrix();
  82. virtual void onKeyDown(PolyKEY key, wchar_t charCode){}
  83. virtual void onKeyUp(PolyKEY key, wchar_t charCode){}
  84. bool hitTest(Number x, Number y) const;
  85. Matrix4 buildPositionMatrix();
  86. void adjustMatrixForChildren();
  87. /**
  88. * Returns the width of the screen entity.
  89. * @return Height of the screen entity.
  90. */
  91. Number getWidth() const;
  92. /**
  93. * Returns the height of the screen entity.
  94. */
  95. Number getHeight() const;
  96. /**
  97. * Sets the width of the screen entity.
  98. * @param w New height value.
  99. */
  100. void setWidth(Number w) { width = w; hitwidth = w; }
  101. /**
  102. * Sets the height of the screen entity.
  103. * @param h New height value.
  104. */
  105. void setHeight(Number h) { height = h; hitheight = h; }
  106. virtual void onGainFocus(){}
  107. virtual void onLoseFocus(){}
  108. void startDrag(Number xOffset, Number yOffset);
  109. void stopDrag();
  110. void setBlendingMode(int newBlendingMode);
  111. /**
  112. * Changes the positioning mode of the screen entity.
  113. If the positioning mode is ScreenEntity::POSITION_TOPLEFT, the entity is translated by half its width and half its height when it's rendered, making all other transformations relative to its top-left cornder.instead of the center.
  114. If the mode is ScreenEntity::POSITION_CENTER, the entity is rendered as is.
  115. Set to POSITION_CENTER by default.
  116. @param newPositionMode The new positioning mode.
  117. */
  118. void setPositionMode(int newPositionMode);
  119. int getPositionMode();
  120. void setDragLimits(Rectangle rect);
  121. void clearDragLimits();
  122. void focusChild(ScreenEntity *child);
  123. void focusNextChild();
  124. Vector2 getPosition2D() const;
  125. static const int POSITION_TOPLEFT = 0;
  126. static const int POSITION_CENTER = 1;
  127. bool isFocusable() const;
  128. bool hasFocus;
  129. bool blockMouseInput;
  130. int zindex;
  131. /**
  132. * If this option is true, the screen entity's positions will be roudnded to whole pixels. This only works if the screen is using pixel coordinates.
  133. */
  134. bool snapToPixels;
  135. bool processInputEvents;
  136. Vector2 getHitbox();
  137. protected:
  138. bool focusable;
  139. bool focusChildren;
  140. bool isDragged;
  141. Number dragOffsetX;
  142. Number dragOffsetY;
  143. bool mouseOver;
  144. Number width;
  145. Number height;
  146. Number hitwidth;
  147. Number hitheight;
  148. Number xmouse;
  149. Number ymouse;
  150. int positionMode;
  151. Rectangle *dragLimits;
  152. int lastClickTicks;
  153. ScreenEntity *focusedChild;
  154. };
  155. }