Skip to content

Instantly share code, notes, and snippets.

@immersivegamer
Created June 8, 2020 00:56
Show Gist options
  • Save immersivegamer/e5b0b7edc6a24685c1e13ff73d3103eb to your computer and use it in GitHub Desktop.
Save immersivegamer/e5b0b7edc6a24685c1e13ff73d3103eb to your computer and use it in GitHub Desktop.
DOME (Wren Code) For Input Key Was Pressed or Released
import "graphics" for Canvas, Color
import "math" for Vector
import "input" for Keyboard
// InputManager Class
// Provides a way to get if a key was pressed down or released on the current
// update loop. Will only fire once if key is held down. Also implements a simple
// event based way to handle input changes.
class InputManager {
construct new() {
// key state
_changelist = {}
// event callbacks (currently only one callback per key event)
_onDownEvents = {}
_onPressedEvents = {}
_onReleasedEvents = {}
}
// KEY PRESS STATE STUFF
// ---------------------
// check if key has changes, returns true if it has
hasKeyChanged(key) {
// TODO: instead of adding keys as they are requested to be checked
// one could instead preload _changelist with all key codes
// would prevent bug where possible first time check of key returns
// incorrect value
if(!_changelist.containsKey(key)) {
_changelist[key] = false
}
if(_changelist[key] != Keyboard.isKeyDown(key)) {
return true
}
return false
}
// key pressed down, was previously up
// will only be true on the game loop where the key changed
isKeyPressed(key) {
if(hasKeyChanged(key) && Keyboard.isKeyDown(key)) {
return true
}
return false
}
// key released, was previously pressed down
// will only be true on the game loop where the key changed
isKeyReleased(key) {
if(hasKeyChanged(key) && !Keyboard.isKeyDown(key)) {
return true
}
return false
}
// REQUIRED: call at the very end of Game.update()
// saves new state of keys to compare in the next update
finalCheck() {
for(code in _changelist.keys) {
_changelist[code] = Keyboard.isKeyDown(code)
}
}
// EVENT STUFF
// -----------
// use these to register callback functions
onDown(key, fn) {
_onDownEvents[key] = fn
}
onPressed(key, fn) {
_onPressedEvents[key] = fn
}
onReleased(key, fn) {
_onReleasedEvents[key] = fn
}
// REQUIRED (if using events)
// call in update to fire any event callback functions
// suggested at begining of update
eventCheck() {
for(key in _onDownEvents.keys) {
if(Keyboard.isKeyDown(key)) {
_onDownEvents[key].call()
}
}
for(key in _onPressedEvents.keys) {
if(isKeyPressed(key)) {
_onPressedEvents[key].call()
}
}
for(key in _onReleasedEvents.keys) {
if(isKeyReleased(key)) {
_onReleasedEvents[key].call()
}
}
}
}
// INPUT MANAGER EXAMPLE
class Game {
static init() {
// init logger values
__logpos = Vector.new(10, 10)
__logcolor = Color.white
// counters to display different key presses
__counterA = 0
__counterB = 0
__counterC = 0
__counterD = 0
__counterE = 0
__counterF = 0
// create log manager
__input = InputManager.new()
// can bind event callbacks any time
// suggested at soon as possible
__input.onDown("s") {
__counterD = __counterD + 1
}
__input.onPressed("s") {
__counterE = __counterE + 1
}
__input.onReleased("s") {
__counterF = __counterF + 1
}
}
static update() {
// check events and fire callbacks if registered
__input.eventCheck()
// direct check of key status
if(Keyboard.isKeyDown("Space")) {
__counterA = __counterA + 1
}
if(__input.isKeyPressed("Space")) {
__counterB = __counterB + 1
}
if(__input.isKeyReleased("Space")) {
__counterC = __counterC + 1
}
// record new key states for next update, should be last
__input.finalCheck()
}
static draw(dt) {
// display counters
Canvas.cls()
log("INPUTMANAGER EXAMPLE")
log("")
log("DIRECT (Press Space)")
log("Down: %(__counterA)")
log("Pressed: %(__counterB)")
log("Released: %(__counterC)")
log("")
log("EVENT (Press 'S')")
log("Event Down: %(__counterD)")
log("Event Pressed: %(__counterE)")
log("Event Released: %(__counterF)")
__logpos = Vector.new(10, 10)
}
static log(msg) {
Canvas.print(msg, __logpos.x, __logpos.y, __logcolor)
__logpos.y = __logpos.y + 10
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment