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