sources/interface.cpp

changeset 15
33da84af4bba
parent 14
33b8f428bacb
child 16
33bac54867bf
--- a/sources/interface.cpp	Sat Dec 13 07:36:00 2014 +0200
+++ b/sources/interface.cpp	Sun Dec 14 17:53:24 2014 +0200
@@ -35,6 +35,12 @@
 static int g_cursor = 0;
 static int g_pan = 0;
 static bool g_needRefresh = false;
+static bool g_needStatusBarRender = false;
+static bool g_needInputRender = false;
+static String g_statusBarText;
+static struct { char ch; int x; } g_cursorChar;
+static Vector<String> g_output = {""};
+static int g_outputScroll = 0;
 
 // -------------------------------------------------------------------------------------------------
 //
@@ -61,7 +67,17 @@
 static FUNCTION
 interface_render_log_area() -> void
 {
+	int height = LINES - 3;
+	int start = max (0, g_output.size() - height - 1);
+	int end = min (g_output.size(), start + height);
+	int y = 1;
+	assert (end - start <= height);
 
+	for (int i = start; i < end; ++i)
+	{
+		mvhline (y, 0, ' ', COLS);
+		mvprintw (y++, 0, "%s", g_output[i].chars());
+	}
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -101,7 +117,10 @@
 	mvprintw (y, 0, "%s", prompt);
 	mvprintw (y, strlen (prompt) + displayTextBegin.length(), "%s", displayTextEnd.chars());
 	mvprintw (y, strlen (prompt), "%s", displayTextBegin.chars());
+	g_cursorChar.ch = g_cursor != 0 ? g_input[g_cursor - 1] : '\0';
+	g_cursorChar.x = strlen (prompt) + displayTextBegin.length() - 1;
 	g_needRefresh = true;
+	g_needInputRender = false;
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -109,7 +128,20 @@
 static FUNCTION
 interface_render_statusbar() -> void
 {
+	int y = LINES - 1;
+	mvhline (y, 0, ' ', COLS);
+	mvprintw (y, 0, "%s", g_statusBarText.chars());
+	g_needRefresh = true;
+	g_needStatusBarRender = false;
+}
 
+// -------------------------------------------------------------------------------------------------
+//
+static FUNCTION
+set_statusbar_text (const String& a) -> void
+{
+	g_statusBarText = a;
+	g_needStatusBarRender = true;
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -121,7 +153,19 @@
 	interface_render_log_area();
 	interface_render_statusbar();
 	interface_render_input();
-	g_needRefresh = true;
+}
+
+// -------------------------------------------------------------------------------------------------
+//
+static FUNCTION
+interface_position_cursor() -> void
+{
+	int y = LINES - 2;
+
+	if (g_cursorChar.ch != '\0')
+		mvprintw (y, g_cursorChar.x, "%c", g_cursorChar.ch);
+	else
+		mvprintw (y, 1, " ");
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -147,61 +191,75 @@
 Interface::handle_input() -> void
 {
 	int ch = ::getch();
+	set_statusbar_text (String::from_number (ch));
 
-	if (ch == KEY_F(1))
+	if (ch >= 0x20 and ch <= 0x7E)
 	{
+		g_input.insert (g_cursor++, char (ch));
+		g_needInputRender = true;
+	}
+	else switch (ch)
+	{
+	case KEY_F(1):
 		endwin();
 		exit (EXIT_SUCCESS);
-	}
-	else if (ch >= 0x20 and ch <= 0x7E)
-	{
-		g_input.insert (g_cursor++, char (ch));
-		interface_render_input();
-		refresh();
-	}
-	else if (ch == KEY_LEFT)
-	{
+		break;
+
+	case KEY_LEFT:
 		if (g_cursor > 0)
 		{
 			g_cursor--;
-			interface_render_input();
+			g_needInputRender = true;
 		}
-	}
-	else if (ch == KEY_RIGHT)
-	{
+		break;
+
+	case KEY_RIGHT:
 		if (g_cursor < g_input.length())
 		{
 			g_cursor++;
-			interface_render_input();
+			g_needInputRender = true;
 		}
-	}
-	else if (ch == KEY_HOME)
-	{
+		break;
+
+	case KEY_HOME:
 		if (g_cursor != 0)
 		{
 			g_cursor = 0;
-			interface_render_input();
+			g_needInputRender = true;
 		}
-	}
-	else if (ch == KEY_END)
-	{
+		break;
+
+	case KEY_END:
 		if (g_cursor != g_input.length())
 		{
 			g_cursor = g_input.length();
-			interface_render_input();
+			g_needInputRender = true;
 		}
-	}
-	else if (ch == KEY_BACKSPACE)
-	{
-		if (not g_input.is_empty() and g_cursor > 0)
+		break;
+
+	case KEY_BACKSPACE:
+		if (g_cursor > 0)
 		{
 			g_input.remove_at (--g_cursor);
-			interface_render_input();
+			g_needInputRender = true;
 		}
+		break;
+
+	case KEY_DC:
+		if (g_cursor < g_input.length())
+		{
+			g_input.remove_at (g_cursor);
+			g_needInputRender = true;
+		}
+		break;
 	}
 
+	if (g_needStatusBarRender) interface_render_statusbar();
+	if (g_needInputRender) interface_render_input();
+
 	if (g_needRefresh)
 	{
+		interface_position_cursor();
 		refresh();
 		g_needRefresh = false;
 	}
@@ -209,5 +267,18 @@
 
 FUNCTION print_to_console (const String& a) -> void
 {
-	
+	for (char ch : a)
+	{
+		if (ch == '\n')
+		{
+			g_output << "";
+			continue;
+		}
+
+		g_output[g_output.size() - 1] += ch;
+	}
+
+	interface_render_log_area();
+	interface_position_cursor();
+	refresh();
 }

mercurial