Skip to content

Commit

Permalink
add "safeSelectedItem" field to DropDown
Browse files Browse the repository at this point in the history
  • Loading branch information
lemz1 committed Nov 20, 2024
1 parent 212883f commit acd85bc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions project.hxp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions source/funkin/util/macro/FlxMacro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<haxe.macro.Expr.Field>
{
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.Expr.Field> = 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

0 comments on commit acd85bc

Please sign in to comment.