Skip to content

Replace FlxTextFormatRange and FlxTextFormatMarkerPair class with typedef #3604

@SimonArs

Description

@SimonArs

FlxTextFormatRange and FlxTextFormatMarkerPair currently exists as lightweight data container:

class FlxTextFormatRange
{
	public var range(default, null):FlxRange<Int>;
	public var format(default, null):FlxTextFormat;

	public function new(f:FlxTextFormat, start:Int, end:Int):Void {
		range = new FlxRange<Int>(start, end);
		format = f;
	}
}

class FlxTextFormatMarkerPair
{
	public var format:FlxTextFormat;
	public var marker:UnicodeString;

	public function new(f:FlxTextFormat, m:UnicodeString):Void {
		format = f;
		marker = m;
	}
}

Since the classes has no behavior, doesn't use inheritance, only stores fields it could potentially be replaced with typedef approach

typedef FlxTextFormatRange = {
	range:FlxRange<Int>,
	format:FlxTextFormat
};

typedef FlxTextFormatMarkerPair = {
	format:FlxTextFormat,
	marker:UnicodeString
};

and use it like

add(new FlxText(100, 100).applyMarkup("Yo <r>red<r> and <g>green<g> txt", [
	{format: new FlxTextFormat(0xFFFF0000), marker: "<r>"},
	{format: new FlxTextFormat(0xFF00FF00), marker: "<g>"}
]));

public function addFormat(f:FlxTextFormat, start:Int = -1, end:Int = -1):FlxText {
	_formatRanges.push({range: new FlxRange<Int>(start, end), format: f});
	_formatRanges.sort((left:FlxTextFormatRange, right:FlxTextFormatRange) -> return left.range.start < right.range.start ? -1 : 1);
	_regen = true;
	return this;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions