Skip to content

Commit

Permalink
Added number ranges for indices (1..6)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeeEoo committed Aug 27, 2024
1 parent 6c34dbf commit 1b55bef
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 18 deletions.
85 changes: 85 additions & 0 deletions source/funkin/backend/utils/CoolUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,91 @@ class CoolUtil
var file = new haxe.io.Path(file);
return file.file;
}

/**
* Converts a string of "1..3,5,7..9,8..5" into an array of numbers like [1,2,3,5,7,8,9,8,7,6,5]
* @param input String to parse
* @return Array of numbers
*/
public static function parseNumberRange(input:String):Array<Int> {
var result:Array<Int> = [];
var parts:Array<String> = input.split(",");

for (part in parts) {
part = part.trim();
var idx = part.indexOf("..");
if (idx != -1) {
var start = Std.parseInt(part.substring(0, idx).trim());
var end = Std.parseInt(part.substring(idx + 2).trim());

if(start == null || end == null) {
continue;
}

if (start < end) {
for (j in start...end + 1) {
result.push(j);
}
} else {
for (j in end...start + 1) {
result.push(start + end - j);
}
}
} else {
var num = Std.parseInt(part);
if (num != null) {
result.push(num);
}
}
}
return result;
}

/**
* Converts an array of numbers into a string of ranges.
* Example: [1,2,3,5,7,8,9,8,7,6,5] -> "1..3,5,7..9,8..5"
* @param numbers Array of numbers
* @return String representing the ranges
*/
public static function formatNumberRange(numbers:Array<Int>, seperator:String = ","):String {
if (numbers.length == 0) return "";

var result:Array<String> = [];
var i = 0;

while (i < numbers.length) {
var start = numbers[i];
var end = start;
var direction = 0; // 0: no sequence, 1: increasing, -1: decreasing

if (i + 1 < numbers.length) { // detect direction of sequence
if (numbers[i + 1] == end + 1) {
direction = 1;
} else if (numbers[i + 1] == end - 1) {
direction = -1;
}
}

if(direction != 0) {
while (i + 1 < numbers.length && (numbers[i + 1] == end + direction)) {
end = numbers[i + 1];
i++;
}
}

if (start == end) { // no direction
result.push('${start}');
} else if (start + direction == end) { // 1 step increment
result.push('${start},${end}');
} else { // store as range
result.push('${start}..${end}');
}

i++;
}

return result.join(seperator);
}
}

/**
Expand Down
9 changes: 1 addition & 8 deletions source/funkin/backend/utils/XMLUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,7 @@ class XMLUtil {
if (anim.has.y) animData.y = Std.parseFloat(anim.att.y).getDefault(animData.y);
if (anim.has.loop) animData.loop = anim.att.loop == "true";
if (anim.has.forced) animData.forced = anim.att.forced == "true";
if (anim.has.indices) {
var indicesSplit = anim.att.indices.split(",");
for(indice in indicesSplit) {
var i = Std.parseInt(indice.trim());
if (i != null)
animData.indices.push(i);
}
}
if (anim.has.indices) animData.indices = CoolUtil.parseNumberRange(anim.att.indices);

return animData;
}
Expand Down
10 changes: 2 additions & 8 deletions source/funkin/editors/character/CharacterAnimScreen.hx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CharacterAnimScreen extends UISubstateWindow {
addLabelOn(loopedCheckbox, "Looping");
loopedCheckbox.x += 8; loopedCheckbox.y += 6;

indicesTextBox = new UITextBox(nameTextBox.x, nameTextBox.y + 32 + 40, animData.indices.join(", "), 270);
indicesTextBox = new UITextBox(nameTextBox.x, nameTextBox.y + 32 + 40, CoolUtil.formatNumberRange(animData.indices, ", "), 270);
add(indicesTextBox);
addLabelOn(indicesTextBox, "Indices");

Expand Down Expand Up @@ -109,14 +109,8 @@ class CharacterAnimScreen extends UISubstateWindow {
animType: NONE,
x: offsetXStepper.value,
y: offsetYStepper.value,
indices: []
indices: CoolUtil.parseNumberRange(indicesTextBox.label.text)
};
var indicesSplit = indicesTextBox.label.text.split(",");
for(indice in indicesSplit) {
var i = Std.parseInt(indice.trim());
if (i != null)
animData.indices.push(i);
}

if (onSave != null) onSave(animData);
}
Expand Down
4 changes: 3 additions & 1 deletion source/funkin/editors/character/CharacterInfoScreen.hx
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@ class CharacterInfoScreen extends UISubstateWindow {
var offset:FlxPoint = character.getAnimOffset(anim.name);
animXml.set("x", Std.string(offset.x));
animXml.set("y", Std.string(offset.y));
offset.put();

if (anim.indices.length > 0)
animXml.set("indices", anim.indices.join(","));
animXml.set("indices", CoolUtil.formatNumberRange(anim.indices));
xml.addChild(animXml);
}

Expand Down
2 changes: 1 addition & 1 deletion source/funkin/game/Character.hx
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
offset.putWeak();

if (anim.indices.length > 0)
animXml.set("indices", anim.indices.join(","));
animXml.set("indices", CoolUtil.formatNumberRange(anim.indices));

xml.addChild(animXml);
}
Expand Down

0 comments on commit 1b55bef

Please sign in to comment.