68 } |
69 } |
69 |
70 |
70 // ------------------------------------------------------------------------------------------------- |
71 // ------------------------------------------------------------------------------------------------- |
71 // |
72 // |
72 static FUNCTION |
73 static FUNCTION |
|
74 current_input() -> const String& |
|
75 { |
|
76 return g_input[g_inputCursor]; |
|
77 } |
|
78 |
|
79 // ------------------------------------------------------------------------------------------------- |
|
80 // |
|
81 // Makes current_input() the lastmost input (so that we won't modify history) |
|
82 // |
|
83 static FUNCTION |
|
84 detach_input() -> void |
|
85 { |
|
86 if (g_inputCursor > 0) |
|
87 { |
|
88 g_input[0] = current_input(); |
|
89 g_inputCursor = 0; |
|
90 } |
|
91 } |
|
92 |
|
93 // ------------------------------------------------------------------------------------------------- |
|
94 // A version of current_input() that allows changing the contents of it. |
|
95 // |
|
96 static FUNCTION |
|
97 mutable_current_input() -> String& |
|
98 { |
|
99 detach_input(); |
|
100 return g_input[g_inputCursor]; |
|
101 } |
|
102 |
|
103 // ------------------------------------------------------------------------------------------------- |
|
104 // |
|
105 static FUNCTION |
|
106 move_input_cursor (int delta) -> void |
|
107 { |
|
108 // No input history when inputting addresses or passwords |
|
109 if (g_inputState != INPUTSTATE_NORMAL) |
|
110 { |
|
111 g_inputCursor = 0; |
|
112 return; |
|
113 } |
|
114 |
|
115 int oldcursor = g_inputCursor; |
|
116 g_inputCursor = clamp (g_inputCursor + delta, 0, g_input.size() - 1); |
|
117 |
|
118 if (g_inputCursor != oldcursor) |
|
119 { |
|
120 g_cursor = current_input().length(); |
|
121 g_needInputRender = true; |
|
122 } |
|
123 } |
|
124 |
|
125 // ------------------------------------------------------------------------------------------------- |
|
126 // |
|
127 static FUNCTION |
73 interface_prompt_string() -> String |
128 interface_prompt_string() -> String |
74 { |
129 { |
75 String prompt; |
130 String prompt; |
76 |
131 |
77 switch (g_inputState) |
132 switch (g_inputState) |
230 attroff (promptColor); |
285 attroff (promptColor); |
231 return; |
286 return; |
232 } |
287 } |
233 |
288 |
234 String prompt = interface_prompt_string(); |
289 String prompt = interface_prompt_string(); |
235 int displaylength = COLS - prompt.length() - 2; |
290 int displayLength = COLS - prompt.length() - 2; |
|
291 String displayString = current_input(); |
236 int y = LINES - 2; |
292 int y = LINES - 2; |
237 |
293 |
|
294 // If we're inputting a password, replace it with asterisks |
|
295 if (g_inputState == INPUTSTATE_PASSWORD) |
|
296 { |
|
297 for (char& ch : displayString) |
|
298 ch = '*'; |
|
299 } |
|
300 |
238 // Ensure the cursor is within bounds |
301 // Ensure the cursor is within bounds |
239 g_cursor = clamp (g_cursor, 0, g_input.length()); |
302 g_cursor = clamp (g_cursor, 0, displayString.length()); |
240 |
303 |
241 // Ensure that the cursor is always in view, adjust panning if this is not the case |
304 // Ensure that the cursor is always in view, adjust panning if this is not the case |
242 if (g_cursor > g_pan + displaylength) |
305 if (g_cursor > g_pan + displayLength) |
243 g_pan = g_cursor - displaylength; // cursor went too far right |
306 g_pan = g_cursor - displayLength; // cursor went too far right |
244 else if (g_cursor < g_pan) |
307 else if (g_cursor < g_pan) |
245 g_pan = g_cursor; // cursor went past the pan value to the left |
308 g_pan = g_cursor; // cursor went past the pan value to the left |
246 |
309 |
247 // What part of the string to draw? |
310 // What part of the string to draw? |
248 int start = g_pan; |
311 int start = g_pan; |
249 int end = min<int> (g_input.length(), start + displaylength); |
312 int end = min<int> (displayString.length(), start + displayLength); |
250 assert (g_cursor >= start and g_cursor <= end); |
313 assert (g_cursor >= start and g_cursor <= end); |
251 |
|
252 String displayinput = g_input; |
|
253 |
|
254 // If we're inputting a password, replace it with asterisks |
|
255 if (g_inputState == INPUTSTATE_PASSWORD) |
|
256 { |
|
257 for (char& ch : displayinput) |
|
258 ch = '*'; |
|
259 } |
|
260 |
314 |
261 // Render the input string |
315 // Render the input string |
262 mvhline (LINES - 2, 0, ' ', COLS); |
316 mvhline (LINES - 2, 0, ' ', COLS); |
263 mvprintw (y, prompt.length() + 1, "%s", displayinput.chars()); |
317 mvprintw (y, prompt.length() + 1, "%s", displayString.chars()); |
264 |
318 |
265 // Render the prompt |
319 // Render the prompt |
266 attron (promptColor); |
320 attron (promptColor); |
267 mvprintw (y, 0, "%s", prompt.chars()); |
321 mvprintw (y, 0, "%s", prompt.chars()); |
268 attroff (promptColor); |
322 attroff (promptColor); |
269 |
323 |
270 // Store in memory where the cursor is now (so that we can re-draw it to position the terminal |
324 // Store in memory where the cursor is now (so that we can re-draw it to position the terminal |
271 // cursor). |
325 // cursor). |
272 g_cursorChar.ch = g_cursor != 0 ? displayinput[g_cursor - 1] : '\0'; |
326 g_cursorChar.ch = g_cursor != 0 ? displayString[g_cursor - 1] : '\0'; |
273 g_cursorChar.x = prompt.length() + (g_cursor - g_pan); |
327 g_cursorChar.x = prompt.length() + (g_cursor - g_pan); |
274 g_needRefresh = true; |
328 g_needRefresh = true; |
275 g_needInputRender = false; |
329 g_needInputRender = false; |
276 } |
330 } |
277 |
331 |
296 Interface::update_statusbar() -> void |
350 Interface::update_statusbar() -> void |
297 { |
351 { |
298 String text; |
352 String text; |
299 RCONSession* session = RCONSession::get_session(); |
353 RCONSession* session = RCONSession::get_session(); |
300 |
354 |
301 if (session == nullptr) |
355 switch (session->state()) |
302 { |
356 { |
303 text = ""; |
357 case RCON_DISCONNECTED: |
304 } |
358 text = "Disconnected."; |
305 else |
359 break; |
306 { |
360 |
307 switch (session->state()) |
361 case RCON_CONNECTING: |
308 { |
362 case RCON_AUTHENTICATING: |
309 case RCON_DISCONNECTED: |
363 text = "Connecting to " + session->address().to_string (IP_WITH_PORT) + "..."; |
310 text = "Disconnected."; |
364 break; |
311 break; |
365 |
312 |
366 case RCON_CONNECTED: |
313 case RCON_CONNECTING: |
367 { |
314 case RCON_AUTHENTICATING: |
368 String adminText = (session->num_admins() == 0) ? "No other admins" |
315 text = "Connecting to " + session->address().to_string (IP_WITH_PORT) + "..."; |
369 : format ("%1 other admin%s1", session->num_admins()); |
316 break; |
370 text = format ("%1 | %2 | %3", session->address().to_string (IP_WITH_PORT), |
317 |
371 session->level(), adminText); |
318 case RCON_CONNECTED: |
372 } |
319 { |
373 break; |
320 String adminText = (session->num_admins() == 0) ? "No other admins" |
|
321 : format ("%1 other admin%s1", session->num_admins()); |
|
322 text = format ("%1 | %2 | %3", session->address().to_string (IP_WITH_PORT), |
|
323 session->level(), adminText); |
|
324 } |
|
325 break; |
|
326 } |
|
327 } |
374 } |
328 |
375 |
329 if (not text.is_empty()) |
376 if (not text.is_empty()) |
330 text += " | "; |
377 text += " | "; |
331 |
378 |
431 g_needInputRender = true; |
488 g_needInputRender = true; |
432 } |
489 } |
433 break; |
490 break; |
434 |
491 |
435 case KEY_RIGHT: |
492 case KEY_RIGHT: |
436 if (g_cursor < g_input.length()) |
493 if (g_cursor < current_input().length()) |
437 { |
494 { |
438 g_cursor++; |
495 g_cursor++; |
439 g_needInputRender = true; |
496 g_needInputRender = true; |
440 } |
497 } |
441 break; |
498 break; |
442 |
499 |
|
500 case KEY_DOWN: |
|
501 case KEY_UP: |
|
502 move_input_cursor (ch == KEY_DOWN ? -1 : 1); |
|
503 break; |
|
504 |
443 case KEY_HOME: |
505 case KEY_HOME: |
444 if (g_cursor != 0) |
506 if (g_cursor != 0) |
445 { |
507 { |
446 g_cursor = 0; |
508 g_cursor = 0; |
447 g_needInputRender = true; |
509 g_needInputRender = true; |
448 } |
510 } |
449 break; |
511 break; |
450 |
512 |
451 case KEY_END: |
513 case KEY_END: |
452 if (g_cursor != g_input.length()) |
514 if (g_cursor != current_input().length()) |
453 { |
515 { |
454 g_cursor = g_input.length(); |
516 g_cursor = current_input().length(); |
455 g_needInputRender = true; |
517 g_needInputRender = true; |
456 } |
518 } |
457 break; |
519 break; |
458 |
520 |
459 case KEY_BACKSPACE: |
521 case KEY_BACKSPACE: |
460 if (g_cursor > 0) |
522 if (g_cursor > 0) |
461 { |
523 { |
462 g_input.remove_at (--g_cursor); |
524 mutable_current_input().remove_at (--g_cursor); |
463 g_needInputRender = true; |
525 g_needInputRender = true; |
464 } |
526 } |
465 break; |
527 break; |
466 |
528 |
467 case KEY_DC: |
529 case KEY_DC: |
468 if (g_cursor < g_input.length()) |
530 if (g_cursor < current_input().length()) |
469 { |
531 { |
470 g_input.remove_at (g_cursor); |
532 mutable_current_input().remove_at (g_cursor); |
471 g_needInputRender = true; |
533 g_needInputRender = true; |
472 } |
534 } |
473 break; |
535 break; |
474 |
536 |
475 case KEY_PPAGE: |
537 case KEY_PPAGE: |
505 |
567 |
506 set_input_state (INPUTSTATE_PASSWORD); |
568 set_input_state (INPUTSTATE_PASSWORD); |
507 break; |
569 break; |
508 |
570 |
509 case INPUTSTATE_PASSWORD: |
571 case INPUTSTATE_PASSWORD: |
510 if (g_inputState == INPUTSTATE_PASSWORD and not g_input.is_empty()) |
572 if (g_inputState == INPUTSTATE_PASSWORD and not current_input().is_empty()) |
511 { |
573 { |
512 RCONSession* session = RCONSession::new_session(); |
574 RCONSession* session = RCONSession::new_session(); |
513 session->set_password (g_input); |
575 session->set_password (current_input()); |
514 session->connect (g_address); |
576 session->connect (g_address); |
515 set_input_state (INPUTSTATE_NORMAL); |
577 set_input_state (INPUTSTATE_NORMAL); |
516 } |
578 } |
517 break; |
579 break; |
518 |
580 |
519 case INPUTSTATE_NORMAL: |
581 case INPUTSTATE_NORMAL: |
520 if (RCONSession::get_session() != nullptr |
582 if (RCONSession::get_session()->send_command (current_input())) |
521 and RCONSession::get_session()->send_command (g_input)) |
|
522 { |
583 { |
523 g_input.clear(); |
584 g_input.insert (0, ""); |
524 g_needInputRender = true; |
585 g_needInputRender = true; |
525 } |
586 } |
526 break; |
587 break; |
527 } |
588 } |
528 break; |
589 break; |