]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu/item/label.c
cvar list editor
[divverent/nexuiz.git] / data / 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, keepspaceLeft, float, 0) // for use by subclasses (radiobuttons for example)
13         ATTRIB(Label, keepspaceRight, float, 0)
14         ATTRIB(Label, marginLeft, float, 0) // alternate way to specify keepspace* (in characters from the font)
15         ATTRIB(Label, marginRight, float, 0)
16         ATTRIB(Label, realFontSize, vector, '0 0 0')
17         ATTRIB(Label, realOrigin, vector, '0 0 0')
18         ATTRIB(Label, alpha, float, 0.7)
19         ATTRIB(Label, colorL, vector, '1 1 1')
20         ATTRIB(Label, disabled, float, 0)
21         ATTRIB(Label, disabledAlpha, float, 0.3)
22         ATTRIB(Label, textEntity, entity, NULL)
23         ATTRIB(Label, allowWrap, float, 0)
24 ENDCLASS(Label)
25 #endif
26
27 #ifdef IMPLEMENTATION
28 string toStringLabel(entity me)
29 {
30         return me.text;
31 }
32 void setTextLabel(entity me, string txt)
33 {
34         me.text = txt;
35         me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - min(me.realFontSize_x * draw_TextWidth(me.text, 0), (1 - me.keepspaceLeft - me.keepspaceRight))) + me.keepspaceLeft;
36 }
37 void resizeNotifyLabel(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
38 {
39         // absSize_y is height of label
40         me.realFontSize_y = me.fontSize / absSize_y;
41         me.realFontSize_x = me.fontSize / absSize_x;
42         if(me.marginLeft)
43                 me.keepspaceLeft = me.marginLeft * me.realFontSize_x;
44         if(me.marginRight)
45                 me.keepspaceRight = me.marginRight * me.realFontSize_x;
46         me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - min(me.realFontSize_x * draw_TextWidth(me.text, 0), (1 - me.keepspaceLeft - me.keepspaceRight))) + me.keepspaceLeft;
47         me.realOrigin_y = 0.5 * (1 - me.realFontSize_y);
48 }
49 void configureLabelLabel(entity me, string txt, float sz, float algn)
50 {
51         me.fontSize = sz;
52         me.align = algn;
53         me.setText(me, txt);
54 }
55 void drawLabel(entity me)
56 {
57         string t;
58         vector o;
59         if(me.disabled)
60                 draw_alpha *= me.disabledAlpha;
61
62         if(me.textEntity)
63         {
64                 t = me.textEntity.toString(me.textEntity);
65                 me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - min(me.realFontSize_x * draw_TextWidth(t, 0), (1 - me.keepspaceLeft - me.keepspaceRight))) + me.keepspaceLeft;
66         }
67         else
68                 t = me.text;
69         
70         if(me.fontSize)
71                 if(t)
72                 {
73                         if(me.allowCut) // FIXME allowCut incompatible with align != 0
74                                 draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight) / me.realFontSize_x, 0), me.realFontSize, me.colorL, me.alpha, 0);
75                         else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0
76                         {
77                                 getWrappedLine_remaining = t;
78                                 o = me.realOrigin;
79                                 while(getWrappedLine_remaining)
80                                 {
81                                         t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight) / me.realFontSize_x);
82                                         draw_Text(o, t, me.realFontSize, me.colorL, me.alpha, 0);
83                                         o_y += me.realFontSize_y;
84                                 }
85                         }
86                         else
87                                 draw_Text(me.realOrigin, t, me.realFontSize, me.colorL, me.alpha, 0);
88                 }
89 }
90 #endif