]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/label.qc
7148b1f0d49868910ac629bcd0ab66017b87e6e2
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / label.qc
1 #include "label.qh"
2 #ifndef ITEM_LABEL_H
3         #define ITEM_LABEL_H
4         #include "../item.qc"
5         CLASS(Label, Item)
6                 METHOD(Label, configureLabel, void(entity, string, float, float));
7                 METHOD(Label, draw, void(entity));
8                 METHOD(Label, resizeNotify, void(entity, vector, vector, vector, vector));
9                 METHOD(Label, setText, void(entity, string));
10                 METHOD(Label, toString, string(entity));
11                 METHOD(Label, recalcPositionWithText, void(entity, string));
12                 ATTRIB(Label, isBold, float, 0)
13                 ATTRIB(Label, text, string, string_null)
14                 ATTRIB(Label, currentText, string, string_null)
15                 ATTRIB(Label, fontSize, float, 8)
16                 ATTRIB(Label, align, float, 0.5)
17                 ATTRIB(Label, allowCut, float, 0)
18                 ATTRIB(Label, allowColors, float, 0)
19                 ATTRIB(Label, keepspaceLeft, float, 0) // for use by subclasses (radiobuttons for example)
20                 ATTRIB(Label, keepspaceRight, float, 0)
21                 ATTRIB(Label, marginLeft, float, 0)    // alternate way to specify keepspace* (in characters from the font)
22                 ATTRIB(Label, marginRight, float, 0)
23                 ATTRIB(Label, realFontSize, vector, '0 0 0')
24                 ATTRIB(Label, realOrigin, vector, '0 0 0')
25                 ATTRIB(Label, alpha, float, 0.7)
26                 ATTRIB(Label, colorL, vector, SKINCOLOR_TEXT)
27                 ATTRIB(Label, disabled, float, 0)
28                 ATTRIB(Label, disabledAlpha, float, 0.3)
29                 ATTRIB(Label, textEntity, entity, NULL)
30                 ATTRIB(Label, allowWrap, float, 0)
31                 ATTRIB(Label, recalcPos, float, 0)
32                 ATTRIB(Label, condenseFactor, float, 1)
33                 ATTRIB(Label, overrideRealOrigin, vector, '0 0 0')
34                 ATTRIB(Label, overrideCondenseFactor, float, 0)
35         ENDCLASS(Label)
36 #endif
37
38 #ifdef IMPLEMENTATION
39         string Label_toString(entity me)
40         {
41                 return me.text;
42         }
43         void Label_setText(entity me, string txt)
44         {
45                 me.text = txt;
46                 if (txt != me.currentText)
47                 {
48                         if (me.currentText) strunzone(me.currentText);
49                         me.currentText = strzone(txt);
50                         me.recalcPos = 1;
51                 }
52         }
53         void Label_recalcPositionWithText(entity me, string t)
54         {
55                 float spaceAvail;
56                 spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight;
57
58                 if (me.isBold) draw_beginBoldFont();
59
60                 float spaceUsed;
61                 spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize);
62
63                 if (spaceUsed <= spaceAvail)
64                 {
65                         if (!me.overrideRealOrigin_x) me.realOrigin_x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft;
66                         if (!me.overrideCondenseFactor) me.condenseFactor = 1;
67                 }
68                 else if (me.allowCut || me.allowWrap)
69                 {
70                         if (!me.overrideRealOrigin_x) me.realOrigin_x = me.keepspaceLeft;
71                         if (!me.overrideCondenseFactor) me.condenseFactor = 1;
72                 }
73                 else
74                 {
75                         if (!me.overrideRealOrigin_x) me.realOrigin_x = me.keepspaceLeft;
76                         if (!me.overrideCondenseFactor) me.condenseFactor = spaceAvail / spaceUsed;
77                         LOG_TRACEF("NOTE: label text %s too wide for label, condensed by factor %f\n", t, me.condenseFactor);
78                 }
79
80                 if (!me.overrideRealOrigin_y)
81                 {
82                         float lines;
83                         vector dfs;
84                         vector fs;
85
86                         // set up variables to draw in condensed size, but use hinting for original size
87                         fs = me.realFontSize;
88                         fs.x *= me.condenseFactor;
89
90                         dfs = draw_fontscale;
91                         draw_fontscale.x *= me.condenseFactor;
92
93                         if (me.allowCut)  // FIXME allowCut incompatible with align != 0
94                         {
95                                 lines = 1;
96                         }
97                         else if (me.allowWrap)  // FIXME allowWrap incompatible with align != 0
98                         {
99                                 getWrappedLine_remaining = me.text;
100                                 lines = 0;
101                                 while (getWrappedLine_remaining)
102                                 {
103                                         if (me.allowColors) getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
104                                         else getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
105                                         ++lines;
106                                 }
107                         }
108                         else
109                         {
110                                 lines = 1;
111                         }
112
113                         draw_fontscale = dfs;
114
115                         me.realOrigin_y = 0.5 * (1 - lines * me.realFontSize.y);
116                 }
117
118                 if (me.isBold) draw_endBoldFont();
119
120                 me.recalcPos = 0;
121         }
122         void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
123         {
124                 SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
125
126                 // absSize_y is height of label
127                 me.realFontSize_y = absSize.y == 0 ? 0 : (me.fontSize / absSize.y);
128                 me.realFontSize_x = absSize.x == 0 ? 0 : (me.fontSize / absSize.x);
129                 if (me.marginLeft) me.keepspaceLeft = me.marginLeft * me.realFontSize.x;
130                 if (me.marginRight) me.keepspaceRight = me.marginRight * me.realFontSize.x;
131
132                 me.recalcPos = 1;
133         }
134         void Label_configureLabel(entity me, string txt, float sz, float algn)
135         {
136                 me.fontSize = sz;
137                 me.align = algn;
138                 me.setText(me, txt);
139         }
140         void Label_draw(entity me)
141         {
142                 string t;
143                 vector o;
144                 if (me.disabled) draw_alpha *= me.disabledAlpha;
145
146                 if (me.textEntity)
147                 {
148                         t = me.textEntity.toString(me.textEntity);
149                         if (t != me.currentText)
150                         {
151                                 if (me.currentText) strunzone(me.currentText);
152                                 me.currentText = strzone(t);
153                                 me.recalcPos = 1;
154                         }
155                 }
156                 else
157                 {
158                         t = me.text;
159                 }
160
161                 if (me.recalcPos) me.recalcPositionWithText(me, t);
162
163                 if (me.fontSize)
164                         if (t)
165                         {
166                                 vector dfs;
167                                 vector fs;
168
169                                 if (me.isBold) draw_beginBoldFont();
170
171                                 // set up variables to draw in condensed size, but use hinting for original size
172                                 fs = me.realFontSize;
173                                 fs.x *= me.condenseFactor;
174
175                                 dfs = draw_fontscale;
176                                 draw_fontscale.x *= me.condenseFactor;
177
178                                 if (me.allowCut)  // FIXME allowCut incompatible with align != 0
179                                 {
180                                         draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs), fs, me.colorL, me.alpha, me.allowColors);
181                                 }
182                                 else if (me.allowWrap)  // FIXME allowWrap incompatible with align != 0
183                                 {
184                                         getWrappedLine_remaining = t;
185                                         o = me.realOrigin;
186                                         while (getWrappedLine_remaining)
187                                         {
188                                                 if (me.allowColors) t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
189                                                 else t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
190                                                 draw_Text(o, t, fs, me.colorL, me.alpha, me.allowColors);
191                                                 o.y += me.realFontSize.y;
192                                         }
193                                 }
194                                 else
195                                 {
196                                         draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
197                                 }
198
199                                 draw_fontscale = dfs;
200
201                                 if (me.isBold) draw_endBoldFont();
202                         }
203
204                 SUPER(Label).draw(me);
205         }
206 #endif