5 METHOD(Label, configureLabel, void(entity, string, float, float));
6 METHOD(Label, draw, void(entity));
7 METHOD(Label, resizeNotify, void(entity, vector, vector, vector, vector));
8 METHOD(Label, setText, void(entity, string));
9 METHOD(Label, toString, string(entity));
10 METHOD(Label, recalcPositionWithText, void(entity, string));
11 ATTRIB(Label, isBold, float, 0)
12 ATTRIB(Label, text, string, string_null)
13 ATTRIB(Label, currentText, string, string_null)
14 ATTRIB(Label, fontSize, float, 8)
15 ATTRIB(Label, align, float, 0.5)
16 ATTRIB(Label, allowCut, float, 0)
17 ATTRIB(Label, allowColors, float, 0)
18 ATTRIB(Label, keepspaceLeft, float, 0) // for use by subclasses (radiobuttons for example)
19 ATTRIB(Label, keepspaceRight, float, 0)
20 ATTRIB(Label, marginLeft, float, 0) // alternate way to specify keepspace* (in characters from the font)
21 ATTRIB(Label, marginRight, float, 0)
22 ATTRIB(Label, realFontSize, vector, '0 0 0')
23 ATTRIB(Label, realOrigin, vector, '0 0 0')
24 ATTRIB(Label, alpha, float, 0.7)
25 ATTRIB(Label, colorL, vector, SKINCOLOR_TEXT)
26 ATTRIB(Label, disabled, float, 0)
27 ATTRIB(Label, disabledAlpha, float, 0.3)
28 ATTRIB(Label, textEntity, entity, NULL)
29 ATTRIB(Label, allowWrap, float, 0)
30 ATTRIB(Label, recalcPos, float, 0)
31 ATTRIB(Label, condenseFactor, float, 1)
32 ATTRIB(Label, overrideRealOrigin, vector, '0 0 0')
33 ATTRIB(Label, overrideCondenseFactor, float, 0)
38 string Label_toString(entity me)
42 void Label_setText(entity me, string txt)
45 if (txt != me.currentText)
47 if (me.currentText) strunzone(me.currentText);
48 me.currentText = strzone(txt);
52 void Label_recalcPositionWithText(entity me, string t)
55 spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight;
57 if (me.isBold) draw_beginBoldFont();
60 spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize);
62 if (spaceUsed <= spaceAvail)
64 if (!me.overrideRealOrigin_x) me.realOrigin_x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft;
65 if (!me.overrideCondenseFactor) me.condenseFactor = 1;
67 else if (me.allowCut || me.allowWrap)
69 if (!me.overrideRealOrigin_x) me.realOrigin_x = me.keepspaceLeft;
70 if (!me.overrideCondenseFactor) me.condenseFactor = 1;
74 if (!me.overrideRealOrigin_x) me.realOrigin_x = me.keepspaceLeft;
75 if (!me.overrideCondenseFactor) me.condenseFactor = spaceAvail / spaceUsed;
76 LOG_TRACEF("NOTE: label text %s too wide for label, condensed by factor %f\n", t, me.condenseFactor);
79 if (!me.overrideRealOrigin_y)
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
96 else if (me.allowWrap) // FIXME allowWrap incompatible with align != 0
98 getWrappedLine_remaining = me.text;
100 while (getWrappedLine_remaining)
102 if (me.allowColors) getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
103 else getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
112 draw_fontscale = dfs;
114 me.realOrigin_y = 0.5 * (1 - lines * me.realFontSize.y);
117 if (me.isBold) draw_endBoldFont();
121 void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
123 SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
125 // absSize_y is height of label
126 me.realFontSize_y = absSize.y == 0 ? 0 : (me.fontSize / absSize.y);
127 me.realFontSize_x = absSize.x == 0 ? 0 : (me.fontSize / absSize.x);
128 if (me.marginLeft) me.keepspaceLeft = me.marginLeft * me.realFontSize.x;
129 if (me.marginRight) 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)
143 if (me.disabled) draw_alpha *= me.disabledAlpha;
147 t = me.textEntity.toString(me.textEntity);
148 if (t != me.currentText)
150 if (me.currentText) strunzone(me.currentText);
151 me.currentText = strzone(t);
160 if (me.recalcPos) me.recalcPositionWithText(me, t);
168 if (me.isBold) draw_beginBoldFont();
170 // set up variables to draw in condensed size, but use hinting for original size
171 fs = me.realFontSize;
172 fs.x *= me.condenseFactor;
174 dfs = draw_fontscale;
175 draw_fontscale.x *= me.condenseFactor;
177 if (me.allowCut) // FIXME allowCut incompatible with align != 0
179 draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs), fs, me.colorL, me.alpha, me.allowColors);
181 else if (me.allowWrap) // FIXME allowWrap incompatible with align != 0
183 getWrappedLine_remaining = t;
185 while (getWrappedLine_remaining)
187 if (me.allowColors) t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
188 else t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
189 draw_Text(o, t, fs, me.colorL, me.alpha, me.allowColors);
190 o.y += me.realFontSize.y;
195 draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
198 draw_fontscale = dfs;
200 if (me.isBold) draw_endBoldFont();
203 SUPER(Label).draw(me);