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