Add dragging for the NumberPreferenceItem

This commit is contained in:
sector-a
2025-07-05 18:35:23 +03:00
parent 313cf4b253
commit d0b64fe4da
2 changed files with 44 additions and 35 deletions
+17 -22
View File
@@ -19,7 +19,6 @@ import funkin.data.song.SongData.SongNoteData;
import funkin.data.notestyle.NoteStyleRegistry;
import funkin.play.notes.notestyle.NoteStyle;
import funkin.ui.options.items.NumberPreferenceItem;
import haxe.Int64;
import flixel.FlxSprite;
import flixel.text.FlxText;
@@ -123,10 +122,10 @@ class OffsetMenu extends Page<OptionsState.OptionsMenuPageName>
add(arrow);
/*var debugText = new FlxText(0, 0);
debugText.setFormat(Paths.font('vcr.ttf'), 16, FlxColor.WHITE, FlxTextAlign.CENTER);
debugText.text = 'Beat: ' + beat;
debugText.setBorderStyle(FlxTextBorderStyle.OUTLINE, FlxColor.BLACK);
add(debugText); */
debugText.setFormat(Paths.font('vcr.ttf'), 16, FlxColor.WHITE, FlxTextAlign.CENTER);
debugText.text = 'Beat: ' + beat;
debugText.setBorderStyle(FlxTextBorderStyle.OUTLINE, FlxColor.BLACK);
add(debugText); */
arrows.push(
{
@@ -216,13 +215,13 @@ class OffsetMenu extends Page<OptionsState.OptionsMenuPageName>
add(blackRect);
/*debugBeatText = new FlxText(0, 0);
debugBeatText.setFormat(Paths.font('vcr.ttf'), 16, FlxColor.WHITE, FlxTextAlign.LEFT);
debugBeatText.setBorderStyle(FlxTextBorderStyle.OUTLINE, FlxColor.BLACK);
debugBeatText.setPosition(10, 10);
debugBeatText.scrollFactor.set(0, 0);
add(debugBeatText);
debugBeatText.setFormat(Paths.font('vcr.ttf'), 16, FlxColor.WHITE, FlxTextAlign.LEFT);
debugBeatText.setBorderStyle(FlxTextBorderStyle.OUTLINE, FlxColor.BLACK);
debugBeatText.setPosition(10, 10);
debugBeatText.scrollFactor.set(0, 0);
add(debugBeatText);
debugBeatText.alpha = 0; */
debugBeatText.alpha = 0; */
receptor = new FunkinSprite(0, 0);
receptor.loadGraphic(Paths.image('latencyReceptor'));
@@ -294,7 +293,7 @@ class OffsetMenu extends Page<OptionsState.OptionsMenuPageName>
offsetItem = createPrefItemNumber('Offset (Global)', 'Offset (Global)', function(value:Float) {
Preferences.globalOffset = Std.int(value);
}, null, Preferences.globalOffset, -1500, 1500, 1.0, 2);
}, null, Preferences.globalOffset, -1500, 1500, 1.0, 2, 5);
createButtonItem('Reset Offset', function() {
Preferences.globalOffset = 0;
offsetItem.currentValue = Preferences.globalOffset;
@@ -351,7 +350,6 @@ class OffsetMenu extends Page<OptionsState.OptionsMenuPageName>
#end
jumpInText.y = testStrumline.y + 175;
#if !mobile if (Preferences.downscroll) #end jumpInText.y = testStrumline.y - 175;
});
PreciseInputManager.instance.onInputPressed.add(onKeyPress);
PreciseInputManager.instance.onInputReleased.add(onKeyRelease);
@@ -480,6 +478,7 @@ class OffsetMenu extends Page<OptionsState.OptionsMenuPageName>
var _lastBeat:Float = 0;
var _lastTime:Float = 0;
override function update(elapsed:Float):Void
{
super.update(elapsed);
@@ -530,7 +529,6 @@ class OffsetMenu extends Page<OptionsState.OptionsMenuPageName>
_lastTime = FlxG.sound.music.time;
if (controls.BACK && shouldOffset == 1)
{
exitCalibration(true);
@@ -762,9 +760,9 @@ class OffsetMenu extends Page<OptionsState.OptionsMenuPageName>
}
/*debugBeatText.x = receptor.x + receptor.width * 2;
debugBeatText.y = receptor.y - 20;
debugBeatText.y = receptor.y - 20;
debugBeatText.text = 'Beat: ' + b; */
debugBeatText.text = 'Beat: ' + b; */
// receptor.angle += angleVel * elapsed;
@@ -779,9 +777,7 @@ class OffsetMenu extends Page<OptionsState.OptionsMenuPageName>
var numPref:NumberPreferenceItem = cast(daItem, NumberPreferenceItem);
thyTextWidth = numPref.lefthandText.getWidth();
numPref.lefthandText.x = xLerp
+ (FlxG.width / 2)
- ((thyTextWidth + daItem.atlasText.getWidth() + 20) / 2);
numPref.lefthandText.x = xLerp + (FlxG.width / 2) - ((thyTextWidth + daItem.atlasText.getWidth() + 20) / 2);
numPref.lefthandText.y = yLerp + ((120 * ind) + 30);
daItem.x = numPref.lefthandText.x + thyTextWidth + 20;
default:
@@ -891,13 +887,12 @@ class OffsetMenu extends Page<OptionsState.OptionsMenuPageName>
// Creates a preference item with a number input.
function createPrefItemNumber(prefName:String, prefDesc:String, onChange:Float->Void, ?valueFormatter:Float->String, defaultValue:Int, min:Int, max:Int,
step:Float = 0.1, precision:Int):NumberPreferenceItem
step:Float = 0.1, precision:Int, dragStepMultiplier:Float = 1):NumberPreferenceItem
{
var item = new NumberPreferenceItem(funkin.ui.FullScreenScaleMode.gameNotchSize.x, (120 * items.length) + 30, prefName, defaultValue, min, max, step,
precision, onChange, valueFormatter);
precision, onChange, valueFormatter, dragStepMultiplier);
items.addItem(prefName, item);
preferenceItems.add(item.lefthandText);
return item;
}
}
@@ -3,9 +3,8 @@ package funkin.ui.options.items;
import funkin.ui.TextMenuList.TextMenuItem;
import funkin.ui.AtlasText;
import funkin.input.Controls;
#if mobile
import funkin.util.TouchUtil;
import funkin.util.SwipeUtil;
#end
/**
* Preference item that allows the player to pick a value between min and max
@@ -32,6 +31,7 @@ class NumberPreferenceItem extends TextMenuItem
public var precision:Int;
public var onChangeCallback:Null<Float->Void>;
public var valueFormatter:Null<Float->String>;
public var dragStepMultiplier:Float;
// Variables
var holdDelayTimer:Float = HOLD_DELAY; // seconds
@@ -43,9 +43,10 @@ class NumberPreferenceItem extends TextMenuItem
* @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
* @param dragStepMultiplier The multiplier for step value in case player does touch drag.
*/
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
?valueFormatter:Float->String, dragStepMultiplier:Float = 1):Void
{
super(x, y, name, function() {
callback(this.currentValue);
@@ -61,6 +62,7 @@ class NumberPreferenceItem extends TextMenuItem
this.precision = precision;
this.onChangeCallback = callback;
this.valueFormatter = valueFormatter;
this.dragStepMultiplier = dragStepMultiplier;
this.fireInstantly = true;
}
@@ -89,29 +91,41 @@ class NumberPreferenceItem extends TextMenuItem
var shouldDecrease:Bool = jpLeft;
var shouldIncrease:Bool = jpRight;
var valueChangeMultiplier:Float = 1;
if (controls().UI_LEFT && holdDelayTimer <= 0.0 && changeRateTimer <= 0.0)
final dragThreshold:Float = 24 / elapsed / 100;
trace(elapsed);
trace(dragThreshold);
if (TouchUtil.touch != null && (TouchUtil.touch.deltaViewX <= -dragThreshold || TouchUtil.touch.deltaViewX >= dragThreshold))
{
shouldDecrease = true;
changeRateTimer = CHANGE_RATE;
valueChangeMultiplier = dragStepMultiplier;
}
else if (controls().UI_RIGHT && holdDelayTimer <= 0.0 && changeRateTimer <= 0.0)
if (holdDelayTimer <= 0.0 && changeRateTimer <= 0.0)
{
shouldIncrease = true;
changeRateTimer = CHANGE_RATE;
if (controls().UI_LEFT || (TouchUtil.touch != null && TouchUtil.touch.deltaX <= -dragThreshold))
{
shouldDecrease = true;
changeRateTimer = CHANGE_RATE;
}
else if (controls().UI_RIGHT || (TouchUtil.touch != null && TouchUtil.touch.deltaX >= dragThreshold))
{
shouldIncrease = true;
changeRateTimer = CHANGE_RATE;
}
}
// Actually increasing/decreasing the value
if (shouldDecrease)
{
var isBelowMin:Bool = currentValue - step < min;
currentValue = (currentValue - step).clamp(min, max);
var isBelowMin:Bool = currentValue - step * valueChangeMultiplier < min;
currentValue = (currentValue - step * valueChangeMultiplier).clamp(min, max);
if (onChangeCallback != null && !isBelowMin) onChangeCallback(currentValue);
}
else if (shouldIncrease)
{
var isAboveMax:Bool = currentValue + step > max;
currentValue = (currentValue + step).clamp(min, max);
var isAboveMax:Bool = currentValue + step * valueChangeMultiplier > max;
currentValue = (currentValue + step * valueChangeMultiplier).clamp(min, max);
if (onChangeCallback != null && !isAboveMax) onChangeCallback(currentValue);
}
}