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