sources/interface.cpp

changeset 25
88b41eea08e0
parent 24
e651d02802c0
child 27
089e37c0887e
equal deleted inserted replaced
24:e651d02802c0 25:88b41eea08e0
127 if (g_title.length() <= COLS) 127 if (g_title.length() <= COLS)
128 { 128 {
129 int pair = interface_color_pair (WHITE, BLUE); 129 int pair = interface_color_pair (WHITE, BLUE);
130 int startx = (COLS - g_title.length()) / 2; 130 int startx = (COLS - g_title.length()) / 2;
131 int endx = startx + g_title.length(); 131 int endx = startx + g_title.length();
132 print ("startx: %1, endx: %2\n", startx, endx);
133
134 attron (pair); 132 attron (pair);
135 mvprintw (0, startx, "%s", g_title.chars()); 133 mvprintw (0, startx, "%s", g_title.chars());
136 mvhline (0, 0, ' ', startx); 134 mvhline (0, 0, ' ', startx);
137 mvhline (0, endx, ' ', COLS - endx); 135 mvhline (0, endx, ' ', COLS - endx);
138 attroff (pair); 136 attroff (pair);
180 // ------------------------------------------------------------------------------------------------- 178 // -------------------------------------------------------------------------------------------------
181 // 179 //
182 static FUNCTION 180 static FUNCTION
183 interface_render_input() -> void 181 interface_render_input() -> void
184 { 182 {
185 int pair = interface_color_pair (WHITE, BLUE); 183 int promptColor = interface_color_pair (WHITE, BLUE);
186 184
185 // If we're asking the user if they want to disconnect, we don't render any input strings,
186 // just the confirmation message.
187 if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION) 187 if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION)
188 { 188 {
189 attron (pair); 189 attron (promptColor);
190 mvhline (LINES - 2, 0, ' ', COLS); 190 mvhline (LINES - 2, 0, ' ', COLS);
191 mvprintw (LINES - 2, 0, "Are you sure you want to disconnect? y/n"); 191 mvprintw (LINES - 2, 0, "Are you sure you want to disconnect? y/n");
192 attroff (pair); 192 attroff (promptColor);
193 return; 193 return;
194 } 194 }
195 195
196 String prompt = interface_prompt_string(); 196 String prompt = interface_prompt_string();
197 int displaylength = COLS - prompt.length() - 2; 197 int displaylength = COLS - prompt.length() - 2;
204 if (g_cursor > g_pan + displaylength) 204 if (g_cursor > g_pan + displaylength)
205 g_pan = g_cursor - displaylength; // cursor went too far right 205 g_pan = g_cursor - displaylength; // cursor went too far right
206 else if (g_cursor < g_pan) 206 else if (g_cursor < g_pan)
207 g_pan = g_cursor; // cursor went past the pan value to the left 207 g_pan = g_cursor; // cursor went past the pan value to the left
208 208
209 // What part of the string to draw?
209 int start = g_pan; 210 int start = g_pan;
210 int end = min<int> (g_input.length(), start + displaylength); 211 int end = min<int> (g_input.length(), start + displaylength);
211 assert (g_cursor >= start and g_cursor <= end); 212 assert (g_cursor >= start and g_cursor <= end);
212 213
213 // Render the input line, with the part of the input string that's before the cursor written 214 // Render the input string
214 // AFTER the part that comes afterwards. This is to ensure that the cursor is placed at the
215 // position where our cursor is. It looks nice like that. :)
216
217 mvhline (LINES - 2, 0, ' ', COLS); 215 mvhline (LINES - 2, 0, ' ', COLS);
218 mvprintw (y, prompt.length() + 1, "%s", g_input.chars()); 216 mvprintw (y, prompt.length() + 1, "%s", g_input.chars());
219 attron (pair); 217
218 // Render the prompt
219 attron (promptColor);
220 mvprintw (y, 0, "%s", prompt.chars()); 220 mvprintw (y, 0, "%s", prompt.chars());
221 attroff (pair); 221 attroff (promptColor);
222
223 // Store in memory where the cursor is now (so that we can re-draw it to position the terminal
224 // cursor).
222 g_cursorChar.ch = g_cursor != 0 ? g_input[g_cursor - 1] : '\0'; 225 g_cursorChar.ch = g_cursor != 0 ? g_input[g_cursor - 1] : '\0';
223 g_cursorChar.x = prompt.length() + (g_cursor - g_pan); 226 g_cursorChar.x = prompt.length() + (g_cursor - g_pan);
224 g_needRefresh = true; 227 g_needRefresh = true;
225 g_needInputRender = false; 228 g_needInputRender = false;
226 } 229 }
260 // ------------------------------------------------------------------------------------------------- 263 // -------------------------------------------------------------------------------------------------
261 // 264 //
262 static FUNCTION 265 static FUNCTION
263 interface_position_cursor() -> void 266 interface_position_cursor() -> void
264 { 267 {
268 // This is only relevant if the input string is being drawn
269 if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION)
270 return;
271
265 int y = LINES - 2; 272 int y = LINES - 2;
266 273
267 if (g_cursorChar.ch != '\0') 274 if (g_cursorChar.ch != '\0')
268 mvprintw (y, g_cursorChar.x, "%c", g_cursorChar.ch); 275 mvprintw (y, g_cursorChar.x, "%c", g_cursorChar.ch);
269 else 276 else
270 mvprintw (y, interface_prompt_string().length(), " "); 277 mvprintw (y, interface_prompt_string().length(), " ");
271 } 278 }
272 279
273 // ------------------------------------------------------------------------------------------------- 280 // -------------------------------------------------------------------------------------------------
274 // 281 //
282 static FUNCTION
283 set_input_state (InputState newstate) -> void
284 {
285 // Clear the input row (unless going to or from confirm state)
286 if (newstate != INPUTSTATE_CONFIRM_DISCONNECTION
287 and g_inputState != INPUTSTATE_CONFIRM_DISCONNECTION)
288 {
289 g_input.clear();
290 }
291
292 switch (newstate)
293 {
294 case INPUTSTATE_ADDRESS:
295 if (g_address.host != 0)
296 g_input = g_address.to_string (IP_WITH_PORT);
297 break;
298
299 default:
300 break;
301 }
302
303 g_inputState = newstate;
304 g_needInputRender = true;
305 }
306
307 // -------------------------------------------------------------------------------------------------
308 //
275 FUNCTION 309 FUNCTION
276 Interface::handle_input() -> void 310 Interface::handle_input() -> void
277 { 311 {
278 int ch = ::getch(); 312 int ch = ::getch();
279 set_statusbar_text (String::from_number (ch)); 313 set_statusbar_text (String::from_number (ch));
280 314
281 if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION) 315 if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION)
282 { 316 {
283 if (ch == 'y' or ch == 'Y') 317 if (ch == 'y' or ch == 'Y')
284 g_inputState = INPUTSTATE_ADDRESS; 318 set_input_state (INPUTSTATE_ADDRESS);
285 else if (ch == 'n' or ch == 'N') 319 else if (ch == 'n' or ch == 'N')
286 g_inputState = INPUTSTATE_NORMAL; 320 set_input_state (INPUTSTATE_NORMAL);
287 return; 321 return;
288 } 322 }
289 323
290 if (ch >= 0x20 and ch <= 0x7E) 324 if (ch >= 0x20 and ch <= 0x7E)
291 { 325 {
376 } 410 }
377 411
378 if (g_address.port == 0) 412 if (g_address.port == 0)
379 g_address.port = 10666; 413 g_address.port = 10666;
380 414
381 g_input.clear(); 415 set_input_state (INPUTSTATE_PASSWORD);
382 g_inputState = INPUTSTATE_PASSWORD; 416 break;
383 g_needInputRender = true;
384 417
385 case INPUTSTATE_PASSWORD: 418 case INPUTSTATE_PASSWORD:
386 if (g_inputState == INPUTSTATE_PASSWORD and not g_input.is_empty()) 419 if (g_inputState == INPUTSTATE_PASSWORD and not g_input.is_empty())
387 { 420 {
388 RCONSession* session = RCONSession::new_session(); 421 RCONSession* session = RCONSession::new_session();
389 session->set_password (g_input); 422 session->set_password (g_input);
390 session->connect (g_address); 423 session->connect (g_address);
391 g_input.clear(); 424 set_input_state (INPUTSTATE_NORMAL);
392 g_inputState = INPUTSTATE_NORMAL;
393 g_needInputRender = true;
394 } 425 }
395 break; 426 break;
396 427
397 case INPUTSTATE_NORMAL: 428 case INPUTSTATE_NORMAL:
398 if (RCONSession::get_session() != nullptr 429 if (RCONSession::get_session() != nullptr
409 if (g_inputState == INPUTSTATE_NORMAL) 440 if (g_inputState == INPUTSTATE_NORMAL)
410 { 441 {
411 if (RCONSession::get_session() != nullptr 442 if (RCONSession::get_session() != nullptr
412 and RCONSession::get_session()->state() != RCON_DISCONNECTED) 443 and RCONSession::get_session()->state() != RCON_DISCONNECTED)
413 { 444 {
414 g_inputState = INPUTSTATE_CONFIRM_DISCONNECTION; 445 set_input_state (INPUTSTATE_CONFIRM_DISCONNECTION);
415 } 446 }
416 else 447 else
417 { 448 {
418 g_inputState = INPUTSTATE_ADDRESS; 449 set_input_state (INPUTSTATE_ADDRESS);
419 } 450 }
420
421 g_needInputRender = true;
422 } 451 }
423 break; 452 break;
424 } 453 }
425 } 454 }
426 455

mercurial