]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/label.c
Merge remote branch 'origin/terencehill/mapvote_fade'
[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, recalcPosition, void(entity))
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_recalcPosition(entity me)
42 {
43         float spaceAvail;
44         spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight;
45
46         float spaceUsed;
47         spaceUsed = draw_TextWidth(me.text, 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"), me.text, me.condenseFactor));
64         }
65         me.recalcPos = 0;
66 }
67 void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
68 {
69         SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
70         // absSize_y is height of label
71         me.realFontSize_y = me.fontSize / absSize_y;
72         me.realFontSize_x = me.fontSize / absSize_x;
73         if(me.marginLeft)
74                 me.keepspaceLeft = me.marginLeft * me.realFontSize_x;
75         if(me.marginRight)
76                 me.keepspaceRight = me.marginRight * me.realFontSize_x;
77         me.realOrigin_y = 0.5 * (1 - me.realFontSize_y);
78
79         me.recalcPosition(me);
80 }
81 void Label_configureLabel(entity me, string txt, float sz, float algn)
82 {
83         me.fontSize = sz;
84         me.align = algn;
85         me.setText(me, txt);
86 }
87 void Label_draw(entity me)
88 {
89         string t;
90         vector o;
91         if(me.disabled)
92                 draw_alpha *= me.disabledAlpha;
93
94         if(me.textEntity)
95         {
96                 t = me.textEntity.toString(me.textEntity);
97                 me.recalcPos = 1;
98         }
99         else
100                 t = me.text;
101
102         if(me.recalcPos)
103                 me.recalcPosition(me);
104
105         if(me.fontSize)
106                 if(t)
107                 {
108                         vector dfs;
109                         vector fs;
110
111                         // set up variables to draw in condensed size, but use hinting for original size
112                         fs = me.realFontSize;
113                         fs_x *= me.condenseFactor;
114
115                         dfs = draw_fontscale;
116                         draw_fontscale_x *= me.condenseFactor;
117
118                         if(me.allowCut) // FIXME allowCut incompatible with align != 0
119                                 draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs), fs, me.colorL, me.alpha, me.allowColors);
120                         else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
121                         {
122                                 getWrappedLine_remaining = t;
123                                 o = me.realOrigin;
124                                 while(getWrappedLine_remaining)
125                                 {
126                                         if (me.allowColors)
127                                                 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors);
128                                         else
129                                                 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors);
130                                         draw_Text(o, t, fs, me.colorL, me.alpha, me.allowColors);
131                                         o_y += me.realFontSize_y;
132                                 }
133                         }
134                         else
135                                 draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors);
136
137                         draw_fontscale = dfs;
138                 }
139 }
140 #endif