Skip to content

Instantly share code, notes, and snippets.

@Eggbertx
Last active December 24, 2018 20:37
Show Gist options
  • Select an option

  • Save Eggbertx/b2ac8a86c58b46fc2a3ccf7b003afa07 to your computer and use it in GitHub Desktop.

Select an option

Save Eggbertx/b2ac8a86c58b46fc2a3ccf7b003afa07 to your computer and use it in GitHub Desktop.
A simple but handy button press/release event handler
/*
* A simple but handy button press/release event handler
*/
import { Thread } from "sphere-runtime";
var events = [];
export class ButtonEvent extends Thread {
constructor(device, button, onPress, onRelease) {
super();
this.device = device;
this.button = button;
this.onPress = onPress;
this.onRelease = onRelease;
if(this.onRelease === undefined) this.onRelease = function() {} // optional
var isDown = false; // private
this.on_update = function() {
if(device.isPressed(button) && !isDown) {
// button was up, is now pressed
isDown = true;
this.onPress();
} else if(!device.isPressed(button) && isDown) {
// button was pressed, is now released
isDown = false;
this.onRelease();
}
}
this.start();
}
}
/*
* Example:
* registerEvent(Mouse.Default, MouseKey.Left,
* function() {
* // onPress
* const x = Mouse.Default.x;
* const y = Mouse.Default.y;
* for(button of guiButtons) {
* if(button.contains(x,y)) button.pushAnim();
* }
* },
* function() {
* // onRelease
* const x = Mouse.Default.x;
* const y = Mouse.Default.y;
* for(button of guiButtons) {
* if(button.contains(x,y)) {
* button.releaseAnim();
* button.activate();
* }
* }
* }
* );
*/
export function registerEvent(device, button, onPress, onRelease) {
if(getEvent(device, button) !== undefined)
throw Error("Event already registered, call deleteEvent first");
events.push(new ButtonEvent(device, button, onPress, onRelease));
}
/*
* Example:
* var button_event_obj = getEvent(Mouse.Default, MouseKey.Left);
*/
export function getEvent(device, button) {
for(const event of events) {
if(event.device == device && event.button == button) return event;
}
}
/*
* Example:
* deleteEvent(Mouse.Default, MouseKey.Left);
*/
export function deleteEvent(device, button) {
for(const e in events) {
if(events[e] !== undefined && events[e].device == device && events[e].button == button) {
events[e].stop();
events.splice(e, 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment