From 1baf22ad58cc1a9aa9089ca9a09fc80a453cb3c9 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Wed, 9 Dec 2020 16:12:53 +0000 Subject: gui: receive msg from server to chat and move it --- src/client/gui/Client.cpp | 37 +++++++++++++++++++++++++++++++++++-- src/client/gui/Client.hpp | 13 +++++++++---- src/client/gui/OO_FLTK.cpp | 26 +++++++++++++++++--------- src/client/gui/OO_FLTK.hpp | 18 ++++++++++++------ src/client/gui/main.cpp | 46 ++++++++++++++++++++++------------------------ 5 files changed, 95 insertions(+), 45 deletions(-) (limited to 'src/client/gui') diff --git a/src/client/gui/Client.cpp b/src/client/gui/Client.cpp index 2465ab7..137558d 100644 --- a/src/client/gui/Client.cpp +++ b/src/client/gui/Client.cpp @@ -1,6 +1,39 @@ #include "Client.hpp" -void Client::ShowMessage(const char *msg) +void Client::AddMessage(const char *msg, int type) { - chat->value(msg); + int len_msg = strlen(msg); + char *source = new char[len_msg+1]; + strcpy(source, msg); + + int lines = (len_msg / oneline_len) + 1; + + for(int i = lines; i < lines_in_chat; i++) { + chat[i-lines]->value(chat[i]->value()); + chat[i-lines]->textfont(chat[i]->textfont()); + } + + int need_print = lines; + while(need_print > 0) + { + int len = strlen(source); + int size = len > oneline_len ? oneline_len : len; + + char *str = new char[oneline_len + 1]; + int str_ptr = oneline_len * (lines - need_print); + memcpy(str, source + str_ptr, oneline_len); + str[size] = '\0'; + + int spec = 0; + if(type == system_msg) spec = FL_ITALIC; + + int p = lines_in_chat-need_print; + chat[p]->textfont(STD_FONT+spec); + chat[p]->value(str); + delete[] str; + + need_print--; + } + + delete[] source; } \ No newline at end of file diff --git a/src/client/gui/Client.hpp b/src/client/gui/Client.hpp index 1ab7a84..a8d2376 100644 --- a/src/client/gui/Client.hpp +++ b/src/client/gui/Client.hpp @@ -1,18 +1,23 @@ #ifndef WC_GUI_CLIENT_H #define WC_GUI_CLIENT_H +#include "../../const_vars.hpp" #include "../ClientBase.hpp" #include "OO_FLTK.hpp" class Client : public ClientBase { - ChatBaseOutput *chat; + ChatBaseOutput **chat; public: - Client(const char* ip, int port, ChatBaseOutput *cb_out) - : ClientBase(ip, port), chat(cb_out) {} + 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() {} virtual void HandleActions() {} - virtual void ShowMessage(const char *msg); + virtual void AddMessage(const char *msg, int type); }; #endif \ No newline at end of file diff --git a/src/client/gui/OO_FLTK.cpp b/src/client/gui/OO_FLTK.cpp index 9dc088d..a86ecd6 100644 --- a/src/client/gui/OO_FLTK.cpp +++ b/src/client/gui/OO_FLTK.cpp @@ -1,24 +1,27 @@ #include "OO_FLTK.hpp" #include "../../const_vars.hpp" +#include "Client.hpp" -BoxOutline::BoxOutline(int x, int y, int w, int h, const char *lb) +BoxBackground::BoxBackground(int x, int y, int w, int h, + const char *lb, Fl_Color clr) : Fl_Box(x, y, w, h, lb) { box(FL_FLAT_BOX); - color(FL_WHITE); + color(clr); } -ChatInput::ChatInput(int x, int y, int w, int h, const char *lb) +ChatInput::ChatInput(int x, int y, int w, int h, + const char *lb, Client *user) : Fl_Input(x, y, w, h, lb) { - callback(CallbackFunction, 0); + callback(CallbackFunction, (void *)user); when(FL_WHEN_ENTER_KEY | FL_WHEN_NOT_CHANGED); box(FL_FLAT_BOX); color(FL_BLACK); cursor_color(FL_WHITE); - textfont(FL_COURIER); + textfont(STD_FONT); textsize(20); textcolor(FL_WHITE); @@ -30,19 +33,24 @@ void ChatInput::SendMessage(void *user) if(strlen(value()) < 1) return; - printf("SendMessage\n"); - // send message to server + Client *cl = (Client *)user; + if(cl != 0) { // send message to server + const char *message = value(); + printf("U sent: %s\n", message); + cl->SendMessage(message); + } + value(""); take_focus(); } ChatBaseOutput::ChatBaseOutput(int x, int y, int w, int h, const char *lb) - : Fl_Multiline_Output(x, y, w, h, lb) + : Fl_Output(x, y, w, h, lb) { box(FL_FLAT_BOX); color(FL_BLACK); - textfont(FL_COURIER); + textfont(STD_FONT); textsize(20); textcolor(FL_WHITE); } \ No newline at end of file diff --git a/src/client/gui/OO_FLTK.hpp b/src/client/gui/OO_FLTK.hpp index b581321..a2f7183 100644 --- a/src/client/gui/OO_FLTK.hpp +++ b/src/client/gui/OO_FLTK.hpp @@ -4,17 +4,23 @@ #include #include #include -#include +#include -class BoxOutline : public Fl_Box { +#define STD_FONT FL_COURIER + +class Client; + +class BoxBackground : public Fl_Box { public: - BoxOutline(int x, int y, int w, int h, const char *lb = 0); - ~BoxOutline() {} + BoxBackground(int x, int y, int w, int h, + const char *lb = 0, Fl_Color clr = FL_WHITE); + ~BoxBackground() {} }; class ChatInput : public Fl_Input { public: - ChatInput(int x, int y, int w, int h, const char *lb = 0); + ChatInput(int x, int y, int w, int h, + const char *lb = 0, Client *user = 0); virtual ~ChatInput() {} virtual void SendMessage(void *user); private: @@ -22,7 +28,7 @@ private: { static_cast(w)->SendMessage(user); } }; -class ChatBaseOutput : public Fl_Multiline_Output { +class ChatBaseOutput : public Fl_Output { public: ChatBaseOutput(int x, int y, int w, int h, const char *lb = 0); ~ChatBaseOutput() {} diff --git a/src/client/gui/main.cpp b/src/client/gui/main.cpp index 3839454..211e020 100644 --- a/src/client/gui/main.cpp +++ b/src/client/gui/main.cpp @@ -9,9 +9,11 @@ const int spacing = 20; const int border = 2; +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; @@ -21,6 +23,7 @@ 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"); @@ -28,52 +31,47 @@ int main(int argc, char **argv) // - BoxOutline ol_chat(spacing, spacing, chat_w+border*2, chat_h+border*2); - ChatBaseOutput *chat = new ChatBaseOutput( - spacing+border, spacing+border, chat_w, chat_h); - - // only for test, please not going to beat me :) - /*char buffer[2048] = ""; - for(int i = 0; i < 17; i++) - sprintf(buffer, "%s\n", buffer); - sprintf(buffer, "%sWelcome to WantChat! What is your name?\n", buffer); - chat->value(buffer);*/ + BoxBackground ol_chat(spacing, spacing, + chat_w+border*2, chat_h+border*2); + BoxBackground ol_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; + } // int start_players_x = spacing + border + chat_w + border + spacing; - BoxOutline ol_players(start_players_x, spacing, player_w+border*2, player_h+border*2); + 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); - // only for test, please not going to beat me :) (x2) - /*char buffer1[1024] = ""; - for(int i = 0; i < 18; i++) - sprintf(buffer1, "%sHackerspronickname\n", buffer1); - players->value(buffer1);*/ - // + Client *user = new Client(SERVER_IP, SERVER_PORT, chat); + int start_input_y = spacing + border + chat_h + border + spacing; - BoxOutline ol_input(spacing, start_input_y, input_w+border*2, input_h+border*2); - ChatInput *input = new ChatInput(spacing+border, - start_input_y+border, input_w, input_h); + 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); // win->end(); win->show(); - Client *user = new Client(SERVER_IP, SERVER_PORT, chat); if(user->ConstuctorError()) { - perror("server"); fl_choice("Server error", "Exit", 0, 0); win->hide(); } - while(Fl::wait()) + while(user->Run()) { - user->Run(); + Fl::wait(0); } return 0; -- cgit v1.2.3-18-g5258