2 // control/visual/button.qc
8 bool( string pString ) _IB_IsPicture =
10 return (substring( pString, 0, 1 ) == "$" );
13 string( string pString ) _IB_GetPath =
15 return substring( pString, 1, 1000 );
18 // pString should be strzoned
19 vector( string pString ) _IB_GetSize =
22 if( _IB_IsPicture( pString ) )
23 return Gfx_GetImageSize( _IB_GetPath( pString ) );
25 lSize_x = strlen( pString ) * self.fontSize_x;
26 lSize_y = self.fontSize_y;
32 vector() _IB_GetMaxSize =
34 local vector lNormal, lSelected, lPressed, lSize;
36 lNormal = _IB_GetSize( self.normal );
37 lSelected = _IB_GetSize( self.selected );
38 lPressed = _IB_GetSize( self.pressed );
40 lSize_x = max( lNormal_x, lSelected_x, lPressed_x );
41 lSize_y = max( lNormal_y, lSelected_y, lPressed_y );
47 void( string pName, vector pColor, float pAlpha, float pDrawFlag ) _IB_Draw =
51 local vector lMaxSize;
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 );
61 // Get the position - it depends on the alignment
62 //if( self.alignment == ITEM_ALIGN_LEFT ) // do nothing
64 if( self.alignment == ITEM_ALIGN_LEFT )
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;
73 if( _IB_IsPicture( pName ) )
74 Menu_DrawPicture( lPos, _IB_GetPath( pName ), lSize, pColor, pAlpha, pDrawFlag );
76 Menu_DrawString( lPos, pName, self.fontSize, pColor, pAlpha, pDrawFlag );
79 void() Item_Button_Draw =
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 ) {
87 lText = self.selected;
90 _IB_Draw( lText, self.colorSelected, self.alphas_y, self.drawFlags_y );
91 } else if( self._state == ITEM_STATE_PRESSED ) {
94 else if( self.selected )
95 lText = self.selected;
98 _IB_Draw( lText, self.colorPressed, self.alphas_z, self.drawFlags_z );
104 if( self.size == '0 0 0' )
105 self.size = _IB_GetMaxSize();
108 void() Item_Button_Update =
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;
119 self._state = ITEM_STATE_NORMAL;
122 void( bool pSelect, bool pUser ) Item_Button_Select =
124 if( pSelect && pUser )
125 Sound_Play( self.soundSelected );
128 bool( float pKey, float pAscii ) Item_Button_Key =
130 if( Item_Link_Key( pKey, pAscii ) )
133 if( pKey == K_SPACE || pKey == K_ENTER || pKey == K_MOUSE1 ) {
134 Sound_Play( self.soundPressed );
135 self._presstime = Timer_Time;
143 void() Item_Button_Destroy =
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 );
156 void() Item_Button_Spawn =
158 self._presstime = Timer_Time - 1 - ITEM_BUTTON_ACTIONTIME;
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 );
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 ) );
177 Sound_Precache( self.soundSelected );
178 Sound_Precache( self.soundPressed );
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;