]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/label.qc
Merge branch 'master' into Mario/vaporizer_damage
[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)
48                         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)
59                 draw_beginBoldFont();
60
61         float spaceUsed;
62         spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize);
63
64         if(spaceUsed <= spaceAvail)
65         {
66                 if(!me.overrideRealOrigin_x)
67                         me.realOrigin_x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft;
68                 if(!me.overrideCondenseFactor)
69                         me.condenseFactor = 1;
70         }
71         else if(me.allowCut || me.allowWrap)
72         {
73                 if(!me.overrideRealOrigin_x)
74                         me.realOrigin_x = me.keepspaceLeft;
75                 if(!me.overrideCondenseFactor)
76                         me.condenseFactor = 1;
77         }
78         else
79         {
80                 if(!me.overrideRealOrigin_x)
81                         me.realOrigin_x = me.keepspaceLeft;
82                 if(!me.overrideCondenseFactor)
83                         me.condenseFactor = spaceAvail / spaceUsed;
84                 LOG_TRACEF("NOTE: label text %s too wide for label, condensed by factor %f\n", t, me.condenseFactor);
85         }
86
87         if(!me.overrideRealOrigin_y)
88         {
89                 float lines;
90                 vector dfs;
91                 vector fs;
92
93                 // set up variables to draw in condensed size, but use hinting for original size
94                 fs = me.realFontSize;
95                 fs.x *= me.condenseFactor;
96
97                 dfs = draw_fontscale;
98                 draw_fontscale.x *= me.condenseFactor;
99
100                 if(me.allowCut) // FIXME allowCut incompatible with align != 0
101                         lines = 1;
102                 else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
103                 {
104                         getWrappedLine_remaining = me.text;
105                         lines = 0;
106                         while(getWrappedLine_remaining)
107                         {
108                                 if (me.allowColors)
109                                         getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
110                                 else
111                                         getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
112                                 ++lines;
113                         }
114                 }
115                 else
116                         lines = 1;
117
118                 draw_fontscale = dfs;
119
120                 me.realOrigin_y = 0.5 * (1 - lines * me.realFontSize.y);
121         }
122
123         if(me.isBold)
124                 draw_endBoldFont();
125
126         me.recalcPos = 0;
127 }
128 void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
129 {
130         SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
131
132         // absSize_y is height of label
133         me.realFontSize_y = me.fontSize / absSize.y;
134         me.realFontSize_x = me.fontSize / absSize.x;
135         if(me.marginLeft)
136                 me.keepspaceLeft = me.marginLeft * me.realFontSize.x;
137         if(me.marginRight)
138                 me.keepspaceRight = me.marginRight * me.realFontSize.x;
139
140         me.recalcPos = 1;
141 }
142 void Label_configureLabel(entity me, string txt, float sz, float algn)
143 {
144         me.fontSize = sz;
145         me.align = algn;
146         me.setText(me, txt);
147 }
148 void Label_draw(entity me)
149 {
150         string t;
151         vector o;
152         if(me.disabled)
153                 draw_alpha *= me.disabledAlpha;
154
155         if(me.textEntity)
156         {
157                 t = me.textEntity.toString(me.textEntity);
158                 if(t != me.currentText)
159                 {
160                         if(me.currentText)
161                                 strunzone(me.currentText);
162                         me.currentText = strzone(t);
163                         me.recalcPos = 1;
164                 }
165         }
166         else
167                 t = me.text;
168
169         if(me.recalcPos)
170                 me.recalcPositionWithText(me, t);
171
172         if(me.fontSize)
173                 if(t)
174                 {
175                         vector dfs;
176                         vector fs;
177
178                         if(me.isBold)
179                                 draw_beginBoldFont();
180
181                         // set up variables to draw in condensed size, but use hinting for original size
182                         fs = me.realFontSize;
183                         fs.x *= me.condenseFactor;
184
185                         dfs = draw_fontscale;
186                         draw_fontscale.x *= me.condenseFactor;
187
188                         if(me.allowCut) // FIXME allowCut incompatible with align != 0
189                                 draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs), fs, me.colorL, me.alpha, me.allowColors);
190                         else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
191                         {
192                                 getWrappedLine_remaining = t;
193                                 o = me.realOrigin;
194                                 while(getWrappedLine_remaining)
195                                 {
196                                         if (me.allowColors)
197                                                 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
198                                         else
199                                                 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
200                                         draw_Text(o, t, fs, me.colorL, me.alpha, me.allowColors);
201                                         o.y += me.realFontSize.y;
202                                 }
203                         }
204                         else
205                                 draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
206
207                         draw_fontscale = dfs;
208
209                         if(me.isBold)
210                                 draw_endBoldFont();
211                 }
212
213         SUPER(Label).draw(me);
214 }
215 #endif