From 4849f7d3e40c929866b56d393b53bf7c207528bc Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Fri, 20 Dec 2019 16:17:32 +1100 Subject: [PATCH] Implemented setting/resetting entity's parent in editor --- src/game/editor.c | 41 ++++++++++++++++++++++++++++++++++++++++- src/game/scene.c | 2 +- todo.txt | 4 +++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/game/editor.c b/src/game/editor.c index e1f4c3b..1880b62 100755 --- a/src/game/editor.c +++ b/src/game/editor.c @@ -1524,7 +1524,46 @@ void editor_window_property_inspector(struct nk_context* context, struct Editor* nk_layout_row_dynamic(context, row_height, 2); nk_label(context, "ID", NK_TEXT_ALIGN_LEFT); nk_labelf(context, NK_TEXT_ALIGN_RIGHT, "%d", entity->id); nk_layout_row_dynamic(context, row_height, 2); nk_label(context, "Selected", NK_TEXT_ALIGN_LEFT); nk_labelf(context, NK_TEXT_ALIGN_RIGHT, "%s", (entity->flags & EF_SELECTED_IN_EDITOR) ? "True" : "False"); nk_layout_row_dynamic(context, row_height, 2); nk_label(context, "Entity Type", NK_TEXT_ALIGN_LEFT); nk_labelf(context, NK_TEXT_ALIGN_RIGHT, "%s", entity_type_name_get(entity)); - nk_layout_row_dynamic(context, row_height, 2); nk_label(context, "Parent Name", NK_TEXT_ALIGN_LEFT); nk_label(context, parent_ent ? parent_ent->name : "NONE", NK_TEXT_ALIGN_RIGHT); + + nk_layout_row_dynamic(context, row_height + 5, 2); + nk_label(context, "Parent Name", NK_TEXT_ALIGN_LEFT); + static char parent_name[MAX_ENTITY_NAME_LEN]; + static bool copy_parent_name = true; + + if(copy_parent_name) + { + memset(parent_name, '\0', MAX_ENTITY_NAME_LEN); + strncpy(parent_name, parent_ent->name, MAX_ENTITY_NAME_LEN); + } + + int rename_parent_edit_flags = NK_EDIT_GOTO_END_ON_ACTIVATE | NK_EDIT_FIELD | NK_EDIT_SIG_ENTER; + int rename_parent_edit_state = nk_edit_string_zero_terminated(context, rename_parent_edit_flags, parent_name, MAX_ENTITY_NAME_LEN, NULL); + if(rename_parent_edit_state & NK_EDIT_ACTIVATED) + { + copy_parent_name = false; + } + else if(rename_parent_edit_state & NK_EDIT_DEACTIVATED) + { + copy_parent_name = true; + } + else if(rename_parent_edit_state & NK_EDIT_COMMITED) + { + if(strncmp(parent_name, "NONE", MAX_ENTITY_NAME_LEN) == 0 || strncmp(parent_name, "ROOT", MAX_ENTITY_NAME_LEN) == 0) + { + scene_entity_parent_reset(scene, entity); + } + else + { + struct Entity* new_parent = scene_find(scene, parent_name); + if(new_parent) + scene_entity_parent_set(scene, entity, new_parent); + else + log_warning("Could not find new parent %s for %s", parent_name, entity->name); + } + copy_parent_name = true; + nk_edit_unfocus(context); + } + //nk_label(context, parent_ent ? parent_ent->name : "NONE", NK_TEXT_ALIGN_RIGHT); /* Transform */ { diff --git a/src/game/scene.c b/src/game/scene.c index 8701be6..f50a7b9 100755 --- a/src/game/scene.c +++ b/src/game/scene.c @@ -788,7 +788,7 @@ void scene_entity_parent_reset(struct Scene* scene, struct Entity* entity) void scene_entity_parent_set(struct Scene* scene, struct Entity* entity, struct Entity* parent) { - assert(scene && entity && parent); + assert(scene && entity && parent && entity != parent); transform_parent_set(entity, parent, true); } diff --git a/todo.txt b/todo.txt index bf99ef0..b7870c8 100644 --- a/todo.txt +++ b/todo.txt @@ -135,6 +135,7 @@ Bugs: - Fix weird rotational bug when rotation resets or inverts after 180 degrees - Fix crash if player's mesh is deleted in editor - Fix camera not rotating if the cursor overlaps any ui element + - Fix hierarchichal transformations in the editor when the entity being transformed is a child entity of another entity Done: * Input @@ -368,4 +369,5 @@ Done: * Move debug vars display settings to debug_vars struct * Command history in console * Added button to reset local transformations for selected entity in property inspector - * Implmented renaming scene objects in editor \ No newline at end of file + * Implmented renaming scene objects in editor + * Implemented setting/resetting parent entity for entity \ No newline at end of file