From 8693eef8febde4df90b7fbd1fb9ac813b132fa50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Mon, 20 Jul 2020 01:50:14 -0300 Subject: Wrap external functionality into classes --- src/client/window.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-) (limited to 'src/client/window.cpp') diff --git a/src/client/window.cpp b/src/client/window.cpp index e718e63..9e389fe 100644 --- a/src/client/window.cpp +++ b/src/client/window.cpp @@ -1,16 +1,85 @@ -struct Window { +#include + +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); - void init() { 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); + } }; -- cgit v1.2.3