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