summaryrefslogtreecommitdiffstats
path: root/src/client/clui/Client.cpp
blob: fa89710b5cd165410b792e55f4f65902c3a5237d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <string.h>

#include "../dimensions_client.hpp"
#include "Client.hpp"
#include "clui.hpp"

const int input_lines = 2;
const int input_columns = 78;
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)
{
	// y+2 and x+2 for outlines
	chat = new WindowChat(CHAT_LINES+2, CHAT_COLUMNS+2, 0, 0, 0);
	players = new WindowPlayers(PLAYERS_LINES+2, PLAYERS_COLUMNS+2, 0, CHAT_COLUMNS+2+1, 0);
	tips = new WindowTips(TIPS_LINES+2, TIPS_COLUMNS+2, PLAYERS_LINES+2, CHAT_COLUMNS+2+1, 0);
	input = new WindowInput(input_lines+2, input_columns+2, CHAT_LINES+2, 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 = input->GetChar();
	switch(key)
	{
		case key_escape: {
			BreakLoop();
			break;
		}
		case ' '...'~': { // ascii table 32...126
			AddCharToBuffer(key);
			input->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;

			input->Clear(false);
			input->SetPosCursor(1, 1);
			break;
		}
		case key_backspace: {
			RemoveCharFromBuffer();
			input->RemoveCharFromMsg();
			break;
		}
		default: break;
	}
}

void Client::AddMessage(const char *msg, const char spec_char)
{
	if(spec_char == USERS_CHAR)
		players->SetPlayersList(msg);
	else if(spec_char == GONLINE_CHAR)
		tips->SetGeneralOnline(msg);
	else if(spec_char == RONLINE_CHAR)
		tips->SetRoomOnline(msg);
	else
		chat->AddMessage(msg, spec_char);
}

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--;
}