From 927e37b35bda92888f0c0b47256884a0285cadd3 Mon Sep 17 00:00:00 2001 From: lemz Date: Tue, 2 Jul 2024 17:18:46 +0200 Subject: [PATCH] use new numberpreferenceitem --- source/funkin/ui/options/PreferencesMenu.hx | 8 +-- .../ui/options/items/NumberPreferenceItem.hx | 55 ++++++++++++++----- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/source/funkin/ui/options/PreferencesMenu.hx b/source/funkin/ui/options/PreferencesMenu.hx index d004a46c7..80dad0241 100644 --- a/source/funkin/ui/options/PreferencesMenu.hx +++ b/source/funkin/ui/options/PreferencesMenu.hx @@ -54,7 +54,7 @@ class PreferencesMenu extends Page #if !web createPrefItemNumber('FPS', 'The framerate that the game is running on', function(value:Float) { Preferences.framerate = Std.int(value); - }, Preferences.framerate, 60, 360, 1, 0); + }, null, Preferences.framerate, 60, 360, 1, 0); #end createPrefItemCheckbox('Naughtyness', 'Toggle displaying raunchy content', function(value:Bool):Void { Preferences.naughtyness = value; @@ -120,10 +120,10 @@ class PreferencesMenu extends Page preferenceItems.add(checkbox); } - function createPrefItemNumber(prefName:String, prefDesc:String, onChange:Float->Void, defaultValue:Float, min:Float, max:Float, step:Float, - precision:Int):Void + function createPrefItemNumber(prefName:String, prefDesc:String, onChange:Float->Void, ?valueFormatter:Float->String, defaultValue:Int, min:Int, max:Int, + step:Float = 0.1, precision:Int):Void { - var item = new NumberPreferenceItem(0, (120 * items.length) + 30, prefName, defaultValue, min, max, step, precision, onChange); + var item = new NumberPreferenceItem(0, (120 * items.length) + 30, prefName, defaultValue, min, max, step, precision, onChange, valueFormatter); items.addItem(prefName, item); preferenceItems.add(item.lefthandText); } diff --git a/source/funkin/ui/options/items/NumberPreferenceItem.hx b/source/funkin/ui/options/items/NumberPreferenceItem.hx index eeea3112e..f3cd3cd46 100644 --- a/source/funkin/ui/options/items/NumberPreferenceItem.hx +++ b/source/funkin/ui/options/items/NumberPreferenceItem.hx @@ -14,16 +14,35 @@ class NumberPreferenceItem extends TextMenuItem return PlayerSettings.player1.controls; } + // Widgets public var lefthandText:AtlasText; + // Constants + static final HOLD_DELAY:Float = 0.3; // seconds + static final CHANGE_RATE:Float = 0.08; // seconds + + // Constructor-initialized variables public var currentValue:Float; public var min:Float; public var max:Float; public var step:Float; public var precision:Int; public var onChangeCallback:NullVoid>; + public var valueFormatter:NullString>; - public function new(x:Float, y:Float, name:String, defaultValue:Float, min:Float, max:Float, step:Float, precision:Int, ?callback:Float->Void):Void + // Variables + var holdDelayTimer:Float = HOLD_DELAY; // seconds + var changeRateTimer:Float = 0.0; // seconds + + /** + * @param min Minimum value (example: 0) + * @param max Maximum value (example: 100) + * @param step The value to increment/decrement by (example: 10) + * @param callback Will get called every time the user changes the setting; use this to apply/save the setting. + * @param valueFormatter Will get called every time the game needs to display the float value; use this to change how the displayed string looks + */ + public function new(x:Float, y:Float, name:String, defaultValue:Float, min:Float, max:Float, step:Float, precision:Int, ?callback:Float->Void, + ?valueFormatter:Float->String):Void { super(x, y, name, function() { callback(this.currentValue); @@ -38,14 +57,9 @@ class NumberPreferenceItem extends TextMenuItem this.step = step; this.precision = precision; this.onChangeCallback = callback; + this.valueFormatter = valueFormatter; } - static final HOLD_DELAY:Float = 0.5; // seconds - static final CHANGE_RATE:Float = 0.02; // seconds - - var holdDelayTimer:Float = HOLD_DELAY; // seconds - var changeRateTimer:Float = 0.0; // seconds - override function update(elapsed:Float):Void { super.update(elapsed); @@ -82,21 +96,36 @@ class NumberPreferenceItem extends TextMenuItem changeRateTimer = CHANGE_RATE; } - if (shouldDecrease) currentValue -= step; - else if (shouldIncrease) currentValue += step; - currentValue = currentValue.clamp(min, max); - if (onChangeCallback != null && (shouldIncrease || shouldDecrease)) + // Actually increasing/decreasing the value + if (shouldDecrease) { - onChangeCallback(currentValue); + var isBelowMin:Bool = currentValue - step < min; + currentValue = (currentValue - step).clamp(min, max); + if (onChangeCallback != null && !isBelowMin) onChangeCallback(currentValue); + } + else if (shouldIncrease) + { + var isAboveMax:Bool = currentValue + step > max; + currentValue = (currentValue + step).clamp(min, max); + if (onChangeCallback != null && !isAboveMax) onChangeCallback(currentValue); } } lefthandText.text = formatted(currentValue); } + /** Turns the float into a string */ function formatted(value:Float):String { - return '${toFixed(value)}'; + var float:Float = toFixed(value); + if (valueFormatter != null) + { + return valueFormatter(float); + } + else + { + return '${float}'; + } } function toFixed(value:Float):Float