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