From 9f6cdb671ca3c045085bd8eed623e0a797894e1f Mon Sep 17 00:00:00 2001 From: Joursoir Date: Sat, 12 Dec 2020 12:41:39 +0000 Subject: GUI feature: handle players and online info from server, show it; fix: close app; --- src/client/dimensions_client.hpp | 16 +++++++ src/client/gui/Client.cpp | 74 +++++++++++++++++++++++++++---- src/client/gui/Client.hpp | 20 ++++++--- src/client/gui/OO_FLTK.cpp | 1 + src/client/gui/main.cpp | 95 +++++++++++++++++++++++++++------------- 5 files changed, 159 insertions(+), 47 deletions(-) create mode 100644 src/client/dimensions_client.hpp diff --git a/src/client/dimensions_client.hpp b/src/client/dimensions_client.hpp new file mode 100644 index 0000000..1cf8d36 --- /dev/null +++ b/src/client/dimensions_client.hpp @@ -0,0 +1,16 @@ +#ifndef WC_DIMENSIONS_CLIENT_H +#define WC_DIMENSIONS_CLIENT_H + +const int CHAT_LINES = 18; +const int CHAT_COLUMNS = 57; + +const int PLAYERS_LINES = 10; +const int PLAYERS_COLUMNS = 18; // must be >= max_name_len + +const int TIPS_LINES = 6; +const int TIPS_COLUMNS = PLAYERS_COLUMNS; + +const int TIPS_LINE_GONLINE = 2; +const int TIPS_LINE_RONLINE = 3; + +#endif \ No newline at end of file diff --git a/src/client/gui/Client.cpp b/src/client/gui/Client.cpp index 137558d..1d743ae 100644 --- a/src/client/gui/Client.cpp +++ b/src/client/gui/Client.cpp @@ -1,14 +1,69 @@ +#include "../dimensions_client.hpp" #include "Client.hpp" -void Client::AddMessage(const char *msg, int type) +void Client::AddMessage(const char *msg, const char spec_char) +{ + if(spec_char == USERS_CHAR) + UpdatePlayerList(msg); + else if(spec_char == GONLINE_CHAR) + SetGeneralOnline(msg); + else if(spec_char == RONLINE_CHAR) + SetRoomOnline(msg); + else + UpdateMessagesInChat(msg, spec_char); +} + +void Client::UpdatePlayerList(const char *list) +{ + // clear line: + for(int i = 0; i < PLAYERS_LINES-1; i++) + players[i]->value(""); + + + int len_list = strlen(list); + int p = 0; + int start = 0; + for(int i = 0; i < len_list; i++) { + if(list[i] == ';') + { + char *str = new char[max_name_len+1]; + int size = i - start; + memcpy(str, list+start, size); + str[size] = '\0'; + + players[p]->value(str); + start = i+1; + p++; + delete[] str; + } + } +} + +void Client::SetGeneralOnline(const char *online) +{ + char *str = new char[max_name_len+1]; + sprintf(str, "Online: %s", online); + tips[TIPS_LINE_GONLINE-1]->value(str); + delete[] str; +} + +void Client::SetRoomOnline(const char *online) +{ + char *str = new char[max_name_len+1]; + sprintf(str, "Online room: %s", online); + tips[TIPS_LINE_RONLINE-1]->value(str); + delete[] str; +} + +void Client::UpdateMessagesInChat(const char *msg, const char spec_char) { int len_msg = strlen(msg); char *source = new char[len_msg+1]; strcpy(source, msg); - int lines = (len_msg / oneline_len) + 1; + int lines = (len_msg / CHAT_COLUMNS) + 1; - for(int i = lines; i < lines_in_chat; i++) { + for(int i = lines; i < CHAT_LINES; i++) { chat[i-lines]->value(chat[i]->value()); chat[i-lines]->textfont(chat[i]->textfont()); } @@ -17,17 +72,18 @@ void Client::AddMessage(const char *msg, int type) while(need_print > 0) { int len = strlen(source); - int size = len > oneline_len ? oneline_len : len; + int size = len > CHAT_COLUMNS ? CHAT_COLUMNS : len; - char *str = new char[oneline_len + 1]; - int str_ptr = oneline_len * (lines - need_print); - memcpy(str, source + str_ptr, oneline_len); + char *str = new char[CHAT_COLUMNS + 1]; + int str_ptr = CHAT_COLUMNS * (lines - need_print); + memcpy(str, source + str_ptr, CHAT_COLUMNS); str[size] = '\0'; int spec = 0; - if(type == system_msg) spec = FL_ITALIC; + if(spec_char == SYSTEM_CHAR) spec = FL_ITALIC; + else spec = FL_BOLD; - int p = lines_in_chat-need_print; + int p = CHAT_LINES-need_print; chat[p]->textfont(STD_FONT+spec); chat[p]->value(str); delete[] str; diff --git a/src/client/gui/Client.hpp b/src/client/gui/Client.hpp index a8d2376..75144b7 100644 --- a/src/client/gui/Client.hpp +++ b/src/client/gui/Client.hpp @@ -7,17 +7,23 @@ class Client : public ClientBase { ChatBaseOutput **chat; + ChatBaseOutput **players; + ChatBaseOutput **tips; public: - Client(const char* ip, int port, ChatBaseOutput **cb_out) - : ClientBase(ip, port), chat(cb_out) - { - for(int i = 0; i < lines_in_chat; i++) - cb_out[i]->value(""); - } + Client(const char* ip, int port, ChatBaseOutput **i_chat, + ChatBaseOutput **i_players, ChatBaseOutput **i_tips) + : ClientBase(ip, port), chat(i_chat), players(i_players), + tips(i_tips) { } ~Client() {} virtual void HandleActions() {} - virtual void AddMessage(const char *msg, int type); + virtual void AddMessage(const char *msg, const char spec_char); + +private: + void UpdatePlayerList(const char *list); + void SetGeneralOnline(const char *online); + void SetRoomOnline(const char *online); + void UpdateMessagesInChat(const char *msg, const char spec_char); }; #endif \ No newline at end of file diff --git a/src/client/gui/OO_FLTK.cpp b/src/client/gui/OO_FLTK.cpp index a86ecd6..6492481 100644 --- a/src/client/gui/OO_FLTK.cpp +++ b/src/client/gui/OO_FLTK.cpp @@ -53,4 +53,5 @@ ChatBaseOutput::ChatBaseOutput(int x, int y, int w, int h, const char *lb) textfont(STD_FONT); textsize(20); textcolor(FL_WHITE); + value(""); } \ No newline at end of file diff --git a/src/client/gui/main.cpp b/src/client/gui/main.cpp index 211e020..50aa02f 100644 --- a/src/client/gui/main.cpp +++ b/src/client/gui/main.cpp @@ -3,8 +3,9 @@ #include #include -#include "OO_FLTK.hpp" #include "../../config.hpp" +#include "../dimensions_client.hpp" +#include "OO_FLTK.hpp" #include "Client.hpp" const int spacing = 20; @@ -13,66 +14,98 @@ const int indent = 5; const int chat_w = 695; const int chat_h = 440; -const int chat_line_h = 440 / lines_in_chat; -const int player_w = 225; -const int player_h = chat_h; -const int input_w = chat_w + border + spacing + border + player_w; -const int input_h = 40; // 55 +const int chat_line_h = chat_h / CHAT_LINES; + +const int players_w = 225; +const int players_h = chat_line_h*PLAYERS_LINES+8; + +const int tips_w = players_w; +const int tips_h = chat_line_h*TIPS_LINES+8; + +const int input_w = chat_w + border + spacing + border + players_w; +const int input_h = 40; const int win_w = spacing + border + (input_w) + border + spacing; const int win_h = spacing + border + chat_h + border + spacing + border + input_h + border + spacing; - int main(int argc, char **argv) { Fl_Window *win = new Fl_Window(win_w, win_h, "WantChat"); win->color(FL_BLACK); - // - + // Draw chat: BoxBackground ol_chat(spacing, spacing, - chat_w+border*2, chat_h+border*2); - BoxBackground ol_black(spacing+border, spacing+border, + chat_w + border*2, chat_h + border*2); + BoxBackground ol_chat_black(spacing + border, spacing + border, chat_w, chat_h, 0, FL_BLACK); - ChatBaseOutput **chat = new ChatBaseOutput*[18]; - int y = 0; - for(int i = 0 ; i < 18; i++) { - chat[i] = new ChatBaseOutput(spacing+border+indent, - spacing+border+y, chat_w-indent, chat_line_h); - y += chat_line_h; + ChatBaseOutput **chat = new ChatBaseOutput*[CHAT_LINES]; + for(int i = 0, y = 0; i < CHAT_LINES; i++, y += chat_line_h) { + chat[i] = new ChatBaseOutput(spacing + border + indent, + spacing + border + y, chat_w - indent, chat_line_h); } + // End draw chat - // - + // Draw players: int start_players_x = spacing + border + chat_w + border + spacing; - BoxBackground ol_players(start_players_x, spacing, player_w+border*2, player_h+border*2); - ChatBaseOutput *players = new ChatBaseOutput( - start_players_x + border, spacing+border, player_w, player_h); - - // + BoxBackground ol_players(start_players_x, spacing, + players_w + border*2, players_h + border*2); + BoxBackground ol_players_black(start_players_x + border, + spacing + border, players_w, players_h, 0, FL_BLACK); + + ChatBaseOutput **players = new ChatBaseOutput*[PLAYERS_LINES]; + for(int i = 0, y = 0; i < PLAYERS_LINES; i++, y+= chat_line_h) { + players[i] = new ChatBaseOutput(start_players_x + border + indent, + spacing + border + y, players_w - indent, chat_line_h); + } + players[PLAYERS_LINES-1]->value("<- /prev /next ->"); + // End draw players + + // Draw tips: + int start_tips_y = spacing + border + players_h + border + spacing + 16; // please, don't ask where i took 16 + BoxBackground ol_tips(start_players_x, start_tips_y, + tips_w + border*2, tips_h + border*2); + BoxBackground ol_tips_black(start_players_x + border, + start_tips_y + border, tips_w, tips_h, 0, FL_BLACK); + + ChatBaseOutput **tips = new ChatBaseOutput*[TIPS_LINES]; + for(int i = 0, y = 0; i < TIPS_LINES; i++, y += chat_line_h) { + tips[i] = new ChatBaseOutput(start_players_x + border + indent, + start_tips_y + border + y, tips_w - indent, chat_line_h); + } + tips[TIPS_LINES-6]->value(" Tips"); + tips[TIPS_LINE_GONLINE-1]->value("Online: "); + tips[TIPS_LINE_RONLINE-1]->value("Online room: "); + tips[TIPS_LINES-3]->value(""); + tips[TIPS_LINES-2]->value("ESC - exit"); + tips[TIPS_LINES-1]->value("ENTER - send msg"); + // End draw tips - Client *user = new Client(SERVER_IP, SERVER_PORT, chat); + Client *user = new Client(SERVER_IP, SERVER_PORT, chat, players, tips); + // Draw input: int start_input_y = spacing + border + chat_h + border + spacing; - BoxBackground ol_input(spacing, start_input_y, input_w+border*2, input_h+border*2); - ChatInput input(spacing+border, start_input_y+border, input_w, input_h, 0, user); - - // + BoxBackground ol_input(spacing, start_input_y, + input_w + border*2, input_h + border*2); + ChatInput input(spacing + border, start_input_y + border, + input_w, input_h, 0, user); + // End draw input win->end(); win->show(); if(user->ConstuctorError()) { - fl_choice("Server error", "Exit", 0, 0); + fl_choice("Application cannot connect to WantChat server\n\ + Sorry, try later :(", "Exit", 0, 0); win->hide(); } while(user->Run()) { Fl::wait(0); + if(!win->visible_r()) + break; } - return 0; } \ No newline at end of file -- cgit v1.2.3-18-g5258