1 /// 2 module nanogui.label; 3 4 /* 5 nanogui/label.h -- Text label with an arbitrary font, color, and size 6 7 NanoGUI was developed by Wenzel Jakob <wenzel.jakob@epfl.ch>. 8 The widget drawing code is based on the NanoVG demo application 9 by Mikko Mononen. 10 11 All rights reserved. Use of this source code is governed by a 12 BSD-style license that can be found in the LICENSE.txt file. 13 */ 14 15 import nanogui.widget; 16 import nanogui.common; 17 import nanogui.theme; 18 19 /** 20 * Text label widget. 21 * 22 * The font and color can be customized. When `Widget.fixedWidth`` 23 * is used, the text is wrapped when it surpasses the specified width. 24 */ 25 class Label : Widget 26 { 27 public: 28 this(Widget parent, string caption, string font = "sans", int fontSize = -1) 29 { 30 super(parent); 31 mCaption = caption; 32 mFont = font; 33 if (mTheme) { 34 mFontSize = mTheme.mStandardFontSize; 35 mColor = mTheme.mTextColor; 36 } 37 if (fontSize >= 0) mFontSize = fontSize; 38 } 39 40 /// Get the label's text caption 41 final string caption() const { return mCaption; } 42 /// Set the label's text caption 43 final void caption(string caption) { mCaption = caption; } 44 45 /// Set the currently active font (2 are available by default: 'sans' and 'sans-bold') 46 final void font(string font) { mFont = font; } 47 /// Get the currently active font 48 final string font() const { return mFont; } 49 50 /// Get the label color 51 final Color color() const { return mColor; } 52 /// Set the label color 53 final void color(Color color) { mColor = color; } 54 55 /// Set the `Theme` used to draw this widget 56 override void theme(Theme theme) 57 { 58 Widget.theme(theme); 59 if (mTheme) { 60 mFontSize = mTheme.mStandardFontSize; 61 mColor = mTheme.mTextColor; 62 } 63 } 64 65 /// Compute the size needed to fully display the label 66 override Vector2i preferredSize(NanoContext ctx) const 67 { 68 if (mCaption == "") 69 return Vector2i(); 70 ctx.fontFace(mFont); 71 ctx.fontSize(fontSize()); 72 73 float[4] bounds; 74 if (mFixedSize.x > 0) { 75 NVGTextAlign algn; 76 algn.left = true; 77 algn.top = true; 78 ctx.textAlign(algn); 79 ctx.textBoxBounds(mPos.x, mPos.y, mFixedSize.x, mCaption, bounds); 80 return Vector2i(mFixedSize.x, cast(int) (bounds[3] - bounds[1])); 81 } else { 82 NVGTextAlign algn; 83 algn.left = true; 84 algn.middle = true; 85 ctx.textAlign(algn); 86 return Vector2i( 87 cast(int) ctx.textBounds(0, 0, mCaption, bounds) + 2, 88 fontSize() 89 ); 90 } 91 } 92 93 /// Draw the label 94 override void draw(ref NanoContext ctx) 95 { 96 Widget.draw(ctx); 97 ctx.fontFace(mFont); 98 ctx.fontSize(fontSize()); 99 ctx.fillColor(mColor); 100 if (mFixedSize.x > 0) 101 { 102 NVGTextAlign algn; 103 algn.left = true; 104 algn.top = true; 105 ctx.textAlign(algn); 106 ctx.textBox(mPos.x, mPos.y, mFixedSize.x, mCaption); 107 } else { 108 NVGTextAlign algn; 109 algn.left = true; 110 algn.middle = true; 111 ctx.textAlign(algn); 112 ctx.text(mPos.x, mPos.y + mSize.y * 0.5f, mCaption); 113 } 114 } 115 116 //override void save(Serializer &s) const; 117 //override bool load(Serializer &s); 118 protected: 119 string mCaption; 120 string mFont; 121 Color mColor; 122 }