Skip to content

Commit

Permalink
Move quick menu click listener registration to class for...
Browse files Browse the repository at this point in the history
...global use

#417
  • Loading branch information
AntumDeluge committed Mar 2, 2024
1 parent ccb16cb commit 24c7aef
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
31 changes: 4 additions & 27 deletions srcjs/stendhal/ui/quickmenu/QuickMenuButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare var stendhal: any;
import { Component } from "../toolkit/Component";
import { ui } from "../UI";
import { Paths } from "../../data/Paths";
import { ElementClickHandler } from "../../util/ElementClickHandler";


/**
Expand Down Expand Up @@ -48,34 +49,10 @@ export abstract class QuickMenuButton extends Component {
(this.componentElement as HTMLImageElement).src = Paths.gui + "/quickmenu/" + id + ".png";
this.componentElement.style["cursor"] = "url(" + Paths.sprites + "/cursor/highlight.png) 1 3, auto";
this.componentElement.draggable = false;

// listen for click events
this.componentElement.addEventListener("mousedown", (evt: MouseEvent) => {
evt.preventDefault();
if (evt.button == 0) {
this.clickEngaged = true;
}
});
this.componentElement.addEventListener("touchstart", (evt: TouchEvent) => {
evt.preventDefault();
this.touchEngaged = evt.changedTouches.length;
});
this.componentElement.addEventListener("mouseup", (evt: MouseEvent) => {
evt.preventDefault();
if (this.clickEngaged && evt.button == 0) {
// FIXME: should veto if moved too much before release
this.onClick(evt);
}
this.clickEngaged = false;
});
this.componentElement.addEventListener("touchend", (evt: TouchEvent) => {
evt.preventDefault();
const target = stendhal.ui.html.extractTarget(evt, this.touchEngaged - 1);
if (this.touchEngaged == evt.changedTouches.length && target == this.componentElement) {
// FIXME: should veto if moved too much before release
this.onClick(evt);
}
this.touchEngaged = 0;
});
// FIXME: do we need to store this value for potential cleanup?
new ElementClickHandler(this.componentElement).onClick = this.onClick;
this.update();
}

Expand Down
61 changes: 61 additions & 0 deletions srcjs/stendhal/util/ElementClickHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/***************************************************************************
* Copyright © 2024 - Faiumoni e. V. *
***************************************************************************
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

declare var stendhal: any;


/**
* Adds event listeners to handle clicks & multiple touches.
*
* TODO: add support long & double click/touch
*/
export class ElementClickHandler {

/** Function executed when click is detected. */
public onClick?: Function;
/** Property denoting button was pressed with mouse click. */
private clickEngaged = false;
/** Property denoting button was pressed with tap/touch. */
private touchEngaged = 0;


constructor(element: HTMLElement) {
// add supported listeners
element.addEventListener("mousedown", (evt: MouseEvent) => {
evt.preventDefault();
if (evt.button == 0) {
this.clickEngaged = true;
}
});
element.addEventListener("touchstart", (evt: TouchEvent) => {
evt.preventDefault();
this.touchEngaged = evt.changedTouches.length;
});
element.addEventListener("mouseup", (evt: MouseEvent) => {
evt.preventDefault();
if (this.clickEngaged && evt.button == 0 && this.onClick) {
// FIXME: should veto if moved too much before release
this.onClick(evt);
}
this.clickEngaged = false;
});
element.addEventListener("touchend", (evt: TouchEvent) => {
evt.preventDefault();
const target = stendhal.ui.html.extractTarget(evt, this.touchEngaged - 1);
if (this.touchEngaged == evt.changedTouches.length && target == element && this.onClick) {
// FIXME: should veto if moved too much before release
this.onClick(evt);
}
this.touchEngaged = 0;
});
}
}

0 comments on commit 24c7aef

Please sign in to comment.