Merge branch 'main' into translations-system

This commit is contained in:
Ne_Eo
2025-01-09 20:26:29 +01:00
119 changed files with 1207 additions and 416 deletions
+92 -26
View File
@@ -5,7 +5,6 @@ import flixel.util.typeLimit.OneOfTwo;
import funkin.backend.FunkinSprite.XMLAnimType;
import funkin.backend.FunkinSprite;
import funkin.game.Character;
import funkin.backend.system.ErrorCode;
import funkin.backend.FunkinSprite.XMLAnimType;
import flixel.util.FlxColor;
import haxe.xml.Access;
@@ -18,17 +17,53 @@ import haxe.xml.Access;
using StringTools;
@:dox(hide)
enum abstract ErrorCode(Int) {
var OK = 0;
var FAILED = 1;
var MISSING_PROPERTY = 2;
var TYPE_INCORRECT = 3;
var VALUE_NULL = 4;
var REFLECT_ERROR = 5;
}
@:dox(hide)
typedef TextFormat = { text:String, format:Dynamic }
/**
* Class made to make XML parsing easier.
* Used in Stage.hx, Character.hx, and more.
*/
class XMLUtil {
/**
* Applies a property XML node to an object.
* The format for the XML is as follows:
* ```xml
* <!-- float -->
* <property name="name" type="float" value="value" />
* <property name="name" type="number" value="value" />
* <property name="name" type="f" value="value" />
* <!-- string -->
* <property name="name" type="string" value="value" />
* <property name="name" type="str" value="value" />
* <property name="name" type="text" value="value" />
* <property name="name" type="s" value="value" />
* <!-- int -->
* <property name="name" type="int" value="value" />
* <property name="name" type="integer" value="value" />
* <property name="name" type="i" value="value" />
* <!-- bool -->
* <property name="name" type="bool" value="value" />
* <property name="name" type="boolean" value="value" />
* <property name="name" type="b" value="value" />
* <!-- color -->
* <property name="name" type="color" value="value" />
* <property name="name" type="c" value="value" />
* ```
*
* @param object Object to which the xml property will be applied
* @param property `property` node.
* @return Error code (0 = OK, 1 = FAILED, 2 = MISSING_PROPERTY, 3 = TYPE_INCORRECT, 4 = VALUE_NULL, 5 = REFLECT_ERROR)
*/
public static function applyXMLProperty(object:Dynamic, property:Access):ErrorCode {
if (!property.has.name || !property.has.type || !property.has.value) {
@@ -47,7 +82,8 @@ class XMLUtil {
var value:Dynamic = switch(property.att.type.toLowerCase()) {
case "f" | "float" | "number": Std.parseFloat(property.att.value);
case "i" | "int" | "integer" | "color": Std.parseInt(property.att.value);
case "i" | "int" | "integer": Std.parseInt(property.att.value);
case "c" | "color": FlxColor.fromString(property.att.value);
case "s" | "string" | "str" | "text": property.att.value;
case "b" | "bool" | "boolean": property.att.value.toLowerCase() == "true";
default: return TYPE_INCORRECT;
@@ -72,7 +108,11 @@ class XMLUtil {
}
/**
* Overrides a sprite based on a XML node.
* Sets the properties of a sprite based on a XML node.
* @param spr The sprite
* @param node The XML node
* @param parentFolder The parent folder
* @param defaultAnimType The default animation type
*/
public static function loadSpriteFromXML(spr:FunkinSprite, node:Access, parentFolder:String = "", defaultAnimType:XMLAnimType = BEAT):FunkinSprite {
if (parentFolder == null) parentFolder = "";
@@ -147,8 +187,8 @@ class XMLUtil {
var graphicSizey:Null<Int> = Std.parseInt(node.att.graphicSizey);
if (graphicSizey.isNotNull()) spr.setGraphicSize(0, graphicSizey);
}
if (node.has.flipX && node.att.flipX == "true") spr.flipX = true;
if (node.has.flipX) spr.flipX = node.att.flipX == "true";
if (node.has.flipY) spr.flipY = node.att.flipY == "true";
if (node.has.updateHitbox && node.att.updateHitbox == "true") spr.updateHitbox();
if (node.has.zoomfactor)
@@ -170,19 +210,17 @@ class XMLUtil {
if(node.hasNode.anim) {
for(anim in node.nodes.anim)
addXMLAnimation(spr, anim);
} else {
if (spr.frames != null && spr.frames.frames != null) {
addAnimToSprite(spr, {
name: "idle",
anim: null,
fps: 24,
loop: spr.spriteAnimType == LOOP,
animType: spr.spriteAnimType,
x: 0,
y: 0,
indices: [for(i in 0...spr.frames.frames.length) i]
});
}
} else if (spr.frames != null && spr.frames.frames != null) {
addAnimToSprite(spr, {
name: "idle",
anim: null,
fps: 24,
loop: spr.spriteAnimType == LOOP,
animType: spr.spriteAnimType,
x: 0,
y: 0,
indices: [for(i in 0...spr.frames.frames.length) i]
});
}
return spr;
@@ -190,6 +228,11 @@ class XMLUtil {
/**
* Creates a new sprite based on a XML node.
* @param node The XML node
* @param parentFolder The parent folder
* @param defaultAnimType The default animation type
* @param cl The class to create (advanced)
* @param args The arguments to pass to the class (advanced)
*/
public static inline function createSpriteFromXML(node:Access, parentFolder:String = "", defaultAnimType:XMLAnimType = BEAT, ?cl:Class<FunkinSprite>, ?args:Array<Dynamic>):FunkinSprite {
if(cl == null) cl = FunkinSprite;
@@ -197,6 +240,12 @@ class XMLUtil {
return loadSpriteFromXML(Type.createInstance(cl, args), node, parentFolder, defaultAnimType);
}
/**
* Extracts an animation from an XML node.
* @param anim The animation node
* @param animType The animation type
* @param loop Whether the animation should loop
**/
public static function extractAnimFromXML(anim:Access, animType:XMLAnimType = NONE, loop:Bool = false):AnimData {
var animData:AnimData = {
name: null,
@@ -235,6 +284,11 @@ class XMLUtil {
return addAnimToSprite(sprite, extractAnimFromXML(anim, animType, loop));
}
/**
* Adds an animation to a sprite.
* @param sprite The sprite
* @param animData The animation data (gotten from `extractAnimFromXML`)
**/
public static function addAnimToSprite(sprite:FlxSprite, animData:AnimData):ErrorCode {
if (animData.name != null) {
if (animData.fps <= 0 #if web || animData.fps == null #end) animData.fps = 24;
@@ -244,16 +298,17 @@ class XMLUtil {
if(animData.anim == null)
return MISSING_PROPERTY;
if (animData.indices.length > 0)
if (animData.indices != null && animData.indices.length > 0)
animateAnim.addBySymbolIndices(animData.name, animData.anim, animData.indices, animData.fps, animData.loop);
else
animateAnim.addBySymbol(animData.name, animData.anim, animData.fps, animData.loop);
} else {
if (animData.anim == null && animData.indices.length > 0)
sprite.animation.add(animData.name, animData.indices, animData.fps, animData.loop);
else if (animData.indices.length > 0)
sprite.animation.addByIndices(animData.name, animData.anim, animData.indices, "", animData.fps, animData.loop);
else
if (animData.indices != null && animData.indices.length > 0) {
if (animData.anim == null)
sprite.animation.add(animData.name, animData.indices, animData.fps, animData.loop);
else
sprite.animation.addByIndices(animData.name, animData.anim, animData.indices, "", animData.fps, animData.loop);
} else
sprite.animation.addByPrefix(animData.name, animData.anim, animData.fps, animData.loop);
}
@@ -309,6 +364,17 @@ class XMLUtil {
return node;
}
/**
* Gets the text formats from an XML node.
* The format for the XML is as follows:
* ```xml
* <text>Hello <format color="#FF0000">World</format></text>
* ```
*
* @param _node The XML node
* @param currentFormat The current format
* @param parsedSegments The parsed segments
**/
public static function getTextFormats(_node:OneOfTwo<Xml, Access>, currentFormat:Dynamic = null, parsedSegments:Array<TextFormat> = null):Array<TextFormat> {
var node:Xml = cast _node;
if (currentFormat == null)
@@ -401,4 +467,4 @@ typedef BeatAnim = {
interface IXMLEvents {
public function onPropertySet(property:String, value:Dynamic):Void;
}
}