From 8cba2626be1d8279550acd19bdc93ce6d3346a52 Mon Sep 17 00:00:00 2001 From: Mergul Date: Wed, 10 Jun 2020 15:35:42 +0200 Subject: [PATCH] Added entity filering support and fixed minor bug --- demos/source/app.d | 16 +++++++++++++++ demos/source/gui/manager.d | 42 ++++++++++++++++++++++++++++---------- source/bubel/ecs/entity.d | 2 +- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/demos/source/app.d b/demos/source/app.d index d526f97..6808e42 100644 --- a/demos/source/app.d +++ b/demos/source/app.d @@ -134,9 +134,21 @@ struct Launcher vec2 position; ComponentRef[] add_comps; ushort[] rem_comps; + ushort[] filter; + + bool filterEntity(ref const Entity entity) + { + EntityMeta meta = entity.getMeta(); + foreach(id;filter) + { + if(!meta.hasComponent(id))return false; + } + return true; + } void removeEntity(IteratorSystem.EntitiesData data) { + if(!filterEntity(data.entity[0]))return; foreach(i;0..data.length) { vec2 rel_vec = data.location[i] - position; @@ -147,6 +159,7 @@ struct Launcher void addComponent(IteratorSystem.EntitiesData data) { + if(!filterEntity(data.entity[0]))return; foreach(i;0..data.length) { vec2 rel_vec = data.location[i] - position; @@ -157,6 +170,7 @@ struct Launcher void overrideComponent(IteratorSystem.EntitiesData data) { + if(!filterEntity(data.entity[0]))return; foreach(i;0..data.length) { vec2 rel_vec = data.location[i] - position; @@ -171,6 +185,7 @@ struct Launcher void removeComponent(IteratorSystem.EntitiesData data) { + if(!filterEntity(data.entity[0]))return; foreach(i;0..data.length) { vec2 rel_vec = data.location[i] - position; @@ -185,6 +200,7 @@ struct Launcher Iterator iterator; iterator.size2 = size2; iterator.position = position; + iterator.filter = gui_manager.filter_list[]; switch(used_tool) { diff --git a/demos/source/gui/manager.d b/demos/source/gui/manager.d index 4ae9688..6fb3165 100644 --- a/demos/source/gui/manager.d +++ b/demos/source/gui/manager.d @@ -27,6 +27,8 @@ struct GUIManager Vector!ComponentGUI components; Vector!TemplateGUI templates; Vector!ComponentEditGUI edit_components; + Vector!bool filter; + Vector!ushort filter_list; int selected_template = 0; int selected_component = 0; @@ -62,8 +64,14 @@ struct GUIManager if(comp.variables)Mallocator.dispose(comp.variables); comp.variables = null; comp.used = 0; + comp.name = null; + } + foreach(ref comp; filter) + { + comp = false; } + filter_list.clear(); systems.clear(); templates.clear(); components.clear(); @@ -384,16 +392,14 @@ struct GUIManager { if(igListBoxHeaderInt("Components",cast(int)components.length,cast(int)components.length)) { + foreach(i, comp; components) { - foreach(i, comp; components) + if(igSelectable(comp.name,selected_component == i,0,ImVec2(0,0))) { - if(igSelectable(comp.name,selected_component == i,0,ImVec2(0,0))) - { - selected_component = cast(uint)i; - } + selected_component = cast(uint)i; } - igListBoxFooter(); } + igListBoxFooter(); } if(igIsItemHovered(0))igSetTooltip("Select component to add/remove (SHIFT + Scroll)"); } @@ -407,26 +413,40 @@ struct GUIManager style.Colors[ImGuiCol_Header] = col; } + private void genFilterList() + { + filter_list.clear(); + foreach(i, comp; filter) + { + if(comp) + { + filter_list.add(cast(ushort)i); + } + } + } + void filterGUI() { ImGuiStyle * style = igGetStyle(); ImVec4 col = style.Colors[ImGuiCol_Header]; style.Colors[ImGuiCol_Header] = style.Colors[ImGuiCol_TextSelectedBg]; + if(filter.length < edit_components.length)filter.length(edit_components.length); + int length = 0; foreach(comp; edit_components) { if(comp.name !is null)length++; } - if(length && igListBoxHeaderInt("Components",cast(int)length,cast(int)length)) + if(length && igListBoxHeaderInt("Components##FilterComponents",cast(int)length,cast(int)length)) { - foreach(i, comp; edit_components) + foreach(i, ref comp; edit_components) { - if(comp.name is null)return; - if(igSelectable(comp.name,false,0,ImVec2(0,0))) + if(comp.name is null)continue; + if(igSelectableBoolPtr(comp.name,&filter[i],0,ImVec2(0,0))) { - + genFilterList(); } } igListBoxFooter(); diff --git a/source/bubel/ecs/entity.d b/source/bubel/ecs/entity.d index 6a64d50..2c64c60 100644 --- a/source/bubel/ecs/entity.d +++ b/source/bubel/ecs/entity.d @@ -50,7 +50,7 @@ struct Entity return true; } - EntityMeta getMeta() + EntityMeta getMeta() const { EntityMeta meta; meta.block = gEM.getMetaData(&this);