From acd85bc24131f935ab0155b2bfbb3d2b9f860439 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Wed, 20 Nov 2024 22:36:56 +0100 Subject: [PATCH] add "safeSelectedItem" field to DropDown --- project.hxp | 1 + .../components/dialogs/ResultsAnimDialog.hx | 2 +- source/funkin/util/macro/FlxMacro.hx | 31 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/project.hxp b/project.hxp index adecc20328..280f840eb4 100644 --- a/project.hxp +++ b/project.hxp @@ -585,6 +585,7 @@ class Project extends HXProject { function configureCustomMacros() { // This macro allows addition of new functionality to existing Flixel. --> addHaxeMacro("addMetadata('@:build(funkin.util.macro.FlxMacro.buildFlxBasic())', 'flixel.FlxBasic')"); + addHaxeMacro("addMetadata('@:build(funkin.util.macro.FlxMacro.buildDropDown())', 'haxe.ui.components.DropDown')"); } function configureOutputDir() { diff --git a/source/funkin/ui/debug/char/components/dialogs/ResultsAnimDialog.hx b/source/funkin/ui/debug/char/components/dialogs/ResultsAnimDialog.hx index 300cd66e05..056074b5c7 100644 --- a/source/funkin/ui/debug/char/components/dialogs/ResultsAnimDialog.hx +++ b/source/funkin/ui/debug/char/components/dialogs/ResultsAnimDialog.hx @@ -311,7 +311,7 @@ private class RankAnimationData extends VBox function get_animData():PlayerResultsAnimationData { return { - renderType: animRenderType.dataSource.get(animRenderType.selectedIndex), + renderType: animRenderType.safeSelectedItem, assetPath: animAssetPath.text, offsets: [animOffsetX.value, animOffsetY.value], zIndex: animZIndex.value, diff --git a/source/funkin/util/macro/FlxMacro.hx b/source/funkin/util/macro/FlxMacro.hx index cec6b72ec4..7807f01fea 100644 --- a/source/funkin/util/macro/FlxMacro.hx +++ b/source/funkin/util/macro/FlxMacro.hx @@ -32,6 +32,37 @@ class FlxMacro return fields; } + + /** + * A macro to be called targeting the `DropDown` class. + * @return An array of fields that the class contains. + */ + public static macro function buildDropDown():Array + { + var pos:haxe.macro.Expr.Position = haxe.macro.Context.currentPos(); + // The DropDown class. We can add new properties to this class. + var cls:haxe.macro.Type.ClassType = haxe.macro.Context.getLocalClass().get(); + // The fields of the DropDown class. + var fields:Array = haxe.macro.Context.getBuildFields(); + + // haxe.macro.Context.info('[INFO] ${cls.name}: Adding safeSelectedItem attribute...', pos); + + // Here, we add the safeSelectedItem attribute to all DropDown objects. + // This attribute is a getter which returns dataSource.get(selectedIndex) + // This is a safer way than directly using selectedItem because + // selectedItem isn't immediately updated after changing selectedIndex + fields = fields.concat((macro class TempClass + { + public var safeSelectedItem(get, never):Dynamic; + + function get_safeSelectedItem():Dynamic + { + return dataSource.get(selectedIndex); + } + }).fields); + + return fields; + } } #end #end