]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/label.c
initial checkin from nexuiz svn r8756
[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 ENDCLASS(Label)
26 #endif
27
28 #ifdef IMPLEMENTATION
29 string toStringLabel(entity me)
30 {
31         return me.text;
32 }
33 void setTextLabel(entity me, string txt)
34 {
35         me.text = txt;
36         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;
37 }
38 void resizeNotifyLabel(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
39 {
40         resizeNotifyItem(me, relOrigin, relSize, absOrigin, absSize);
41         // absSize_y is height of label
42         me.realFontSize_y = me.fontSize / absSize_y;
43         me.realFontSize_x = me.fontSize / absSize_x;
44         if(me.marginLeft)
45                 me.keepspaceLeft = me.marginLeft * me.realFontSize_x;
46         if(me.marginRight)
47                 me.keepspaceRight = me.marginRight * me.realFontSize_x;
48         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;
49         me.realOrigin_y = 0.5 * (1 - me.realFontSize_y);
50 }
51 void configureLabelLabel(entity me, string txt, float sz, float algn)
52 {
53         me.fontSize = sz;
54         me.align = algn;
55         me.setText(me, txt);
56 }
57 void drawLabel(entity me)
58 {
59         string t;
60         vector o;
61         if(me.disabled)
62                 draw_alpha *= me.disabledAlpha;
63
64         if(me.textEntity)
65         {
66                 t = me.textEntity.toString(me.textEntity);
67                 me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - min(draw_TextWidth(t, 0, me.realFontSize), (1 - me.keepspaceLeft - me.keepspaceRight))) + me.keepspaceLeft;
68         }
69         else
70                 t = me.text;
71         
72         if(me.fontSize)
73                 if(t)
74                 {
75                         if(me.allowCut) // FIXME allowCut incompatible with align != 0
76                                 draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, me.realFontSize), me.realFontSize, me.colorL, me.alpha, me.allowColors);
77                         else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
78                         {
79                                 getWrappedLine_remaining = t;
80                                 o = me.realOrigin;
81                                 while(getWrappedLine_remaining)
82                                 {
83                                         if (me.allowColors)
84                                                 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), me.realFontSize, draw_TextWidth_WithColors);
85                                         else
86                                                 t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), me.realFontSize, draw_TextWidth_WithoutColors);
87                                         draw_Text(o, t, me.realFontSize, me.colorL, me.alpha, me.allowColors);
88                                         o_y += me.realFontSize_y;
89                                 }
90                         }
91                         else
92                                 draw_Text(me.realOrigin, t, me.realFontSize, me.colorL, me.alpha, me.allowColors);
93                 }
94 }
95 #endif