From 4131de177e36b19b20b8bbdb7bd43b18b6e22690 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Wed, 9 Dec 2020 19:34:04 +0000 Subject: clui: architecture go to base class for clients; fix bugs --- src/client/clui/Client.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/client/clui/Client.cpp (limited to 'src/client/clui/Client.cpp') diff --git a/src/client/clui/Client.cpp b/src/client/clui/Client.cpp new file mode 100644 index 0000000..36859ce --- /dev/null +++ b/src/client/clui/Client.cpp @@ -0,0 +1,65 @@ +#include + +#include "Client.hpp" +#include "clui.hpp" + +const int key_enter = 10; +const int key_escape = 27; +const int key_backspace = 127; + +void Client::HandleActions() +{ + int key = room->InputGetch(); + switch(key) + { + case key_escape: { + this->BreakLoop(); + break; + } + case ' '...'~': { // ascii table 32...126 + AddCharToBuffer(key); + room->AddCharToSendMsg(key); + break; + } + case '\n': { // send message + if(in_buf_used <= 0) + return; + SendMessage(in_buffer); + memset(in_buffer, 0, (in_buf_used+1)*sizeof(char)); + in_buf_used = 0; + + room->InputClear(); + room->SetInputCursor(1, 1); + break; + } + case key_backspace: { + RemoveCharFromBuffer(); + room->RemoveCharFromMsg(); + break; + } + default: break; + } +} + +void Client::AddMessage(const char *msg, int type) +{ + room->AddMessage(msg, type); +} + +void Client::AddCharToBuffer(char ch) +{ + if(in_buf_used >= max_usermsg_len-1) + return; + + in_buffer[in_buf_used] = ch; + in_buf_used++; +} + +void Client::RemoveCharFromBuffer() +{ + if(in_buf_used <= 0) + return; + + in_buffer[in_buf_used] = '\0'; + in_buf_used--; +} \ No newline at end of file -- cgit v1.2.3-18-g5258