Author: mesyeti <mesyeti@mesyeti.uk>
add tabs
source/app.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++- source/app.h | 6 +++++ source/input.c | 32 +++++++++++++++++++----------- source/input.h | 12 +++++-----
diff --git a/source/app.c b/source/app.c index 54249c6eccf8a8b2ea410997f13bc5ae454c84db..317dd975ef735ea6633ffc705b1dfa0a1678cc5a 100644 --- a/source/app.c +++ b/source/app.c @@ -27,6 +27,51 @@ if (!success) { Error("Failed to start terminal"); } + + app.bindNewTab = Input_AddKeyBind(KEY_LCTRL, KEY_LSHIFT, 0, KEY_T); + app.bindPrevTab = Input_AddKeyBind(KEY_LCTRL, 0, 0, KEY_PAGE_UP); + app.bindNextTab = Input_AddKeyBind(KEY_LCTRL, 0, 0, KEY_PAGE_DOWN); +} + +static Pane* GetSelectedPane(Pane* pane) { + if (pane->type == PANE_SPLIT) { + return GetSelectedPane(pane->v.split.left); + } + else { + return pane; + } +} + +static bool RunBind(Event* e) { + if (Input_MatchBind(app.bindNewTab, e)) { + bool success; + Pane newPane = Pane_NewRootPane(&success); + + if (!success) { + Warn("Failed to open new tab"); + return true; + } + + ++ app.tabNum; + app.tabs = SafeRealloc(app.tabs, app.tabNum * sizeof(Pane)); + app.tabs[app.tabNum - 1] = newPane; + + app.tabSel = app.tabNum - 1; + app.paneSel = GetSelectedPane(&app.tabs[app.tabSel]); + return true; + } + if (Input_MatchBind(app.bindPrevTab, e)) { + app.tabSel = app.tabSel == 0? app.tabNum - 1 : app.tabSel - 1; + app.paneSel = GetSelectedPane(&app.tabs[app.tabSel]); + return true; + } + if (Input_MatchBind(app.bindNextTab, e)) { + app.tabSel = (app.tabSel + 1) % app.tabNum; + app.paneSel = GetSelectedPane(&app.tabs[app.tabSel]); + return true; + } + + return false; } void App_Update(void) { @@ -39,6 +84,8 @@ Event e; while (Event_Poll(&e)) { + Input_HandleEvent(&e); + switch (e.type) { case EVENT_QUIT: { app.running = false; @@ -49,8 +96,10 @@ Backend_OnWindowResize(); Screen_OnWindowResize(); break; } - default: { - + case EVENT_KEY_DOWN: { + if (RunBind(&e)) { + continue; + } } } diff --git a/source/app.h b/source/app.h index 78916f749859d02e2617e78b896c50f35f20704c..b2c8561dcef4562dc841049663cbe7fd042c96a3 100644 --- a/source/app.h +++ b/source/app.h @@ -2,6 +2,7 @@ #ifndef YT_APP_H #define YT_APP_H #include "pane.h" +#include "input.h" #include "video.h" #include "common.h" #include "platform.h" @@ -14,6 +15,11 @@ Pane* tabs; size_t tabNum; size_t tabSel; Pane* paneSel; + + // binds + Input_BindID bindNewTab; + Input_BindID bindPrevTab; + Input_BindID bindNextTab; } App; extern App app; diff --git a/source/input.c b/source/input.c index 6e4a070987ff8c3063e541f36e15750596eaceaf..ffea2c178aae8678d41e425ddad48826c387f29c 100644 --- a/source/input.c +++ b/source/input.c @@ -24,11 +24,11 @@ void Input_PrintBind(char* dest, size_t size, Input_BindID bindID) { Input_Bind* bind = &input.binds[bindID]; switch (bind->type) { - case YT_INPUT_BIND_KEY: { + case INPUT_BIND_KEY: { snprintf(dest, size, "key %s", Key_ToString(bind->key.key)); break; } - case YT_INPUT_BIND_MOUSE_BUTTON: { + case INPUT_BIND_MOUSE_BUTTON: { snprintf(dest, size, "button %d", bind->mouseButton.button); break; } @@ -45,14 +45,20 @@ Input_Bind* bind = &input.binds[bindID]; switch (bind->type) { - case YT_INPUT_BIND_KEY: { + case INPUT_BIND_KEY: { if ((e->type != EVENT_KEY_DOWN) && (e->type != EVENT_KEY_UP)) { return false; } - return bind->key.key == e->key.key; // TODO: modifiers + for (int i = 0; i < 3; ++ i) { + if (!bind->key.mod[i]) continue; + + if (!Input_KeyPressed(bind->key.mod[i])) return false; + } + + return bind->key.key == e->key.key; } - case YT_INPUT_BIND_MOUSE_BUTTON: { + case INPUT_BIND_MOUSE_BUTTON: { if ( (e->type != EVENT_MOUSE_BUTTON_DOWN) && (e->type != EVENT_MOUSE_BUTTON_UP) @@ -73,10 +79,10 @@ Input_Bind* bind = &input.binds[bindID]; switch (bind->type) { - case YT_INPUT_BIND_KEY: { + case INPUT_BIND_KEY: { return Input_KeyPressed(bind->key.key); // TODO: modifiers } - case YT_INPUT_BIND_MOUSE_BUTTON: { + case INPUT_BIND_MOUSE_BUTTON: { return Input_MouseButtonPressed(bind->mouseButton.button); } } @@ -92,18 +98,20 @@ input.binds[input.bindAmount - 1] = bind; return input.bindAmount - 1; } -Input_BindID Input_AddKeyBind(Key mod[3], Key key) { +Input_BindID Input_AddKeyBind(Key mod0, Key mod1, Key mod2, Key key) { Input_Bind bind; - bind.type = YT_INPUT_BIND_KEY; - bind.key.key = key; - memcpy(bind.key.mod, mod, sizeof(bind.key.mod)); + bind.type = INPUT_BIND_KEY; + bind.key.key = key; + bind.key.mod[0] = mod0; + bind.key.mod[1] = mod1; + bind.key.mod[2] = mod2; return Input_AddBind(bind); } Input_BindID Input_AddMouseButtonBind(uint8_t button) { Input_Bind bind; - bind.type = YT_INPUT_BIND_MOUSE_BUTTON; + bind.type = INPUT_BIND_MOUSE_BUTTON; bind.mouseButton.button = button; return Input_AddBind(bind); diff --git a/source/input.h b/source/input.h index 073849888e907cb9dde41be4ad318a203a17f787..d4f8b902324815ecfdbb53fed6d77e32112d23c1 100644 --- a/source/input.h +++ b/source/input.h @@ -6,13 +6,13 @@ #include "types.h" #include "keyboard.h" // mouse stuff -#define YT_MOUSE_BUTTON_1 0 -#define YT_MOUSE_BUTTON_2 1 -#define YT_MOUSE_BUTTON_3 2 +#define MOUSE_BUTTON_1 0 +#define MOUSE_BUTTON_2 1 +#define MOUSE_BUTTON_3 2 typedef enum { - YT_INPUT_BIND_KEY = 0, - YT_INPUT_BIND_MOUSE_BUTTON + INPUT_BIND_KEY = 0, + INPUT_BIND_MOUSE_BUTTON } Input_BindType; typedef struct { @@ -58,7 +58,7 @@ void Input_PrintBind(char* dest, size_t size, Input_BindID bindID); bool Input_MatchBind(Input_BindID bindID, Event* e); bool Input_BindPressed(Input_BindID bindID); Input_BindID Input_AddBind(Input_Bind bind); -Input_BindID Input_AddKeyBind(Key mod[3], Key key); +Input_BindID Input_AddKeyBind(Key mod0, Key mod1, Key mod2, Key key); Input_BindID Input_AddMouseButtonBind(uint8_t button); void Input_HandleEvent(Event* e);