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

@@ -11,46 +11,6 @@ struct
_symbol "RELEASE" public : ( unit -> int ) * ( int -> unit ); _symbol "RELEASE" public : ( unit -> int ) * ( int -> unit );
val RELEASE = RELEASE () val RELEASE = RELEASE ()
val (ARROW_UP, _) =
_symbol "ARROW_UP" public : ( unit -> int ) * ( int -> unit );
val ARROW_UP = ARROW_UP ()
val (ARROW_DOWN, _) =
_symbol "ARROW_DOWN" public : ( unit -> int ) * ( int -> unit );
val ARROW_DOWN = ARROW_DOWN ()
val (ARROW_LEFT, _) =
_symbol "ARROW_LEFT" public : ( unit -> int ) * ( int -> unit );
val ARROW_LEFT = ARROW_LEFT ()
val (ARROW_RIGHT, _) =
_symbol "ARROW_RIGHT" public : ( unit -> int ) * ( int -> unit );
val ARROW_RIGHT = ARROW_RIGHT ()
val (KEY_S, _) =
_symbol "KEY_S" public : ( unit -> int ) * ( int -> unit );
val KEY_S = KEY_S ()
val (KEY_D, _) =
_symbol "KEY_D" public : ( unit -> int ) * ( int -> unit );
val KEY_D = KEY_D ()
val (KEY_F, _) =
_symbol "KEY_F" public : ( unit -> int ) * ( int -> unit );
val KEY_F = KEY_F ()
val (KEY_J, _) =
_symbol "KEY_J" public : ( unit -> int ) * ( int -> unit );
val KEY_J = KEY_J ()
val (KEY_K, _) =
_symbol "KEY_K" public : ( unit -> int ) * ( int -> unit );
val KEY_K = KEY_K ()
val (KEY_L, _) =
_symbol "KEY_L" public : ( unit -> int ) * ( int -> unit );
val KEY_L = KEY_L ()
val exportKeyCallback = val exportKeyCallback =
_export "mltonKeyCallback" public : (int * int * int * int -> unit) -> unit; _export "mltonKeyCallback" public : (int * int * int * int -> unit) -> unit;
val setKeyCallback = val setKeyCallback =

View File

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

View File

@@ -1,5 +1,14 @@
structure InputState = structure InputState =
struct 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 *) (* global state detecting button inputs *)
val state = val state =
{ leftHeld = ref false { leftHeld = ref false
@@ -19,6 +28,30 @@ struct
, attackHeld = !(#attackHeld state) , 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 () = fun getWidth () =
!(#width state) !(#width state)
@@ -28,37 +61,6 @@ struct
fun sizeCallback (width, height) = fun sizeCallback (width, height) =
(#width state := width; #height state := 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 = fun registerCallbacks window =
let let
val () = Input.exportKeyCallback keyCallback val () = Input.exportKeyCallback keyCallback