]> icculus.org git repositories - divverent/nexuiz.git/blob - data/source/control/visual/button.qc
restructure
[divverent/nexuiz.git] / data / source / control / visual / button.qc
1 // DP/Nex Menu
2 // control/visual/button.qc
3
4 ////////////////
5 // Item_Button
6 ///
7
8 bool( string pString ) _IB_IsPicture =
9 {
10         return (substring( pString, 0, 1 ) == "$" );
11 };
12
13 string( string pString ) _IB_GetPath =
14 {
15         return substring( pString, 1, 1000 );
16 };
17
18 // pString should be strzoned
19 vector( string pString ) _IB_GetSize =
20 {
21         local vector lSize;
22         if( _IB_IsPicture( pString ) )
23                 return Gfx_GetImageSize( _IB_GetPath( pString ) );
24
25         lSize_x = strlen( pString ) * self.fontSize_x;
26         lSize_y = self.fontSize_y;
27         lSize_z = 0;
28
29         return lSize;
30 };
31
32 vector() _IB_GetMaxSize =
33 {
34         local vector lNormal, lSelected, lPressed, lSize;
35
36         lNormal = _IB_GetSize( self.normal );
37         lSelected = _IB_GetSize( self.selected );
38         lPressed = _IB_GetSize( self.pressed );
39
40         lSize_x = max( lNormal_x, lSelected_x, lPressed_x );
41         lSize_y = max( lNormal_y, lSelected_y, lPressed_y );
42         lSize_z = 0;
43
44         return lSize;
45 };
46
47 void( string pName, vector pColor, float pAlpha, float pDrawFlag ) _IB_Draw =
48 {
49         local vector lSize;
50         local vector lPos;
51         local vector lMaxSize;
52
53         // Calculate the real size of the current state
54         lSize = _IB_GetSize( pName );
55         lMaxSize = _IB_GetMaxSize();
56         if( _IB_IsPicture( pName ) ) {
57                 lSize_x = lSize_x * ( self.size_x / lMaxSize_x );
58                 lSize_y = lSize_y * ( self.size_y / lMaxSize_y );
59         }
60
61         // Get the position - it depends on the alignment
62         //if( self.alignment == ITEM_ALIGN_LEFT ) // do nothing
63         lPos_y = self.pos_y;
64         if( self.alignment == ITEM_ALIGN_LEFT )
65                 lPos_x = self.pos_x;
66         if( self.alignment == ITEM_ALIGN_CENTER )
67                 lPos_x = self.pos_x + (self.size_x - lSize_x ) / 2;
68         else if( self.alignment == ITEM_ALIGN_RIGHT )
69                 lPos_x = self.pos_x + self.size_x - lSize_x;
70         else
71                 lPos_x = self.pos_x;
72
73         if( _IB_IsPicture( pName ) )
74                 Menu_DrawPicture( lPos, _IB_GetPath( pName ), lSize, pColor, pAlpha, pDrawFlag );
75         else
76                 Menu_DrawString( lPos, pName, self.fontSize, pColor, pAlpha, pDrawFlag );
77 };
78
79 void() Item_Button_Draw =
80 {
81         local string lText;
82
83         if( self._state == ITEM_STATE_NORMAL )
84                 _IB_Draw( self.normal, self.color, self.alphas_x, self.drawFlags_x );
85         else if( self._state == ITEM_STATE_SELECTED ) {
86                 if( self.selected )
87                         lText = self.selected;
88                 else
89                         lText = self.normal;
90                 _IB_Draw( lText, self.colorSelected, self.alphas_y, self.drawFlags_y );
91         } else if( self._state == ITEM_STATE_PRESSED ) {
92                 if( self.pressed )
93                         lText = self.pressed;
94                 else if( self.selected )
95                         lText = self.selected;
96                 else
97                         lText = self.normal;
98                 _IB_Draw( lText, self.colorPressed, self.alphas_z, self.drawFlags_z );
99         }
100 };
101
102 void() _IB_Calc =
103 {
104         if( self.size == '0 0 0' )
105                 self.size = _IB_GetMaxSize();
106 };
107
108 void() Item_Button_Update =
109 {
110         Item_Link_Update();
111
112         _IB_Calc();
113
114         if( self._presstime + ITEM_BUTTON_ACTIONTIME > Timer_Time )
115                 self._state = ITEM_STATE_PRESSED;
116         else if( Item_Link_IsSelected() )
117                 self._state = ITEM_STATE_SELECTED;
118         else
119                 self._state = ITEM_STATE_NORMAL;
120 };
121
122 void( bool pSelect, bool pUser ) Item_Button_Select =
123 {
124         if( pSelect && pUser )
125                 Sound_Play( self.soundSelected );
126 };
127
128 bool( float pKey, float pAscii ) Item_Button_Key =
129 {
130         if( Item_Link_Key( pKey, pAscii ) )
131                 return true;
132
133         if( pKey == K_SPACE || pKey == K_ENTER || pKey == K_MOUSE1 ) {
134                 Sound_Play( self.soundPressed );
135                 self._presstime = Timer_Time;
136
137                 CtCall_Action();
138                 return true;
139         }
140         return false;
141 };
142
143 void() Item_Button_Destroy =
144 {
145         CtCall_Destroy();
146
147         String_EntityFree( self, normal );
148         String_EntityFree( self, selected );
149         String_EntityFree( self, pressed );
150         String_EntityFree( self, soundSelected );
151         String_EntityFree( self, soundPressed );
152
153         Item_Link_Destroy();
154 };
155
156 void() Item_Button_Spawn =
157 {
158         self._presstime = Timer_Time - 1 - ITEM_BUTTON_ACTIONTIME;
159
160         // zone all the strings
161         String_EntityZone( self, normal );
162         String_EntityZone( self, selected );
163         String_EntityZone( self, pressed );
164         String_EntityZone( self, soundSelected );
165         String_EntityZone( self, soundPressed );
166
167         Item_Link_Init();
168
169         // set the defaults, etc
170         if( _IB_IsPicture( self.normal ) )
171                 Gfx_Precache( _IB_GetPath( self.normal ) );
172         if( _IB_IsPicture( self.selected ) )
173                 Gfx_Precache( _IB_GetPath( self.selected ) );
174         if( _IB_IsPicture( self.pressed ) )
175                 Gfx_Precache( _IB_GetPath( self.pressed ) );
176
177         Sound_Precache( self.soundSelected );
178         Sound_Precache( self.soundPressed );
179
180         _IB_Calc();
181
182         self._reinit = CtCall_Reinit;
183         self._destroy = Item_Button_Destroy;
184         self._key = Item_Button_Key;
185         self._draw = Item_Button_Draw;
186         self._select = Item_Button_Select;
187         self._update = Item_Button_Update;
188
189         CtCall_Init();
190 };