2024-12-14 07:59:43 +00:00
|
|
|
structure InputState =
|
|
|
|
|
struct
|
2025-02-16 13:45:14 +00:00
|
|
|
val keyMappings = ref
|
|
|
|
|
{ left = CoreKey.KEY_S
|
|
|
|
|
, right = CoreKey.KEY_F
|
|
|
|
|
, down = CoreKey.KEY_D
|
|
|
|
|
, up = CoreKey.KEY_E
|
|
|
|
|
, attack = CoreKey.KEY_J
|
|
|
|
|
, jump = CoreKey.KEY_K
|
2025-02-20 13:32:37 +00:00
|
|
|
, escape = CoreKey.KEY_ESCAPE
|
2025-02-16 13:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-17 03:53:04 +00:00
|
|
|
fun setControls controls = keyMappings := controls
|
|
|
|
|
|
2024-12-14 07:59:43 +00:00
|
|
|
(* global state detecting button inputs *)
|
|
|
|
|
val state =
|
|
|
|
|
{ leftHeld = ref false
|
|
|
|
|
, rightHeld = ref false
|
|
|
|
|
, upHeld = ref false
|
|
|
|
|
, downHeld = ref false
|
2025-02-16 13:51:52 +00:00
|
|
|
, jumpHeld = ref false
|
2024-12-22 05:40:33 +00:00
|
|
|
, attackHeld = ref false
|
2025-02-20 13:32:37 +00:00
|
|
|
, escapeHeld = ref false
|
2025-02-21 11:57:29 +00:00
|
|
|
, newKeys = ref []
|
2024-12-16 00:58:59 +00:00
|
|
|
, width = ref (1920.0 : Real32.real)
|
|
|
|
|
, height = ref (1080.0 : Real32.real)
|
2024-12-14 07:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun getSnapshot () =
|
2025-02-21 11:57:29 +00:00
|
|
|
let
|
|
|
|
|
val input =
|
|
|
|
|
{ leftHeld = !(#leftHeld state)
|
|
|
|
|
, rightHeld = !(#rightHeld state)
|
|
|
|
|
, upHeld = !(#upHeld state)
|
|
|
|
|
, downHeld = !(#downHeld state)
|
|
|
|
|
, attackHeld = !(#attackHeld state)
|
|
|
|
|
, jumpHeld = !(#jumpHeld state)
|
|
|
|
|
, escapeHeld = !(#escapeHeld state)
|
|
|
|
|
, newKeys = !(#newKeys state)
|
|
|
|
|
}
|
|
|
|
|
val () = #newKeys state := []
|
|
|
|
|
in
|
|
|
|
|
input
|
|
|
|
|
end
|
2024-12-14 07:59:43 +00:00
|
|
|
|
2025-02-17 01:39:41 +00:00
|
|
|
(* there are three action states reported by OS: PRESS, REPEAT and RELEASE.
|
|
|
|
|
* If input is PRESS or REPEAT, then return true, or else return false. *)
|
|
|
|
|
fun actionToBool action = action <> Input.RELEASE
|
2025-02-16 13:45:14 +00:00
|
|
|
|
|
|
|
|
fun handleKey (key, action) =
|
|
|
|
|
case GlfwKeyMap.codeFromKey key of
|
|
|
|
|
SOME code =>
|
|
|
|
|
let
|
2025-02-21 14:46:54 +00:00
|
|
|
val () =
|
|
|
|
|
if action = Input.PRESS then
|
|
|
|
|
#newKeys state := code :: !(#newKeys state)
|
|
|
|
|
else
|
|
|
|
|
()
|
2025-02-20 13:32:37 +00:00
|
|
|
val {left, right, down, up, attack, jump, escape} = !keyMappings
|
2025-02-16 13:45:14 +00:00
|
|
|
val action = actionToBool action
|
|
|
|
|
in
|
|
|
|
|
if code = left then #leftHeld state := action
|
|
|
|
|
else if code = up then #upHeld state := action
|
|
|
|
|
else if code = right then #rightHeld state := action
|
|
|
|
|
else if code = down then #downHeld state := action
|
|
|
|
|
else if code = attack then #attackHeld state := action
|
2025-02-16 13:51:52 +00:00
|
|
|
else if code = jump then #jumpHeld state := action
|
2025-02-20 13:32:37 +00:00
|
|
|
else if code = escape then #escapeHeld state := action
|
2025-02-16 13:45:14 +00:00
|
|
|
else ()
|
|
|
|
|
end
|
|
|
|
|
| NONE => ()
|
|
|
|
|
|
|
|
|
|
fun keyCallback (key, scancode, action, mods) =
|
2025-02-21 11:57:29 +00:00
|
|
|
if mods = 0 then handleKey (key, action) else ()
|
2025-02-16 13:45:14 +00:00
|
|
|
|
2024-12-16 00:58:59 +00:00
|
|
|
fun getWidth () =
|
|
|
|
|
!(#width state)
|
2024-12-14 07:59:43 +00:00
|
|
|
|
2024-12-16 00:58:59 +00:00
|
|
|
fun getHeight () =
|
|
|
|
|
!(#height state)
|
|
|
|
|
|
|
|
|
|
fun sizeCallback (width, height) =
|
|
|
|
|
(#width state := width; #height state := height)
|
2024-12-14 07:59:43 +00:00
|
|
|
|
|
|
|
|
fun registerCallbacks window =
|
|
|
|
|
let
|
|
|
|
|
val () = Input.exportKeyCallback keyCallback
|
|
|
|
|
val () = Input.setKeyCallback window
|
2024-12-16 00:58:59 +00:00
|
|
|
|
|
|
|
|
val () = Input.exportFramebufferSizeCallback sizeCallback
|
|
|
|
|
val () = Input.setFramebufferSizeCallback window
|
2024-12-14 07:59:43 +00:00
|
|
|
in
|
|
|
|
|
()
|
|
|
|
|
end
|
|
|
|
|
end
|