1 module re.gfx.text; 2 3 import re.ecs; 4 import re.math; 5 import re.ng.diag; 6 import std.string; 7 static import raylib; 8 9 /// renderable text 10 class Text : Component, Renderable2D { 11 private string _text; 12 /// text font 13 public raylib.Font font; 14 /// font size 15 public int size; 16 /// text color 17 public raylib.Color color; 18 private Vector2 _text_size; 19 private Vector2 _origin; 20 /// default font size 21 public enum default_size = 10; 22 private Align _horiz_align; 23 private Align _vert_align; 24 25 /// alignment style 26 public enum Align { 27 /// left or top 28 Close, 29 /// center 30 Center, 31 /// right or bottom 32 Far 33 } 34 35 /// create a new text 36 this(raylib.Font font, string text, int size, raylib.Color color) { 37 this._text = text; 38 this.font = font; 39 this.size = size; 40 this.color = color; 41 update_dimens(); 42 } 43 44 /// gets text string 45 @property string text() { 46 return _text; 47 } 48 49 /// sets text string 50 @property string text(string value) { 51 _text = value; 52 update_dimens(); 53 return value; 54 } 55 56 @property Rectangle bounds() { 57 return RectangleExt.calculate_bounds(entity.position2, _origin, 58 entity.transform.scale2, entity.transform.rotation, _text_size.x, _text_size.y); 59 } 60 61 @property private int spacing() { 62 return size / default_size; 63 } 64 65 /// default font (builtin to raylib) 66 @property public static raylib.Font default_font() { 67 return raylib.GetFontDefault(); 68 } 69 70 private void update_dimens() { 71 _text_size = raylib.MeasureTextEx(font, toStringz(_text), size, spacing); 72 // calculate origin 73 auto ori_x = 0.0; 74 auto ori_y = 0.0; 75 if (_horiz_align == Align.Close) 76 ori_x = 0; 77 else if (_horiz_align == Align.Center) 78 ori_x = _text_size.x / 2; 79 else 80 ori_x = _text_size.x; 81 if (_vert_align == Align.Close) 82 ori_y = 0; 83 else if (_vert_align == Align.Center) 84 ori_y = _text_size.y / 2; 85 else 86 ori_y = _text_size.y; 87 _origin = Vector2(ori_x, ori_y); 88 } 89 90 public void set_align(Align horiz, Align vert) { 91 _horiz_align = horiz; 92 _vert_align = vert; 93 update_dimens(); 94 } 95 96 void render() { 97 raylib.DrawTextEx(font, toStringz(_text), entity.position2 - _origin, size, spacing, color); 98 } 99 100 void debug_render() { 101 DebugRender.default_debug_render(this); 102 } 103 }