diff --git a/source/ui/AtlasText.hx b/source/ui/AtlasText.hx new file mode 100644 index 000000000..579c35729 --- /dev/null +++ b/source/ui/AtlasText.hx @@ -0,0 +1,140 @@ +package ui; + +import flixel.FlxSprite; +import flixel.group.FlxSpriteGroup; +import flixel.graphics.frames.FlxAtlasFrames; + +@: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 +{ + static var maxHeights = new Map(); + public var text(default, set):String; + + var atlas:FlxAtlasFrames; + var maxHeight = 0.0; + + public function new (x = 0.0, y = 0.0, text:String, font:AtlasFont = Default) + { + atlas = Paths.getSparrowAtlas("fonts/" + font.getName().toLowerCase()); + if (maxHeights.exists(font)) + { + maxHeight = 0; + for (frame in atlas.frames) + maxHeight = Math.max(maxHeight, frame.frame.height); + maxHeights[font] = maxHeight; + } + maxHeight = maxHeights[font]; + + super(x, y); + + this.text = text; + } + + function set_text(value:String) + { + if (this.text == value) + return this.text; + + group.kill(); + + var xPos:Float = 0; + var yPos:Float = 0; + + var charCount = 0; + for (char in value.split("")) + { + switch(char) + { + case " ": + { + xPos += 40; + } + case "\n": + { + xPos = 0; + yPos += 55; + } + default: + { + var charSprite:AtlasChar; + if (group.members.length <= charCount) + charSprite = new AtlasChar(atlas, char); + else + { + charSprite = group.members[charCount]; + charSprite.revive(); + charSprite.char = char; + } + charSprite.x = xPos; + charSprite.y = yPos + maxHeight - charSprite.height; + add(charSprite); + + xPos += charSprite.width; + charCount++; + } + } + } + // updateHitbox(); + return this.text = value; + } +} + +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) + { + animation.addByPrefix("anim", getAnimPrefix(value), 24); + 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; + } + } +} + +enum AtlasFont +{ + Default; + Bold; +} \ No newline at end of file diff --git a/source/ui/Prompt.hx b/source/ui/Prompt.hx index 6cbf450c0..ccac5d576 100644 --- a/source/ui/Prompt.hx +++ b/source/ui/Prompt.hx @@ -1,5 +1,6 @@ package ui; +import ui.AtlasText; import ui.MenuList; import flixel.FlxG; @@ -14,8 +15,8 @@ class Prompt extends flixel.FlxSubState public var onYes:Void->Void; public var onNo:Void->Void; - public var buttons:AlphabetMenuList; - public var field:Alphabet; + public var buttons:TextMenuList; + public var field:AtlasText; var style:ButtonStyle; @@ -24,9 +25,9 @@ class Prompt extends flixel.FlxSubState this.style = style; super(0xA0000000); - buttons = new AlphabetMenuList(Horizontal); + buttons = new TextMenuList(Horizontal); - field = new Alphabet(text, true); + field = new BoldText(text); field.scrollFactor.set(0, 0); } diff --git a/source/ui/AlphabetMenuList.hx b/source/ui/TextMenuList.hx similarity index 54% rename from source/ui/AlphabetMenuList.hx rename to source/ui/TextMenuList.hx index 47832e582..bfc356c26 100644 --- a/source/ui/AlphabetMenuList.hx +++ b/source/ui/TextMenuList.hx @@ -1,31 +1,33 @@ package ui; +import ui.AtlasText; import ui.MenuList; -class AlphabetMenuList extends MenuTypedList +class TextMenuList extends MenuTypedList { public function new (navControls:NavControls = Vertical) { super(navControls); } - public function createItem(x = 0.0, y = 0.0, name:String, bold = true, callback, fireInstantly = false) + public function createItem(x = 0.0, y = 0.0, name:String, font:AtlasFont = Bold, callback, fireInstantly = false) { - var item = new AlphabetMenuItem(x, y, name, bold, callback); + var item = new TextMenuItem(x, y, name, font, callback); item.fireInstantly = fireInstantly; return addItem(name, item); } } -class AlphabetMenuItem extends AlphabetTypedMenuItem +class TextMenuItem extends TextTypedMenuItem { - public function new (x = 0.0, y = 0.0, name:String, bold = true, callback) + public function new (x = 0.0, y = 0.0, name:String, font:AtlasFont = Bold, callback) { - super(x, y, new Alphabet(x, y, name, bold), name, callback); + super(x, y, new AtlasText(0, 0, name, font), name, callback); + setEmptyBackground(); } } -class AlphabetTypedMenuItem extends MenuTypedItem +class TextTypedMenuItem extends MenuTypedItem { public function new (x = 0.0, y = 0.0, label:T, name:String, callback) {