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