]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/label.c
7ed6e4f68ef82daf3b33821aba270e691eba3a18
[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         ATTRIB(Label, text, string, string_null)
9         ATTRIB(Label, fontSize, float, 8)
10         ATTRIB(Label, align, float, 0.5)
11         ATTRIB(Label, allowCut, float, 0)
12         ATTRIB(Label, allowColors, float, 0)
13         ATTRIB(Label, keepspaceLeft, float, 0) // for use by subclasses (radiobuttons for example)
14         ATTRIB(Label, keepspaceRight, float, 0)
15         ATTRIB(Label, marginLeft, float, 0) // alternate way to specify keepspace* (in characters from the font)
16         ATTRIB(Label, marginRight, float, 0)
17         ATTRIB(Label, realFontSize, vector, '0 0 0')
18         ATTRIB(Label, realOrigin, vector, '0 0 0')
19         ATTRIB(Label, alpha, float, 0.7)
20         ATTRIB(Label, colorL, vector, '1 1 1')
21         ATTRIB(Label, disabled, float, 0)
22         ATTRIB(Label, disabledAlpha, float, 0.3)
23         ATTRIB(Label, textEntity, entity, NULL)
24         ATTRIB(Label, allowWrap, float, 0)
25         ATTRIB(Label, recalcPos, float, 0)
26 ENDCLASS(Label)
27 #endif
28
29 #ifdef IMPLEMENTATION
30 string Label_toString(entity me)
31 {
32         return me.text;
33 }
34 void Label_setText(entity me, string txt)
35 {
36         me.text = txt;
37         me.recalcPos = 1;
38 }
39 void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
40 {
41         SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
42         // absSize_y is height of label
43         me.realFontSize_y = me.fontSize / absSize_y;
44         me.realFontSize_x = me.fontSize / absSize_x;
45         if(me.marginLeft)
46                 me.keepspaceLeft = me.marginLeft * me.realFontSize_x;
47         if(me.marginRight)
48                 me.keepspaceRight = me.marginRight * me.realFontSize_x;
49         me.realOrigin_y = 0.5 * (1 - me.realFontSize_y);
50         me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - min(draw_TextWidth(me.text, me.allowColors, me.realFontSize), (1 - me.keepspaceLeft - me.keepspaceRight))) + me.keepspaceLeft;
51         me.recalcPos = 0;
52 }
53 void Label_configureLabel(entity me, string txt, float sz, float algn)
54 {
55         me.fontSize = sz;
56         me.align = algn;
57         me.setText(me, txt);
58 }
59 void Label_draw(entity me)
60 {
61         string t;
62         vector o;
63         if(me.disabled)
64                 draw_alpha *= me.disabledAlpha;
65
66         if(me.textEntity)
67         {
68                 t = me.textEntity.toString(me.textEntity);
69                 me.recalcPos = 1;
70         }
71         else
72                 t = me.text;
73
74         if(me.recalcPos)
75                 me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - min(draw_TextWidth(t, me.allowColors, me.realFontSize), (1 - me.keepspaceLeft - me.keepspaceRight))) + me.keepspaceLeft;
76         me.recalcPos = 0;
77
78         //if(me.text == "Bookmark")
79         //      draw_Fill(me.realOrigin, '0 1 0' + '1 0 0' * draw_TextWidth(t, me.allowColors, me.realFontSize), '1 0 1', 1);
80         
81         if(me.fontSize)
82                 if(t)
83                 {
84                         if(me.allowCut) // FIXME allowCut incompatible with align != 0
85                                 draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, me.realFontSize), me.realFontSize, me.colorL, me.alpha, me.allowColors);
86                         else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
87                         {
88                                 getWrappedLine_remaining = t;
89                                 o = me.realOrigin;
90                                 while(getWrappedLine_remaining)
91                                 {
92                                         if (me.allowColors)
93                                                 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), me.realFontSize, draw_TextWidth_WithColors);
94                                         else
95                                                 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), me.realFontSize, draw_TextWidth_WithoutColors);
96                                         draw_Text(o, t, me.realFontSize, me.colorL, me.alpha, me.allowColors);
97                                         o_y += me.realFontSize_y;
98                                 }
99                         }
100                         else
101                                 draw_Text(me.realOrigin, t, me.realFontSize, me.colorL, me.alpha, me.allowColors);
102                 }
103 }
104 #endif