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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// chop this - :324
|
String16 SkipNumberEx(String16 *string) {
|
||||||
String16 ChopNumberEx(String16 *string) {
|
String16 col = {string->data, 0};
|
||||||
String16 col = {};
|
for (int64_t i = 0; i < string->len; i += 1) {
|
||||||
for (int64_t i = string->len - 1; i >= 0; i -= 1) {
|
|
||||||
if (IsDigit(string->data[i])) {
|
if (IsDigit(string->data[i])) {
|
||||||
col.data = string->data + i;
|
|
||||||
col.len += 1;
|
col.len += 1;
|
||||||
} else if (string->data[i] == L':') {
|
|
||||||
break;
|
|
||||||
} else {
|
} else {
|
||||||
return {};
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*string = Chop(*string, col.len + 1);
|
*string = Skip(*string, col.len);
|
||||||
return col;
|
return col;
|
||||||
}
|
}
|
||||||
|
|
||||||
Int ChopNumber(String16 *string) {
|
Int SkipNumber(String16 *string) {
|
||||||
Scratch scratch;
|
String16 col = SkipNumberEx(string);
|
||||||
String16 col = ChopNumberEx(string);
|
|
||||||
if (col.len == 0) return -1;
|
if (col.len == 0) return -1;
|
||||||
|
Scratch scratch;
|
||||||
String num_string = ToString(scratch, col);
|
String num_string = ToString(scratch, col);
|
||||||
Int result = strtoll(num_string.data, NULL, 10) - 1;
|
Int result = strtoll(num_string.data, NULL, 10);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
String16 ChopWhitespace(String16 *string) {
|
String16 SkipUntil(String16 *string, String16 str) {
|
||||||
String16 new_string = *string;
|
String16 begin = *string;
|
||||||
String16 whitespace = {string->data, 0};
|
begin.len = 0;
|
||||||
for (int64_t i = 0; i < string->len; i += 1) {
|
for (Int i = 0; string->len; begin.len += 1) {
|
||||||
if (IsWhitespace(string->data[i])) {
|
String16 match = GetPrefix(*string, str.len);
|
||||||
new_string = Skip(new_string, 1);
|
if (StartsWith(match, str)) break;
|
||||||
whitespace.len += 1;
|
*string = Skip(*string, 1);
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
return begin;
|
||||||
*string = new_string;
|
|
||||||
return whitespace;
|
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,64 @@
|
|||||||
bool AppIsRunning = true;
|
struct Serializer {
|
||||||
bool WaitForEvents = true;
|
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_NONE,
|
||||||
EVENT_UPDATE,
|
EVENT_UPDATE,
|
||||||
EVENT_QUIT,
|
EVENT_QUIT,
|
||||||
@@ -29,16 +86,40 @@ struct Event {
|
|||||||
int16_t xwindow;
|
int16_t xwindow;
|
||||||
int16_t ywindow;
|
int16_t ywindow;
|
||||||
uint8_t clicks;
|
uint8_t clicks;
|
||||||
|
union {
|
||||||
struct {
|
struct {
|
||||||
uint8_t shift : 1;
|
uint8_t shift : 1;
|
||||||
uint8_t ctrl : 1;
|
uint8_t ctrl : 1;
|
||||||
uint8_t alt : 1;
|
uint8_t alt : 1;
|
||||||
uint8_t super : 1;
|
uint8_t super : 1;
|
||||||
};
|
};
|
||||||
|
uint8_t flags;
|
||||||
|
};
|
||||||
Vec2 wheel;
|
Vec2 wheel;
|
||||||
const char *text;
|
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; }
|
inline bool IsMouseEvent(EventKind kind) { return kind >= EVENT_MOUSE_LEFT && kind <= EVENT_MOUSE_MOVE; }
|
||||||
|
|
||||||
const int DIR_RIGHT = 0;
|
const int DIR_RIGHT = 0;
|
||||||
@@ -49,6 +130,9 @@ const int DIR_COUNT = 4;
|
|||||||
const bool CTRL_PRESSED = true;
|
const bool CTRL_PRESSED = true;
|
||||||
bool SHIFT_PRESSED = true;
|
bool SHIFT_PRESSED = true;
|
||||||
|
|
||||||
|
bool AppIsRunning = true;
|
||||||
|
bool WaitForEvents = true;
|
||||||
|
|
||||||
#define Press(KEY) (event.key == KEY)
|
#define Press(KEY) (event.key == KEY)
|
||||||
#define Ctrl(KEY) (event.key == KEY && event.ctrl)
|
#define Ctrl(KEY) (event.key == KEY && event.ctrl)
|
||||||
#define Shift(KEY) (event.key == KEY && event.shift)
|
#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 Mouse(x) (event.kind == EVENT_MOUSE_##x)
|
||||||
#define MousePress() (Mouse(LEFT) || Mouse(RIGHT) || Mouse(MIDDLE))
|
#define MousePress() (Mouse(LEFT) || Mouse(RIGHT) || Mouse(MIDDLE))
|
||||||
#define MouseUp() (Mouse(LEFT_UP) || Mouse(RIGHT_UP) || Mouse(MIDDLE_UP))
|
#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};
|
WindowID DocumentSelected = {-1};
|
||||||
Range DocumentRangeAnchor;
|
Range DocumentRangeAnchor;
|
||||||
|
|
||||||
View *GCInfoView;
|
|
||||||
Buffer *GCInfoBuffer;
|
Buffer *GCInfoBuffer;
|
||||||
|
Buffer *EventBuffer;
|
||||||
|
|
||||||
void InitScratchBuffer() {
|
void InitScratchBuffer() {
|
||||||
Allocator sys_allocator = GetSystemAllocator();
|
Allocator sys_allocator = GetSystemAllocator();
|
||||||
@@ -29,7 +29,8 @@ void InitScratchBuffer() {
|
|||||||
Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID);
|
Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID);
|
||||||
|
|
||||||
GCInfoBuffer = CreateBuffer(sys_allocator, BuffCWD("+gc"));
|
GCInfoBuffer = CreateBuffer(sys_allocator, BuffCWD("+gc"));
|
||||||
GCInfoView = CreateView(GCInfoBuffer->id);
|
EventBuffer = CreateBuffer(sys_allocator, BuffCWD("+events"));
|
||||||
|
EventBuffer->no_history = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool IsDocumentSelectionValid() {
|
inline bool IsDocumentSelectionValid() {
|
||||||
@@ -424,7 +425,7 @@ void GarbageCollect() {
|
|||||||
remove_item = true;
|
remove_item = true;
|
||||||
// @todo: add view to free list
|
// @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;
|
remove_item = true;
|
||||||
// @todo: add buffer to free list
|
// @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) {
|
} else if (buffer->file_mod_time) {
|
||||||
int64_t new_file_mod_time = GetFileModTime(buffer->name);
|
int64_t new_file_mod_time = GetFileModTime(buffer->name);
|
||||||
if (buffer->file_mod_time != new_file_mod_time) {
|
if (buffer->file_mod_time != new_file_mod_time) {
|
||||||
|
|||||||
@@ -305,6 +305,8 @@ int main(int argc, char **argv)
|
|||||||
WindowOpenBufferView(window, it);
|
WindowOpenBufferView(window, it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Serializer ser = {EventBuffer, {}, true};
|
||||||
|
|
||||||
while (AppIsRunning) {
|
while (AppIsRunning) {
|
||||||
FrameID += 1;
|
FrameID += 1;
|
||||||
|
|
||||||
@@ -312,6 +314,7 @@ int main(int argc, char **argv)
|
|||||||
Array<Event> frame_events = GetEventsForFrame(scratch);
|
Array<Event> frame_events = GetEventsForFrame(scratch);
|
||||||
For(frame_events) {
|
For(frame_events) {
|
||||||
if (it.kind == EVENT_QUIT) goto end_of_editor_loop;
|
if (it.kind == EVENT_QUIT) goto end_of_editor_loop;
|
||||||
|
Serialize(&ser, &it);
|
||||||
Update(it);
|
Update(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user