# HG changeset patch # User Teemu Piippo # Date 1418578727 -7200 # Node ID 50341dec533e76bb34be123339ab9ad02a5862db # Parent 33bac54867bfc5e11b71da91ae56e14d956860d4 - added command sending diff -r 33bac54867bf -r 50341dec533e sources/basics.h --- 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; FUNCTION print_to_console (const String& a) -> void; + +template inline FUNCTION +clamp (T a, T b, T c) -> T +{ + return (a < b) ? b : (a > c) ? c : a; +} diff -r 33bac54867bf -r 50341dec533e sources/interface.cpp --- 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 #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 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; } diff -r 33bac54867bf -r 50341dec533e sources/interface.h --- 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; }; diff -r 33bac54867bf -r 50341dec533e sources/main.cpp --- 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; diff -r 33bac54867bf -r 50341dec533e sources/network/rconsession.cpp --- 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; +} diff -r 33bac54867bf -r 50341dec533e sources/network/rconsession.h --- 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*;