summaryrefslogtreecommitdiffstats
path: root/src/client/clui.cpp
blob: 4ae6a5aed1eb3a25acfac1bf3d3b569c786c9002 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <string.h>
#include <ncurses.h>

#include "clui.hpp"

#define CHAT_WIDTH 20
#define CHAT_HEIGHT 59
#define PLAYERS_WIDTH 20
#define PLAYERS_HEIGHT 20
#define INPUT_WIDTH 4
#define INPUT_HEIGHT 80

#define MAXLEN_MSG 158

Interface_wc::Interface_wc(int num_y, int num_x, int by,
	int bx, char ch)
{
	w = newwin(num_y, num_x, by, bx);
	box(w, ch, ch);
	this->Update();
}

void Interface_wc::Hide()
{
	werase(this->GetWindow()); // clear

	this->Update();
	this->Delete();
}

////////////////////////////////////////////////////////////////////////

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()
{
	chat = new Interface_wc(CHAT_WIDTH, CHAT_HEIGHT, 0, 0, 0);
	players = new Interface_wc(PLAYERS_WIDTH, PLAYERS_HEIGHT, 0, 60, 0);
	input = new Interface_wc(INPUT_WIDTH, INPUT_HEIGHT, 20, 0, 0);
	i_nx = 1;
	i_ny = 1;

	/* WINDOW *newwin(int nlines, int ncols, int begin_y, int begin_x) */
	/*WINDOW *chat = newwin(20, 59, 0, 0);
	box(chat, 0, 0);
	wrefresh(chat);

	WINDOW *players = newwin(20, 20, 0, 60);
	box(players, 0, 0);
	wrefresh(players);

	WINDOW *input = newwin(4, 80, 20, 0);
	box(input, 0, 0);
	wmove(input, 1, 1);
	wrefresh(input);*/
}

ChatRoom::~ChatRoom()
{
	if(chat)
		delete chat;
	if(players)
		delete players;
	if(input)
		delete input;
}

void ChatRoom::PrintMessage(const char *msg)
{
	// если msg <= **, то одна строка
	// иначе 2 строки
	WINDOW *win = chat->GetWindow();
	wmove(win, 1, 2);
	wprintw(win, msg);

	chat->Update();
}

bool ChatRoom::AddCharToSendMsg(char ch)
{
	if(i_ny == 2 && i_nx == MAXLEN_MSG/2-1)
		return 0;

	mvwaddch(input->GetWindow(), i_ny, i_nx, ch);
	i_nx++;
	if(i_nx >= MAXLEN_MSG/2) {
		if(i_ny == 1) {
			i_ny++;
			i_nx = 1;
		}
		else if(i_ny == 2) 
			i_nx--;
		wmove(input->GetWindow(), i_ny, i_nx);
	}

	input->Update();
	return 1;
}

bool ChatRoom::RemoveCharFromMsg()
{
	if(i_ny == 1 && i_nx == 1)
		return 0;

	i_nx--;
	if(i_nx < 1) {
		i_ny--;
		i_nx = MAXLEN_MSG/2-1;
	}
	mvwaddch(input->GetWindow(), i_ny, i_nx, ' ');
	wmove(input->GetWindow(), i_ny, i_nx);

	input->Update();
	return 1;
}