- added command sending

Sun, 14 Dec 2014 19:38:47 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 14 Dec 2014 19:38:47 +0200
changeset 17
50341dec533e
parent 16
33bac54867bf
child 18
56a1ac7d931b

- added command sending

sources/basics.h file | annotate | diff | comparison | revisions
sources/interface.cpp file | annotate | diff | comparison | revisions
sources/interface.h file | annotate | diff | comparison | revisions
sources/main.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.h file | annotate | diff | comparison | revisions
--- a/sources/basics.h	Sun Dec 14 18:15:28 2014 +0200
+++ b/sources/basics.h	Sun Dec 14 19:38:47 2014 +0200
@@ -63,3 +63,9 @@
 using Function = std::function<Signature>;
 
 FUNCTION print_to_console (const String& a) -> void;
+
+template<typename T> inline FUNCTION
+clamp (T a, T b, T c) -> T
+{
+	return (a < b) ? b : (a > c) ? c : a;
+}
--- a/sources/interface.cpp	Sun Dec 14 18:15:28 2014 +0200
+++ b/sources/interface.cpp	Sun Dec 14 19:38:47 2014 +0200
@@ -30,6 +30,7 @@
 
 #include <string.h>
 #include "interface.h"
+#include "network/rconsession.h"
 
 enum { PAGE_SIZE = 10 };
 
@@ -39,6 +40,7 @@
 static bool g_needRefresh = false;
 static bool g_needStatusBarRender = false;
 static bool g_needInputRender = false;
+static bool g_needOutputRender = false;
 static String g_statusBarText;
 static struct { char ch; int x; } g_cursorChar;
 static Vector<String> g_output = {""};
@@ -67,7 +69,7 @@
 // -------------------------------------------------------------------------------------------------
 //
 static FUNCTION
-interface_render_log_area() -> void
+interface_render_output() -> void
 {
 	int height = LINES - 3;
 
@@ -87,6 +89,8 @@
 		mvhline (y, 0, ' ', COLS);
 		mvprintw (y++, 0, "%s", g_output[i].chars());
 	}
+
+	g_needRefresh = true;
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -98,6 +102,9 @@
 	int displaylength = COLS - strlen (prompt) - 1;
 	int y = LINES - 2;
 
+	// Ensure the cursor is within bounds
+	g_cursor = clamp (g_cursor, 0, g_input.length());
+
 	// Ensure that the cursor is always in view, adjust panning if this is not the case
 	if (g_cursor > g_pan + displaylength)
 		g_pan = g_cursor - displaylength; // cursor went too far right
@@ -159,7 +166,7 @@
 interface_render_full() -> void
 {
 	interface_render_titlebar();
-	interface_render_log_area();
+	interface_render_output();
 	interface_render_statusbar();
 	interface_render_input();
 }
@@ -265,19 +272,33 @@
 
 	case KEY_PPAGE:
 		g_outputScroll += PAGE_SIZE;
-		interface_render_log_area();
-		g_needRefresh = true;
+		g_needOutputRender = true;
 		break;
 
 	case KEY_NPAGE:
 		g_outputScroll -= PAGE_SIZE;
-		interface_render_log_area();
-		g_needRefresh = true;
+		g_needOutputRender = true;
+		break;
+
+	case '\n':
+	case KEY_ENTER:
+		if (RCONSession::get_session()->send_command (g_input))
+		{
+			g_input.clear();
+			g_needInputRender = true;
+		}
 		break;
 	}
+}
 
+// -------------------------------------------------------------------------------------------------
+//
+FUNCTION
+Interface::render() -> void
+{
 	if (g_needStatusBarRender) interface_render_statusbar();
 	if (g_needInputRender) interface_render_input();
+	if (g_needOutputRender) interface_render_output();
 
 	if (g_needRefresh)
 	{
@@ -287,6 +308,8 @@
 	}
 }
 
+// -------------------------------------------------------------------------------------------------
+//
 FUNCTION print_to_console (const String& a) -> void
 {
 	for (char ch : a)
@@ -300,7 +323,5 @@
 		g_output[g_output.size() - 1] += ch;
 	}
 
-	interface_render_log_area();
-	interface_position_cursor();
-	refresh();
+	g_needOutputRender = true;
 }
--- a/sources/interface.h	Sun Dec 14 18:15:28 2014 +0200
+++ b/sources/interface.h	Sun Dec 14 19:38:47 2014 +0200
@@ -31,10 +31,9 @@
 #pragma once
 #include "main.h"
 
-class RCONSession;
-
 namespace Interface
 {
-	void initialize();
-	void handle_input();
+	FUNCTION initialize() -> void;
+	FUNCTION handle_input() -> void;
+	FUNCTION render() -> void;
 };
--- a/sources/main.cpp	Sun Dec 14 18:15:28 2014 +0200
+++ b/sources/main.cpp	Sun Dec 14 19:38:47 2014 +0200
@@ -73,6 +73,8 @@
 
 		if (session)
 			session->tick();
+
+		Interface::render();
 	}
 
 	return EXIT_SUCCESS;
--- a/sources/network/rconsession.cpp	Sun Dec 14 18:15:28 2014 +0200
+++ b/sources/network/rconsession.cpp	Sun Dec 14 19:38:47 2014 +0200
@@ -258,3 +258,20 @@
 {
 	return g_rconSession;
 }
+
+// -------------------------------------------------------------------------------------------------
+// Returns true if the message was successfully sent.
+//
+METHOD
+RCONSession::send_command (const String& message) -> bool
+{
+	if (m_state != RCON_CONNECTED)
+		return false;
+
+	Bytestream packet;
+	packet.write_byte (CLRC_COMMAND);
+	packet.write_string (message);
+	send (packet);
+	bump_last_ping();
+	return true;
+}
--- a/sources/network/rconsession.h	Sun Dec 14 18:15:28 2014 +0200
+++ b/sources/network/rconsession.h	Sun Dec 14 19:38:47 2014 +0200
@@ -102,6 +102,7 @@
 	METHOD socket() -> UDPSocket*;
 	METHOD tick() -> void;
 	METHOD bump_last_ping() -> void;
+	METHOD send_command (const String& message) -> bool;
 
 	static METHOD get_session() -> RCONSession*;
 

mercurial