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