From a58953e90ddbdd63a715ec615756848089ac8ac0 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Fri, 29 Aug 2025 12:33:45 +0100 Subject: [PATCH] refactor options to use Time.time to track when key is pressed, instead of using a Real.real value --- fcore/constants.sml | 7 ++++++- fcore/options/options-type.sml | 14 +++++++------- fcore/options/options-update.sml | 18 ++++++++++++------ shell/gl-draw.sml | 2 +- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/fcore/constants.sml b/fcore/constants.sml index 637353d..6f36086 100644 --- a/fcore/constants.sml +++ b/fcore/constants.sml @@ -47,5 +47,10 @@ struct val moveProjectileBy = 11 - val keyDelay = 0.3 + val keyDelay = + let + val time = LargeInt.fromInt 300 + in + Time.fromMilliseconds time + end end diff --git a/fcore/options/options-type.sml b/fcore/options/options-type.sml index 76e00b6..cbcd87a 100644 --- a/fcore/options/options-type.sml +++ b/fcore/options/options-type.sml @@ -13,15 +13,15 @@ sig type options_type = { focus: focus , isSelected: bool - , lastUpPress: real - , lastDownPress: real + , lastUpPress: Time.time + , lastDownPress: Time.time , tempKeys: CoreKey.user_key } val init: CoreKey.user_key -> options_type end -structure OptionsType :> OPTIONS_TYPE = +structure OptionsType : OPTIONS_TYPE = struct datatype focus = LEFT_KEY @@ -36,16 +36,16 @@ struct type options_type = { focus: focus , isSelected: bool - , lastUpPress: real - , lastDownPress: real + , lastUpPress: Time.time + , lastDownPress: Time.time , tempKeys: CoreKey.user_key } fun init userKeys = { focus = LEFT_KEY , isSelected = false - , lastUpPress = 0.0 - , lastDownPress = 0.0 + , lastUpPress = Time.zeroTime + , lastDownPress = Time.zeroTime , tempKeys = userKeys } end diff --git a/fcore/options/options-update.sml b/fcore/options/options-update.sml index 89fcfbf..3648a4f 100644 --- a/fcore/options/options-update.sml +++ b/fcore/options/options-update.sml @@ -49,8 +49,8 @@ struct * as neither is being pressed. *) val options = { focus = focus - , lastUpPress = 0.0 - , lastDownPress = 0.0 + , lastUpPress = Time.zeroTime + , lastDownPress = Time.zeroTime , isSelected = isSelected , tempKeys = tempKeys } @@ -60,6 +60,9 @@ struct fun moveFocusUp (options: OptionsType.options_type, newFocus, userKeys, time) = let + (* only opening Time for time comparison and adding; no impurities here *) + open Time + 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 @@ -68,14 +71,14 @@ struct if lastUpPress + Constants.keyDelay <= time then { focus = newFocus , lastUpPress = time - , lastDownPress = 0.0 + , lastDownPress = Time.zeroTime , isSelected = isSelected , tempKeys = tempKeys } else { focus = focus , lastUpPress = lastUpPress - , lastDownPress = 0.0 + , lastDownPress = Time.zeroTime , isSelected = isSelected , tempKeys = tempKeys } @@ -86,18 +89,21 @@ struct fun moveFocusDown (options: OptionsType.options_type, newFocus, userKeys, time) = let + (* only opening Time for time comparison and adding; no impurities here *) + open Time + val {focus, isSelected, lastDownPress, tempKeys, ...} = options val options = if lastDownPress + Constants.keyDelay <= time then { focus = newFocus - , lastUpPress = 0.0 + , lastUpPress = Time.zeroTime , lastDownPress = time , isSelected = isSelected , tempKeys = tempKeys } else { focus = focus - , lastUpPress = 0.0 + , lastUpPress = Time.zeroTime , lastDownPress = lastDownPress , isSelected = isSelected , tempKeys = tempKeys diff --git a/shell/gl-draw.sml b/shell/gl-draw.sml index 6500cb8..5ec07ae 100644 --- a/shell/gl-draw.sml +++ b/shell/gl-draw.sml @@ -303,7 +303,7 @@ struct val _ = Gles3.clearColor (1.0, 1.0, 1.0, 1.0) val _ = Gles3.clear () - val time = Glfw.getTime () + val time = Time.now () val input = InputState.getSnapshot () val game = GameUpdate.update (game, input, time)