From 05750c44046f320109055aa5954ef0bbd977705f Mon Sep 17 00:00:00 2001 From: Joursoir Date: Thu, 10 Dec 2020 16:02:46 +0000 Subject: refactor clui: delete class ChatRoom, create own class for chat, players, tips (new), input --- src/client/clui/Client.cpp | 44 ++++++++++--- src/client/clui/Client.hpp | 42 +++---------- src/client/clui/Makefile | 4 +- src/client/clui/WindowInterface.cpp | 33 ++++++++++ src/client/clui/WindowInterface.hpp | 22 +++++++ src/client/clui/clui.cpp | 121 +++++++++++++----------------------- src/client/clui/clui.hpp | 69 +++++++++----------- src/client/clui/main.cpp | 5 +- 8 files changed, 180 insertions(+), 160 deletions(-) create mode 100644 src/client/clui/WindowInterface.cpp create mode 100644 src/client/clui/WindowInterface.hpp (limited to 'src/client/clui') diff --git a/src/client/clui/Client.cpp b/src/client/clui/Client.cpp index 36859ce..9859794 100644 --- a/src/client/clui/Client.cpp +++ b/src/client/clui/Client.cpp @@ -3,22 +3,52 @@ #include "Client.hpp" #include "clui.hpp" +#define CHAT_HEIGHT 20 +#define CHAT_WIDTH 59 +#define PLAYERS_WIDTH 20 +#define PLAYERS_HEIGHT 12 +#define TIPS_WIDTH 20 +#define TIPS_HEIGHT 8 +#define INPUT_HEIGHT 4 +#define INPUT_WIDTH 80 + const int key_enter = 10; const int key_escape = 27; const int key_backspace = 127; +Client::Client(const char* ip, int port) + : ClientBase(ip, port), in_buf_used(0), exit_flag(false) +{ + chat = new WindowChat(CHAT_HEIGHT, CHAT_WIDTH, 0, 0, 0); + players = new WindowPlayers(PLAYERS_HEIGHT, PLAYERS_WIDTH, 0, 60, 0); + tips = new WindowTips(TIPS_HEIGHT, TIPS_WIDTH, PLAYERS_HEIGHT, 60, 0); + input = new WindowInput(INPUT_HEIGHT, INPUT_WIDTH, 20, 0, 0); +} + +Client::~Client() +{ + if(chat) + delete chat; + if(players) + delete players; + if(tips) + delete tips; + if(input) + delete input; +} + void Client::HandleActions() { - int key = room->InputGetch(); + int key = input->GetChar(); switch(key) { case key_escape: { - this->BreakLoop(); + BreakLoop(); break; } case ' '...'~': { // ascii table 32...126 AddCharToBuffer(key); - room->AddCharToSendMsg(key); + input->AddCharToSendMsg(key); break; } case '\n': { // send message @@ -28,13 +58,13 @@ void Client::HandleActions() memset(in_buffer, 0, (in_buf_used+1)*sizeof(char)); in_buf_used = 0; - room->InputClear(); - room->SetInputCursor(1, 1); + input->Clear(false); + input->SetPosCursor(1, 1); break; } case key_backspace: { RemoveCharFromBuffer(); - room->RemoveCharFromMsg(); + input->RemoveCharFromMsg(); break; } default: break; @@ -43,7 +73,7 @@ void Client::HandleActions() void Client::AddMessage(const char *msg, int type) { - room->AddMessage(msg, type); + chat->AddMessage(msg, type); } void Client::AddCharToBuffer(char ch) diff --git a/src/client/clui/Client.hpp b/src/client/clui/Client.hpp index 37bc3e0..5dbcfd6 100644 --- a/src/client/clui/Client.hpp +++ b/src/client/clui/Client.hpp @@ -4,7 +4,10 @@ #include "../../const_vars.hpp" #include "../ClientBase.hpp" -class ChatRoom; +class WindowChat; +class WindowPlayers; +class WindowTips; +class WindowInput; class Client : public ClientBase { char in_buffer[max_usermsg_len]; // for input @@ -12,12 +15,13 @@ class Client : public ClientBase { bool exit_flag; - ChatRoom *room; + WindowChat *chat; + WindowPlayers *players; + WindowTips *tips; + WindowInput *input; public: - Client(const char* ip, int port, ChatRoom *i_room) - : ClientBase(ip, port), in_buf_used(0), - exit_flag(false), room(i_room) {} - ~Client() {} + Client(const char* ip, int port); + ~Client(); virtual void HandleActions(); virtual void AddMessage(const char *msg, int type); @@ -26,30 +30,4 @@ private: void RemoveCharFromBuffer(); }; - -/*class Client { - int fd; - char in_buffer[max_usermsg_len]; // for input - int in_buf_used; - - char out_buffer[max_msg_len]; // for message - int out_buf_used; - - bool exit_flag; - - Client(int i_fd) : fd(i_fd), in_buf_used(0), - out_buf_used(0), exit_flag(false) {} -public: - ~Client() { close(fd); } - - static Client *Start(const char* ip, int port); - void Run(ChatRoom *room); - void BreakLoop() { exit_flag = true; } - void HandleButton(ChatRoom *room); - - void AddCharToBuffer(char ch); - void RemoveCharFromBuffer(); - void SendMessage(); -};*/ - #endif \ No newline at end of file diff --git a/src/client/clui/Makefile b/src/client/clui/Makefile index b77c8f6..3b0c825 100644 --- a/src/client/clui/Makefile +++ b/src/client/clui/Makefile @@ -1,7 +1,7 @@ CPP = g++ CPPFLAGS = -Wall -g -lncurses -SOURCES = main.cpp clui.cpp Client.cpp ../ClientBase.cpp -OBJECTS = main.o clui.o Client.o ClientBase.o +SOURCES = main.cpp clui.cpp Client.cpp ../ClientBase.cpp WindowInterface.cpp +OBJECTS = main.o clui.o Client.o ClientBase.o WindowInterface.o EXECUTABLE = client .PHONY: all clean diff --git a/src/client/clui/WindowInterface.cpp b/src/client/clui/WindowInterface.cpp new file mode 100644 index 0000000..336d3b1 --- /dev/null +++ b/src/client/clui/WindowInterface.cpp @@ -0,0 +1,33 @@ +#include "WindowInterface.hpp" + +WindowInterface::WindowInterface(int num_y, int num_x, int by, + int bx, char ch) + : ny(num_y), nx(num_x), beg_y(by), beg_x(bx), ch_line(ch) +{ + w = newwin(ny, nx, beg_y, beg_x); + box(w, ch_line, ch_line); + Update(); +} +WindowInterface::~WindowInterface() +{ + Clear(true); + Update(); + delwin(w); +} + +void WindowInterface::SetCursor(int y, int x) +{ + wmove(w, y, x); +} + +void WindowInterface::Clear(bool full) +{ + werase(this->GetWindow()); + if(!full) + box(this->GetWindow(), ch_line, ch_line); +} + +void WindowInterface::Update() +{ + wrefresh(w); +} \ No newline at end of file diff --git a/src/client/clui/WindowInterface.hpp b/src/client/clui/WindowInterface.hpp new file mode 100644 index 0000000..9151637 --- /dev/null +++ b/src/client/clui/WindowInterface.hpp @@ -0,0 +1,22 @@ +#ifndef WC_WINDOW_INTERFACE_H +#define WC_WINDOW_INTERFACE_H + +#include + +class WindowInterface { +protected: + WINDOW *w; + int ny, nx; + int beg_y, beg_x; + int ch_line; +public: + WindowInterface(int num_y, int num_x, int by, int bx, char ch); + ~WindowInterface(); + + WINDOW *GetWindow() { return w; } + void SetCursor(int y, int x); + void Clear(bool full); + void Update(); +}; + +#endif \ No newline at end of file diff --git a/src/client/clui/clui.cpp b/src/client/clui/clui.cpp index a7f908e..0010ff7 100644 --- a/src/client/clui/clui.cpp +++ b/src/client/clui/clui.cpp @@ -1,60 +1,11 @@ #include #include +#include #include "clui.hpp" -#define CHAT_HEIGHT 20 -#define CHAT_WIDTH 59 -#define PLAYERS_WIDTH 20 -#define PLAYERS_HEIGHT 20 -#define INPUT_HEIGHT 4 -#define INPUT_WIDTH 80 - -Interface_wc::Interface_wc(int num_y, int num_x, int by, - int bx, char ch) : ny(num_y), nx(num_x), beg_y(by), - beg_x(bx), ch_line(ch) -{ - w = newwin(ny, nx, beg_y, beg_x); - box(w, ch_line, ch_line); - this->Update(); -} - -void Interface_wc::Hide() -{ - this->Clear(true); - this->Update(); - this->Delete(); -} - -void Interface_wc::Clear(bool full) -{ - werase(this->GetWindow()); - if(!full) - box(this->GetWindow(), ch_line, ch_line); -} - -//////////////////////////////////////////////////////////////////////// - -ChatRoom::ChatRoom() : first(0) -{ - chat = new Interface_wc(CHAT_HEIGHT, CHAT_WIDTH, 0, 0, 0); - players = new Interface_wc(PLAYERS_HEIGHT, PLAYERS_WIDTH, 0, 60, 0); - input = new Interface_wc(INPUT_HEIGHT, INPUT_WIDTH, 20, 0, 0); - nodelay(input->GetWindow(), true); - keypad(input->GetWindow(), true); - i_nx = 1; - i_ny = 1; -} - -ChatRoom::~ChatRoom() +WindowChat::~WindowChat() { - if(chat) - delete chat; - if(players) - delete players; - if(input) - delete input; - while(first) { message *tmp = first; first = first->prev; @@ -62,14 +13,14 @@ ChatRoom::~ChatRoom() } } -void ChatRoom::AddMessage(const char *msg, int type) +void WindowChat::AddMessage(const char *msg, int type) { message *recent_msg = new message; int len = strlen(msg); int lines = (len / oneline_len) + 1; - strcpy(recent_msg->msg, msg); + strcpy(recent_msg->text, msg); recent_msg->num_lines = lines; recent_msg->type = type; @@ -79,12 +30,12 @@ void ChatRoom::AddMessage(const char *msg, int type) ChatRedraw(); } -void ChatRoom::ChatRedraw() +void WindowChat::ChatRedraw() { if(!first) return; - chat->Clear(false); + Clear(false); int available_lines = lines_in_chat; bool remove = false; message *tmp; @@ -112,39 +63,55 @@ void ChatRoom::ChatRedraw() } } - SetInputCursor(i_ny, i_nx); - chat->Update(); + //SetInputCursor(i_ny, i_nx); + Update(); } -void ChatRoom::PrintMessage(int line, message *m) +void WindowChat::PrintMessage(int line, message *m) { - WINDOW *win = this->chat->GetWindow(); int need_print = m->num_lines; - while(need_print != 0) { - if(m->type == system_msg) wattron(win, A_ITALIC); - else wattron(win, A_BOLD); - wmove(win, line-need_print+1, 1); + if(m->type == system_msg) wattron(w, A_ITALIC); + else wattron(w, A_BOLD); + wmove(w, line-need_print+1, 1); char *tmp = new char[oneline_len]; int str = oneline_len * (m->num_lines - need_print); - memcpy(tmp, m->msg + str, oneline_len); + memcpy(tmp, m->text + str, oneline_len); - wprintw(win, tmp); - if(m->type == system_msg) wattroff(win, A_ITALIC); - else wattroff(win, A_BOLD); + wprintw(w, tmp); + if(m->type == system_msg) wattroff(w, A_ITALIC); + else wattroff(w, A_BOLD); delete[] tmp; need_print--; } } -bool ChatRoom::AddCharToSendMsg(char ch) +//////////////////////////////////////////////////////////////////////// + + + +//////////////////////////////////////////////////////////////////////// + +WindowInput::WindowInput(int num_y, int num_x, int by, int bx, char ch) + : WindowInterface(num_y, num_x, by, bx, ch), i_ny(1), i_nx(1) +{ + nodelay(w, true); + keypad(w, true); +} + +int WindowInput::GetChar() +{ + return wgetch(w); +} + +bool WindowInput::AddCharToSendMsg(char ch) { if(i_ny == 2 && i_nx == max_usermsg_len/2-1) return 0; - mvwaddch(input->GetWindow(), i_ny, i_nx, ch); + mvwaddch(w, i_ny, i_nx, ch); i_nx++; if(i_nx >= max_usermsg_len/2-1) { if(i_ny == 1) { @@ -153,14 +120,14 @@ bool ChatRoom::AddCharToSendMsg(char ch) } else if(i_ny == 2) i_nx--; - wmove(input->GetWindow(), i_ny, i_nx); + wmove(w, i_ny, i_nx); } - input->Update(); + Update(); return 1; } -bool ChatRoom::RemoveCharFromMsg() +bool WindowInput::RemoveCharFromMsg() { if(i_ny == 1 && i_nx == 1) return 0; @@ -170,16 +137,16 @@ bool ChatRoom::RemoveCharFromMsg() i_ny--; i_nx = max_usermsg_len/2-1; } - mvwaddch(input->GetWindow(), i_ny, i_nx, ' '); - wmove(input->GetWindow(), i_ny, i_nx); + mvwaddch(w, i_ny, i_nx, ' '); + wmove(w, i_ny, i_nx); - input->Update(); + Update(); return 1; } -void ChatRoom::SetInputCursor(int y, int x) +void WindowInput::SetPosCursor(int y, int x) { - input->SetCursor(y, x); + SetCursor(y, x); i_ny = y; i_nx = x; } \ No newline at end of file diff --git a/src/client/clui/clui.hpp b/src/client/clui/clui.hpp index e337aa9..87445db 100644 --- a/src/client/clui/clui.hpp +++ b/src/client/clui/clui.hpp @@ -3,59 +3,52 @@ #ifndef WC_CLUI_H #define WC_CLUI_H -#include +#include "WindowInterface.hpp" #include "../../const_vars.hpp" -class Interface_wc { - WINDOW *w; - int ny, nx; - int beg_y, beg_x; - int ch_line; -public: - Interface_wc(int num_y, int num_x, int by, int bx, char ch); - - WINDOW *GetWindow() { return w; } - void SetCursor(int y, int x) { wmove(w, y, x); } - void Clear(bool full); - void Update() { wrefresh(w); } - void Delete() { delwin(w); } - - void Hide(); -}; - -class ChatRoom { - Interface_wc *chat; - Interface_wc *players; - Interface_wc *input; - int i_nx, i_ny; - +class WindowChat : public WindowInterface { struct message { - char msg[max_msg_len]; + char text[max_msg_len]; int num_lines; // number of lines int type; message *prev; }; message *first; public: - ChatRoom(); - ~ChatRoom(); + WindowChat(int num_y, int num_x, int by, int bx, char ch) + : WindowInterface(num_y, num_x, by, bx, ch), first(0) {} + ~WindowChat(); - // for chat: void AddMessage(const char *msg, int type); +private: + void ChatRedraw(); + void PrintMessage(int line, message *m); +}; - // for players: - //void AddPlayer() +class WindowPlayers : public WindowInterface { +public: + WindowPlayers(int num_y, int num_x, int by, int bx, char ch) + : WindowInterface(num_y, num_x, by, bx, ch) {} + ~WindowPlayers() {} +}; + +class WindowTips : public WindowInterface { +public: + WindowTips(int num_y, int num_x, int by, int bx, char ch) + : WindowInterface(num_y, num_x, by, bx, ch) {} + ~WindowTips() {} +}; - // for input: - int InputGetch() { return wgetch(input->GetWindow()); } +class WindowInput : public WindowInterface { + int i_ny, i_nx; +public: + WindowInput(int num_y, int num_x, int by, int bx, char ch); + ~WindowInput() {} + + int GetChar(); bool AddCharToSendMsg(char ch); bool RemoveCharFromMsg(); - void InputClear() { input->Clear(false); } - void SetInputCursor(int y, int x); -private: - // for chat: - void ChatRedraw(); - void PrintMessage(int line, message *m); + void SetPosCursor(int y, int x); }; #endif \ No newline at end of file diff --git a/src/client/clui/main.cpp b/src/client/clui/main.cpp index fd77708..876e6f9 100644 --- a/src/client/clui/main.cpp +++ b/src/client/clui/main.cpp @@ -5,7 +5,6 @@ #include "../../config.hpp" #include "Client.hpp" -#include "clui.hpp" int main(int argc, char *argv[]) { @@ -20,8 +19,7 @@ int main(int argc, char *argv[]) return 1; } - ChatRoom *room = new ChatRoom(); - Client *user = new Client(SERVER_IP, SERVER_PORT, room); + Client *user = new Client(SERVER_IP, SERVER_PORT); if(user->ConstuctorError()) { endwin(); perror("server"); @@ -29,7 +27,6 @@ int main(int argc, char *argv[]) } while(user->Run()); - delete room; delete user; endwin(); -- cgit v1.2.3-18-g5258