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, recalcPosition, void(entity))
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)
32 string Label_toString(entity me)
36 void Label_setText(entity me, string txt)
41 void Label_recalcPosition(entity me)
44 spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight;
47 spaceUsed = draw_TextWidth(me.text, me.allowColors, me.realFontSize);
49 if(spaceUsed <= spaceAvail)
51 me.realOrigin_x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft;
52 me.condenseFactor = 1;
54 else if(me.allowCut || me.allowWrap)
56 me.realOrigin_x = me.keepspaceLeft;
57 me.condenseFactor = 1;
61 me.realOrigin_x = me.keepspaceLeft;
62 me.condenseFactor = spaceAvail / spaceUsed;
63 dprint(sprintf(_("NOTE: label text %s too wide for label, condensed by factor %f\n"), me.text, me.condenseFactor));
67 void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
69 SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
70 // absSize_y is height of label
71 me.realFontSize_y = me.fontSize / absSize_y;
72 me.realFontSize_x = me.fontSize / absSize_x;
74 me.keepspaceLeft = me.marginLeft * me.realFontSize_x;
76 me.keepspaceRight = me.marginRight * me.realFontSize_x;
78 me.recalcPosition(me);
85 // set up variables to draw in condensed size, but use hinting for original size
87 fs_x *= me.condenseFactor;
90 draw_fontscale_x *= me.condenseFactor;
92 if(me.allowCut) // FIXME allowCut incompatible with align != 0
94 else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
96 getWrappedLine_remaining = me.text;
98 while(getWrappedLine_remaining)
101 getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
103 getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
110 draw_fontscale = dfs;
112 me.realOrigin_y = 0.5 * (1 - lines * me.realFontSize_y);
114 void Label_configureLabel(entity me, string txt, float sz, float algn)
120 void Label_draw(entity me)
125 draw_alpha *= me.disabledAlpha;
129 t = me.textEntity.toString(me.textEntity);
136 me.recalcPosition(me);
144 // set up variables to draw in condensed size, but use hinting for original size
145 fs = me.realFontSize;
146 fs_x *= me.condenseFactor;
148 dfs = draw_fontscale;
149 draw_fontscale_x *= me.condenseFactor;
151 if(me.allowCut) // FIXME allowCut incompatible with align != 0
152 draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs), fs, me.colorL, me.alpha, me.allowColors);
153 else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
155 getWrappedLine_remaining = t;
157 while(getWrappedLine_remaining)
160 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
162 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
163 draw_Text(o, t, fs, me.colorL, me.alpha, me.allowColors);
164 o_y += me.realFontSize_y;
168 draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
170 draw_fontscale = dfs;