From 0574f5a7c5159a2ae1d7d2182cec982509947db9 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Sat, 7 Dec 2024 19:17:33 +0100 Subject: checkpoint --- chatty2.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 chatty2.c (limited to 'chatty2.c') diff --git a/chatty2.c b/chatty2.c new file mode 100644 index 0000000..5a00fee --- /dev/null +++ b/chatty2.c @@ -0,0 +1,90 @@ +/* Macro's */ + +#define TB_IMPL +#include "external/termbox2.h" +#undef TB_IMPL + +#define DEBUG +#define MAX_INPUT_LEN 255 + +#define CHATTY_IMPL +#include "ui.h" +#undef CHATTY_IMPL +#include "protocol.h" + +#include + +#ifdef DEBUG +#define Assert(expr) \ + if (!(expr)) \ + { \ + tb_shutdown(); \ + raise(SIGTRAP); \ + } +#else +#define Assert(expr) ; +#endif + +int +main(int Argc, char *Argv[]) +{ + struct tb_event ev; + rect TextBox = {0, 0, 24, 4}; + rect TextR = { + TextBox.X + TEXTBOX_BORDER_WIDTH + TEXTBOX_PADDING_X, + TextBox.Y + TEXTBOX_BORDER_WIDTH, + TextBox.W - TEXTBOX_BORDER_WIDTH * 2 - TEXTBOX_PADDING_X * 2, + TextBox.H - TEXTBOX_BORDER_WIDTH * 2 + }; + wchar_t Input[MAX_INPUT_LEN] = {0}; + u32 InputLen = 0; + u32 InputOffset = 0; + u32 InputPos = 0; + u32 DidParseKey = 0; + + Assert(setlocale(LC_ALL, "")); + tb_init(); + global.cursor_x = TextR.X; + global.cursor_y = TextR.Y; + bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]); + + while (ev.key != TB_KEY_CTRL_C) + { + tb_clear(); + + DrawBox(TextBox, 0); + TextBoxDraw(TextR, Input + InputOffset, InputLen); + + InputPos = InputOffset + (global.cursor_x - TextR.X) + (global.cursor_y - TextR.Y) * TextR.W; + Assert(InputPos <= InputLen); + + tb_present(); + + tb_poll_event(&ev); + + // TODO: Handle resize event + + // Intercept keys + if (ev.key == TB_KEY_CTRL_M) + { + tb_printf(26, 0, 0, 0, "sent."); + continue; + } + else + { + DidParseKey = TextBoxKeypress(ev, TextR, + Input, &InputLen, InputPos, &InputOffset); + } + + u32 ShouldInsert = (!DidParseKey) && (ev.ch && InputLen < MAX_INPUT_LEN); + if (ShouldInsert) + { + TextBoxInsert(Input, InputPos, InputLen++, ev.ch); + ScrollRight(TextR, &InputOffset); + } + + // tb_clear(); + } + + tb_shutdown(); +} -- cgit v1.2.3