refactor options to use Time.time to track when key is pressed, instead of using a Real.real value

This commit is contained in:
2025-08-29 12:33:45 +01:00
parent 91bf6854ea
commit a58953e90d
4 changed files with 26 additions and 15 deletions

View File

@@ -47,5 +47,10 @@ struct
val moveProjectileBy = 11 val moveProjectileBy = 11
val keyDelay = 0.3 val keyDelay =
let
val time = LargeInt.fromInt 300
in
Time.fromMilliseconds time
end
end end

View File

@@ -13,15 +13,15 @@ sig
type options_type = type options_type =
{ focus: focus { focus: focus
, isSelected: bool , isSelected: bool
, lastUpPress: real , lastUpPress: Time.time
, lastDownPress: real , lastDownPress: Time.time
, tempKeys: CoreKey.user_key , tempKeys: CoreKey.user_key
} }
val init: CoreKey.user_key -> options_type val init: CoreKey.user_key -> options_type
end end
structure OptionsType :> OPTIONS_TYPE = structure OptionsType : OPTIONS_TYPE =
struct struct
datatype focus = datatype focus =
LEFT_KEY LEFT_KEY
@@ -36,16 +36,16 @@ struct
type options_type = type options_type =
{ focus: focus { focus: focus
, isSelected: bool , isSelected: bool
, lastUpPress: real , lastUpPress: Time.time
, lastDownPress: real , lastDownPress: Time.time
, tempKeys: CoreKey.user_key , tempKeys: CoreKey.user_key
} }
fun init userKeys = fun init userKeys =
{ focus = LEFT_KEY { focus = LEFT_KEY
, isSelected = false , isSelected = false
, lastUpPress = 0.0 , lastUpPress = Time.zeroTime
, lastDownPress = 0.0 , lastDownPress = Time.zeroTime
, tempKeys = userKeys , tempKeys = userKeys
} }
end end

View File

@@ -49,8 +49,8 @@ struct
* as neither is being pressed. *) * as neither is being pressed. *)
val options = val options =
{ focus = focus { focus = focus
, lastUpPress = 0.0 , lastUpPress = Time.zeroTime
, lastDownPress = 0.0 , lastDownPress = Time.zeroTime
, isSelected = isSelected , isSelected = isSelected
, tempKeys = tempKeys , tempKeys = tempKeys
} }
@@ -60,6 +60,9 @@ struct
fun moveFocusUp (options: OptionsType.options_type, newFocus, userKeys, time) = fun moveFocusUp (options: OptionsType.options_type, newFocus, userKeys, time) =
let let
(* only opening Time for time comparison and adding; no impurities here *)
open Time
val {focus, isSelected, lastUpPress, tempKeys, ...} = 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
@@ -68,14 +71,14 @@ struct
if lastUpPress + Constants.keyDelay <= time then if lastUpPress + Constants.keyDelay <= time then
{ focus = newFocus { focus = newFocus
, lastUpPress = time , lastUpPress = time
, lastDownPress = 0.0 , lastDownPress = Time.zeroTime
, isSelected = isSelected , isSelected = isSelected
, tempKeys = tempKeys , tempKeys = tempKeys
} }
else else
{ focus = focus { focus = focus
, lastUpPress = lastUpPress , lastUpPress = lastUpPress
, lastDownPress = 0.0 , lastDownPress = Time.zeroTime
, isSelected = isSelected , isSelected = isSelected
, tempKeys = tempKeys , tempKeys = tempKeys
} }
@@ -86,18 +89,21 @@ struct
fun moveFocusDown fun moveFocusDown
(options: OptionsType.options_type, newFocus, userKeys, time) = (options: OptionsType.options_type, newFocus, userKeys, time) =
let let
(* only opening Time for time comparison and adding; no impurities here *)
open Time
val {focus, isSelected, lastDownPress, tempKeys, ...} = 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 = Time.zeroTime
, lastDownPress = time , lastDownPress = time
, isSelected = isSelected , isSelected = isSelected
, tempKeys = tempKeys , tempKeys = tempKeys
} }
else else
{ focus = focus { focus = focus
, lastUpPress = 0.0 , lastUpPress = Time.zeroTime
, lastDownPress = lastDownPress , lastDownPress = lastDownPress
, isSelected = isSelected , isSelected = isSelected
, tempKeys = tempKeys , tempKeys = tempKeys

View File

@@ -303,7 +303,7 @@ struct
val _ = Gles3.clearColor (1.0, 1.0, 1.0, 1.0) val _ = Gles3.clearColor (1.0, 1.0, 1.0, 1.0)
val _ = Gles3.clear () val _ = Gles3.clear ()
val time = Glfw.getTime () val time = Time.now ()
val input = InputState.getSnapshot () val input = InputState.getSnapshot ()
val game = GameUpdate.update (game, input, time) val game = GameUpdate.update (game, input, time)