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 import re.core : Core; 38 39 this._text = text; 40 this.font = font; 41 this.size = size; 42 this.color = color; 43 update_dimens(); 44 45 raylib.SetTextureFilter(font.texture, Core.default_filter_mode); 46 } 47 48 /// gets text string 49 @property string text() { 50 return _text; 51 } 52 53 /// sets text string 54 @property string text(string value) { 55 _text = value; 56 update_dimens(); 57 return value; 58 } 59 60 @property Rectangle bounds() { 61 return Bounds.calculate(entity.transform, _origin, _text_size.x, _text_size.y); 62 } 63 64 @property private int spacing() { 65 return size / default_size; 66 } 67 68 /// default font (builtin to raylib) 69 @property public static raylib.Font default_font() { 70 return raylib.GetFontDefault(); 71 } 72 73 private void update_dimens() { 74 _text_size = raylib.MeasureTextEx(font, toStringz(_text), size, spacing); 75 // calculate origin 76 auto ori_x = 0.0; 77 auto ori_y = 0.0; 78 if (_horiz_align == Align.Close) 79 ori_x = 0; 80 else if (_horiz_align == Align.Center) 81 ori_x = _text_size.x / 2; 82 else 83 ori_x = _text_size.x; 84 if (_vert_align == Align.Close) 85 ori_y = 0; 86 else if (_vert_align == Align.Center) 87 ori_y = _text_size.y / 2; 88 else 89 ori_y = _text_size.y; 90 _origin = Vector2(ori_x, ori_y); 91 } 92 93 public void set_align(Align horiz, Align vert) { 94 _horiz_align = horiz; 95 _vert_align = vert; 96 update_dimens(); 97 } 98 99 void render() { 100 raylib.DrawTextEx(font, toStringz(_text), entity.position2 - _origin, size, spacing, color); 101 } 102 103 void debug_render() { 104 DebugRender.default_debug_render(this); 105 } 106 }