]> icculus.org git repositories - divverent/nexuiz.git/blob - data/menuqc/control/visual/editbox.qc
rename menu directories
[divverent/nexuiz.git] / data / menuqc / control / visual / editbox.qc
1 // DP/Nex Menu
2 // control/visual/editbox.qc
3
4 /////////////////
5 // Item_EditBox
6 ///
7
8 void() Item_EditBox_Draw =
9 {
10         local vector lCursor;
11         local float lAlpha;
12
13         if( !self._target )
14                 return;
15
16         Raise_DataEvent( self._target, ITEM_DATALINK_GET );
17
18         if( self._state == ITEM_STATE_NORMAL ) {
19                 Menu_DrawString( self.pos + self.origin, self._target.value, self.fontSize, self.color, self.alphas_x, self.drawFlags_x );
20                 return;
21         }
22
23         lCursor_x = self._cursorPos * self.fontSize_x;
24         lCursor_y = self.fontSize_y;
25
26         if( mod( Timer_Time * 1000, 1000 / ITEM_EDITBOX_CURSOR_FREQ ) > 500 / ITEM_EDITBOX_CURSOR_FREQ )
27                 lAlpha = 1;
28         else
29                 lAlpha = 0;
30         //lAlpha = 1.2 - fabs( 1 + floor( Timer_Time * ITEM_EDITBOX_CURSOR_FREQ / 2 ) * 2 - Timer_Time * ITEM_EDITBOX_CURSOR_FREQ );
31
32         if( self._state == ITEM_STATE_SELECTED ) {
33                 lCursor_y = lCursor_y - self.sizeCursor_y;
34                 Menu_DrawString( self.pos + self.origin, self._target.value, self.fontSize, self.colorSelected, self.alphas_y, self.drawFlags_y );
35                 Menu_Fill( self.pos + self.origin + lCursor, self.sizeCursor, self.colorCursor, self.alphasCursor_x * lAlpha, self.drawFlagsCursor_x );
36         } else {
37                 lCursor_y = lCursor_y - self.sizeCursorFlash_y;
38                 Menu_DrawString( self.pos + self.origin, self._target.value, self.fontSize, self.colorPressed, self.alphas_z, self.drawFlags_z );
39                 Menu_Fill( self.pos + self.origin + lCursor, self.sizeCursorFlash, self.colorCursorFlash, self.alphasCursor_y * lAlpha, self.drawFlagsCursor_y );
40         }
41 };
42
43 void() Item_EditBox_Update =
44 {
45         Item_DataUser_Update();
46
47         if( self._presstime + ITEM_EDITBOX_FLASHTIME > Timer_Time )
48                 self._state = ITEM_STATE_PRESSED;
49         else if( Menu_ActiveItem == self )
50                 self._state = ITEM_STATE_SELECTED;
51         else
52                 self._state = ITEM_STATE_NORMAL;
53
54         if( !self._target )
55                 return;
56
57         // clamp the cursor position if necessary
58         Raise_DataEvent( self._target, ITEM_DATALINK_GET );
59         self._cursorPos = bound( 0, self._cursorPos, strlen( self._target.value ) );
60
61         // scroll the text if necessary
62         // save the scrolled position in origin
63         self.origin_y = 0;
64         if( ( self._cursorPos + ITEM_EDITBOX_SCROLLDISTANCE > self.size_x / self.fontSize_x )
65                 && ( self._target.maxValue - ITEM_EDITBOX_SCROLLDISTANCE > self.size_x / self.fontSize_x ) )
66                 self.origin_x = self.fontSize_x * ( floor( self.size_x / self.fontSize_x ) - self._cursorPos - ITEM_EDITBOX_SCROLLDISTANCE );
67         else
68                 self.origin_x = 0;
69 };
70
71 void() _IEB_RemoveChar =
72 {
73         local string lTemp;
74         local string lValue;
75
76         lValue = self._target.value;
77         // TODO: perhaps use strlen perhaps
78         // FIXME: FteQCC bug: lTemp = strcat( substring( lValue, 0, self._cursorPos ), substring( lValue, self._cursorPos + 1, 100000 ) );
79         lTemp = strcat( substring( lValue, 0, self._cursorPos ) );
80         lTemp = strcat( lTemp, substring( lValue, self._cursorPos + 1, 100000 ) );
81         String_EntitySet( self._target, value, lTemp );
82         Raise_DataEvent( self._target, ITEM_DATALINK_SET );
83
84         self._presstime = Timer_Time;
85         Sound_Play( self.soundKey );
86 };
87
88 void( float pAscii ) _IEB_InsertChar =
89 {
90         local string lValue;
91         local string lTemp;
92
93         lValue = self._target.value;
94         lTemp = strcat( substring( lValue, 0, self._cursorPos ) , chr( pAscii ) );
95         lTemp = strcat( lTemp, substring( lValue, self._cursorPos, 100000 ) );
96         // FIXME: FTEQCC bug: lTemp = strcat( substring( lValue, 0, self._cursorPos ) , chr( pAscii ), substring( lValue, self._cursorPos, 100000 ) );
97         String_EntitySet( self._target, value, lTemp );
98         Raise_DataEvent( self._target, ITEM_DATALINK_SET );
99
100         self._cursorPos = self._cursorPos + 1;
101
102         self._presstime = Timer_Time;
103         Sound_Play( self.soundKey );
104 };
105
106 bool( float pKey, float pAscii ) Item_EditBox_Key =
107 {
108         if( !self._target )
109                 return false;
110         else if( pKey == K_ENTER ) {
111                 CtCall_Action();
112                 Sound_Play( self.soundKey );
113                 return true;
114         } else if( pKey == K_LEFTARROW ) {
115                 if( self._cursorPos )
116                         self._cursorPos = self._cursorPos - 1;
117                 Sound_Play( self.soundMove );
118                 return true;
119         } else if( pKey == K_RIGHTARROW ) {
120                 if( self._cursorPos < strlen( self._target.value ) )
121                         self._cursorPos = self._cursorPos + 1;
122                 Sound_Play( self.soundMove );
123                 return true;
124         } else if( pKey == K_BACKSPACE ) {
125                 if( self._cursorPos > 0 ) {
126                         self._cursorPos = self._cursorPos - 1;
127                         _IEB_RemoveChar();
128                 }
129                 return true;
130         } else if( pKey == K_DEL ) {
131                 if( self._cursorPos < strlen( self._target.value ) )
132                         _IEB_RemoveChar();
133                 return true;
134         } else if( 30 <= pAscii &&  pAscii <= 126 ) {
135                 _IEB_InsertChar( pAscii );
136                 return true;
137         }
138         return false;
139 };
140
141 void( bool pSelect, bool pUser ) Item_EditBox_Select =
142 {
143         CtCall_Select( pSelect, pUser );
144         if( pSelect && pUser )
145                 Sound_Play( self.soundSelected );
146 };
147
148 void() Item_EditBox_Destroy =
149 {
150         CtCall_Destroy();
151         Item_DataUser_Destroy();
152
153         String_EntityFree( self, soundSelected );
154         String_EntityFree( self, soundKey );
155         String_EntityFree( self, soundMove );
156 };
157
158 void() Item_EditBox_Reinit =
159 {
160         self._cursorPos = 0;
161         CtCall_Reinit();
162 };
163
164 void() Item_EditBox_Spawn =
165 {
166         Item_DataUser_Init();
167
168         String_EntityZone( self, soundSelected );
169         String_EntityZone( self, soundKey );
170         String_EntityZone( self, soundMove );
171
172         Sound_Precache( self.soundSelected );
173         Sound_Precache( self.soundKey );
174         Sound_Precache( self.soundMove );
175
176         if( self.size == '0 0 0' && self._target && self._target.maxValue >= 0 ) {
177                 self.size_x = self.fontSize_x * self._target.maxValue + max( self.sizeCursor_x, self.sizeCursorFlash );
178                 self.size_y = self.fontSize_y;
179         }
180
181         self._reinit = Item_EditBox_Reinit;
182         self._destroy = Item_EditBox_Destroy;
183         self._key = Item_EditBox_Key;
184         self._draw = Item_EditBox_Draw;
185         self._select = Item_EditBox_Select;
186         self._update = Item_EditBox_Update;
187
188         CtCall_Init();
189 };