1 /* 2 Copyright © 2020, Luna Nielsen 3 Distributed under the 2-Clause BSD License, see LICENSE file. 4 5 Authors: Luna Nielsen 6 */ 7 module engine.ui.widgets.label; 8 import engine.ui; 9 import engine; 10 11 /** 12 A label 13 */ 14 class Label : Widget { 15 private: 16 17 protected: 18 override { 19 /** 20 Code run when updating the widget 21 */ 22 void onUpdate() { } 23 24 /** 25 Code run when drawing 26 */ 27 void onDraw() { 28 GameFont.changeSize(size); 29 GameFont.draw(this.text, vec2(area.x, area.y)); 30 GameFont.flush(); 31 } 32 } 33 34 public: 35 36 /** 37 The text buffer for the label 38 */ 39 dstring text; 40 41 /** 42 The font size 43 */ 44 int size = 24; 45 46 this(T)(T text, int size = 24) if (isString!T) { 47 super("Label"); 48 49 this.text = text.toEngineString(); 50 this.size = size; 51 } 52 53 /** 54 Set the text of the label 55 This function supports UTF8 text 56 */ 57 void setText(T)(T text) if (isString!T) { 58 this.text = text.toEngineString(); 59 } 60 61 override: 62 63 /** 64 Area of the widget 65 */ 66 vec4 area() { 67 vec4 sArea = scissorArea(); 68 return vec4(sArea.x-4, sArea.y-4, sArea.z+8, sArea.w+8); 69 } 70 71 /** 72 Area in which the widget cuts out child widgets 73 */ 74 vec4i scissorArea() { 75 GameFont.changeSize(size); 76 vec2 measure = GameFont.measure(text); 77 vec2 pos = this.calculatedPosition(); 78 return vec4i( 79 cast(int)pos.x, 80 cast(int)pos.y, 81 cast(int)measure.x, 82 cast(int)measure.y 83 ); 84 } 85 }