Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
label.qc
Go to the documentation of this file.
1#include "label.qh"
2
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 = true;
14 }
15 }
17 {
18 float spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight;
19
20 if (me.isBold)
22
23 float spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize);
24
25 if (spaceUsed <= spaceAvail)
26 {
27 if (!me.overrideRealOrigin.x)
28 me.realOrigin.x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft;
29 if (!me.overrideCondenseFactor)
30 me.condenseFactor = 1;
31 }
32 else if (me.allowCut || me.allowWrap)
33 {
34 if (!me.overrideRealOrigin.x)
35 me.realOrigin.x = me.keepspaceLeft;
36 if (!me.overrideCondenseFactor)
37 me.condenseFactor = 1;
38 }
39 else
40 {
41 if (!me.overrideRealOrigin.x)
42 me.realOrigin.x = me.keepspaceLeft;
43 if (!me.overrideCondenseFactor)
44 me.condenseFactor = spaceAvail / spaceUsed;
45 LOG_TRACEF("NOTE: label text %s too wide for label, condensed by factor %f", t, me.condenseFactor);
46 }
47
48 if (!me.overrideRealOrigin.y)
49 {
50 float lines;
51 vector dfs;
52 vector fs;
53
54 // set up variables to draw in condensed size, but use hinting for original size
55 fs = me.realFontSize;
56 fs.x *= me.condenseFactor;
57
58 dfs = draw_fontscale;
59 draw_fontscale.x *= me.condenseFactor;
60
61 if (me.allowCut) // FIXME allowCut incompatible with align != 0
62 lines = 1;
63 else if (me.allowWrap) // FIXME allowWrap incompatible with align != 0
64 {
66 lines = 0;
68 {
69 if (me.allowColors) getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
70 else getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
71 ++lines;
72 }
73 }
74 else
75 lines = 1;
76
77 draw_fontscale = dfs;
78
79 float text_height = lines * me.realFontSize.y;
80 if (lines > 1)
81 text_height += (lines - 1) * me.realFontSize.y * me.allowWrap_spacing;
82 me.realOrigin.y = 0.5 * (1 - text_height);
83 }
84
85 if (me.isBold) draw_endBoldFont();
86
87 me.recalcPos = false;
88 }
89 void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
90 {
91 SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
92
93 // absSize.y is height of label
94 me.realFontSize.y = absSize.y == 0 ? 0 : (me.fontSize / absSize.y);
95 me.realFontSize.x = absSize.x == 0 ? 0 : (me.fontSize / absSize.x);
96 if (me.marginLeft)
97 me.keepspaceLeft = me.marginLeft * me.realFontSize.x;
98 if (me.marginRight)
99 me.keepspaceRight = me.marginRight * me.realFontSize.x;
100
101 me.recalcPos = true;
102 }
103 void Label_configureLabel(entity me, string txt, float sz, float algn)
104 {
105 me.fontSize = sz;
106 me.align = algn;
107 me.setText(me, txt);
108 }
110 {
111 string t;
112 vector o;
113 if (me.disabled)
114 draw_alpha *= me.disabledAlpha;
115
116 if (me.textEntity)
117 {
118 t = me.textEntity.toString(me.textEntity);
119 if (t != me.currentText)
120 {
121 strcpy(me.currentText, t);
122 me.recalcPos = true;
123 }
124 }
125 else
126 t = me.text;
127
128 if (me.recalcPos)
129 me.recalcPositionWithText(me, t);
130
131 if (me.fontSize && t)
132 {
133 if (me.isBold)
135
136 // set up variables to draw in condensed size, but use hinting for original size
137 vector fs = me.realFontSize;
138 fs.x *= me.condenseFactor;
139
141 draw_fontscale.x *= me.condenseFactor;
142
143 if (me.allowCut) // FIXME allowCut incompatible with align != 0
144 {
145 t = draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs);
146 draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
147 }
148 else if (me.allowWrap) // FIXME allowWrap incompatible with align != 0
149 {
151 o = me.realOrigin;
153 {
154 if (me.allowColors)
155 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
156 else
157 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
158 draw_Text(o, t, fs, me.colorL, me.alpha, me.allowColors);
159 o.y += me.realFontSize.y * (1 + me.allowWrap_spacing);
160 }
161 }
162 else
163 draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
164
165 draw_fontscale = dfs;
166
167 if (me.isBold)
169 }
170
171 SUPER(Label).draw(me);
172 }
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
Definition label.qh:4
#define draw_beginBoldFont()
Definition draw.qh:4
#define draw_endBoldFont()
Definition draw.qh:5
string getWrappedLine(float maxWidth, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
Definition util.qc:1133
string getWrappedLine_remaining
Definition util.qh:154
void Label_setText(entity me, string txt)
Definition label.qc:7
void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
Definition label.qc:89
string Label_toString(entity me)
Definition label.qc:3
void Label_recalcPositionWithText(entity me, string t)
Definition label.qc:16
void Label_configureLabel(entity me, string txt, float sz, float algn)
Definition label.qc:103
void Label_draw(entity me)
Definition label.qc:109
#define LOG_TRACEF(...)
Definition log.qh:75
float draw_TextWidth_WithoutColors(string s, vector theFontSize)
Definition draw.qc:382
float draw_TextWidth(string theText, bool allowColorCodes, vector SizeThxBye)
Definition draw.qc:302
string draw_TextShortenToWidth(string theText, float maxWidth, bool allowColorCodes, vector SizeThxBye)
Definition draw.qc:369
void draw_Text(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, bool allowColorCodes)
Definition draw.qc:281
float draw_TextWidth_WithColors(string s, vector theFontSize)
Definition draw.qc:377
#define draw_fontscale
Definition draw.qh:5
float draw_alpha
Definition draw.qh:9
#define SUPER(cname)
Definition oo.qh:236
vector
Definition self.qh:96
#define strcpy(this, s)
Definition string.qh:51