summaryrefslogtreecommitdiff
path: root/src/client/window.cpp
blob: 9e389feaefbd9ac997ff319baa74bb556df53318 (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
#include <cursesw.h>

class Window {
	Socket io;
	char *view;
	int width;
	int height;
	int cursor_x;
	int cursor_y;

	public:

	Window() : cursor_x(0), cursor_y(0) {
		initscr();
		cbreak();
		noecho();
		intrflush(stdscr, FALSE);
		keypad(stdscr, TRUE);

		getmaxyx(stdscr, height, width);

		view = new char[width * height];
		for (int i = 0; i < width * height; i++) {
			view[i] = 0;
		}

		io.connect();
	}

	~Window() {
		endwin();
	}

	void redraw() {
		clear();

		int8_t msg[5];
		msg[0] = OP_SHOW;
		encode2(width, msg, 1);
		encode2(height, msg, 3);
		io.send(msg, 5);
		io.recv(view, width * height);
		for (int i = 0; i < width * height; i++) {
			printw("%c", view[i]);
		}
		move(cursor_y, cursor_x);
	}

	char pos(size_t x, size_t y) {
		return view[x + y * width];
	}

	void move_left() {
		int8_t mov[2];
		if (cursor_x > 0) {
			mov[0] = OP_MOVE1;
			mov[1] = -1;
			io.send(mov, 2);
			cursor_x--;
		}
	}

	void move_right() {
		int8_t mov[2];
		if (cursor_x < width - 1 && pos(cursor_x + 1, cursor_y) != 0) {
			mov[0] = OP_MOVE1;
			mov[1] = 1;
			io.send(mov, 2);
		   	cursor_x++;
		}
	}

	void delete_element() {
		int8_t del[1];
		del[0] = OP_DELETE;
		io.send(del, 1);
	}

	void insert_element(int input) {
		int8_t ins[2];
		ins[0] = OP_INSERT;
		ins[1] = input;
		io.send(ins, 2);
	}
};