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, fontSize, float, 8)
12 ATTRIB(Label, align, float, 0.5)
13 ATTRIB(Label, allowCut, float, 0)
14 ATTRIB(Label, allowColors, float, 0)
15 ATTRIB(Label, keepspaceLeft, float, 0) // for use by subclasses (radiobuttons for example)
16 ATTRIB(Label, keepspaceRight, float, 0)
17 ATTRIB(Label, marginLeft, float, 0) // alternate way to specify keepspace* (in characters from the font)
18 ATTRIB(Label, marginRight, float, 0)
19 ATTRIB(Label, realFontSize, vector, '0 0 0')
20 ATTRIB(Label, realOrigin, vector, '0 0 0')
21 ATTRIB(Label, alpha, float, 0.7)
22 ATTRIB(Label, colorL, vector, '1 1 1')
23 ATTRIB(Label, disabled, float, 0)
24 ATTRIB(Label, disabledAlpha, float, 0.3)
25 ATTRIB(Label, textEntity, entity, NULL)
26 ATTRIB(Label, allowWrap, float, 0)
27 ATTRIB(Label, recalcPos, float, 0)
28 ATTRIB(Label, condenseFactor, float, 1)
29 ATTRIB(Label, overrideRealOrigin, vector, '0 0 0')
30 ATTRIB(Label, overrideCondenseFactor, float, 0)
35 string Label_toString(entity me)
39 void Label_setText(entity me, string txt)
44 void Label_recalcPositionWithText(entity me, string t)
47 spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight;
53 spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize);
55 if(spaceUsed <= spaceAvail)
57 if(!me.overrideRealOrigin_x)
58 me.realOrigin_x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft;
59 if(!me.overrideCondenseFactor)
60 me.condenseFactor = 1;
62 else if(me.allowCut || me.allowWrap)
64 if(!me.overrideRealOrigin_x)
65 me.realOrigin_x = me.keepspaceLeft;
66 if(!me.overrideCondenseFactor)
67 me.condenseFactor = 1;
71 if(!me.overrideRealOrigin_x)
72 me.realOrigin_x = me.keepspaceLeft;
73 if(!me.overrideCondenseFactor)
74 me.condenseFactor = spaceAvail / spaceUsed;
75 dprint(sprintf(_("NOTE: label text %s too wide for label, condensed by factor %f\n"), t, me.condenseFactor));
78 if(!me.overrideRealOrigin_y)
84 // set up variables to draw in condensed size, but use hinting for original size
86 fs_x *= me.condenseFactor;
89 draw_fontscale_x *= me.condenseFactor;
91 if(me.allowCut) // FIXME allowCut incompatible with align != 0
93 else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
95 getWrappedLine_remaining = me.text;
97 while(getWrappedLine_remaining)
100 getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
102 getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
109 draw_fontscale = dfs;
111 me.realOrigin_y = 0.5 * (1 - lines * me.realFontSize_y);
119 void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
121 SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
123 // absSize_y is height of label
124 me.realFontSize_y = me.fontSize / absSize_y;
125 me.realFontSize_x = me.fontSize / absSize_x;
127 me.keepspaceLeft = me.marginLeft * me.realFontSize_x;
129 me.keepspaceRight = me.marginRight * me.realFontSize_x;
133 void Label_configureLabel(entity me, string txt, float sz, float algn)
139 void Label_draw(entity me)
144 draw_alpha *= me.disabledAlpha;
148 t = me.textEntity.toString(me.textEntity);
155 me.recalcPositionWithText(me, t);
164 draw_beginBoldFont();
166 // set up variables to draw in condensed size, but use hinting for original size
167 fs = me.realFontSize;
168 fs_x *= me.condenseFactor;
170 dfs = draw_fontscale;
171 draw_fontscale_x *= me.condenseFactor;
173 if(me.allowCut) // FIXME allowCut incompatible with align != 0
174 draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs), fs, me.colorL, me.alpha, me.allowColors);
175 else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
177 getWrappedLine_remaining = t;
179 while(getWrappedLine_remaining)
182 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
184 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
185 draw_Text(o, t, fs, me.colorL, me.alpha, me.allowColors);
186 o_y += me.realFontSize_y;
190 draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
192 draw_fontscale = dfs;
198 SUPER(Label).draw(me);