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 containsKey: key_code * key_code list -> bool
val containsAttack: user_key * key_code list -> bool
val containsEscape: user_key * key_code list -> bool
end
structure CoreKey :> CORE_KEY =
@@ -402,4 +406,15 @@ struct
| "KEY_RIGHT_SUPER" => SOME KEY_RIGHT_SUPER
| "KEY_MENU" => SOME KEY_MENU
| _ => 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

View File

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