1 /** 2 Static text label. 3 Note: this is a very bad widget to copy. Obsolete, but works. 4 In particular, the setters should be script properties. 5 6 Copyright: Copyright Auburn Sounds 2015-2024. 7 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) 8 Authors: Guillaume Piolat 9 */ 10 module dplug.pbrwidgets.label; 11 12 import dplug.gui.element; 13 import dplug.client.params; 14 15 /// Simple area with text. 16 class UILabel : UIElement 17 { 18 public: 19 nothrow: 20 @nogc: 21 22 /// Sets to true if this is clickable 23 @ScriptProperty bool clickable = false; 24 string targetURL = "http://example.com"; 25 26 this(UIContext context, Font font, string text = "") 27 { 28 super(context, flagAnimated | flagPBR); 29 _text = text; 30 _font = font; 31 setCursorWhenMouseOver(MouseCursor.linkSelect); 32 } 33 34 /// Returns: Font used. 35 Font font() 36 { 37 return _font; 38 } 39 40 /// Sets text size. 41 Font font(Font font_) 42 { 43 setDirtyWhole(); 44 return _font = font_; 45 } 46 47 /// Returns: Displayed text. 48 string text() 49 { 50 return _text; 51 } 52 53 /// Sets displayed text. 54 string text(string text_) 55 { 56 setDirtyWhole(); 57 return _text = text_; 58 } 59 60 /// Returns: Size of displayed text. 61 float textSize() 62 { 63 return _textSize; 64 } 65 66 /// Sets size of displayed text. 67 float textSize(float textSize_) 68 { 69 setDirtyWhole(); 70 return _textSize = textSize_; 71 } 72 73 float letterSpacing() 74 { 75 return _letterSpacing; 76 } 77 78 float letterSpacing(float letterSpacing_) 79 { 80 setDirtyWhole(); 81 return _letterSpacing = letterSpacing_; 82 } 83 84 /// Returns: Diffuse color of displayed text. 85 RGBA textColor() 86 { 87 return _textColor; 88 } 89 90 /// Sets diffuse color of displayed text. 91 RGBA textColor(RGBA textColor_) 92 { 93 setDirtyWhole(); 94 return _textColor = textColor_; 95 } 96 97 override void onBeginDrag() 98 { 99 if (clickable) 100 setDirtyWhole(); 101 } 102 103 override void onStopDrag() 104 { 105 if (clickable) 106 setDirtyWhole(); 107 } 108 109 override void onMouseEnter() 110 { 111 if (clickable) 112 setDirtyWhole(); 113 } 114 115 override void onMouseExit() 116 { 117 if (clickable) 118 setDirtyWhole(); 119 } 120 121 override Click onMouseClick(int x, int y, int button, bool isDoubleClick, MouseState mstate) 122 { 123 if (clickable) 124 { 125 browseNoGC(targetURL); 126 return Click.startDrag; 127 } 128 return Click.unhandled; 129 } 130 131 override void onDrawPBR(ImageRef!RGBA diffuseMap, ImageRef!L16 depthMap, ImageRef!RGBA materialMap, box2i[] dirtyRects) nothrow @nogc 132 { 133 float textPosx = position.width * 0.5f; 134 float textPosy = position.height * 0.5f; 135 // only draw text which is in dirty areas 136 137 RGBA diffuse = _textColor; 138 int emissive = _textColor.a; 139 bool underline = false; 140 141 if (clickable && isMouseOver) 142 { 143 emissive += 40; 144 underline = true; 145 } 146 else if (clickable && isDragged) 147 { 148 emissive += 80; 149 underline = true; 150 } 151 if (emissive > 255) 152 emissive = 255; 153 diffuse.a = cast(ubyte)(emissive); 154 155 // MAYDO: implement underline? 156 157 foreach(dirtyRect; dirtyRects) 158 { 159 auto croppedDiffuse = diffuseMap.cropImageRef(dirtyRect); 160 vec2f positionInDirty = vec2f(textPosx, textPosy) - dirtyRect.min; 161 croppedDiffuse.fillText(_font, _text, _textSize, _letterSpacing, diffuse, positionInDirty.x, positionInDirty.y); 162 } 163 } 164 165 protected: 166 167 /// The font used for text. 168 Font _font; 169 170 /// Text to draw 171 string _text; 172 173 /// Size of displayed text in pixels. 174 float _textSize = 16.0f; 175 176 /// Additional space between letters, in pixels. 177 float _letterSpacing = 0.0f; 178 179 /// Diffuse color of displayed text. 180 RGBA _textColor = RGBA(0, 0, 0, 0); 181 }