Serializing and de serializing events
This commit is contained in:
@@ -237,43 +237,35 @@ bool StartsWith(String16 a, String16 start, unsigned ignore_case = false) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// chop this - :324
|
||||
String16 ChopNumberEx(String16 *string) {
|
||||
String16 col = {};
|
||||
for (int64_t i = string->len - 1; i >= 0; i -= 1) {
|
||||
String16 SkipNumberEx(String16 *string) {
|
||||
String16 col = {string->data, 0};
|
||||
for (int64_t i = 0; i < string->len; i += 1) {
|
||||
if (IsDigit(string->data[i])) {
|
||||
col.data = string->data + i;
|
||||
col.len += 1;
|
||||
} else if (string->data[i] == L':') {
|
||||
break;
|
||||
} else {
|
||||
return {};
|
||||
break;
|
||||
}
|
||||
}
|
||||
*string = Chop(*string, col.len + 1);
|
||||
*string = Skip(*string, col.len);
|
||||
return col;
|
||||
}
|
||||
|
||||
Int ChopNumber(String16 *string) {
|
||||
Scratch scratch;
|
||||
String16 col = ChopNumberEx(string);
|
||||
Int SkipNumber(String16 *string) {
|
||||
String16 col = SkipNumberEx(string);
|
||||
if (col.len == 0) return -1;
|
||||
String num_string = ToString(scratch, col);
|
||||
Int result = strtoll(num_string.data, NULL, 10) - 1;
|
||||
Scratch scratch;
|
||||
String num_string = ToString(scratch, col);
|
||||
Int result = strtoll(num_string.data, NULL, 10);
|
||||
return result;
|
||||
}
|
||||
|
||||
String16 ChopWhitespace(String16 *string) {
|
||||
String16 new_string = *string;
|
||||
String16 whitespace = {string->data, 0};
|
||||
for (int64_t i = 0; i < string->len; i += 1) {
|
||||
if (IsWhitespace(string->data[i])) {
|
||||
new_string = Skip(new_string, 1);
|
||||
whitespace.len += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
String16 SkipUntil(String16 *string, String16 str) {
|
||||
String16 begin = *string;
|
||||
begin.len = 0;
|
||||
for (Int i = 0; string->len; begin.len += 1) {
|
||||
String16 match = GetPrefix(*string, str.len);
|
||||
if (StartsWith(match, str)) break;
|
||||
*string = Skip(*string, 1);
|
||||
}
|
||||
*string = new_string;
|
||||
return whitespace;
|
||||
}
|
||||
return begin;
|
||||
}
|
||||
@@ -1,7 +1,64 @@
|
||||
bool AppIsRunning = true;
|
||||
bool WaitForEvents = true;
|
||||
struct Serializer {
|
||||
Buffer *buffer;
|
||||
String16 string;
|
||||
bool is_writing;
|
||||
};
|
||||
|
||||
enum EventKind {
|
||||
void Serialize(Serializer *s, String name, Int *datum) {
|
||||
if (s->is_writing) {
|
||||
IKnowWhatImDoing_Appendf(s->buffer, "%.*s = %lld, ", FmtString(name), (long long)*datum);
|
||||
} else {
|
||||
s->string = Skip(s->string, name.len + 3);
|
||||
*datum = SkipNumber(&s->string);
|
||||
s->string = Skip(s->string, 2);
|
||||
}
|
||||
}
|
||||
|
||||
void Serialize(Serializer *s, String name, uint32_t *datum) {
|
||||
Int d = *datum;
|
||||
Serialize(s, name, &d);
|
||||
}
|
||||
|
||||
void Serialize(Serializer *s, String name, uint8_t *datum) {
|
||||
Int d = *datum;
|
||||
Serialize(s, name, &d);
|
||||
}
|
||||
|
||||
void Serialize(Serializer *s, String name, int16_t *datum) {
|
||||
Int d = *datum;
|
||||
Serialize(s, name, &d);
|
||||
}
|
||||
|
||||
void Serialize(Serializer *s, String name, Vec2 *datum) {
|
||||
if (s->is_writing) {
|
||||
IKnowWhatImDoing_Appendf(s->buffer, "%.*s = {%lld, %lld}, ", FmtString(name), (long long)datum->x, (long long)datum->y);
|
||||
} else {
|
||||
s->string = Skip(s->string, name.len + 3 + 1);
|
||||
datum->x = (float)SkipNumber(&s->string);
|
||||
s->string = Skip(s->string, 2);
|
||||
datum->y = (float)SkipNumber(&s->string);
|
||||
s->string = Skip(s->string, 1 + 2);
|
||||
}
|
||||
}
|
||||
|
||||
void Serialize(Serializer *s, String name, char **text) {
|
||||
if (s->is_writing) {
|
||||
String str = *text;
|
||||
IKnowWhatImDoing_Appendf(s->buffer, "%.*s = \"'%.*s'\", ", FmtString(name), FmtString(str));
|
||||
} else {
|
||||
s->string = Skip(s->string, name.len + 3 + 2);
|
||||
String16 str = SkipUntil(&s->string, L"'\"");
|
||||
if (str.len) {
|
||||
Scratch scratch;
|
||||
String s8 = ToString(scratch, str);
|
||||
*text = Intern(&GlobalInternTable, s8).data;
|
||||
}
|
||||
s->string = Skip(s->string, 2 + 2);
|
||||
}
|
||||
}
|
||||
|
||||
using EventKind = uint32_t;
|
||||
enum {
|
||||
EVENT_NONE,
|
||||
EVENT_UPDATE,
|
||||
EVENT_QUIT,
|
||||
@@ -29,16 +86,40 @@ struct Event {
|
||||
int16_t xwindow;
|
||||
int16_t ywindow;
|
||||
uint8_t clicks;
|
||||
struct {
|
||||
uint8_t shift : 1;
|
||||
uint8_t ctrl : 1;
|
||||
uint8_t alt : 1;
|
||||
uint8_t super : 1;
|
||||
union {
|
||||
struct {
|
||||
uint8_t shift : 1;
|
||||
uint8_t ctrl : 1;
|
||||
uint8_t alt : 1;
|
||||
uint8_t super : 1;
|
||||
};
|
||||
uint8_t flags;
|
||||
};
|
||||
Vec2 wheel;
|
||||
const char *text;
|
||||
Vec2 wheel;
|
||||
char *text;
|
||||
};
|
||||
|
||||
void Serialize(Serializer *s, Event *e) {
|
||||
if (s->is_writing) {
|
||||
IKnowWhatImDoing_Appendf(s->buffer, "{");
|
||||
} else {
|
||||
s->string = Skip(s->string, 1);
|
||||
}
|
||||
Serialize(s, "kind", &e->kind);
|
||||
Serialize(s, "key", &e->key);
|
||||
Serialize(s, "xmouse", &e->xmouse);
|
||||
Serialize(s, "ymouse", &e->ymouse);
|
||||
Serialize(s, "flags", &e->flags);
|
||||
Serialize(s, "wheel", &e->wheel);
|
||||
Serialize(s, "text", &e->text);
|
||||
if (s->is_writing) {
|
||||
IKnowWhatImDoing_Appendf(s->buffer, "},\n");
|
||||
} else {
|
||||
s->string = Skip(s->string, 3);
|
||||
Assert(s->string.len == 0);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool IsMouseEvent(EventKind kind) { return kind >= EVENT_MOUSE_LEFT && kind <= EVENT_MOUSE_MOVE; }
|
||||
|
||||
const int DIR_RIGHT = 0;
|
||||
@@ -49,6 +130,9 @@ const int DIR_COUNT = 4;
|
||||
const bool CTRL_PRESSED = true;
|
||||
bool SHIFT_PRESSED = true;
|
||||
|
||||
bool AppIsRunning = true;
|
||||
bool WaitForEvents = true;
|
||||
|
||||
#define Press(KEY) (event.key == KEY)
|
||||
#define Ctrl(KEY) (event.key == KEY && event.ctrl)
|
||||
#define Shift(KEY) (event.key == KEY && event.shift)
|
||||
@@ -63,3 +147,9 @@ bool SHIFT_PRESSED = true;
|
||||
#define Mouse(x) (event.kind == EVENT_MOUSE_##x)
|
||||
#define MousePress() (Mouse(LEFT) || Mouse(RIGHT) || Mouse(MIDDLE))
|
||||
#define MouseUp() (Mouse(LEFT_UP) || Mouse(RIGHT_UP) || Mouse(MIDDLE_UP))
|
||||
|
||||
// Serializer GetSerializer(Arena *arena, bool is_writing) {
|
||||
// Serializer s = {arena, is_writing};
|
||||
// arena->align = 0;
|
||||
// return s;
|
||||
// }
|
||||
|
||||
@@ -19,8 +19,8 @@ WindowID ScrollbarSelected = {-1};
|
||||
WindowID DocumentSelected = {-1};
|
||||
Range DocumentRangeAnchor;
|
||||
|
||||
View *GCInfoView;
|
||||
Buffer *GCInfoBuffer;
|
||||
Buffer *EventBuffer;
|
||||
|
||||
void InitScratchBuffer() {
|
||||
Allocator sys_allocator = GetSystemAllocator();
|
||||
@@ -28,8 +28,9 @@ void InitScratchBuffer() {
|
||||
View *null_view = CreateView(null_buffer->id);
|
||||
Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID);
|
||||
|
||||
GCInfoBuffer = CreateBuffer(sys_allocator, BuffCWD("+gc"));
|
||||
GCInfoView = CreateView(GCInfoBuffer->id);
|
||||
GCInfoBuffer = CreateBuffer(sys_allocator, BuffCWD("+gc"));
|
||||
EventBuffer = CreateBuffer(sys_allocator, BuffCWD("+events"));
|
||||
EventBuffer->no_history = true;
|
||||
}
|
||||
|
||||
inline bool IsDocumentSelectionValid() {
|
||||
@@ -424,7 +425,7 @@ void GarbageCollect() {
|
||||
remove_item = true;
|
||||
// @todo: add view to free list
|
||||
|
||||
Command_Appendf(GCInfoView, "view %.*s", FmtString(buffer->name));
|
||||
IKnowWhatImDoing_Appendf(GCInfoBuffer, "view %.*s", FmtString(buffer->name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -442,7 +443,7 @@ void GarbageCollect() {
|
||||
remove_item = true;
|
||||
// @todo: add buffer to free list
|
||||
|
||||
Command_Appendf(GCInfoView, "buffer %.*s", FmtString(buffer->name));
|
||||
IKnowWhatImDoing_Appendf(GCInfoBuffer, "buffer %.*s", FmtString(buffer->name));
|
||||
} else if (buffer->file_mod_time) {
|
||||
int64_t new_file_mod_time = GetFileModTime(buffer->name);
|
||||
if (buffer->file_mod_time != new_file_mod_time) {
|
||||
|
||||
@@ -305,6 +305,8 @@ int main(int argc, char **argv)
|
||||
WindowOpenBufferView(window, it);
|
||||
}
|
||||
|
||||
Serializer ser = {EventBuffer, {}, true};
|
||||
|
||||
while (AppIsRunning) {
|
||||
FrameID += 1;
|
||||
|
||||
@@ -312,6 +314,7 @@ int main(int argc, char **argv)
|
||||
Array<Event> frame_events = GetEventsForFrame(scratch);
|
||||
For(frame_events) {
|
||||
if (it.kind == EVENT_QUIT) goto end_of_editor_loop;
|
||||
Serialize(&ser, &it);
|
||||
Update(it);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user