-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move quick menu click listener registration to class for...
...global use #417
- Loading branch information
1 parent
ccb16cb
commit 24c7aef
Showing
2 changed files
with
65 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} |