partially done coding key remapping functionality (what works: GLFW key code -> CoreKey.key_code, acting on CoreKey.key_code every frame; todo: load from config file on start up, have screen where we can remap keys, and save to config while when key mappings are changed)

This commit is contained in:
2025-02-16 13:45:14 +00:00
parent f2f1eeab8e
commit 0df453f9c9
3 changed files with 41 additions and 80 deletions

View File

@@ -1,7 +1,5 @@
structure GlfwKeyMap =
struct
open CoreKey
structure KeyMap =
MakeGapMap
(struct
@@ -15,14 +13,9 @@ struct
val maxNodeSize = 1024
end)
val map = ref KeyMap.empty
fun codeFromKey (key: int) =
KeyMap.get (key, !map)
fun helpInitKeyMap (pos, pairs, acc) =
if pos = Vector.length pairs then
map := acc
acc
else
let
val (code, key) = Vector.sub (pairs, pos)
@@ -33,6 +26,8 @@ struct
fun initKeyMap () =
let
open CoreKey
(* vector of (glfw_key_code, Core.key_code) tuples. *)
val pairs =
#[ (32, KEY_SPACE)
@@ -157,6 +152,10 @@ struct
, (348, KEY_MENU)
]
in
KeyMap.empty
helpInitKeyMap (0, pairs, KeyMap.empty)
end
val map = initKeyMap ()
fun codeFromKey (key: int) = KeyMap.get (key, map)
end

View File

@@ -1,5 +1,14 @@
structure InputState =
struct
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
}
(* global state detecting button inputs *)
val state =
{ leftHeld = ref false
@@ -19,6 +28,30 @@ struct
, attackHeld = !(#attackHeld state)
}
fun actionToBool action = action = Input.PRESS
fun handleKey (key, action) =
case GlfwKeyMap.codeFromKey key of
SOME code =>
let
val {left, right, down, up, attack, jump} = !keyMappings
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
else if code = jump then #upHeld state := action
else ()
end
| NONE => ()
fun keyCallback (key, scancode, action, mods) =
let open Input
in if mods = 0 then handleKey (key, action) else ()
end
fun getWidth () =
!(#width state)
@@ -28,37 +61,6 @@ struct
fun sizeCallback (width, height) =
(#width state := width; #height state := height)
open Input
fun handleKey (key, action) =
if key = KEY_K then
if action = PRESS then (#upHeld state) := true
else if action = RELEASE then (#upHeld state) := false
else ()
else if key = KEY_D then
if action = PRESS then (#downHeld state) := true
else if action = RELEASE then (#downHeld state) := false
else ()
else if key = KEY_S then
if action = PRESS then (#leftHeld state) := true
else if action = RELEASE then (#leftHeld state) := false
else ()
else if key = KEY_F then
if action = PRESS then (#rightHeld state) := true
else if action = RELEASE then (#rightHeld state) := false
else ()
else if key = KEY_J then
if action = PRESS then (#attackHeld state) := true
else if action = RELEASE then (#attackHeld state) := false
else ()
else
()
fun keyCallback (key, scancode, action, mods) =
let open Input
in if mods = 0 then handleKey (key, action) else ()
end
fun registerCallbacks window =
let
val () = Input.exportKeyCallback keyCallback