diff --git a/src/text_editor/event.cpp b/src/text_editor/event.cpp index dda89ee..6e9f9c0 100644 --- a/src/text_editor/event.cpp +++ b/src/text_editor/event.cpp @@ -11,7 +11,6 @@ enum { EVENT_MOUSE_RIGHT_UP, EVENT_MOUSE_MIDDLE_UP, EVENT_MOUSE_WHEEL, - EVENT_MOUSE_MOVE, EVENT_KEY_PRESS, EVENT_TEXT_INPUT, @@ -39,6 +38,7 @@ struct Event { Vec2 wheel; char *text; }; +Array EventPlayback; void Serialize(Serializer *s, Event *e) { SerializeBegin(s); @@ -48,6 +48,7 @@ void Serialize(Serializer *s, Event *e) { 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); @@ -80,13 +81,11 @@ bool WaitForEvents = true; #define MousePress() (Mouse(LEFT) || Mouse(RIGHT) || Mouse(MIDDLE)) #define MouseUp() (Mouse(LEFT_UP) || Mouse(RIGHT_UP) || Mouse(MIDDLE_UP)) -Array ParseEvents(Allocator allocator, Buffer *buffer) { - Array events = {allocator}; - Serializer s = {NULL, GetString(buffer), false}; +void ParseEvents(Array *events, Buffer *buffer) { + Serializer s = {NULL, GetString(buffer), false}; for (; s.string.len;) { Event event = {}; Serialize(&s, &event); - Add(&events, event); + Add(events, event); } - return events; } \ No newline at end of file diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 336c12a..69230b7 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -298,6 +298,63 @@ Color GetColor(String name, Color default_color) { return result; } +Int GetInt(const char *name) { + lua_getfield(LuaState, -1, name); + lua_Integer num = lua_tointeger(LuaState, -1); + lua_pop(LuaState, 1); + return (Int)num; +} + +const char *GetString(const char *name) { + lua_getfield(LuaState, -1, name); + const char *result = lua_tostring(LuaState, -1); + lua_pop(LuaState, 1); + return result; +} + +Vec2 GetVec2(const char *name) { + Vec2 result = {}; + lua_getfield(LuaState, -1, name); + defer { lua_pop(LuaState, 1); }; + + lua_rawgeti(LuaState, -1, 1); + result.x = (float)lua_tonumber(LuaState, -1); + lua_pop(LuaState, 1); + + lua_rawgeti(LuaState, -1, 2); + result.x = (float)lua_tonumber(LuaState, -1); + lua_pop(LuaState, 1); + + lua_pop(LuaState, 3); + return result; +} + +// #Play { {kind = 10, key = 1073741902, xmouse = 612, ymouse = 357, xwindow = 1280, ywindow = 720 } } +int Lua_Play(lua_State *L) { + if (!lua_istable(L, -1)) luaL_error(LuaState, "expected a table of events"); + + size_t size = lua_rawlen(L, -1); + for (size_t i = 0; i < size; i += 1) { + lua_rawgeti(L, -1, i + 1); + defer { lua_pop(L, 1); }; + + Event event = {}; + event.kind = (EventKind)GetInt("kind"); + event.key = (SDL_Keycode)GetInt("key"); + event.xmouse = (int16_t)GetInt("xmouse"); + event.ymouse = (int16_t)GetInt("ymouse"); + event.xwindow = (int16_t)GetInt("xwindow"); + event.ywindow = (int16_t)GetInt("ywindow"); + event.clicks = (uint8_t)GetInt("clicks"); + event.flags = (uint8_t)GetInt("flags"); + event.text = (char *)GetString("text"); + event.wheel = GetVec2("wheel"); + Add(&EventPlayback, event); + } + + return 0; +} + Int LuaBufferChangeID = 0; void ReloadStyle(); extern String BaseLuaConfig; diff --git a/src/text_editor/lua_api_generated.cpp b/src/text_editor/lua_api_generated.cpp index 587c111..e6777df 100644 --- a/src/text_editor/lua_api_generated.cpp +++ b/src/text_editor/lua_api_generated.cpp @@ -16,5 +16,6 @@ luaL_Reg LuaFunctions[] = { {"Search", Lua_Search}, {"SearchB", Lua_SearchB}, {"Rename", Lua_Rename}, + {"Play", Lua_Play}, {NULL, NULL}, }; diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index d260d8a..2bc7691 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -132,7 +132,7 @@ Event TranslateSDLEvent(SDL_Event *input_event) { } break; case SDL_EVENT_MOUSE_MOTION: { - event.kind = EVENT_MOUSE_MOVE; + event.kind = EVENT_UPDATE; } break; case SDL_EVENT_DROP_FILE: { @@ -180,6 +180,10 @@ void Update(Event event) { Array GetEventsForFrame(Allocator allocator) { Array result = {allocator}; + if (EventPlayback.len) { + result = TightCopy(allocator, EventPlayback); + EventPlayback.len = 0; + } SDL_Event event; if (WaitForEvents) { @@ -290,16 +294,11 @@ int main(int argc, char **argv) InitWindows(); InitOS(ReportWarningf); - Buffer *playback_buffer = NULL; for (int i = 1; i < argc; i += 1) { String it = argv[i]; if (!FileExists(it)) continue; - if (EndsWith(it, ".te_events")) { - playback_buffer = BufferOpenFile(it); - } else { - Window *window = GetWindow({0}); - WindowOpenBufferView(window, it); - } + Window *window = GetWindow({0}); + WindowOpenBufferView(window, it); } Serializer ser = {EventBuffer, {}, true}; @@ -308,13 +307,9 @@ int main(int argc, char **argv) Scratch scratch; Array frame_events = GetEventsForFrame(scratch); - if (playback_buffer) { - frame_events = ParseEvents(Perm, playback_buffer); - playback_buffer = NULL; - } For(frame_events) { if (it.kind == EVENT_QUIT) goto end_of_editor_loop; - if (it.kind != EVENT_UPDATE && it.kind != EVENT_MOUSE_MOVE) Serialize(&ser, &it); + if (it.kind != EVENT_UPDATE) Serialize(&ser, &it); Update(it); } @@ -323,7 +318,7 @@ int main(int argc, char **argv) WaitForEvents = false; } - // This shouldn't matter to the state of the program, only appearence for + // This shouldn't matter to the state of the program, only appearance for // the user { Window *window = GetActiveWindow(); diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 43b800d..ba8b88b 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -9,9 +9,6 @@ - Fuzzy search buffer which uses titlebar as query! - Check if file exists in ApplyRules otherwise return null -- Store editor metadata in user accessible buffers? (read only) -- Add the event buffer with serialized events -- event serialization to lua object format - Gotos, jumping between views should preserve cursors - OnWindowCommand allow config user to overwrite the WindowCommand keybinding, introduce his own