a little refactoring

This commit is contained in:
2025-02-21 18:57:28 +00:00
parent e6b0a5f37f
commit 9c82a624b6
2 changed files with 48 additions and 43 deletions

View File

@@ -138,6 +138,10 @@ sig
} }
val keyFromString: string -> key_code option val keyFromString: string -> key_code option
val containsKey: key_code * key_code list -> bool
val containsAttack: user_key * key_code list -> bool
val containsEscape: user_key * key_code list -> bool
end end
structure CoreKey :> CORE_KEY = structure CoreKey :> CORE_KEY =
@@ -402,4 +406,15 @@ struct
| "KEY_RIGHT_SUPER" => SOME KEY_RIGHT_SUPER | "KEY_RIGHT_SUPER" => SOME KEY_RIGHT_SUPER
| "KEY_MENU" => SOME KEY_MENU | "KEY_MENU" => SOME KEY_MENU
| _ => NONE | _ => NONE
fun containsKey (searchKey, lst) =
case lst of
hd :: tl => hd = searchKey orelse containsKey (searchKey, tl)
| [] => false
fun containsAttack (userKeys: user_key, lst) =
containsKey (#attack userKeys, lst)
fun containsEscape (userKeys: user_key, lst) =
containsKey (#escape userKeys, lst)
end end

View File

@@ -1,6 +1,5 @@
signature MAKE_UPDATE_SELECTED_KEY = signature MAKE_UPDATE_SELECTED_KEY =
sig sig
val containsEscape: CoreKey.user_key * CoreKey.key_code list -> bool
val updateKeys: CoreKey.key_code * CoreKey.user_key -> CoreKey.user_key val updateKeys: CoreKey.key_code * CoreKey.user_key -> CoreKey.user_key
val default: OptionsType.options_type * CoreKey.user_key -> GameType.game_type val default: OptionsType.options_type * CoreKey.user_key -> GameType.game_type
val deselect: OptionsType.options_type * CoreKey.user_key val deselect: OptionsType.options_type * CoreKey.user_key
@@ -27,12 +26,12 @@ struct
case #newKeys input of case #newKeys input of
key :: tl => key :: tl =>
(* change key *) (* change key *)
if key = CoreKey.KEY_ESCAPE orelse Fn.containsEscape (userKeys, tl) then if key = CoreKey.KEY_ESCAPE orelse CoreKey.containsEscape (userKeys, tl) then
(* deslect as that is the function of the escape key *) (* deslect as that is the function of the escape key *)
Fn.deselect (options, userKeys) Fn.deselect (options, userKeys)
(* what if new key collides with existing key? todo *) (* what if new key collides with existing key? todo *)
else else
let val tempKeys = Fn.updateKeys (key, userKeys) let val tempKeys = Fn.updateKeys (key, #tempKeys options)
in setNewKeys (options, tempKeys, userKeys, time) in setNewKeys (options, tempKeys, userKeys, time)
end end
| [] => Fn.default (options, userKeys) | [] => Fn.default (options, userKeys)
@@ -44,7 +43,7 @@ struct
fun default (options: OptionsType.options_type, userKeys) = fun default (options: OptionsType.options_type, userKeys) =
let let
val {focus, isSelected, ...} = options val {focus, isSelected, tempKeys, ...} = options
(* `default` function is called when no keys are pressed (* `default` function is called when no keys are pressed
* so set up pressed/down pressed both to 0 * so set up pressed/down pressed both to 0
* as neither is being pressed. *) * as neither is being pressed. *)
@@ -53,7 +52,7 @@ struct
, lastUpPress = 0.0 , lastUpPress = 0.0
, lastDownPress = 0.0 , lastDownPress = 0.0
, isSelected = isSelected , isSelected = isSelected
, tempKeys = userKeys , tempKeys = tempKeys
} }
in in
{mode = GameType.OPTIONS options, userKeys = userKeys, saveKeys = false} {mode = GameType.OPTIONS options, userKeys = userKeys, saveKeys = false}
@@ -61,7 +60,7 @@ struct
fun moveFocusUp (options: OptionsType.options_type, newFocus, userKeys, time) = fun moveFocusUp (options: OptionsType.options_type, newFocus, userKeys, time) =
let let
val {focus, isSelected, lastUpPress, ...} = options val {focus, isSelected, lastUpPress, tempKeys, ...} = options
(* only switch to newFocus if it is time for key delay to be triggered. (* only switch to newFocus if it is time for key delay to be triggered.
* We set lastDownPress to 0 because up is currently being pressed instead * We set lastDownPress to 0 because up is currently being pressed instead
* so we don't want to a key delay for down. *) * so we don't want to a key delay for down. *)
@@ -71,14 +70,14 @@ struct
, lastUpPress = time , lastUpPress = time
, lastDownPress = 0.0 , lastDownPress = 0.0
, isSelected = isSelected , isSelected = isSelected
, tempKeys = userKeys , tempKeys = tempKeys
} }
else else
{ focus = focus { focus = focus
, lastUpPress = lastUpPress , lastUpPress = lastUpPress
, lastDownPress = 0.0 , lastDownPress = 0.0
, isSelected = isSelected , isSelected = isSelected
, tempKeys = userKeys , tempKeys = tempKeys
} }
in in
{mode = GameType.OPTIONS options, userKeys = userKeys, saveKeys = false} {mode = GameType.OPTIONS options, userKeys = userKeys, saveKeys = false}
@@ -87,21 +86,21 @@ struct
fun moveFocusDown fun moveFocusDown
(options: OptionsType.options_type, newFocus, userKeys, time) = (options: OptionsType.options_type, newFocus, userKeys, time) =
let let
val {focus, isSelected, lastDownPress, ...} = options val {focus, isSelected, lastDownPress, tempKeys, ...} = options
val options = val options =
if lastDownPress + Constants.keyDelay <= time then if lastDownPress + Constants.keyDelay <= time then
{ focus = newFocus { focus = newFocus
, lastUpPress = 0.0 , lastUpPress = 0.0
, lastDownPress = time , lastDownPress = time
, isSelected = isSelected , isSelected = isSelected
, tempKeys = userKeys , tempKeys = tempKeys
} }
else else
{ focus = focus { focus = focus
, lastUpPress = 0.0 , lastUpPress = 0.0
, lastDownPress = lastDownPress , lastDownPress = lastDownPress
, isSelected = isSelected , isSelected = isSelected
, tempKeys = userKeys , tempKeys = tempKeys
} }
in in
{mode = GameType.OPTIONS options, userKeys = userKeys, saveKeys = false} {mode = GameType.OPTIONS options, userKeys = userKeys, saveKeys = false}
@@ -109,13 +108,13 @@ struct
fun select (options: OptionsType.options_type, userKeys) = fun select (options: OptionsType.options_type, userKeys) =
let let
val {focus, lastUpPress, lastDownPress, ...} = options val {focus, lastUpPress, lastDownPress, tempKeys, ...} = options
val options = val options =
{ focus = focus { focus = focus
, lastUpPress = lastUpPress , lastUpPress = lastUpPress
, lastDownPress = lastDownPress , lastDownPress = lastDownPress
, isSelected = true , isSelected = true
, tempKeys = userKeys , tempKeys = tempKeys
} }
in in
{mode = GameType.OPTIONS options, userKeys = userKeys, saveKeys = false} {mode = GameType.OPTIONS options, userKeys = userKeys, saveKeys = false}
@@ -123,13 +122,13 @@ struct
fun deselect (options: OptionsType.options_type, userKeys) = fun deselect (options: OptionsType.options_type, userKeys) =
let let
val {focus, lastUpPress, lastDownPress, ...} = options val {focus, lastUpPress, lastDownPress, tempKeys, ...} = options
val options = val options =
{ focus = focus { focus = focus
, lastUpPress = lastUpPress , lastUpPress = lastUpPress
, lastDownPress = lastDownPress , lastDownPress = lastDownPress
, isSelected = false , isSelected = false
, tempKeys = userKeys , tempKeys = tempKeys
} }
in in
{mode = GameType.OPTIONS options, userKeys = userKeys, saveKeys = false} {mode = GameType.OPTIONS options, userKeys = userKeys, saveKeys = false}
@@ -219,23 +218,9 @@ struct
} }
end end
(* Sometimes we only want to act on a key's 'press' event,
* and the list only contains press events. *)
fun containsKey (searchKey, lst) =
case lst of
hd :: tl => hd = searchKey orelse containsKey (searchKey, tl)
| [] => false
fun containsAttack (userKeys: CoreKey.user_key, input: FrameInputType.t) =
containsKey (#attack userKeys, #newKeys input)
fun containsEscape (userKeys: CoreKey.user_key, tl) =
containsKey (#escape userKeys, tl)
structure UpdateLeftKey = structure UpdateLeftKey =
MakeUpdateSelectedKey MakeUpdateSelectedKey
(struct (struct
val containsEscape = containsEscape
val updateKeys = withLeftKeys val updateKeys = withLeftKeys
val default = default val default = default
val deselect = deselect val deselect = deselect
@@ -244,7 +229,6 @@ struct
structure UpdateRightKey = structure UpdateRightKey =
MakeUpdateSelectedKey MakeUpdateSelectedKey
(struct (struct
val containsEscape = containsEscape
val updateKeys = withRightKeys val updateKeys = withRightKeys
val default = default val default = default
val deselect = deselect val deselect = deselect
@@ -253,7 +237,6 @@ struct
structure UpdateUpKey = structure UpdateUpKey =
MakeUpdateSelectedKey MakeUpdateSelectedKey
(struct (struct
val containsEscape = containsEscape
val updateKeys = withUpKeys val updateKeys = withUpKeys
val default = default val default = default
val deselect = deselect val deselect = deselect
@@ -262,7 +245,6 @@ struct
structure UpdateDownKey = structure UpdateDownKey =
MakeUpdateSelectedKey MakeUpdateSelectedKey
(struct (struct
val containsEscape = containsEscape
val updateKeys = withDownKeys val updateKeys = withDownKeys
val default = default val default = default
val deselect = deselect val deselect = deselect
@@ -271,7 +253,6 @@ struct
structure UpdateJumpKey = structure UpdateJumpKey =
MakeUpdateSelectedKey MakeUpdateSelectedKey
(struct (struct
val containsEscape = containsEscape
val updateKeys = withJumpKeys val updateKeys = withJumpKeys
val default = default val default = default
val deselect = deselect val deselect = deselect
@@ -280,18 +261,27 @@ struct
structure UpdateAttackKey = structure UpdateAttackKey =
MakeUpdateSelectedKey MakeUpdateSelectedKey
(struct (struct
val containsEscape = containsEscape
val updateKeys = withAttackKeys val updateKeys = withAttackKeys
val default = default val default = default
val deselect = deselect val deselect = deselect
end) end)
fun saveAndGoToTitle options =
let
val userKeys = #tempKeys options
in
{ mode = GameType.TITLE TitleType.initial
, userKeys = userKeys
, saveKeys = true
}
end
fun update (options, input: FrameInputType.t, userKeys, time) = fun update (options, input: FrameInputType.t, userKeys, time) =
case #focus options of case #focus options of
LEFT_KEY => LEFT_KEY =>
if #isSelected options then if #isSelected options then
UpdateLeftKey.onSelected (options, input, userKeys, time) UpdateLeftKey.onSelected (options, input, userKeys, time)
else if containsAttack (userKeys, input) then else if CoreKey.containsAttack (userKeys, #newKeys input) then
select (options, userKeys) select (options, userKeys)
else if #downHeld input then else if #downHeld input then
moveFocusDown (options, RIGHT_KEY, userKeys, time) moveFocusDown (options, RIGHT_KEY, userKeys, time)
@@ -300,7 +290,7 @@ struct
| RIGHT_KEY => | RIGHT_KEY =>
if #isSelected options then if #isSelected options then
UpdateRightKey.onSelected (options, input, userKeys, time) UpdateRightKey.onSelected (options, input, userKeys, time)
else if containsAttack (userKeys, input) then else if CoreKey.containsAttack (userKeys, #newKeys input) then
select (options, userKeys) select (options, userKeys)
else if #upHeld input then else if #upHeld input then
moveFocusUp (options, LEFT_KEY, userKeys, time) moveFocusUp (options, LEFT_KEY, userKeys, time)
@@ -311,7 +301,7 @@ struct
| UP_KEY => | UP_KEY =>
if #isSelected options then if #isSelected options then
UpdateUpKey.onSelected (options, input, userKeys, time) UpdateUpKey.onSelected (options, input, userKeys, time)
else if containsAttack (userKeys, input) then else if CoreKey.containsAttack (userKeys, #newKeys input) then
select (options, userKeys) select (options, userKeys)
else if #upHeld input then else if #upHeld input then
moveFocusUp (options, RIGHT_KEY, userKeys, time) moveFocusUp (options, RIGHT_KEY, userKeys, time)
@@ -322,7 +312,7 @@ struct
| DOWN_KEY => | DOWN_KEY =>
if #isSelected options then if #isSelected options then
UpdateDownKey.onSelected (options, input, userKeys, time) UpdateDownKey.onSelected (options, input, userKeys, time)
else if containsAttack (userKeys, input) then else if CoreKey.containsAttack (userKeys, #newKeys input) then
select (options, userKeys) select (options, userKeys)
else if #upHeld input then else if #upHeld input then
moveFocusUp (options, UP_KEY, userKeys, time) moveFocusUp (options, UP_KEY, userKeys, time)
@@ -333,7 +323,7 @@ struct
| JUMP_KEY => | JUMP_KEY =>
if #isSelected options then if #isSelected options then
UpdateJumpKey.onSelected (options, input, userKeys, time) UpdateJumpKey.onSelected (options, input, userKeys, time)
else if containsAttack (userKeys, input) then else if CoreKey.containsAttack (userKeys, #newKeys input) then
select (options, userKeys) select (options, userKeys)
else if #upHeld input then else if #upHeld input then
moveFocusUp (options, DOWN_KEY, userKeys, time) moveFocusUp (options, DOWN_KEY, userKeys, time)
@@ -344,7 +334,7 @@ struct
| ATTACK_KEY => | ATTACK_KEY =>
if #isSelected options then if #isSelected options then
UpdateAttackKey.onSelected (options, input, userKeys, time) UpdateAttackKey.onSelected (options, input, userKeys, time)
else if containsAttack (userKeys, input) then else if CoreKey.containsAttack (userKeys, #newKeys input) then
select (options, userKeys) select (options, userKeys)
else if #upHeld input then else if #upHeld input then
moveFocusUp (options, JUMP_KEY, userKeys, time) moveFocusUp (options, JUMP_KEY, userKeys, time)
@@ -353,8 +343,8 @@ struct
else else
default (options, userKeys) default (options, userKeys)
| SAVE_BUTTON => | SAVE_BUTTON =>
if containsAttack (userKeys, input) then if CoreKey.containsAttack (userKeys, #newKeys input) then
select (options, userKeys) saveAndGoToTitle options
else if #upHeld input then else if #upHeld input then
moveFocusUp (options, ATTACK_KEY, userKeys, time) moveFocusUp (options, ATTACK_KEY, userKeys, time)
else if #downHeld input then else if #downHeld input then
@@ -362,7 +352,7 @@ struct
else else
default (options, userKeys) default (options, userKeys)
| CANCEL_BUTTON => | CANCEL_BUTTON =>
if containsAttack (userKeys, input) then if CoreKey.containsAttack (userKeys, #newKeys input) then
select (options, userKeys) select (options, userKeys)
else if #upHeld input then else if #upHeld input then
moveFocusUp (options, SAVE_BUTTON, userKeys, time) moveFocusUp (options, SAVE_BUTTON, userKeys, time)