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