Describe event using macros

This commit is contained in:
Krzosa Karol
2024-08-15 08:50:04 +02:00
parent 5e51e2fbad
commit d4ac1dd817
6 changed files with 126 additions and 135 deletions

View File

@@ -39,7 +39,7 @@ void GlobalCommand(Event event) {
}
// Handle wheel scrolling
if (event.wheel.x || event.wheel.y) {
if (event.xwheel || event.ywheel) {
Vec2I mouse = MouseVec2I();
For(order) {
@@ -49,8 +49,8 @@ void GlobalCommand(Event event) {
bool mouse_in_window = CheckCollisionPointRec(mouse, window->total_rect);
if (mouse_in_window) {
View *view = GetView(window->active_view);
view->scroll.y -= (Int)(event.wheel.y * 48);
view->scroll.x += (Int)(event.wheel.x * 48);
view->scroll.y -= (Int)(event.ywheel * 48);
view->scroll.x += (Int)(event.xwheel * 48);
break;
}
}
@@ -119,12 +119,12 @@ void GlobalCommand(Event event) {
}
}
if (event.ctrl && event.shift && Mouse(RIGHT)) {
if (Ctrl() && Shift() && Mouse(RIGHT)) {
GotoForward(GetActiveMainSet().window);
} else if (event.alt && event.ctrl && Mouse(RIGHT)) {
} else if (event.ctrl && Mouse(RIGHT)) {
} else if (Alt() && Ctrl() && Mouse(RIGHT)) {
} else if (Ctrl() && Mouse(RIGHT)) {
GotoBackward(GetActiveMainSet().window);
} else if (event.alt && Mouse(RIGHT)) {
} else if (Alt() && Mouse(RIGHT)) {
} else if (Mouse(RIGHT)) {
Vec2I mouse = MouseVec2I();
BSet active = GetActiveSet();
@@ -158,9 +158,9 @@ void GlobalCommand(Event event) {
// for now let's leave it because we are relaying on global state
// - maybe just do the check if active window is matching the DocumentSelected window
// - if scrollbar selected then don't invoke window command
if (event.ctrl && event.shift && Mouse(LEFT)) {
if (Ctrl() && Shift() && Mouse(LEFT)) {
MouseExecWord(event);
} else if (event.ctrl && Mouse(LEFT)) {
} else if (Ctrl() && Mouse(LEFT)) {
MouseLoadWord(event);
} else if (Mouse(LEFT)) { // CTRL SHIFT
Vec2I mouse = MouseVec2I();
@@ -175,11 +175,11 @@ void GlobalCommand(Event event) {
DocumentSelected = active.window->id;
Int p = ScreenSpaceToBufferPos(active.window, active.view, active.buffer, mouse);
if (event.alt) Insert(&active.view->carets, MakeCaret(p, p), 0);
if (!event.alt && !event.shift) active.view->carets.len = 1;
if (Alt()) Insert(&active.view->carets, MakeCaret(p, p), 0);
if (!Alt() && !Shift()) active.view->carets.len = 1;
Caret &caret = active.view->carets[0];
if (event.shift) {
if (Shift()) {
if (p <= caret.range.min) {
caret.range.min = p;
caret.ifront = 0;
@@ -226,17 +226,17 @@ void GlobalCommand(Event event) {
}
}
if (Ctrl(SDLK_P)) {
if (CtrlPress(SDLK_P)) {
Command_ListBuffers();
}
if (CtrlShift(SDLK_BACKSLASH)) {
if (CtrlShiftPress(SDLK_BACKSLASH)) {
AddRowWindow();
} else if (Ctrl(SDLK_BACKSLASH)) {
} else if (CtrlPress(SDLK_BACKSLASH)) {
AddColumnWindow();
}
if (Ctrl(SDLK_0)) {
if (CtrlPress(SDLK_0)) {
ToggleVisibility(DebugWindowID);
}
@@ -248,15 +248,15 @@ void GlobalCommand(Event event) {
ToggleFullscreen();
}
if (Ctrl(SDLK_1)) {
if (CtrlPress(SDLK_1)) {
Window *window = GetLayoutWindow(0);
if (window) ActiveWindow = window->id;
}
if (Ctrl(SDLK_2)) {
if (CtrlPress(SDLK_2)) {
Window *window = GetLayoutWindow(1);
if (window) ActiveWindow = window->id;
}
if (Ctrl(SDLK_3)) {
if (CtrlPress(SDLK_3)) {
Window *window = GetLayoutWindow(2);
if (window) ActiveWindow = window->id;
}
@@ -268,120 +268,120 @@ void GlobalCommand(Event event) {
WindowOpenBufferView(active.window, event.text);
}
if (CtrlAlt(SDLK_DOWN)) {
if (CtrlAltPress(SDLK_DOWN)) {
Command_DuplicateLine(active.view, DIR_DOWN);
} else if (AltShift(SDLK_DOWN)) {
} else if (AltShiftPress(SDLK_DOWN)) {
Command_CreateCursorVertical(active.view, DIR_DOWN);
} else if (CtrlShift(SDLK_DOWN)) {
} else if (CtrlShiftPress(SDLK_DOWN)) {
Command_Move(active.view, DIR_DOWN, CTRL_PRESSED, SHIFT_PRESSED);
} else if (Alt(SDLK_DOWN)) {
} else if (AltPress(SDLK_DOWN)) {
Command_MoveLine(active.view, DIR_DOWN);
} else if (Ctrl(SDLK_DOWN)) {
} else if (CtrlPress(SDLK_DOWN)) {
Command_Move(active.view, DIR_DOWN, CTRL_PRESSED);
} else if (Shift(SDLK_DOWN)) {
} else if (ShiftPress(SDLK_DOWN)) {
Command_Move(active.view, DIR_DOWN, false, SHIFT_PRESSED);
} else if (Press(SDLK_DOWN)) {
Command_Move(active.view, DIR_DOWN);
}
if (CtrlAlt(SDLK_UP)) {
if (CtrlAltPress(SDLK_UP)) {
Command_DuplicateLine(active.view, DIR_UP);
} else if (AltShift(SDLK_UP)) {
} else if (AltShiftPress(SDLK_UP)) {
Command_CreateCursorVertical(active.view, DIR_UP);
} else if (CtrlShift(SDLK_UP)) {
} else if (CtrlShiftPress(SDLK_UP)) {
Command_Move(active.view, DIR_UP, CTRL_PRESSED, SHIFT_PRESSED);
} else if (Alt(SDLK_UP)) {
} else if (AltPress(SDLK_UP)) {
Command_MoveLine(active.view, DIR_UP);
} else if (Ctrl(SDLK_UP)) {
} else if (CtrlPress(SDLK_UP)) {
Command_Move(active.view, DIR_UP, CTRL_PRESSED);
} else if (Shift(SDLK_UP)) {
} else if (ShiftPress(SDLK_UP)) {
Command_Move(active.view, DIR_UP, false, SHIFT_PRESSED);
} else if (Press(SDLK_UP)) {
Command_Move(active.view, DIR_UP);
}
if (CtrlShift(SDLK_LEFT)) {
if (CtrlShiftPress(SDLK_LEFT)) {
Command_Move(active.view, DIR_LEFT, CTRL_PRESSED, SHIFT_PRESSED);
} else if (Ctrl(SDLK_LEFT)) {
} else if (CtrlPress(SDLK_LEFT)) {
Command_Move(active.view, DIR_LEFT, CTRL_PRESSED);
} else if (Shift(SDLK_LEFT)) {
} else if (ShiftPress(SDLK_LEFT)) {
Command_Move(active.view, DIR_LEFT, false, SHIFT_PRESSED);
} else if (Press(SDLK_LEFT)) {
Command_Move(active.view, DIR_LEFT);
}
if (CtrlShift(SDLK_RIGHT)) {
if (CtrlShiftPress(SDLK_RIGHT)) {
Command_Move(active.view, DIR_RIGHT, CTRL_PRESSED, SHIFT_PRESSED);
} else if (Ctrl(SDLK_RIGHT)) {
} else if (CtrlPress(SDLK_RIGHT)) {
Command_Move(active.view, DIR_RIGHT, CTRL_PRESSED);
} else if (Shift(SDLK_RIGHT)) {
} else if (ShiftPress(SDLK_RIGHT)) {
Command_Move(active.view, DIR_RIGHT, false, SHIFT_PRESSED);
} else if (Press(SDLK_RIGHT)) {
Command_Move(active.view, DIR_RIGHT);
}
if (CtrlShift(SDLK_Z)) {
if (CtrlShiftPress(SDLK_Z)) {
RedoEdit(active.buffer, &active.view->carets);
} else if (Ctrl(SDLK_Z)) {
} else if (CtrlPress(SDLK_Z)) {
UndoEdit(active.buffer, &active.view->carets);
}
if (Ctrl(SDLK_C)) {
if (CtrlPress(SDLK_C)) {
Command_Copy(active.view);
} else if (Ctrl(SDLK_V)) {
} else if (CtrlPress(SDLK_V)) {
Command_Paste(active.view);
} else if (Ctrl(SDLK_X)) {
} else if (CtrlPress(SDLK_X)) {
PreBeginEdit_SaveCaretHistory(active.buffer, active.view->carets);
Command_Copy(active.view);
Command_Replace(active.view, L"");
}
if (Ctrl(SDLK_A)) {
if (CtrlPress(SDLK_A)) {
Command_SelectEntireBuffer(active.view);
active.view->update_scroll = false;
}
if (Shift(SDLK_PAGEUP)) {
if (ShiftPress(SDLK_PAGEUP)) {
Command_MoveCursorsByPageSize(active.window, DIR_UP, SHIFT_PRESSED);
} else if (Press(SDLK_PAGEUP)) {
Command_MoveCursorsByPageSize(active.window, DIR_UP);
}
if (Shift(SDLK_PAGEDOWN)) {
if (ShiftPress(SDLK_PAGEDOWN)) {
Command_MoveCursorsByPageSize(active.window, DIR_DOWN, SHIFT_PRESSED);
} else if (Press(SDLK_PAGEDOWN)) {
Command_MoveCursorsByPageSize(active.window, DIR_DOWN);
}
if (Shift(SDLK_HOME)) {
if (ShiftPress(SDLK_HOME)) {
Command_MoveCursorsToSide(active.view, DIR_LEFT, SHIFT_PRESSED);
} else if (Press(SDLK_HOME)) {
Command_MoveCursorsToSide(active.view, DIR_LEFT);
}
if (Shift(SDLK_END)) {
if (ShiftPress(SDLK_END)) {
Command_MoveCursorsToSide(active.view, DIR_RIGHT, SHIFT_PRESSED);
} else if (Press(SDLK_END)) {
Command_MoveCursorsToSide(active.view, DIR_RIGHT);
}
if (Shift(SDLK_TAB)) {
if (ShiftPress(SDLK_TAB)) {
Command_IndentSelectedLines(active.view, SHIFT_PRESSED);
} else if (Press(SDLK_TAB)) {
Command_IndentSelectedLines(active.view);
}
if (CtrlShift(SDLK_K)) {
if (CtrlShiftPress(SDLK_K)) {
Command_KillSelectedLines(active.view);
}
if (Ctrl(SDLK_BACKSPACE)) {
if (CtrlPress(SDLK_BACKSPACE)) {
Command_Delete(active.view, DIR_LEFT, CTRL_PRESSED);
} else if (Press(SDLK_BACKSPACE)) {
Command_Delete(active.view, DIR_LEFT);
}
if (Ctrl(SDLK_DELETE)) {
if (CtrlPress(SDLK_DELETE)) {
Command_Delete(active.view, DIR_RIGHT, CTRL_PRESSED);
} else if (Press(SDLK_DELETE)) {
Command_Delete(active.view, DIR_RIGHT);
@@ -394,14 +394,14 @@ void GlobalCommand(Event event) {
Command_Replace(active.view, string16);
}
if (Ctrl(SDLK_D)) {
if (CtrlPress(SDLK_D)) {
String16 string = GetString(active.buffer, active.view->carets[0].range);
Caret caret = FindNext(active.buffer, string, active.view->carets[0]);
Insert(&active.view->carets, caret, 0);
MergeCarets(active.view);
}
if (Shift(SDLK_F3)) {
if (ShiftPress(SDLK_F3)) {
Scratch scratch;
BSet main = GetActiveMainSet();
String16 search_string = ToString16(scratch, main.window->search_string);
@@ -415,57 +415,57 @@ void GlobalCommand(Event event) {
Command_SelectRangeOneCursor(main.view, caret);
}
if (Shift(SDLK_F4)) {
if (ShiftPress(SDLK_F4)) {
Command_GotoNextInList(active.window, -1);
} else if (Press(SDLK_F4)) {
Command_GotoNextInList(active.window, 1);
}
if (CtrlShift(SDLK_RETURN)) {
if (CtrlShiftPress(SDLK_RETURN)) {
Command_MoveCursorsToSide(active.view, DIR_LEFT);
Command_IdentedNewLine(active.view);
Command_Move(active.view, DIR_UP);
} else if (Ctrl(SDLK_RETURN)) {
} else if (CtrlPress(SDLK_RETURN)) {
Command_MoveCursorsToSide(active.view, DIR_RIGHT);
Command_IdentedNewLine(active.view);
} else if (Press(SDLK_RETURN)) {
Command_IdentedNewLine(active.view);
}
if (Ctrl(SDLK_F)) {
if (CtrlPress(SDLK_F)) {
Command_SelectTitlebarCommand(active.window, L"#Search(\"");
}
if (Ctrl(SDLK_S)) {
if (CtrlPress(SDLK_S)) {
SaveBuffer(active.view);
}
if (Ctrl(SDLK_PERIOD)) {
if (CtrlPress(SDLK_PERIOD)) {
BSet main = GetActiveMainSet();
String name = ChopLastSlash(main.buffer->name);
Open(name);
}
if (CtrlShift(SDLK_G)) {
} else if (Ctrl(SDLK_G)) {
if (CtrlShiftPress(SDLK_G)) {
} else if (CtrlPress(SDLK_G)) {
Command_SelectTitlebarCommand(active.window, L"#FuzzySort(\"");
}
if (CtrlShift(SDLK_W)) {
if (CtrlShiftPress(SDLK_W)) {
GotoForward(GetActiveMainSet().window);
} else if (Ctrl(SDLK_W)) {
} else if (CtrlPress(SDLK_W)) {
GotoBackward(GetActiveMainSet().window);
}
bool should_enclose = active.window->auto_enclose && (active.buffer->change_id != buffer_change_id);
if (should_enclose || CtrlShift(SDLK_Q)) {
if (should_enclose || CtrlShiftPress(SDLK_Q)) {
Caret caret = active.view->carets[0];
Range range = caret.range;
if (GetSize(caret.range) == 0) range = EncloseExecWord(active.buffer, GetFront(caret));
String16 string = GetString(active.buffer, range);
Command_EvalLua(active.view, string);
} else if (Ctrl(SDLK_Q)) {
} else if (CtrlPress(SDLK_Q)) {
Caret caret = active.view->carets[0];
Range range = caret.range;
if (GetSize(caret.range) == 0) range = EncloseLoadWord(active.buffer, GetFront(caret));

View File

@@ -18,41 +18,36 @@ enum {
EVENT_DROP_FILE,
};
#define EVENT_FIELDS \
X(EventKind, Int, kind) \
X(SDL_Keycode, Int, key) \
X(int16_t, Int, xmouse) \
X(int16_t, Int, ymouse) \
X(int16_t, Int, xwindow) \
X(int16_t, Int, ywindow) \
X(uint8_t, Int, clicks) \
X(uint8_t, Int, shift) \
X(uint8_t, Int, ctrl) \
X(uint8_t, Int, alt) \
X(uint8_t, Int, super) \
X(float, Float, xwheel) \
X(float, Float, ywheel) \
X(char *, String, text)
#define EVENT_FIELD_COUNT 14
struct Event {
EventKind kind;
SDL_Keycode key;
int16_t xmouse;
int16_t ymouse;
int16_t xwindow;
int16_t ywindow;
uint8_t clicks;
union {
struct {
uint8_t shift : 1;
uint8_t ctrl : 1;
uint8_t alt : 1;
uint8_t super : 1;
};
uint8_t flags;
};
Vec2 wheel;
char *text;
#define X(TYPE, KIND, NAME) TYPE NAME;
EVENT_FIELDS
#undef X
};
Array<Event> EventPlayback;
// :Event
void Serialize(Serializer *s, Event *e) {
SerializeBegin(s);
Serialize(s, "kind", &e->kind);
Serialize(s, "key", &e->key);
Serialize(s, "xmouse", &e->xmouse);
Serialize(s, "ymouse", &e->ymouse);
Serialize(s, "xwindow", &e->xwindow);
Serialize(s, "ywindow", &e->ywindow);
Serialize(s, "clicks", &e->clicks);
Serialize(s, "flags", &e->flags);
Serialize(s, "wheel", &e->wheel);
Serialize(s, "text", &e->text);
#define X(TYPE, KIND, NAME) Serialize(s, #NAME, &e->NAME);
EVENT_FIELDS
#undef X
SerializeEnd(s);
}
@@ -67,13 +62,17 @@ bool SHIFT_PRESSED = true;
bool AppIsRunning = true;
bool WaitForEvents = true;
#define Ctrl() event.ctrl
#define Alt() event.alt
#define Shift() event.shift
#define Press(KEY) (event.key == KEY)
#define Ctrl(KEY) (event.key == KEY && event.ctrl)
#define Shift(KEY) (event.key == KEY && event.shift)
#define Alt(KEY) (event.key == KEY && event.alt)
#define CtrlShift(KEY) (event.key == KEY && event.ctrl && event.shift)
#define CtrlAlt(KEY) (event.key == KEY && event.ctrl && event.alt)
#define AltShift(KEY) (event.key == KEY && event.shift && event.alt)
#define CtrlPress(KEY) (event.key == KEY && event.ctrl)
#define ShiftPress(KEY) (event.key == KEY && event.shift)
#define AltPress(KEY) (event.key == KEY && event.alt)
#define CtrlShiftPress(KEY) (event.key == KEY && event.ctrl && event.shift)
#define CtrlAltPress(KEY) (event.key == KEY && event.ctrl && event.alt)
#define AltShiftPress(KEY) (event.key == KEY && event.shift && event.alt)
#define MouseVec2() \
Vec2 { (float)event.xmouse, (float)event.ymouse }
#define MouseVec2I() \

View File

@@ -299,6 +299,13 @@ Int GetInt(lua_State *L, const char *name) {
return (Int)num;
}
double GetFloat(lua_State *L, const char *name) {
lua_getfield(L, -1, name);
double num = lua_tonumber(L, -1);
lua_pop(L, 1);
return num;
}
const char *GetString(lua_State *L, const char *name) {
lua_getfield(L, -1, name);
const char *result = lua_tostring(L, -1);
@@ -306,25 +313,16 @@ const char *GetString(lua_State *L, const char *name) {
return result;
}
Vec2 GetVec2(lua_State *L, const char *name) {
Vec2 result = {};
lua_getfield(L, -1, name);
defer { lua_pop(L, 1); };
if (!lua_istable(L, -1)) return result;
{
lua_getfield(L, -1, "1");
defer { lua_pop(L, 1); };
result.x = (float)lua_tonumber(L, -1);
}
{
lua_getfield(L, -1, "2");
defer { lua_pop(L, 1); };
result.y = (float)lua_tonumber(L, -1);
}
return result;
void PushEvent(lua_State *L, Event *event) {
lua_createtable(L, 0, EVENT_FIELD_COUNT);
#define lua_pushInt lua_pushinteger
#define lua_pushString lua_pushstring
#define lua_pushFloat lua_pushnumber
#define X(TYPE, KIND, NAME) \
lua_push##KIND(L, event->NAME); \
lua_setfield(L, -2, #NAME);
EVENT_FIELDS
#undef X
}
// :Event
@@ -338,17 +336,10 @@ int Lua_Play(lua_State *L) {
if (!lua_istable(L, -1)) luaL_error(L, "expected a table of events");
defer { lua_pop(L, 1); };
Event event = {};
event.kind = (EventKind)GetInt(L, "kind");
event.key = (SDL_Keycode)GetInt(L, "key");
event.xmouse = (int16_t)GetInt(L, "xmouse");
event.ymouse = (int16_t)GetInt(L, "ymouse");
event.xwindow = (int16_t)GetInt(L, "xwindow");
event.ywindow = (int16_t)GetInt(L, "ywindow");
event.clicks = (uint8_t)GetInt(L, "clicks");
event.flags = (uint8_t)GetInt(L, "flags");
event.text = (char *)GetString(L, "text");
event.wheel = GetVec2(L, "wheel");
Event event = {};
#define X(TYPE, KIND, NAME) event.NAME = (TYPE)Get##KIND(L, #NAME);
EVENT_FIELDS
#undef X
Add(&EventPlayback, event);
}

View File

@@ -24,8 +24,8 @@ void Serialize(Serializer *s, String name, int16_t *datum) {
*datum = (int16_t)d;
}
void Serialize(Serializer *s, String name, Vec2 *datum) {
IKnowWhatImDoing_Appendf(s->buffer, "%.*s = {%lld, %lld}, ", FmtString(name), (long long)datum->x, (long long)datum->y);
void Serialize(Serializer *s, String name, float *datum) {
IKnowWhatImDoing_Appendf(s->buffer, "%.*s = %f, ", FmtString(name), *datum);
}
void Serialize(Serializer *s, String name, char **text) {

View File

@@ -128,7 +128,8 @@ Event TranslateSDLEvent(SDL_Event *input_event) {
SDL_MouseWheelEvent &b = input_event->wheel;
event.xmouse = (int16_t)b.mouse_x;
event.ymouse = (int16_t)b.mouse_y;
event.wheel = {b.x, b.y};
event.xwheel = b.x;
event.ywheel = b.y;
} break;
case SDL_EVENT_MOUSE_MOTION: {
@@ -316,8 +317,8 @@ int main(int argc, char **argv)
Scratch scratch;
Array<Event> frame_events = GetEventsForFrame(scratch);
For(frame_events) {
Serialize(&ser, &it);
if (it.kind == EVENT_QUIT) goto end_of_editor_loop;
if (it.kind != EVENT_UPDATE) Serialize(&ser, &it);
Update(it);
}

View File

@@ -185,8 +185,8 @@ void DrawWindow(Window *window, Event &event) {
}
// Underline word under mouse cursor
if (event.ctrl) {
auto enclose_proc = event.shift ? EncloseExecWord : EncloseLoadWord;
if (Ctrl()) {
auto enclose_proc = Shift() ? EncloseExecWord : EncloseLoadWord;
Caret caret = view->carets[0];
Vec2I mouse = MouseVec2I();