Interactive.hx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. package h2d;
  2. /**
  3. A user input handler.
  4. Hitbox area can be a rectangle, an ellipse or an arbitrary shape (`h2d.col.Collider`).
  5. Note that Interactive does not reports its hitbox bounds in `Object.getBounds`
  6. unless `Interactive.backgroundColor` is set, in which case `width` and `height` are reported.
  7. By default, Interactive only reacts to primary (left) mouse button for actions, see `Interactive.enableRightButton` for details.
  8. **/
  9. @:allow(h2d.Scene)
  10. class Interactive extends Drawable implements hxd.SceneEvents.Interactive {
  11. /**
  12. Width of the Interactive. Ignored if `Interactive.shape` is set.
  13. **/
  14. public var width : Float;
  15. /**
  16. Height of the Interactive. Ignored if `Interactive.shape` is set.
  17. **/
  18. public var height : Float;
  19. /**
  20. Cursor used when Interactive is under mouse cursor.
  21. **/
  22. public var cursor(default,set) : Null<hxd.Cursor> = Button;
  23. /**
  24. Performs an elliptic hit-test instead of rectangular one based on `Interactive.width` and `height`. Ignored if `Interactive.shape` is set.
  25. **/
  26. public var isEllipse : Bool;
  27. /**
  28. Set the default `hxd.Event.cancel` mode.
  29. **/
  30. public var cancelEvents : Bool = false;
  31. /**
  32. Set the default `hxd.Event.propagate` mode.
  33. **/
  34. public var propagateEvents : Bool = false;
  35. /**
  36. If set, Interactive will draw a `Tile` with `[width, height]` dimensions of specified color (including alpha).
  37. **/
  38. public var backgroundColor : Null<Int>;
  39. /**
  40. When enabled, interacting with secondary mouse buttons (right button/wheel) will cause `onPush`, `onClick`, `onRelease` and `onReleaseOutside` callbacks.
  41. Otherwise those callbacks will only be triggered with primary mouse button (left button).
  42. Note that Interactive remembers only the last pressed button when pressing on it, hence pressing Interactive with the left button and then the right button
  43. would not cause `onClick` on either when releasing left button first, as pressed state is reset internally.
  44. **/
  45. public var enableRightButton : Bool = false;
  46. var scene : Scene;
  47. var mouseDownButton : Int = -1;
  48. var parentMask : Mask;
  49. var invDet : Float;
  50. /**
  51. Detailed shape collider for Interactive.
  52. If set, `width` and `height` properties are ignored for collision checks.
  53. **/
  54. public var shape : h2d.col.Collider;
  55. /**
  56. Detailed shape X offset from Interactive.
  57. **/
  58. public var shapeX : Float = 0;
  59. /**
  60. Detailed shape Y offset from Interactive.
  61. **/
  62. public var shapeY : Float = 0;
  63. /**
  64. Create a new Interactive with specified parameters. `width`, `height` and `parent` with optional detailed `shape`.
  65. @param width The width of the Interactive hitbox.
  66. @param height The height of the Interactive hitbox.
  67. @param parent An optional parent `h2d.Object` instance to which Interactive adds itself if set.
  68. @param shape An optional detailed Interactive hitbox.
  69. **/
  70. public function new( width, height, ?parent, ?shape ) {
  71. super(parent);
  72. this.width = width;
  73. this.height = height;
  74. this.shape = shape;
  75. }
  76. override function onAdd() {
  77. this.scene = getScene();
  78. if( scene != null ) scene.addEventTarget(this);
  79. updateMask();
  80. super.onAdd();
  81. }
  82. override function draw( ctx : RenderContext ) {
  83. if( backgroundColor != null ) emitTile(ctx, h2d.Tile.fromColor(backgroundColor, Std.int(width), Std.int(height), (backgroundColor>>>24)/255 ));
  84. }
  85. override function getBoundsRec( relativeTo, out, forSize ) {
  86. super.getBoundsRec(relativeTo, out, forSize);
  87. if( backgroundColor != null || forSize ) addBounds(relativeTo, out, 0, 0, Std.int(width), Std.int(height));
  88. }
  89. override private function onHierarchyMoved(parentChanged:Bool)
  90. {
  91. super.onHierarchyMoved(parentChanged);
  92. if( scene != null ) {
  93. scene.removeEventTarget(this);
  94. scene = getScene();
  95. if( scene != null )
  96. scene.addEventTarget(this);
  97. }
  98. if ( parentChanged )
  99. updateMask();
  100. }
  101. function updateMask() {
  102. parentMask = null;
  103. var p = parent;
  104. while( p != null ) {
  105. var m = hxd.impl.Api.downcast(p, Mask);
  106. if( m != null ) {
  107. parentMask = m;
  108. break;
  109. }
  110. p = p.parent;
  111. }
  112. }
  113. override function onRemove() {
  114. if( scene != null ) {
  115. scene.removeEventTarget(this, true);
  116. scene = null;
  117. }
  118. super.onRemove();
  119. }
  120. function checkBounds( e : hxd.Event ) {
  121. return switch( e.kind ) {
  122. case EOut, EFocus, EFocusLost, EReleaseOutside: false;
  123. default: true;
  124. }
  125. }
  126. /**
  127. Reset current pressed state of the Interactive, preventing the `onClick` or `onReleaseOutside` being triggered when user releases mouse button.
  128. **/
  129. public function preventClick() {
  130. mouseDownButton = -1;
  131. }
  132. @:dox(hide)
  133. @:noCompletion public function getInteractiveScene() : hxd.SceneEvents.InteractiveScene {
  134. return scene;
  135. }
  136. @:dox(hide)
  137. @:noCompletion public function handleEvent( e : hxd.Event ) {
  138. if( parentMask != null && checkBounds(e) ) {
  139. var p = parentMask;
  140. var pt = new h2d.col.Point(e.relX, e.relY);
  141. localToGlobal(pt);
  142. var saveX = pt.x, saveY = pt.y;
  143. while( p != null ) {
  144. pt.x = saveX;
  145. pt.y = saveY;
  146. var pt = p.globalToLocal(pt);
  147. if( pt.x < 0 || pt.y < 0 || pt.x > p.width || pt.y > p.height ) {
  148. e.cancel = true;
  149. return;
  150. }
  151. p = @:privateAccess p.parentMask;
  152. }
  153. }
  154. if(shape == null && isEllipse && checkBounds(e) ) {
  155. var cx = width * 0.5, cy = height * 0.5;
  156. var dx = (e.relX - cx) / cx;
  157. var dy = (e.relY - cy) / cy;
  158. if( dx * dx + dy * dy > 1 ) {
  159. e.cancel = true;
  160. return;
  161. }
  162. }
  163. if( propagateEvents ) e.propagate = true;
  164. if( cancelEvents ) e.cancel = true;
  165. switch( e.kind ) {
  166. case EMove:
  167. onMove(e);
  168. case EPush:
  169. if( enableRightButton || e.button == 0 ) {
  170. mouseDownButton = e.button;
  171. onPush(e);
  172. }
  173. case ERelease:
  174. if( enableRightButton || e.button == 0 ) {
  175. onRelease(e);
  176. if( mouseDownButton == e.button )
  177. onClick(e);
  178. }
  179. mouseDownButton = -1;
  180. case EReleaseOutside:
  181. if( enableRightButton || e.button == 0 ) {
  182. onRelease(e);
  183. if ( mouseDownButton == e.button )
  184. onReleaseOutside(e);
  185. }
  186. mouseDownButton = -1;
  187. case EOver:
  188. onOver(e);
  189. case EOut:
  190. onOut(e);
  191. case EWheel:
  192. onWheel(e);
  193. case EFocusLost:
  194. onFocusLost(e);
  195. case EFocus:
  196. onFocus(e);
  197. case EKeyUp:
  198. onKeyUp(e);
  199. case EKeyDown:
  200. onKeyDown(e);
  201. case ECheck:
  202. onCheck(e);
  203. case ETextInput:
  204. onTextInput(e);
  205. }
  206. }
  207. override private function calcAbsPos()
  208. {
  209. super.calcAbsPos();
  210. invDet = 1 / (matA * matD - matB * matC);
  211. }
  212. function set_cursor(c) {
  213. this.cursor = c;
  214. if ( scene != null && scene.events != null )
  215. scene.events.updateCursor(this);
  216. return c;
  217. }
  218. function eventToLocal( e : hxd.Event ) {
  219. // convert scene event to our local space
  220. var i = this;
  221. var dx = e.relX - i.absX;
  222. var dy = e.relY - i.absY;
  223. e.relX = ( dx * i.matD - dy * i.matC) * i.invDet;
  224. e.relY = (-dx * i.matB + dy * i.matA) * i.invDet;
  225. }
  226. /**
  227. Starts input events capture and redirects them to `callb` method until `Interactive.stopDrag` is called.
  228. While the method name may imply that only mouse events would be captured: This is not the case,
  229. as it will also capture all other input events, including keyboard events.
  230. Starting event capture through `Interactive.startDrag` will convert `Event.relX` and `relY` to local coordinates
  231. of the Interactive and will restore them after invoking `callb`.
  232. In order to receive coordinates in scene coordinate space use `Scene.startDrag`.
  233. @param callb A callback method that receives `hxd.Event` when input event happens.
  234. Unless `callb` sets `Event.propagate` to `true`, event won't be sent to other Interactives.
  235. @param onCancel An optional callback that is invoked when `Interactive.stopDrag` is called.
  236. **/
  237. public function startDrag( callb : hxd.Event -> Void,?onCancel : Void -> Void ) {
  238. scene.startDrag(function(event) {
  239. var x = event.relX, y = event.relY;
  240. eventToLocal(event);
  241. callb(event);
  242. event.relX = x;
  243. event.relY = y;
  244. },onCancel);
  245. }
  246. /**
  247. Stops current input event capture.
  248. **/
  249. public function stopDrag() {
  250. scene.stopDrag();
  251. }
  252. /**
  253. Sets focus on this `Interactive`.
  254. If Interactive was not already focused and it receives focus - `onFocus` event is sent.
  255. Interactive won't become focused if during `onFocus` call it will set `Event.cancel` to `true`.
  256. **/
  257. public function focus() {
  258. if( scene == null || scene.events == null )
  259. return;
  260. scene.events.focus(this);
  261. }
  262. /**
  263. Removes focus from interactive if it's focused.
  264. If Interactive is currently focused - `onFocusLost` event will be sent.
  265. Interactive won't lose focus if during `onFocusLost` call it will set `Event.cancel` to `true`.
  266. **/
  267. public function blur() {
  268. if( hasFocus() ) scene.events.blur();
  269. }
  270. /**
  271. Checks if Interactive is currently hovered by the mouse.
  272. **/
  273. public function isOver() {
  274. return scene != null && scene.events != null && @:privateAccess scene.events.overList.indexOf(this) != -1;
  275. }
  276. /**
  277. Checks if Interactive is currently focused.
  278. **/
  279. public function hasFocus() {
  280. return scene != null && scene.events != null && @:privateAccess scene.events.currentFocus == this;
  281. }
  282. /**
  283. Sent when mouse enters Interactive hitbox area.
  284. `Event.propagate` and `Event.cancel` are ignored during `onOver`.
  285. Propagation can be set with `onMove` event, as well as cancelling `onMove` will prevent `onOver`.
  286. **/
  287. public dynamic function onOver( e : hxd.Event ) {
  288. }
  289. /**
  290. Sent when mouse exits Interactive hitbox area.
  291. `Event.propagate` and `Event.cancel` are ignored during `onOut`.
  292. **/
  293. public dynamic function onOut( e : hxd.Event ) {
  294. }
  295. /**
  296. Sent when Interactive is pressed by the user.
  297. **/
  298. public dynamic function onPush( e : hxd.Event ) {
  299. }
  300. /**
  301. Sent when Interactive is unpressed under multiple circumstances.
  302. * Always sent if user releases mouse while it is inside Interactive hitbox area.
  303. This happens regardless if that Interactive was pressed prior or not,
  304. and due to that it's not guaranteed that `Interactive.onPush` would precede this event.
  305. `Event.kind` is set to `ERelease` during this event.
  306. * Sent before `Interactive.onReleaseOutside` if this Interactive was pressed, but released outside its hitbox area.
  307. `Event.kind` is set to `EReleaseOutside` during this event.
  308. See `Interactive.onClick` and `Interactive.onReleaseOutside` methods for separate events that trigger only when user interacts with this particular Interactive.
  309. **/
  310. public dynamic function onRelease( e : hxd.Event ) {
  311. }
  312. /**
  313. Sent when user presses the Interactive, moves the mouse outside its hitbox area and releases the mouse button.
  314. Can be prevented to fire by calling `Interactive.preventClick` during or after `Interactive.onPush` event.
  315. `Interactive.onRelease` is sent with `Event.kind` being `EReleaseOutside` just before this event.
  316. **/
  317. public dynamic function onReleaseOutside( e : hxd.Event ) {
  318. }
  319. /**
  320. Sent when the Interactive is clicked by the user.
  321. Can be prevented to fire by calling `Interactive.preventClick` during or after `Interactive.onPush` event.
  322. `Interactive.onRelease` is sent with `Event.kind` being `ERelease` just before this event.
  323. **/
  324. public dynamic function onClick( e : hxd.Event ) {
  325. }
  326. /**
  327. Sent when user moves within the Interactive hitbox area.
  328. See `Interactive.onCheck` for event when user does not move the mouse.
  329. Cancelling the `Event` will prevent interactive from becoming overed,
  330. causing `Interactive.onOut` if it was overed previously.
  331. Interactive would be treated as not overed as long as event is cancelled even if mouse is within the hitbox area.
  332. **/
  333. public dynamic function onMove( e : hxd.Event ) {
  334. }
  335. /**
  336. Sent when user scrolls mouse wheel above the Interactive. Wheel delta can be obtained through the `Event.wheelDelta`.
  337. **/
  338. public dynamic function onWheel( e : hxd.Event ) {
  339. }
  340. /**
  341. Sent when Interactive receives focus during `Interactive.focus` call.
  342. Cancelling the `Event` will prevent the Interactive from becoming focused.
  343. **/
  344. public dynamic function onFocus( e : hxd.Event ) {
  345. }
  346. /**
  347. Sent when Interactive lost focus either via `Interactive.blur` call or when user clicks on another Interactive/outside this Interactive hitbox area.
  348. Cancelling the `Event` will prevent the Interactive from losing focus.
  349. **/
  350. public dynamic function onFocusLost( e : hxd.Event ) {
  351. }
  352. /**
  353. Sent when this Interactive is focused and user unpressed a keyboard key.
  354. Unpressed key can be accessed through `Event.keyCode`.
  355. **/
  356. public dynamic function onKeyUp( e : hxd.Event ) {
  357. }
  358. /**
  359. Sent when this Interactive is focused and user pressed a keyboard key.
  360. Pressed key can be accessed through `Event.keyCode`.
  361. **/
  362. public dynamic function onKeyDown( e : hxd.Event ) {
  363. }
  364. /**
  365. Sent every frame when user hovers an Interactive but does not move the mouse.
  366. See `Interactive.onMove` for event when user moves the mouse.
  367. Cancelling the `Event` will prevent interactive from becoming overed,
  368. causing `Interactive.onOut` if it was overed previously.
  369. Interactive would be treated as not overed as long as event is cancelled even if mouse is within the hitbox area.
  370. **/
  371. public dynamic function onCheck( e : hxd.Event ) {
  372. }
  373. /**
  374. Sent when this Interactive is focused and user inputs text. Character added can be accessed through `Event.charCode`.
  375. **/
  376. public dynamic function onTextInput( e : hxd.Event ) {
  377. }
  378. }