1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-14 02:42:55 +00:00
Funkin/source/ui/AtlasText.hx

262 lines
5.5 KiB
Haxe
Raw Normal View History

2021-02-24 00:55:24 +00:00
package ui;
import flixel.FlxSprite;
import flixel.group.FlxSpriteGroup;
import flixel.graphics.frames.FlxAtlasFrames;
2021-03-22 12:48:52 +00:00
import flixel.util.FlxStringUtil;
2021-02-24 00:55:24 +00:00
@:forward
abstract BoldText(AtlasText) from AtlasText to AtlasText
{
inline public function new (x = 0.0, y = 0.0, text:String)
{
this = new AtlasText(x, y, text, Bold);
}
}
/**
* Alphabet.hx has a ton of bugs and does a bunch of stuff I don't need, fuck that class
*/
class AtlasText extends FlxTypedSpriteGroup<AtlasChar>
{
2021-03-14 02:11:56 +00:00
static var fonts = new Map<AtlasFont, AtlasFontData>();
static var casesAllowed = new Map<AtlasFont, Case>();
public var text(default, set):String = "";
2021-02-24 00:55:24 +00:00
2021-03-14 02:11:56 +00:00
var font:AtlasFontData;
2021-02-24 00:55:24 +00:00
2021-03-14 02:11:56 +00:00
public var atlas(get, never):FlxAtlasFrames;
inline function get_atlas() return font.atlas;
public var caseAllowed(get, never):Case;
inline function get_caseAllowed() return font.caseAllowed;
public var maxHeight(get, never):Float;
inline function get_maxHeight() return font.maxHeight;
public function new (x = 0.0, y = 0.0, text:String, fontName:AtlasFont = Default)
2021-02-24 00:55:24 +00:00
{
2021-03-14 02:11:56 +00:00
if (!fonts.exists(fontName))
fonts[fontName] = new AtlasFontData(fontName);
font = fonts[fontName];
2021-02-24 00:55:24 +00:00
super(x, y);
this.text = text;
}
function set_text(value:String)
{
2021-03-14 02:11:56 +00:00
if (value == null)
value = "";
var caseValue = restrictCase(value);
var caseText = restrictCase(this.text);
this.text = value;
if (caseText == caseValue)
return value; // cancel redraw
if (caseValue.indexOf(caseText) == 0)
{
// new text is just old text with additions at the end, append the difference
appendTextCased(caseValue.substr(caseText.length));
2021-02-24 00:55:24 +00:00
return this.text;
2021-03-14 02:11:56 +00:00
}
value = caseValue;
2021-02-24 00:55:24 +00:00
group.kill();
2021-03-14 02:11:56 +00:00
if (value == "")
return this.text;
2021-03-22 12:48:52 +00:00
appendTextCased(caseValue);
2021-03-14 02:11:56 +00:00
return this.text;
}
/**
* Adds new characters, without needing to redraw the previous characters
* @param text The text to add.
* @throws String if `text` is null.
*/
public function appendText(text:String)
{
if (text == null)
throw "cannot append null";
if (text == "")
return;
this.text = this.text + text;
}
/**
* Converts all characters to fit the font's `allowedCase`.
* @param text
*/
function restrictCase(text:String)
{
return switch(caseAllowed)
{
case Both: text;
case Upper: text.toUpperCase();
case Lower: text.toLowerCase();
}
}
/**
* Adds new text on top of the existing text. Helper for other methods; DOESN'T CHANGE `this.text`.
* @param text The text to add, assumed to match the font's `caseAllowed`.
*/
function appendTextCased(text:String)
{
var charCount = group.countLiving();
2021-02-24 00:55:24 +00:00
var xPos:Float = 0;
var yPos:Float = 0;
2021-03-14 02:11:56 +00:00
// `countLiving` returns -1 if group is empty
if (charCount == -1)
charCount = 0;
else if (charCount > 0)
{
var lastChar = group.members[charCount - 1];
2021-03-23 02:39:50 +00:00
xPos = lastChar.x + lastChar.width - x;
yPos = lastChar.y + lastChar.height - maxHeight - y;
2021-03-14 02:11:56 +00:00
}
2021-02-24 00:55:24 +00:00
2021-03-14 02:11:56 +00:00
var splitValues = text.split("");
for (i in 0...splitValues.length)
2021-02-24 00:55:24 +00:00
{
2021-03-14 02:11:56 +00:00
switch(splitValues[i])
2021-02-24 00:55:24 +00:00
{
case " ":
{
xPos += 40;
}
case "\n":
{
xPos = 0;
2021-03-14 02:11:56 +00:00
yPos += maxHeight;
2021-02-24 00:55:24 +00:00
}
2021-03-14 02:11:56 +00:00
case char:
2021-02-24 00:55:24 +00:00
{
var charSprite:AtlasChar;
if (group.members.length <= charCount)
charSprite = new AtlasChar(atlas, char);
else
{
charSprite = group.members[charCount];
charSprite.revive();
charSprite.char = char;
2021-03-23 02:39:50 +00:00
charSprite.alpha = 1;//gets multiplied when added
2021-02-24 00:55:24 +00:00
}
charSprite.x = xPos;
charSprite.y = yPos + maxHeight - charSprite.height;
add(charSprite);
xPos += charSprite.width;
charCount++;
}
}
}
}
2021-03-22 12:48:52 +00:00
override function toString()
{
return "InputItem, " + FlxStringUtil.getDebugString(
[ LabelValuePair.weak("x", x)
, LabelValuePair.weak("y", y)
, LabelValuePair.weak("text", text)
]
);
}
2021-02-24 00:55:24 +00:00
}
class AtlasChar extends FlxSprite
{
public var char(default, set):String;
public function new(x = 0.0, y = 0.0, atlas:FlxAtlasFrames, char:String)
{
super(x, y);
frames = atlas;
this.char = char;
antialiasing = true;
}
function set_char(value:String)
{
if (this.char != value)
{
2021-03-14 02:11:56 +00:00
var prefix = getAnimPrefix(value);
animation.addByPrefix("anim", prefix, 24);
2021-02-24 00:55:24 +00:00
animation.play("anim");
updateHitbox();
}
return this.char = value;
}
function getAnimPrefix(char:String)
{
return switch (char)
{
case '-': '-dash-';
case '.': '-period-';
case ",": '-comma-';
case "'": '-apostraphie-';
case "?": '-question mark-';
case "!": '-exclamation point-';
case "\\": '-back slash-';
case "/": '-forward slash-';
case "*": '-multiply x-';
case "": '-start quote-';
case "": '-end quote-';
default: char;
}
}
}
2021-03-14 02:11:56 +00:00
private class AtlasFontData
{
static public var upperChar = ~/^[A-Z]\d+$/;
static public var lowerChar = ~/^[a-z]\d+$/;
public var atlas:FlxAtlasFrames;
public var maxHeight:Float = 0.0;
public var caseAllowed:Case = Both;
public function new (name:AtlasFont)
{
atlas = Paths.getSparrowAtlas("fonts/" + name.getName().toLowerCase());
atlas.parent.destroyOnNoUse = false;
atlas.parent.persist = true;
var containsUpper = false;
var containsLower = false;
for (frame in atlas.frames)
{
maxHeight = Math.max(maxHeight, frame.frame.height);
if (!containsUpper)
containsUpper = upperChar.match(frame.name);
if (!containsLower)
containsLower = lowerChar.match(frame.name);
}
if (containsUpper != containsLower)
caseAllowed = containsUpper ? Upper : Lower;
}
}
enum Case
{
Both;
Upper;
Lower;
}
2021-02-24 00:55:24 +00:00
enum AtlasFont
{
Default;
Bold;
}