1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2025-05-21 14:41:10 +00:00

use new numberpreferenceitem

This commit is contained in:
lemz 2024-07-02 17:18:46 +02:00
parent 0a7902c4b4
commit 927e37b35b
2 changed files with 46 additions and 17 deletions

View file

@ -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);
}

View file

@ -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:Null<Float->Void>;
public var valueFormatter:Null<Float->String>;
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