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:
@@ -11,46 +11,6 @@ struct
|
||||
_symbol "RELEASE" public : ( unit -> int ) * ( int -> unit );
|
||||
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 =
|
||||
_export "mltonKeyCallback" public : (int * int * int * int -> unit) -> unit;
|
||||
val setKeyCallback =
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user