]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/label.qc
Merge branch 'master' into Mario/bulldozer
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / label.qc
1 #ifndef ITEM_LABEL_H
2         #define ITEM_LABEL_H
3         #include "../item.qc"
4         CLASS(Label, Item)
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)
34         ENDCLASS(Label)
35 #endif
36
37 #ifdef IMPLEMENTATION
38         string Label_toString(entity me)
39         {
40                 return me.text;
41         }
42         void Label_setText(entity me, string txt)
43         {
44                 me.text = txt;
45                 if (txt != me.currentText)
46                 {
47                         if (me.currentText) strunzone(me.currentText);
48                         me.currentText = strzone(txt);
49                         me.recalcPos = 1;
50                 }
51         }
52         void Label_recalcPositionWithText(entity me, string t)
53         {
54                 float spaceAvail;
55                 spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight;
56
57                 if (me.isBold) draw_beginBoldFont();
58
59                 float spaceUsed;
60                 spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize);
61
62                 if (spaceUsed <= spaceAvail)
63                 {
64                         if (!me.overrideRealOrigin_x) me.realOrigin_x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft;
65                         if (!me.overrideCondenseFactor) me.condenseFactor = 1;
66                 }
67                 else if (me.allowCut || me.allowWrap)
68                 {
69                         if (!me.overrideRealOrigin_x) me.realOrigin_x = me.keepspaceLeft;
70                         if (!me.overrideCondenseFactor) me.condenseFactor = 1;
71                 }
72                 else
73                 {
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);
77                 }
78
79                 if (!me.overrideRealOrigin_y)
80                 {
81                         float lines;
82                         vector dfs;
83                         vector fs;
84
85                         // set up variables to draw in condensed size, but use hinting for original size
86                         fs = me.realFontSize;
87                         fs.x *= me.condenseFactor;
88
89                         dfs = draw_fontscale;
90                         draw_fontscale.x *= me.condenseFactor;
91
92                         if (me.allowCut)  // FIXME allowCut incompatible with align != 0
93                         {
94                                 lines = 1;
95                         }
96                         else if (me.allowWrap)  // FIXME allowWrap incompatible with align != 0
97                         {
98                                 getWrappedLine_remaining = me.text;
99                                 lines = 0;
100                                 while (getWrappedLine_remaining)
101                                 {
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);
104                                         ++lines;
105                                 }
106                         }
107                         else
108                         {
109                                 lines = 1;
110                         }
111
112                         draw_fontscale = dfs;
113
114                         me.realOrigin_y = 0.5 * (1 - lines * me.realFontSize.y);
115                 }
116
117                 if (me.isBold) draw_endBoldFont();
118
119                 me.recalcPos = 0;
120         }
121         void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
122         {
123                 SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
124
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;
130
131                 me.recalcPos = 1;
132         }
133         void Label_configureLabel(entity me, string txt, float sz, float algn)
134         {
135                 me.fontSize = sz;
136                 me.align = algn;
137                 me.setText(me, txt);
138         }
139         void Label_draw(entity me)
140         {
141                 string t;
142                 vector o;
143                 if (me.disabled) draw_alpha *= me.disabledAlpha;
144
145                 if (me.textEntity)
146                 {
147                         t = me.textEntity.toString(me.textEntity);
148                         if (t != me.currentText)
149                         {
150                                 if (me.currentText) strunzone(me.currentText);
151                                 me.currentText = strzone(t);
152                                 me.recalcPos = 1;
153                         }
154                 }
155                 else
156                 {
157                         t = me.text;
158                 }
159
160                 if (me.recalcPos) me.recalcPositionWithText(me, t);
161
162                 if (me.fontSize)
163                         if (t)
164                         {
165                                 vector dfs;
166                                 vector fs;
167
168                                 if (me.isBold) draw_beginBoldFont();
169
170                                 // set up variables to draw in condensed size, but use hinting for original size
171                                 fs = me.realFontSize;
172                                 fs.x *= me.condenseFactor;
173
174                                 dfs = draw_fontscale;
175                                 draw_fontscale.x *= me.condenseFactor;
176
177                                 if (me.allowCut)  // FIXME allowCut incompatible with align != 0
178                                 {
179                                         draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs), fs, me.colorL, me.alpha, me.allowColors);
180                                 }
181                                 else if (me.allowWrap)  // FIXME allowWrap incompatible with align != 0
182                                 {
183                                         getWrappedLine_remaining = t;
184                                         o = me.realOrigin;
185                                         while (getWrappedLine_remaining)
186                                         {
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;
191                                         }
192                                 }
193                                 else
194                                 {
195                                         draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
196                                 }
197
198                                 draw_fontscale = dfs;
199
200                                 if (me.isBold) draw_endBoldFont();
201                         }
202
203                 SUPER(Label).draw(me);
204         }
205 #endif