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, text, string, string_null)
10 ATTRIB(Label, fontSize, float, 8)
11 ATTRIB(Label, align, float, 0.5)
12 ATTRIB(Label, allowCut, float, 0)
13 ATTRIB(Label, allowColors, float, 0)
14 ATTRIB(Label, keepspaceLeft, float, 0) // for use by subclasses (radiobuttons for example)
15 ATTRIB(Label, keepspaceRight, float, 0)
16 ATTRIB(Label, marginLeft, float, 0) // alternate way to specify keepspace* (in characters from the font)
17 ATTRIB(Label, marginRight, float, 0)
18 ATTRIB(Label, realFontSize, vector, '0 0 0')
19 ATTRIB(Label, realOrigin, vector, '0 0 0')
20 ATTRIB(Label, alpha, float, 0.7)
21 ATTRIB(Label, colorL, vector, '1 1 1')
22 ATTRIB(Label, disabled, float, 0)
23 ATTRIB(Label, disabledAlpha, float, 0.3)
24 ATTRIB(Label, textEntity, entity, NULL)
25 ATTRIB(Label, allowWrap, float, 0)
26 ATTRIB(Label, recalcPos, float, 0)
27 ATTRIB(Label, condenseFactor, float, 1)
28 ATTRIB(Label, overrideRealOrigin, vector, '0 0 0')
29 ATTRIB(Label, overrideCondenseFactor, float, 0)
34 string Label_toString(entity me)
38 void Label_setText(entity me, string txt)
43 void Label_recalcPositionWithText(entity me, string t)
46 spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight;
49 spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize);
51 if(spaceUsed <= spaceAvail)
53 if(!me.overrideRealOrigin_x)
54 me.realOrigin_x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft;
55 if(!me.overrideCondenseFactor)
56 me.condenseFactor = 1;
58 else if(me.allowCut || me.allowWrap)
60 if(!me.overrideRealOrigin_x)
61 me.realOrigin_x = me.keepspaceLeft;
62 if(!me.overrideCondenseFactor)
63 me.condenseFactor = 1;
67 if(!me.overrideRealOrigin_x)
68 me.realOrigin_x = me.keepspaceLeft;
69 if(!me.overrideCondenseFactor)
70 me.condenseFactor = spaceAvail / spaceUsed;
71 dprint(sprintf(_("NOTE: label text %s too wide for label, condensed by factor %f\n"), t, me.condenseFactor));
74 if(!me.overrideRealOrigin_y)
80 // set up variables to draw in condensed size, but use hinting for original size
82 fs_x *= me.condenseFactor;
85 draw_fontscale_x *= me.condenseFactor;
87 if(me.allowCut) // FIXME allowCut incompatible with align != 0
89 else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
91 getWrappedLine_remaining = me.text;
93 while(getWrappedLine_remaining)
96 getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
98 getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
105 draw_fontscale = dfs;
107 me.realOrigin_y = 0.5 * (1 - lines * me.realFontSize_y);
112 void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
114 SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
116 // absSize_y is height of label
117 me.realFontSize_y = me.fontSize / absSize_y;
118 me.realFontSize_x = me.fontSize / absSize_x;
120 me.keepspaceLeft = me.marginLeft * me.realFontSize_x;
122 me.keepspaceRight = me.marginRight * me.realFontSize_x;
126 void Label_configureLabel(entity me, string txt, float sz, float algn)
132 void Label_draw(entity me)
137 draw_alpha *= me.disabledAlpha;
141 t = me.textEntity.toString(me.textEntity);
148 me.recalcPositionWithText(me, t);
156 // set up variables to draw in condensed size, but use hinting for original size
157 fs = me.realFontSize;
158 fs_x *= me.condenseFactor;
160 dfs = draw_fontscale;
161 draw_fontscale_x *= me.condenseFactor;
163 if(me.allowCut) // FIXME allowCut incompatible with align != 0
164 draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs), fs, me.colorL, me.alpha, me.allowColors);
165 else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
167 getWrappedLine_remaining = t;
169 while(getWrappedLine_remaining)
172 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
174 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
175 draw_Text(o, t, fs, me.colorL, me.alpha, me.allowColors);
176 o_y += me.realFontSize_y;
180 draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
182 draw_fontscale = dfs;
185 SUPER(Label).draw(me);