From 8dfca4755430209504dac5ed2e03a482342265b3 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Wed, 25 Nov 2020 00:18:43 +0300 Subject: client: remove menu, all actions move to chat, fix chat move --- src/client/client.cpp | 63 +++------------------------ src/client/clui.cpp | 117 ++++++++++++++++---------------------------------- src/client/clui.hpp | 13 ------ src/client/user.cpp | 14 ++++-- src/client/user.hpp | 13 +++--- 5 files changed, 61 insertions(+), 159 deletions(-) (limited to 'src') diff --git a/src/client/client.cpp b/src/client/client.cpp index 8ad25d8..8f6a9ff 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -1,45 +1,18 @@ #include +#include #include #include #include "user.hpp" -#define EXIT_BUTTON 4 #define SERVER_IP "127.0.0.1" -static int port = 7777; - -int showMainMenu(int max_row, int max_col) -{ - int num_items = 5; - const char *items[num_items] = { - "Connect to room", - "Create room", - "Help", - "About WantChat", - "Exit" - }; - - int height_menu = num_items + 2; - int width_menu = strlen(items[0]) + 2; - - SelectionMenu main_menu = SelectionMenu("Hello. Welcome to \ - WantChat", items, num_items, height_menu, width_menu, - (max_row-height_menu)/2, (max_col-width_menu)/2, 0); - main_menu.Update(); - - keypad(main_menu.GetWindow(), true); - int choice = main_menu.Handling(); - main_menu.Hide(); - return choice; - // дескриптор будет вызван после выхода из функции неявно -} +static int port = 3030; int main(int argc, char *argv[]) { initscr(); cbreak(); noecho(); - // keypad(stdscr, TRUE); curs_set(false); int rows, columns; @@ -57,34 +30,10 @@ int main(int argc, char *argv[]) return 1; } - int choice; - while( (choice = showMainMenu(rows, columns)) != EXIT_BUTTON) - { - switch(choice) - { - case 0: { - ChatRoom *room = new ChatRoom(); - user->Run(room); - - delete room; - break; - } - } - } + ChatRoom *room = new ChatRoom(); + user->Run(room); + delete room; endwin(); return 0; -} - - /*WINDOW *chat = newwin(21, 59, 0, 0); - box(chat, 0, 0); - wrefresh(chat); - - WINDOW *players = newwin(21, 20, 0, 60); - box(players, 0, 0); - wrefresh(players); - - WINDOW *input = newwin(2, 80, 22, 0); - //box(input, ' ', ' '); - wmove(input, 0, 2); - wrefresh(input);*/ \ No newline at end of file +} \ No newline at end of file diff --git a/src/client/clui.cpp b/src/client/clui.cpp index e0b528b..12bae03 100644 --- a/src/client/clui.cpp +++ b/src/client/clui.cpp @@ -3,6 +3,7 @@ #include #include "clui.hpp" +#include "../const_vars.hpp" #define CHAT_HEIGHT 20 #define CHAT_WIDTH 59 @@ -11,9 +12,6 @@ #define INPUT_HEIGHT 4 #define INPUT_WIDTH 80 -const int maxlen_msg_input = (INPUT_WIDTH - 2) * 2; -const int maxlen_msg_chat = (CHAT_WIDTH - 4) * 2; - 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) @@ -39,47 +37,6 @@ void Interface_wc::Clear(bool full) //////////////////////////////////////////////////////////////////////// -SelectionMenu::SelectionMenu(char const *i_title, const char**i_choises, int choises_num, - int num_y, int num_x, int by, int bx, char ch) - : Interface_wc(num_y, num_x, by, bx, ch), title(i_title), - choises(i_choises), choises_number(choises_num), current_choice(0) -{ } - -int SelectionMenu::Handling() -{ - WINDOW *menu = this->GetWindow(); - while(true) { - for(int i = 0; i < choises_number; i++) - { - // remake: - if(i == current_choice) - wattron(menu, A_REVERSE); - mvwprintw(menu, i+1, 1, choises[i]); - wattroff(menu, A_REVERSE); - } - - switch(wgetch(menu)) - { - case KEY_UP: - { - current_choice--; - if(current_choice < 0) current_choice = choises_number-1; - break; - } - case KEY_DOWN: - { - current_choice++; - if(current_choice > choises_number-1) current_choice = 0; - break; - } - case '\n': return current_choice; - default: break; - } - } -} - -//////////////////////////////////////////////////////////////////////// - ChatRoom::ChatRoom() : first(0) { chat = new Interface_wc(CHAT_HEIGHT, CHAT_WIDTH, 0, 0, 0); @@ -107,8 +64,9 @@ void ChatRoom::AddMessage(char *msg) message *recent_msg = new message; int lines = 1; - if(strlen(msg) > maxlen_msg_chat/2) - lines = 2; + int len = strlen(msg); + if(len > CHAT_WIDTH-2 + CHAT_WIDTH-2) lines = 3; + else if(len > CHAT_WIDTH-2) lines = 2; strcpy(recent_msg->msg, msg); recent_msg->num_lines = lines; @@ -124,65 +82,66 @@ void ChatRoom::ChatRedraw() if(!first) return; - /*wprintw(chat->GetWindow(), "redraw"); - chat->Update(); - sleep(3);*/ - chat->Clear(false); int available_lines = CHAT_HEIGHT - 2; - bool remove = 0; + bool remove = false; message *tmp; - for(tmp = first; tmp; tmp = tmp->prev) { - if(available_lines >= tmp->num_lines) { - PrintMessage(available_lines, tmp); - available_lines -= tmp->num_lines; + message *last; + for(message *m = first; m; m = m->prev) { + if(available_lines-1 >= m->num_lines) { + PrintMessage(available_lines, m); + available_lines -= m->num_lines; + last = m; } - // #TODO: - /* delete */ + else { + tmp = m; + remove = true; + break; + } + } + if(remove) { + last->prev = 0; + while(tmp) { + message *m = tmp; + tmp = tmp->prev; + delete m; + } } + SetInputCursor(i_ny, i_nx); chat->Update(); } void ChatRoom::PrintMessage(int line, message *m) { WINDOW *win = this->GetChatWin(); - if(m->num_lines == 1) { - wmove(win, line, 2); - wprintw(win, m->msg); - } - else { - wmove(win, line-1, 2); - // print a half of message: - int len_fline = maxlen_msg_chat/2; + int need_print = m->num_lines; + int maxlen_oneline = CHAT_WIDTH-2; + while(need_print != 0) { + wmove(win, line-need_print+1, 1); - char *temp = new char[len_fline+1]; - // memccpy(void *restrict s1, const void *restrict s2, int c, size_t n); - memcpy(temp, m->msg, sizeof(char) * len_fline); - temp[len_fline] = '\0'; - wprintw(win, temp); + char *tmp = new char[maxlen_oneline]; + int str = maxlen_oneline * (m->num_lines - need_print); + memcpy(tmp, m->msg + str, maxlen_oneline); - int last_len_msg = strlen(m->msg) - len_fline; - memcpy(temp, m->msg, sizeof(char) * (last_len_msg+1)); - temp[last_len_msg] = '\0'; - wmove(win, line, 2); - wprintw(win, temp); + wprintw(win, tmp); + need_print--; - delete[] temp; + delete[] tmp; } } bool ChatRoom::AddCharToSendMsg(char ch) { - if(i_ny == 2 && i_nx == maxlen_msg_input/2-1) + if(i_ny == 2 && i_nx == max_line_len/2-1) return 0; mvwaddch(input->GetWindow(), i_ny, i_nx, ch); i_nx++; - if(i_nx >= maxlen_msg_input/2-1) { + if(i_nx >= max_line_len/2-1) { if(i_ny == 1) { i_ny++; i_nx = 1; @@ -204,7 +163,7 @@ bool ChatRoom::RemoveCharFromMsg() i_nx--; if(i_nx < 1) { i_ny--; - i_nx = maxlen_msg_input/2-1; + i_nx = max_line_len/2-1; } mvwaddch(input->GetWindow(), i_ny, i_nx, ' '); wmove(input->GetWindow(), i_ny, i_nx); diff --git a/src/client/clui.hpp b/src/client/clui.hpp index 3ebefda..de47177 100644 --- a/src/client/clui.hpp +++ b/src/client/clui.hpp @@ -22,19 +22,6 @@ public: void Hide(); }; -class SelectionMenu : public Interface_wc { - char const *title; - const char**choises; - int choises_number; - int current_choice; - -public: - SelectionMenu(char const *i_title, const char**i_choises, int choises_num, - int num_y, int num_x, int by, int bx, char ch); - - int Handling(); -}; - class ChatRoom { Interface_wc *chat; Interface_wc *players; diff --git a/src/client/user.cpp b/src/client/user.cpp index 85b626e..91b2450 100644 --- a/src/client/user.cpp +++ b/src/client/user.cpp @@ -10,6 +10,7 @@ #include "user.hpp" +const int key_esc = 4; const int key_enter = 10; const int key_escape = 27; const int key_backspace = 127; @@ -57,7 +58,11 @@ void Client::Run(ChatRoom *room) break; } else if(recive > 0) { + /* warning: if we get a message >= maxlen_outbuf then + this code will not work */ + out_buffer[recive-1] = '\0'; // change '\n' to '\0' room->AddMessage(out_buffer); + memset(out_buffer, 0, recive); } usleep(usecs); @@ -69,6 +74,10 @@ void Client::HandleButton(ChatRoom *room) int key = wgetch(room->GetInputWin()); switch(key) { + case key_esc: { + // #todo: exit + break; + } // ascii table 32...126 case ' '...'~': { AddCharToBuffer(key); @@ -92,7 +101,7 @@ void Client::HandleButton(ChatRoom *room) void Client::AddCharToBuffer(char ch) { - if(in_buf_used >= max_line_length-2) // we reserve 1 byte for '\0' + if(in_buf_used >= maxlen_inbuf-1) return; in_buffer[in_buf_used] = ch; @@ -114,9 +123,8 @@ void Client::SendMessage() return; in_buffer[in_buf_used] = '\n'; - // if(max_line_length-2 < 0) write(fd, in_buffer, (in_buf_used+1)*sizeof(char)); - memset(in_buffer, 0, sizeof(in_buffer)); + memset(in_buffer, 0, (in_buf_used+1)*sizeof(char)); in_buf_used = 0; } \ No newline at end of file diff --git a/src/client/user.hpp b/src/client/user.hpp index 45fb5e3..03afb87 100644 --- a/src/client/user.hpp +++ b/src/client/user.hpp @@ -2,21 +2,22 @@ #define USER_H #include "clui.hpp" +#include "../const_vars.hpp" -const int max_line_length = 156; +const int maxlen_inbuf = max_line_len + max_name_len + 1; +const int maxlen_outbuf = max_line_len + 1; class Client { int fd; - char in_buffer[max_line_length]; // мы готовим к отправке + char in_buffer[maxlen_inbuf]; // for input int in_buf_used; - char out_buffer[max_line_length]; // нам пришло - int out_buf_used; + char out_buffer[maxlen_outbuf]; // for message bool exit_flag; Client(int i_fd) - : fd(i_fd), in_buf_used(0), out_buf_used(0) { } + : fd(i_fd), in_buf_used(0) { } public: ~Client() { close(fd); } @@ -27,8 +28,6 @@ public: void AddCharToBuffer(char ch); void RemoveCharFromBuffer(); void SendMessage(); - - int getFd() const { return fd; } // not used }; #endif \ No newline at end of file -- cgit v1.2.3-18-g5258