Introduction
This is an optimised version of the version 1 back in september 2022
with no dependence on Font and BitmapFont because they are merged so that we can further optimise the text drawing process in chronicles/journal notes (See Gemcraft Labyrinth for some refereneces, Chronicles Page) in all the proposed games. as well as the debug drawings and diagnois
learn more about the diagnois https://stencyl-nator.blogspot.com/p/diagnosis-drawing.html
Reverse-engineered from Aside from the G.
com.stencyl.models.Font
com.stencyl.graphics.BitmapFont
com.stencyl.graphics.FontSymbol
CODE
package scripts; //the default package for all scripts
import com.stencyl.Engine;
import com.stencyl.utils.Assets;
import openfl.display.Graphics;
import openfl.display.BitmapData;
import openfl.display.Tile;
import openfl.display.Tilemap;
import openfl.display.Tileset;
import openfl.geom.ColorTransform;
import openfl.geom.Matrix;
import openfl.geom.Rectangle;
class Dtext
{
public var graphi = Engine.engine.transitionLayer.graphics;
public var alpha:Float =1; public var red_pwr:Int = 255;
public var gre_pwr:Int = 255; public var blu_pwr:Int = 255;
public var scale:Float =1; public var bolt:Int = 0;
public var lesp: Int=-1; public var lisp:Int=0;
public var angle:Int=0;
public var tmap: Tilemap; public var tset:Tileset; public var text_height:Int=1;
public var fs_offsx:Array<Int>=[]; public var fs_offsy:Array<Int>=[]; public var fs_char_width:Array<Int>=[]; public var fs_tile_id:Array<Int>=[];
public var matr:Matrix = new Matrix();
public function new()
{
this.set_font(4); // in this case the first font id is 4, varies from project to project.
}
public function drawString(s:String,x:Float,y:Float) // reverse-engineered from G
{
if(s == ""){return;}
this.matr.identity(); this.matr.translate(x,y);
var toDraw:BitmapData = null;
var w: Int = this.get_text_width(s);
var h: Int = this.text_height;
if(w > 0 && h > 0)
{
toDraw = new BitmapData(w,h+1,true, 0); // h has adjustments if the test is not fully displayed
this.render_to_img(toDraw,s);
}
if(toDraw !=null)
{
this.graphi.beginBitmapFill(toDraw, this.matr, false, true);
this.graphi.drawRect(x,y, toDraw.width, toDraw.height);
this.graphi.endFill();
}
}
public function get_text_width(s:String):Int
{
if(s == ""){return 0;}
var length:Int = s.length; var a:Int = 0; var w:Int = 0; var charcode:Int; var char_width:Int;
while(a < length)
{
charcode = s.charCodeAt(a);
if(this.fs_char_width[charcode] == null ||""+this.fs_char_width[charcode] == "undefined"){char_width =1;}
else{char_width = this.fs_char_width[charcode];}
w += char_width + this.lesp;
a = a+1;
}
return w;
}
public function render(tm:Tilemap,s:String):Void
{
var a:Int =0; var length:Int = s.length; var x:Float =0;
while(a < length)
{
var charCode:Int = s.charCodeAt(a);
if(this.fs_char_width[charCode] !=null && ""+this.fs_char_width[charCode] != "undefined")
{
if (charCode != 32)
{
var t: Tile = new Tile(this.fs_tile_id[charCode], x + this.fs_offsx[charCode] * this.scale, this.fs_offsy[charCode] *this.scale);
t.scaleX = t.scaleY = this.scale;
tm.addTile(t);
}
x += this.fs_char_width[charCode] * this.scale + this.lesp;
}
else {x +=1 + this.lesp; }
a = a+1;
}
}
public function render_to_img(b:BitmapData,s:String):Void
{
var tilmap:Tilemap = new Tilemap(b.width,b.height,this.tset,true);
this.render(tilmap,s);
b.draw(tilmap,null,new ColorTransform(this.red_pwr,this.gre_pwr,this.blu_pwr));
tilmap.removeTiles();
}
public function set_font(ID:Int):Void // this id can be gotten from Design mode get font snippet then get
{
var text_bytes = Assets.getText('assets/graphics/${Engine.IMG_BASE}/font-$ID.fnt');
var xml = Xml.parse(text_bytes);
var img = Assets.getBitmapData("assets/graphics/" + Engine.IMG_BASE + "/font-" + ID + ".png",false);
//load Angel code from BitmapFont
this.tset = new Tileset(img);
var chars:Xml = null; var rect:Rectangle = new Rectangle();
var x:Int; var y:Int; var charwidth:Int;
var charCode:Int; var charString:String;
for (node in xml.elements())
{
if (node.nodeName == "font")
{
for (nodeChild in node.elements())
{
if (nodeChild.nodeName == "common"){this.lisp = Std.parseInt(nodeChild.get("lineHeight"));}
else if (nodeChild.nodeName == "chars"){chars = nodeChild;}
}
}
}
if(chars != null)
{ var tid:Int =0;
for (node in chars.elements())
{
if (node.nodeName == "char")
{
rect.x = Std.parseInt(node.get("x")); rect.y = Std.parseInt(node.get("y"));
rect.width = Std.parseInt(node.get("width"));
rect.height = Std.parseInt(node.get("height"));
x = Std.parseInt(node.get("xoffset")); y = Std.parseInt(node.get("yoffset"));
charCode = Std.parseInt(node.get("id")); charString = String.fromCharCode(charCode);
charwidth = Std.parseInt(node.get("xadvance"));
if (charString != " " && charString != ""){this.set_glyph(charCode, rect, Math.floor(x), Math.floor(y),tid, charwidth);}
else{set_glyph(charCode, rect, Math.floor(x), 1,tid, charwidth);}
}
tid +=1;
}
}
}
public function set_glyph(carcod:Int,r:Rectangle,x,y,tid:Int,charwidth:Int):Void
{
if(r.width == 0){r.width = 1;} if(r.height == 0){r.height = 1;}
this.tset.addRect(r);
this.fs_char_width[carcod] = charwidth;
this.fs_offsx[carcod] = x; this.fs_offsy[carcod] = y;
this.fs_tile_id[carcod] = tid;
if(r.height > 0)
{
if(r.height > this.text_height){this.text_height = Std.int(r.height);}
}
}
}
No comments:
Post a Comment