PolyScreenEntity.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "PolyObject.h"
  27. #include "PolyEventDispatcher.h"
  28. namespace Polycode {
  29. class _PolyExport MouseEventResult {
  30. public:
  31. bool hit;
  32. bool blocked;
  33. };
  34. /**
  35. * 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.
  36. */
  37. class _PolyExport ScreenEntity : public Entity, public EventDispatcher {
  38. public:
  39. using Entity::setPosition;
  40. using Entity::setScale;
  41. ScreenEntity();
  42. virtual ~ScreenEntity();
  43. void addEntity(Entity *newChild);
  44. /**
  45. * Set 2d position.
  46. * @param x Horizontal position.
  47. * @param y Vertical position.
  48. */
  49. void setPosition(Number x, Number y);
  50. /**
  51. * Set 2d position.
  52. * @param v New 2D position vector.
  53. */
  54. void setPosition(const Vector2 &v);
  55. /**
  56. * Set 2d scale.
  57. * @param x Horizontal scale.
  58. * @param y Vertical scale.
  59. */
  60. void setScale(Number x, Number y);
  61. /**
  62. * Set 2d scale.
  63. * @param v New 2D scale vector.
  64. */
  65. void setScale(const Vector2 &v);
  66. /**
  67. * Set 2d rotation.
  68. * @param rotation New rotation value in degrees.
  69. */
  70. void setRotation(Number rotation);
  71. /**
  72. * Returns current rotation.
  73. * @return Current rotation value.
  74. */
  75. Number getRotation() const;
  76. MouseEventResult _onMouseDown(Number x, Number y, int mouseButton, int timestamp, Vector2 parentAdjust = Vector2(0,0));
  77. MouseEventResult _onMouseUp(Number x, Number y, int mouseButton, int timestamp, Vector2 parentAdjust = Vector2(0,0));
  78. MouseEventResult _onMouseMove(Number x, Number y, int timestamp, Vector2 parentAdjust = Vector2(0,0));
  79. MouseEventResult _onMouseWheelUp(Number x, Number y, int timestamp, Vector2 parentAdjust = Vector2(0,0));
  80. MouseEventResult _onMouseWheelDown(Number x, Number y, int timestamp, Vector2 parentAdjust = Vector2(0,0));
  81. virtual void onMouseDown(Number x, Number y){}
  82. virtual void onMouseUp(Number x, Number y){}
  83. virtual void onMouseMove(Number x, Number y){}
  84. virtual void onMouseWheelUp(Number x, Number y) {}
  85. virtual void onMouseWheelDown(Number x, Number y) {}
  86. void _onKeyDown(PolyKEY key, wchar_t charCode);
  87. void _onKeyUp(PolyKEY key, wchar_t charCode);
  88. Matrix4 getScreenConcatenatedMatrix();
  89. virtual void onKeyDown(PolyKEY key, wchar_t charCode){}
  90. virtual void onKeyUp(PolyKEY key, wchar_t charCode){}
  91. bool hitTest(Number x, Number y);
  92. Matrix4 buildPositionMatrix();
  93. void adjustMatrixForChildren();
  94. /**
  95. * Returns the width of the screen entity.
  96. * @return Height of the screen entity.
  97. */
  98. Number getWidth() const;
  99. /**
  100. * Returns the height of the screen entity.
  101. */
  102. Number getHeight() const;
  103. /**
  104. * Sets the width of the screen entity.
  105. * @param w New height value.
  106. */
  107. void setWidth(Number w) { width = w; hit.w = w; hit.x = -w/2; }
  108. /**
  109. * Sets the height of the screen entity.
  110. * @param h New height value.
  111. */
  112. void setHeight(Number h) { height = h; hit.h = h; hit.y = -h/2; }
  113. virtual void onGainFocus(){}
  114. virtual void onLoseFocus(){}
  115. void startDrag(Number xOffset, Number yOffset);
  116. void stopDrag();
  117. void setBlendingMode(int newBlendingMode);
  118. /**
  119. * Changes the positioning mode of the screen entity.
  120. 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.
  121. If the mode is ScreenEntity::POSITION_CENTER, the entity is rendered as is.
  122. Set to POSITION_CENTER by default.
  123. @param newPositionMode The new positioning mode.
  124. */
  125. void setPositionMode(int newPositionMode);
  126. int getPositionMode();
  127. void setDragLimits(Rectangle rect);
  128. void clearDragLimits();
  129. void setDefaultScreenOptions(bool snapToPixels);
  130. void focusChild(ScreenEntity *child);
  131. void focusNextChild();
  132. void moveChildUp(ScreenEntity *child);
  133. void moveChildDown(ScreenEntity *child);
  134. void moveChildTop(ScreenEntity *child);
  135. void moveChildBottom(ScreenEntity *child);
  136. Vector2 getPosition2D() const;
  137. Vector2 getScreenPosition() const;
  138. Vector2 getScale2D() const;
  139. static const int POSITION_TOPLEFT = 0;
  140. static const int POSITION_CENTER = 1;
  141. bool isFocusable() const;
  142. bool hasFocus;
  143. /**
  144. * If set to true, will block mouse events for underlaying entities.
  145. * (NOTE: processInputEvents must be set to true)
  146. */
  147. bool blockMouseInput;
  148. /**
  149. * 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.
  150. */
  151. bool snapToPixels;
  152. bool processInputEvents;
  153. Rectangle getHitbox();
  154. void setHitbox(Number width, Number height);
  155. void setHitbox(Number width, Number height, Number left, Number top);
  156. Number width;
  157. Number height;
  158. protected:
  159. bool focusable;
  160. bool focusChildren;
  161. bool isDragged;
  162. Number dragOffsetX;
  163. Number dragOffsetY;
  164. bool mouseOver;
  165. Rectangle hit;
  166. Number xmouse;
  167. Number ymouse;
  168. int positionMode;
  169. Rectangle *dragLimits;
  170. int lastClickTicks;
  171. };
  172. }