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) draw_beginBoldFont();
21
22 float spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize);
23
24 if (spaceUsed <= spaceAvail)
25 {
26 if (!me.overrideRealOrigin_x) me.realOrigin_x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft;
27 if (!me.overrideCondenseFactor) me.condenseFactor = 1;
28 }
29 else if (me.allowCut || me.allowWrap)
30 {
31 if (!me.overrideRealOrigin_x) me.realOrigin_x = me.keepspaceLeft;
32 if (!me.overrideCondenseFactor) me.condenseFactor = 1;
33 }
34 else
35 {
36 if (!me.overrideRealOrigin_x) me.realOrigin_x = me.keepspaceLeft;
37 if (!me.overrideCondenseFactor) me.condenseFactor = spaceAvail / spaceUsed;
38 LOG_TRACEF("NOTE: label text %s too wide for label, condensed by factor %f", t, me.condenseFactor);
39 }
40
41 if (!me.overrideRealOrigin_y)
42 {
43 float lines;
44 vector dfs;
45 vector fs;
46
47 // set up variables to draw in condensed size, but use hinting for original size
48 fs = me.realFontSize;
49 fs.x *= me.condenseFactor;
50
51 dfs = draw_fontscale;
52 draw_fontscale.x *= me.condenseFactor;
53
54 if (me.allowCut) // FIXME allowCut incompatible with align != 0
55 {
56 lines = 1;
57 }
58 else if (me.allowWrap) // FIXME allowWrap incompatible with align != 0
59 {
61 lines = 0;
63 {
64 if (me.allowColors) getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
65 else getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
66 ++lines;
67 }
68 }
69 else
70 {
71 lines = 1;
72 }
73
74 draw_fontscale = dfs;
75
76 float text_height = lines * me.realFontSize.y;
77 if (lines > 1)
78 text_height += (lines - 1) * me.realFontSize.y * me.allowWrap_spacing;
79 me.realOrigin_y = 0.5 * (1 - text_height);
80 }
81
82 if (me.isBold) draw_endBoldFont();
83
84 me.recalcPos = false;
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 = true;
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 }
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 strcpy(me.currentText, t);
116 me.recalcPos = true;
117 }
118 }
119 else
120 {
121 t = me.text;
122 }
123
124 if (me.recalcPos) me.recalcPositionWithText(me, t);
125
126 if (me.fontSize)
127 if (t)
128 {
129 vector dfs;
130 vector fs;
131
132 if (me.isBold) draw_beginBoldFont();
133
134 // set up variables to draw in condensed size, but use hinting for original size
135 fs = me.realFontSize;
136 fs.x *= me.condenseFactor;
137
138 dfs = draw_fontscale;
139 draw_fontscale.x *= me.condenseFactor;
140
141 if (me.allowCut) // FIXME allowCut incompatible with align != 0
142 {
143 draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs), fs, me.colorL, me.alpha, me.allowColors);
144 }
145 else if (me.allowWrap) // FIXME allowWrap incompatible with align != 0
146 {
148 o = me.realOrigin;
150 {
151 if (me.allowColors) t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
152 else t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
153 draw_Text(o, t, fs, me.colorL, me.alpha, me.allowColors);
154 o.y += me.realFontSize.y * (1 + me.allowWrap_spacing);
155 }
156 }
157 else
158 {
159 draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
160 }
161
162 draw_fontscale = dfs;
163
164 if (me.isBold) draw_endBoldFont();
165 }
166
167 SUPER(Label).draw(me);
168 }
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:1131
string getWrappedLine_remaining
Definition util.qh:147
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:86
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:98
void Label_draw(entity me)
Definition label.qc:104
#define LOG_TRACEF(...)
Definition log.qh:77
string draw_TextShortenToWidth(string theText, float maxWidth, float ICanHasKallerz, vector SizeThxBye)
Definition draw.qc:378
float draw_TextWidth_WithoutColors(string s, vector theFontSize)
Definition draw.qc:397
void draw_Text(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, float ICanHasKallerz)
Definition draw.qc:282
float draw_TextWidth(string theText, float ICanHasKallerz, vector SizeThxBye)
Definition draw.qc:304
float draw_TextWidth_WithColors(string s, vector theFontSize)
Definition draw.qc:392
#define draw_fontscale
Definition draw.qh:5
float draw_alpha
Definition draw.qh:9
#define SUPER(cname)
Definition oo.qh:231
vector
Definition self.qh:92
#define strcpy(this, s)
Definition string.qh:52