added rendering all structure members to the editor
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 31 Jan 2022 03:17:09 +0000 (21:17 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 31 Jan 2022 03:17:09 +0000 (21:17 -0600)
src/entity/editor.onyx
src/entity/manager.onyx

index ad08f55365557ebecbb0937dc7becca35c9528c1..f9a7a4fe7e9048770930fbb633d586d6080d1bfa 100644 (file)
@@ -133,7 +133,7 @@ editor_draw :: () {
     }
 
     { // Draw sidebar, if necessary
-        sidebar_width = editor_openness * 300.0f;
+        sidebar_width = editor_openness * 400.0f;
         w := sidebar_width; 
         x := ~~ window_width - w;
         y := 40.0f;
@@ -163,9 +163,13 @@ editor_draw :: () {
 
     font_print(editor_font, x + 2, y + 20, info.name);
 
-    i := 0;
+    render_struct_fields(any.{~~entity, entity.type}, 0, x, y, w, h);
+}
+
+#local render_struct_fields :: (v: any, i: i32, x, y, w, h: f32) -> (new_y: f32, new_i: i32) {
+    info := cast(^type_info.Type_Info_Struct) type_info.get_type_info(v.type);
     for info.members {
-        defer i += 1;
+        i += 1;
         y += 20;
 
         if i % 2 == 0 {
@@ -174,7 +178,26 @@ editor_draw :: () {
         }
 
         font_print(editor_font, x + 22, y + 20, it.name);
+
+        member_any := any.{cast(^u8) v.data + it.offset, it.type};
+
+        if type_info.get_type_info(it.type).kind == .Struct {
+            y, i = render_struct_fields(member_any, i, x + 20, y, w - 20, h);
+
+        } elseif it.type == Entity_ID {
+            value_buf: [1024] u8;
+            entity_type := (entity_manager->get(*cast(^Entity_ID) member_any.data)).type; // Dereferencing null here
+            value_str := conv.format_va(value_buf, "{} ({})", .[member_any, .{^entity_type, type_expr}]);
+            font_print(editor_font, x + w - font_get_width(editor_font, value_str) - 2, y + 20, value_str);
+
+        } else {
+            value_buf: [1024] u8;
+            value_str := conv.format_va(value_buf, "{}", .[member_any]);
+            font_print(editor_font, x + w - font_get_width(editor_font, value_str) - 2, y + 20, value_str);
+        }
     }
+
+    return y, i;
 }
 
 #local {
index 34e9c1522a59eaa2ffce32d3d557a7b06529960f..30aa7e99b77da10c284a158174526b612a7d03d8 100644 (file)
@@ -2,8 +2,11 @@
 use package core
 use package glfw3
 
-Entity_ID :: u32
 Entity_Nothing :: cast(Entity_ID) 0
+Entity_ID :: #distinct u32
+#match    hash.to_u32 macro (e: Entity_ID) => (package core.hash).to_u32(cast(u32) e);
+#operator == (a, b: Entity_ID) => cast(u32) a == cast(u32) b;
+#operator != (a, b: Entity_ID) => cast(u32) a != cast(u32) b;
 
 Entity :: struct {
     id: Entity_ID;
@@ -106,12 +109,12 @@ entity_manager_register :: (use this: ^Entity_Manager, $entity_type: type_expr)
     }
 }
 
-entity_manager_add :: (use this: ^Entity_Manager, entity: ^$T) -> u32 where IsEntity(^T) {
+entity_manager_add :: (use this: ^Entity_Manager, entity: ^$T) -> Entity_ID where IsEntity(^T) {
     this->register(T);
 
     entity.type = T;
     entity.id = next_entity_id;
-    next_entity_id += 1;
+    next_entity_id = ~~(cast(u32) next_entity_id + 1);
 
     entities << entity;
     entity_map[entity.id] = entity;