Radio Buttons + fixed offsets

Please fix the radio button forId code its horrible 😭
Simplified checkbox code
This commit is contained in:
Ne_Eo
2024-10-05 02:22:52 +02:00
parent bf1a9e44c4
commit 997a6801dd
4 changed files with 194 additions and 45 deletions
+16 -1
View File
@@ -91,8 +91,10 @@ class OptionsScreen extends FlxTypedSpriteGroup<OptionType> {
if (members.length > 0) {
members[curSelected].selected = true;
if (controls.ACCEPT || FlxG.mouse.justReleased)
if (controls.ACCEPT || FlxG.mouse.justReleased) {
members[curSelected].onSelect();
onSelect(members[curSelected]);
}
if (controls.LEFT_P)
members[curSelected].onChangeSelection(-1);
if (controls.RIGHT_P)
@@ -121,4 +123,17 @@ class OptionsScreen extends FlxTypedSpriteGroup<OptionType> {
}
public dynamic function onClose(o:OptionsScreen) {}
public dynamic function onSelect(o:OptionType) {
if(o is funkin.options.type.RadioButton) {
var orb:funkin.options.type.RadioButton = cast o;
for(e in members) {
if(e is funkin.options.type.RadioButton) {
var rb:funkin.options.type.RadioButton = cast e;
if(rb.forId == orb.forId && rb != o) {
rb.checked = false;
}
}
}
}
}
}
@@ -1,11 +1,13 @@
package funkin.options.categories;
class LanguageBox extends Checkbox {
class LanguageRadio extends RadioButton {
public var langID:String;
public function new(name:String, langID:String) {
this.langID = langID;
super(name, "LanguageOptions.language-desc", null, null);
super(name, "LanguageOptions.language-desc", null, langID, null, "languageSelector");
checked = langID == TU.curLanguage;
}
override function reloadStrings() {
@@ -21,25 +23,13 @@ class LanguageBox extends Checkbox {
return v;
}
override function update(elapsed:Float) {
var lastChecked = checked;
checked = langID == TU.curLanguage;
if(lastChecked != checked)
checkbox.animation.play("checking", true, !checked);
super.update(elapsed);
}
override function onSelect() {
checked = !checked;
if(checked) {
TranslationUtil.setLanguage(langID);
if(FlxG.state is OptionsMenu) {
var menu:OptionsMenu = cast FlxG.state;
menu.reloadStrings();
trace("Reloading strings");
}
override dynamic function onSet(value:Dynamic) {
TranslationUtil.setLanguage(value);
if(FlxG.state is OptionsMenu) {
var menu:OptionsMenu = cast FlxG.state;
menu.reloadStrings();
trace("Reloading strings");
}
checkbox.animation.play("checking", true, !checked);
}
}
@@ -53,8 +43,8 @@ class LanguageOptions extends OptionsScreen {
var split = lang.split("/");
var langId = split.first();
var langName = split.last();
var checkbox = new LanguageBox(langName, langId);
add(checkbox);
var radio = new LanguageRadio(langName, langId);
add(radio);
}
}
}
+45 -22
View File
@@ -3,23 +3,19 @@ package funkin.options.type;
import flixel.math.FlxPoint;
class Checkbox extends TextOption {
public var checkbox:FlxSprite;
public var checked:Bool;
public var checked(default, set):Bool;
public var parent:Dynamic;
private function get_selected() {
}
public var optionName:String;
private var offsets:Map<String, FlxPoint> = [
"checked" => FlxPoint.get(1, -1),
"unchecked" => FlxPoint.get(-12, -72),
"checking" => FlxPoint.get(12, 30)
"unchecked" => FlxPoint.get(0, -70),
"checked" => FlxPoint.get(23, -30),
"unchecking" => FlxPoint.get(25, -12),
"checking" => FlxPoint.get(35, 31)
];
private var baseCheckboxOffset:FlxPoint = FlxPoint.get();
public function new(text:String, desc:String, optionName:String, ?parent:Dynamic) {
super(text, desc, null);
@@ -31,32 +27,61 @@ class Checkbox extends TextOption {
checkbox = new FlxSprite(10, -40);
checkbox.frames = Paths.getFrames('menus/options/checkboxThingie');
checkbox.animation.addByPrefix("checked", "Check Box Selected Static", 24);
checkbox.animation.addByPrefix("unchecked", "Check Box unselected", 24);
checkbox.animation.addByPrefix("checking", "Check Box selecting animation", 24, false);
checkbox.animation.addByPrefix("unchecked", "Check Box unselected0", 24);
checkbox.animation.addByPrefix("checked", "Check Box Selected Static0", 24);
checkbox.animation.addByPrefix("unchecking", "Check Box deselect animation0", 24, false);
checkbox.animation.addByPrefix("checking", "Check Box selecting animation0", 24, false);
checkbox.antialiasing = true;
checkbox.scale.set(0.75, 0.75);
checkbox.updateHitbox();
add(checkbox);
baseCheckboxOffset.set(checkbox.offset.x, checkbox.offset.y);
this.optionName = optionName;
if(optionName != null) {
checked = Reflect.field(parent, optionName);
} else {
checked = false;
}
}
public var firstFrame:Bool = true;
public override function update(elapsed:Float) {
if (checkbox.animation.curAnim == null || (checkbox.animation.curAnim.finished || (checkbox.animation.curAnim.reversed && checkbox.animation.curAnim.curFrame <= 0))) {
if(checkbox.animation.curAnim == null) {
checkbox.animation.play(checked ? "checked" : "unchecked", true);
}
super.update(elapsed);
var offset = offsets[checkbox.animation.curAnim.name];
if (offset != null)
checkbox.offset.set(
(offset.x * checkbox.scale.x) + baseCheckboxOffset.x,
(offset.y * checkbox.scale.y) + baseCheckboxOffset.y);
switch(checkbox.animation.curAnim.name) {
case "unchecking":
if(checkbox.animation.curAnim.finished) {
checkbox.animation.play("unchecked", true);
}
case "checking":
if(checkbox.animation.curAnim.finished) {
checkbox.animation.play("checked", true);
}
}
firstFrame = false;
}
override function draw() {
if(checkbox.animation.curAnim != null) {
var offset = offsets[checkbox.animation.curAnim.name];
if (offset != null)
checkbox.frameOffset.set(offset.x, offset.y);
}
super.draw();
}
function set_checked(v:Bool) {
if(checked != v) {
checked = v;
if(!firstFrame) {
checkbox.animation.play(checked ? "checking" : "unchecking", true);
}
}
return v;
}
public override function onSelect() {
@@ -64,12 +89,10 @@ class Checkbox extends TextOption {
if(optionName != null) {
Reflect.setField(parent, optionName, checked);
}
checkbox.animation.play("checking", true, !checked);
}
public override function destroy() {
super.destroy();
for(e in offsets) e.put();
baseCheckboxOffset.put();
}
}
+121
View File
@@ -0,0 +1,121 @@
package funkin.options.type;
import flixel.math.FlxPoint;
class RadioButton extends TextOption {
public var radio:FlxSprite;
public var checked(default, set):Bool;
public var parent:Dynamic;
public var value:Dynamic;
public var forId:String;
public var optionName:String;
private var offsets:Map<String, FlxPoint> = [
"unchecked" => FlxPoint.get(0, -65),
"checked" => FlxPoint.get(0, -65),
"unchecking" => FlxPoint.get(15, -55),
"checking" => FlxPoint.get(17, -40)
];
public function new(text:String, desc:String, optionName:String, value:Dynamic, ?parent:Dynamic, ?forId:String, ?options:OptionsScreen) {
super(text, desc, null);
if (parent == null)
parent = Options;
if(forId == null)
forId = optionName;
this.parent = parent;
this.forId = forId;
if(options != null) {
if(FlxG.state is OptionsMenu) {
var menu:OptionsMenu = cast FlxG.state;
options = menu.optionsTree.members.last();
}
}
radio = new FlxSprite(10, -40);
radio.frames = Paths.getFrames('menus/options/radioCrank');
radio.animation.addByPrefix("unchecked", "Radio unselected0", 24);
radio.animation.addByPrefix("checked", "Radio Selected Static0", 24);
radio.animation.addByPrefix("unchecking", "Radio deselect animation0", 24, false);
radio.animation.addByPrefix("checking", "Radio selecting animation0", 24, false);
radio.antialiasing = true;
radio.scale.set(0.75, 0.75);
radio.updateHitbox();
add(radio);
this.value = value;
this.optionName = optionName;
if(optionName != null) {
checked = Reflect.field(parent, optionName) == value;
} else {
checked = false;
}
}
public var firstFrame:Bool = true;
public override function update(elapsed:Float) {
if(radio.animation.curAnim == null) {
radio.animation.play(checked ? "checked" : "unchecked", true);
}
super.update(elapsed);
switch(radio.animation.curAnim.name) {
case "unchecking":
if(radio.animation.curAnim.finished) {
radio.animation.play("unchecked", true);
trace("UNCHECKING finished");
}
case "checking":
if(radio.animation.curAnim.finished) {
radio.animation.play("checked", true);
trace("CHECKING finished");
}
}
firstFrame = false;
}
override function draw() {
if(radio.animation.curAnim != null) {
var offset = offsets[radio.animation.curAnim.name];
if (offset != null)
radio.frameOffset.set(offset.x, offset.y);
}
super.draw();
}
function set_checked(v:Bool) {
if(checked != v) {
checked = v;
if(!firstFrame) {
radio.animation.play(checked ? "checking" : "unchecking", true);
}
}
return v;
}
public override function onSelect() {
checked = true;
if(checked) {
onSet(value);
}
}
public dynamic function onSet(value:Dynamic) {
if(optionName != null) {
Reflect.setField(parent, optionName, value);
}
}
public override function destroy() {
super.destroy();
for(e in offsets) e.put();
}
}