2 CLASS(Label) EXTENDS(Item)
3 METHOD(Label, configureLabel, void(entity, string, float, float))
4 METHOD(Label, draw, void(entity))
5 METHOD(Label, resizeNotify, void(entity, vector, vector, vector, vector))
6 METHOD(Label, setText, void(entity, string))
7 METHOD(Label, toString, string(entity))
8 METHOD(Label, recalcPositionWithText, void(entity, string))
9 ATTRIB(Label, isBold, float, 0)
10 ATTRIB(Label, text, string, string_null)
11 ATTRIB(Label, currentText, string, string_null)
12 ATTRIB(Label, fontSize, float, 8)
13 ATTRIB(Label, align, float, 0.5)
14 ATTRIB(Label, allowCut, float, 0)
15 ATTRIB(Label, allowColors, float, 0)
16 ATTRIB(Label, keepspaceLeft, float, 0) // for use by subclasses (radiobuttons for example)
17 ATTRIB(Label, keepspaceRight, float, 0)
18 ATTRIB(Label, marginLeft, float, 0) // alternate way to specify keepspace* (in characters from the font)
19 ATTRIB(Label, marginRight, float, 0)
20 ATTRIB(Label, realFontSize, vector, '0 0 0')
21 ATTRIB(Label, realOrigin, vector, '0 0 0')
22 ATTRIB(Label, alpha, float, 0.7)
23 ATTRIB(Label, colorL, vector, SKINCOLOR_TEXT)
24 ATTRIB(Label, disabled, float, 0)
25 ATTRIB(Label, disabledAlpha, float, 0.3)
26 ATTRIB(Label, textEntity, entity, NULL)
27 ATTRIB(Label, allowWrap, float, 0)
28 ATTRIB(Label, recalcPos, float, 0)
29 ATTRIB(Label, condenseFactor, float, 1)
30 ATTRIB(Label, overrideRealOrigin, vector, '0 0 0')
31 ATTRIB(Label, overrideCondenseFactor, float, 0)
36 string Label_toString(entity me)
40 void Label_setText(entity me, string txt)
43 if(txt != me.currentText)
46 strunzone(me.currentText);
47 me.currentText = strzone(txt);
51 void Label_recalcPositionWithText(entity me, string t)
54 spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight;
60 spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize);
62 if(spaceUsed <= spaceAvail)
64 if(!me.overrideRealOrigin_x)
65 me.realOrigin_x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft;
66 if(!me.overrideCondenseFactor)
67 me.condenseFactor = 1;
69 else if(me.allowCut || me.allowWrap)
71 if(!me.overrideRealOrigin_x)
72 me.realOrigin_x = me.keepspaceLeft;
73 if(!me.overrideCondenseFactor)
74 me.condenseFactor = 1;
78 if(!me.overrideRealOrigin_x)
79 me.realOrigin_x = me.keepspaceLeft;
80 if(!me.overrideCondenseFactor)
81 me.condenseFactor = spaceAvail / spaceUsed;
82 dprintf("NOTE: label text %s too wide for label, condensed by factor %f\n", t, me.condenseFactor);
85 if(!me.overrideRealOrigin_y)
91 // set up variables to draw in condensed size, but use hinting for original size
93 fs_x *= me.condenseFactor;
96 draw_fontscale_x *= me.condenseFactor;
98 if(me.allowCut) // FIXME allowCut incompatible with align != 0
100 else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
102 getWrappedLine_remaining = me.text;
104 while(getWrappedLine_remaining)
107 getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
109 getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
116 draw_fontscale = dfs;
118 me.realOrigin_y = 0.5 * (1 - lines * me.realFontSize_y);
126 void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
128 SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
130 // absSize_y is height of label
131 me.realFontSize_y = me.fontSize / absSize_y;
132 me.realFontSize_x = me.fontSize / absSize_x;
134 me.keepspaceLeft = me.marginLeft * me.realFontSize_x;
136 me.keepspaceRight = me.marginRight * me.realFontSize_x;
140 void Label_configureLabel(entity me, string txt, float sz, float algn)
146 void Label_draw(entity me)
151 draw_alpha *= me.disabledAlpha;
155 t = me.textEntity.toString(me.textEntity);
156 if(t != me.currentText)
159 strunzone(me.currentText);
160 me.currentText = strzone(t);
168 me.recalcPositionWithText(me, t);
177 draw_beginBoldFont();
179 // set up variables to draw in condensed size, but use hinting for original size
180 fs = me.realFontSize;
181 fs_x *= me.condenseFactor;
183 dfs = draw_fontscale;
184 draw_fontscale_x *= me.condenseFactor;
186 if(me.allowCut) // FIXME allowCut incompatible with align != 0
187 draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs), fs, me.colorL, me.alpha, me.allowColors);
188 else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
190 getWrappedLine_remaining = t;
192 while(getWrappedLine_remaining)
195 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
197 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
198 draw_Text(o, t, fs, me.colorL, me.alpha, me.allowColors);
199 o_y += me.realFontSize_y;
203 draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
205 draw_fontscale = dfs;
211 SUPER(Label).draw(me);