sources/interface.cpp

changeset 25
88b41eea08e0
parent 24
e651d02802c0
child 27
089e37c0887e
--- a/sources/interface.cpp	Sun Dec 14 23:21:38 2014 +0200
+++ b/sources/interface.cpp	Sun Dec 14 23:38:26 2014 +0200
@@ -129,8 +129,6 @@
 		int pair = interface_color_pair (WHITE, BLUE);
 		int startx = (COLS - g_title.length()) / 2;
 		int endx = startx + g_title.length();
-		print ("startx: %1, endx: %2\n", startx, endx);
-
 		attron (pair);
 		mvprintw (0, startx, "%s", g_title.chars());
 		mvhline (0, 0, ' ', startx);
@@ -182,14 +180,16 @@
 static FUNCTION
 interface_render_input() -> void
 {
-	int pair = interface_color_pair (WHITE, BLUE);
+	int promptColor = interface_color_pair (WHITE, BLUE);
 
+	// If we're asking the user if they want to disconnect, we don't render any input strings,
+	// just the confirmation message.
 	if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION)
 	{
-		attron (pair);
+		attron (promptColor);
 		mvhline (LINES - 2, 0, ' ', COLS);
 		mvprintw (LINES - 2, 0, "Are you sure you want to disconnect? y/n");
-		attroff (pair);
+		attroff (promptColor);
 		return;
 	}
 
@@ -206,19 +206,22 @@
 	else if (g_cursor < g_pan)
 		g_pan = g_cursor; // cursor went past the pan value to the left
 
+	// What part of the string to draw?
 	int start = g_pan;
 	int end = min<int> (g_input.length(), start + displaylength);
 	assert (g_cursor >= start and g_cursor <= end);
 
-	// Render the input line, with the part of the input string that's before the cursor written
-	// AFTER the part that comes afterwards. This is to ensure that the cursor is placed at the
-	// position where our cursor is. It looks nice like that. :)
-
+	// Render the input string
 	mvhline (LINES - 2, 0, ' ', COLS);
 	mvprintw (y, prompt.length() + 1, "%s", g_input.chars());
-	attron (pair);
+
+	// Render the prompt
+	attron (promptColor);
 	mvprintw (y, 0, "%s", prompt.chars());
-	attroff (pair);
+	attroff (promptColor);
+
+	// Store in memory where the cursor is now (so that we can re-draw it to position the terminal
+	// cursor).
 	g_cursorChar.ch = g_cursor != 0 ? g_input[g_cursor - 1] : '\0';
 	g_cursorChar.x = prompt.length() + (g_cursor - g_pan);
 	g_needRefresh = true;
@@ -262,6 +265,10 @@
 static FUNCTION
 interface_position_cursor() -> void
 {
+	// This is only relevant if the input string is being drawn
+	if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION)
+		return;
+
 	int y = LINES - 2;
 
 	if (g_cursorChar.ch != '\0')
@@ -272,6 +279,33 @@
 
 // -------------------------------------------------------------------------------------------------
 //
+static FUNCTION
+set_input_state (InputState newstate) -> void
+{
+	// Clear the input row (unless going to or from confirm state)
+	if (newstate != INPUTSTATE_CONFIRM_DISCONNECTION
+		and g_inputState != INPUTSTATE_CONFIRM_DISCONNECTION)
+	{
+		g_input.clear();
+	}
+
+	switch (newstate)
+	{
+	case INPUTSTATE_ADDRESS:
+		if (g_address.host != 0)
+			g_input = g_address.to_string (IP_WITH_PORT);
+		break;
+
+	default:
+		break;
+	}
+
+	g_inputState = newstate;
+	g_needInputRender = true;
+}
+
+// -------------------------------------------------------------------------------------------------
+//
 FUNCTION
 Interface::handle_input() -> void
 {
@@ -281,9 +315,9 @@
 	if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION)
 	{
 		if (ch == 'y' or ch == 'Y')
-			g_inputState = INPUTSTATE_ADDRESS;
+			set_input_state (INPUTSTATE_ADDRESS);
 		else if (ch == 'n' or ch == 'N')
-			g_inputState = INPUTSTATE_NORMAL;
+			set_input_state (INPUTSTATE_NORMAL);
 		return;
 	}
 
@@ -378,9 +412,8 @@
 			if (g_address.port == 0)
 				g_address.port = 10666;
 
-			g_input.clear();
-			g_inputState = INPUTSTATE_PASSWORD;
-			g_needInputRender = true;
+			set_input_state (INPUTSTATE_PASSWORD);
+			break;
 
 		case INPUTSTATE_PASSWORD:
 			if (g_inputState == INPUTSTATE_PASSWORD and not g_input.is_empty())
@@ -388,9 +421,7 @@
 				RCONSession* session = RCONSession::new_session();
 				session->set_password (g_input);
 				session->connect (g_address);
-				g_input.clear();
-				g_inputState = INPUTSTATE_NORMAL;
-				g_needInputRender = true;
+				set_input_state (INPUTSTATE_NORMAL);
 			}
 			break;
 
@@ -411,14 +442,12 @@
 			if (RCONSession::get_session() != nullptr
 				and RCONSession::get_session()->state() != RCON_DISCONNECTED)
 			{
-				g_inputState = INPUTSTATE_CONFIRM_DISCONNECTION;
+				set_input_state (INPUTSTATE_CONFIRM_DISCONNECTION);
 			}
 			else
 			{
-				g_inputState = INPUTSTATE_ADDRESS;
+				set_input_state (INPUTSTATE_ADDRESS);
 			}
-
-			g_needInputRender = true;
 		}
 		break;
 	}

mercurial