add AtlasText cuz Alpabet sux

This commit is contained in:
George FunBook 2021-02-23 18:55:24 -06:00
parent aef55f90c6
commit d9371cc8eb
3 changed files with 154 additions and 11 deletions

140
source/ui/AtlasText.hx Normal file
View file

@ -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<AtlasChar>
{
static var maxHeights = new Map<AtlasFont, Float>();
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;
}

View file

@ -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);
}

View file

@ -1,31 +1,33 @@
package ui;
import ui.AtlasText;
import ui.MenuList;
class AlphabetMenuList extends MenuTypedList<AlphabetMenuItem>
class TextMenuList extends MenuTypedList<TextMenuItem>
{
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<Alphabet>
class TextMenuItem extends TextTypedMenuItem<AtlasText>
{
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<T:Alphabet> extends MenuTypedItem<T>
class TextTypedMenuItem<T:AtlasText> extends MenuTypedItem<T>
{
public function new (x = 0.0, y = 0.0, label:T, name:String, callback)
{