diff --git a/godot/astream/building_layout_data.conf b/godot/astream/building_layout_data.conf index d6ae831..40fc5d9 100644 --- a/godot/astream/building_layout_data.conf +++ b/godot/astream/building_layout_data.conf @@ -1,39 +1,58 @@ [layouts] entries=[ { -"children": [ 1, 2, 3 ], -"floor_index": 0, +"children": [ 2 ], "index": 0, +"name": "v2", +"type": "layout" +}, { +"children": [ 3, 4, 5 ], +"floor_index": 0, +"index": 1, "name": "v1", "type": "layout" }, { -"children": [ 4, 5, 6 ], -"index": 1, +"children": [ 6 ], +"index": 2, +"name": "zone_0", +"type": "zone", +"zone_type": 1 +}, { +"children": [ 7, 8, 9 ], +"index": 3, "name": "zone_0", "type": "zone", "zone_type": 0 }, { -"children": [ 7, 8, 9, 10 ], -"index": 2, +"children": [ 10, 11, 12, 13 ], +"index": 4, "name": "zone_1", "type": "zone", "zone_type": 1 }, { -"children": [ 11, 12 ], -"index": 3, +"children": [ 14, 15 ], +"index": 5, "name": "unit_0", "type": "unit" }, { +"children": [ 16 ], +"index": 6, +"name": "stair_0", +"room_area": 0.0, +"room_type": 305, +"type": "room", +"window": true +}, { "children": [ ], -"index": 4, +"index": 7, "name": "wc_0", -"room_area": 4.0, +"room_area": 16.0, "room_type": 200, "type": "room", "window": false }, { "children": [ ], -"index": 5, +"index": 8, "name": "bathroom_0", "room_area": 16.0, "room_type": 201, @@ -41,31 +60,31 @@ entries=[ { "window": false }, { "children": [ ], -"index": 6, +"index": 9, "name": "bedroom_0", -"room_area": 36.0, +"room_area": 64.0, "room_type": 202, "type": "room", "window": true }, { "children": [ ], -"index": 7, +"index": 10, "name": "living_room_0", -"room_area": 36.0, +"room_area": 144.0, "room_type": 300, "type": "room", "window": true }, { "children": [ ], -"index": 8, +"index": 11, "name": "kitchen_0", -"room_area": 16.0, +"room_area": 64.0, "room_type": 302, "type": "room", "window": true }, { "children": [ ], -"index": 9, +"index": 12, "name": "storage_room_0", "room_area": 16.0, "room_type": 307, @@ -73,27 +92,33 @@ entries=[ { "window": true }, { "children": [ ], -"index": 10, +"index": 13, "name": "enterance_0", "room_area": 16.0, "room_type": 304, "type": "room", "window": true }, { -"children": [ 13, 14, 15 ], -"index": 11, +"children": [ 17, 18, 19 ], +"index": 14, "name": "zone_0", "type": "zone", "zone_type": 0 }, { -"children": [ 16, 17 ], -"index": 12, +"children": [ 20, 21 ], +"index": 15, "name": "zone_1", "type": "zone", "zone_type": 1 }, { +"children": [ 22 ], +"floor_index": 0, +"index": 16, +"name": "floor_0", +"type": "floor" +}, { "children": [ ], -"index": 13, +"index": 17, "name": "bedroom_0", "room_area": 36.0, "room_type": 202, @@ -101,7 +126,7 @@ entries=[ { "window": true }, { "children": [ ], -"index": 14, +"index": 18, "name": "bathroom_0", "room_area": 16.0, "room_type": 201, @@ -109,7 +134,7 @@ entries=[ { "window": false }, { "children": [ ], -"index": 15, +"index": 19, "name": "wc_0", "room_area": 4.0, "room_type": 200, @@ -117,7 +142,7 @@ entries=[ { "window": false }, { "children": [ ], -"index": 16, +"index": 20, "name": "living_room_0", "room_area": 36.0, "room_type": 300, @@ -125,10 +150,27 @@ entries=[ { "window": true }, { "children": [ ], -"index": 17, +"index": 21, "name": "kitchen_0", "room_area": 16.0, "room_type": 302, "type": "room", "window": true +}, { +"children": [ 23, 24 ], +"index": 22, +"name": "unit_0", +"type": "unit" +}, { +"children": [ ], +"index": 23, +"name": "zone_0", +"type": "zone", +"zone_type": 0 +}, { +"children": [ ], +"index": 24, +"name": "zone_1", +"type": "zone", +"zone_type": 1 } ] diff --git a/src/modules/stream/ui/building_layout_graph.cpp b/src/modules/stream/ui/building_layout_graph.cpp index 1384ac7..338c3ce 100644 --- a/src/modules/stream/ui/building_layout_graph.cpp +++ b/src/modules/stream/ui/building_layout_graph.cpp @@ -51,58 +51,6 @@ const Array &BuildingLayoutGraph::get_public_rooms() const return public_rooms; } -flecs::entity -BuildingLayoutGraph::create_graph_entity(const String &base_path, - const String &entity_type) -{ - flecs::world ecs = BaseData::get_singleton()->get(); - flecs::entity base_e = ecs.lookup(base_path.ascii().ptr()); - int count = 0; - const String &type_name = entity_type; - base_e.children([type_name, &count](flecs::entity e) { - String name(e.name()); - if (name.begins_with(type_name + "_" + itos(count))) - count++; - }); - String ename = type_name + "_" + itos(count); - flecs::entity new_e = ecs.entity(ename.ascii().ptr()).child_of(base_e); - return new_e; -} - -void BuildingLayoutGraph::create_zone(const String &base_path, int zone_type) -{ - flecs::entity new_e = create_graph_entity(base_path, "zone"); - new_e.set( - { zone_type }); -} - -void BuildingLayoutGraph::create_unit(const String &base_path) -{ - flecs::entity new_e = create_graph_entity(base_path, "unit"); - new_e.set({ 0 }); -} - -void BuildingLayoutGraph::create_floor(const String &base_path) -{ - flecs::entity new_e = create_graph_entity(base_path, "floor"); - new_e.set({ 0 }); -} - -void BuildingLayoutGraph::create_room(const String &base_path, int id) -{ - Array room; - BuildingLayoutGraph::get_singleton()->get_room_data(id, room); - assert(!room.empty()); - String type_name = room[1]; - Dictionary room_options = room[2]; - bool window = room_options.get("window", false); - type_name = type_name.replace(" ", "_").to_lower(); - flecs::entity new_e = create_graph_entity(base_path, type_name); - new_e.set( - { id, window }); - new_e.set({ 0.0f }); -} - void BuildingLayoutGraph::destroy_graph_entity(const String &path) { flecs::world ecs = BaseData::get_singleton()->get(); @@ -202,32 +150,60 @@ void BuildingLayoutGraph::recalculate_size(const String &layout_name) while (!queue.empty()) { flecs::entity e = queue.front()->get(); queue.pop_front(); - int size = 1; bool readd = false; + int size = 1; e.children([&readd, &size, &queue](flecs::entity fe) { + print_line(String(fe.path())); if (!fe.has()) { - fe.set( - { 0, 0, 0 }); readd = true; + } else { + assert(fe.is_valid()); + assert(fe.has< + WorldEditor::components:: + buildings_layout_graph_node>()); + assert(fe.get< + WorldEditor::components:: + buildings_layout_graph_node>()); + size += fe.get() + ->size; + queue.push_back(fe); } - size += fe.get() - ->size; - queue.push_back(fe); }); if (readd) queue.push_back(e); - if (e.get() - ->size != size) { - e.get_mut()) { + bool is_r = e.has< + WorldEditor::components::buildings_layout_room>(); + bool is_z = e.has< + WorldEditor::components::buildings_layout_zone>(); + bool is_u = e.has< + WorldEditor::components::buildings_layout_unit>(); + bool is_f = e.has(); + if (is_r || is_z || is_u || is_f) + e.set( + { 0, 0, 0 }); + queue.push_back(e); + } else { + assert(e.has()); + if (e.get() - ->size = size; - if (e.parent() - .has()) - queue.push_back(e); + ->size != size) { + e.get_mut() + ->size = size; + e.modified(); + if (e.parent() + .has()) + queue.push_back(e); + } } } } @@ -256,6 +232,8 @@ void BuildingLayoutGraph::update_button_positions(const String &layout_name) ->y_pos = y; int count = 0; int offset = y; + e.modified< + WorldEditor::components::buildings_layout_graph_node>(); e.children([depth, y, &count, &queue, &offset](flecs::entity fe) { int size = fe.get()) { + buildings_layout_floor>()) { /* non-root floor */ entry["type"] = "floor"; - int floor_index = - e.get() - ->index; - entry["floor_index"] = floor_index; + if (e.has()) { + int floor_index = + e.get() + ->index; + print_line("floor_index: " + itos(floor_index)); + entry["floor_index"] = floor_index; + } } else if (e.has()) { entry["type"] = "zone"; @@ -512,6 +494,8 @@ void BuildingLayoutGraph::load_layouts() } else e.set({ 0 }); + e.set({ 0, 0, 0 }); } else if (type == "floor") { if (entry.has("floor_index")) { int floor_index = entry["floor_index"]; @@ -527,15 +511,21 @@ void BuildingLayoutGraph::load_layouts() buildings_layout_floor_index>( { 0 }); } + e.set({ 0, 0, 0 }); } else if (type == "zone") { int zone_type = entry["zone_type"]; e.set( { zone_type }); e.add(); + e.set({ 0, 0, 0 }); } else if (type == "unit") { e.set( { 0 }); e.add(); + e.set({ 0, 0, 0 }); } else if (type == "room") { int room_type = entry["room_type"]; bool window = entry["window"]; @@ -550,6 +540,8 @@ void BuildingLayoutGraph::load_layouts() } else e.set({ 0.0f }); + e.set({ 0, 0, 0 }); } else assert(false); entity_index[index] = e; diff --git a/src/modules/stream/ui/building_layout_graph.h b/src/modules/stream/ui/building_layout_graph.h index d5e9043..1e69016 100644 --- a/src/modules/stream/ui/building_layout_graph.h +++ b/src/modules/stream/ui/building_layout_graph.h @@ -49,6 +49,6 @@ public: void create_interior_tilemap(const String &layout_name); int get_layout_count() const; struct graph_module { - graph_module(flecs::world ecs); + graph_module(flecs::world &ecs); }; }; \ No newline at end of file diff --git a/src/modules/stream/ui/building_layout_graph_ui.h b/src/modules/stream/ui/building_layout_graph_ui.h index 1d7c1f4..449698d 100644 --- a/src/modules/stream/ui/building_layout_graph_ui.h +++ b/src/modules/stream/ui/building_layout_graph_ui.h @@ -35,48 +35,7 @@ public: { dlg->connect("tree_entered", this, "tree_entered"); } - void menu_pressed(int id, Control *button, const String &path) - { - assert(button); - print_line(itos(id)); - print_line(path); - switch (id) { - case 100: - case 101: - case 102: - case 103: - case 104: { - int count = 0; - switch (id) { - case 100: - BuildingLayoutGraph::get_singleton() - ->create_unit(path); - break; - case 101: - BuildingLayoutGraph::get_singleton() - ->create_zone(path, 0); - break; - case 102: - BuildingLayoutGraph::get_singleton() - ->create_zone(path, 1); - break; - case 104: - BuildingLayoutGraph::get_singleton() - ->create_floor(path); - break; - } - } break; - case 1000: - BuildingLayoutGraph::get_singleton() - ->destroy_graph_entity(path); - break; - default: - BuildingLayoutGraph::get_singleton()->create_room(path, - id); - break; - } - update_graph(); - } + void menu_pressed(int id, Control *button, const String &path); void update_layout_item_list() { ItemList *item_list = @@ -104,6 +63,7 @@ public: EditorEvent::get_singleton()->event.emit( "building_layouts_layout_selected", varray(current_layout)); + print_line("select_layout: " + itos(id)); } void handle_event(const String &event, const Vector &args) { @@ -177,6 +137,12 @@ public: update_graph(); EditorEvent::get_singleton()->event.add_listener( this, &BuildingLayoutGraphUI::handle_event); + ItemList *item_list = gui->get_as_node( + "%building_layout_list"); + if (!item_list->is_connected("item_selected", this, + "select_layout")) + item_list->connect("item_selected", this, + "select_layout"); } } void room_size_entered(float value, Control *item, const String &path) @@ -184,12 +150,15 @@ public: print_line(String::num(value)); flecs::world ecs = BaseData::get_singleton()->get(); flecs::entity e = ecs.lookup(path.ascii().ptr()); - float value_r = Math::ceil(Math::sqrt(Math::abs(value))); - float value_div = Math::ceil(value_r / 2); - float area_final = (value_div * 2) * (value_div * 2); + int value_r = (int)Math::ceil(Math::sqrt(Math::abs(value))); + int pdiv = value_r % 4; + int value_div = value_r / 4; + if (pdiv > 0) + value_div++; + float area_final = (value_div * 4) * (value_div * 4); assert(e.is_valid()); e.set( - { area_final }); + { MAX(16.0f, area_final) }); print_line("data set room_size"); } void floor_index_entered(float value, Control *item, const String &path) @@ -207,233 +176,7 @@ public: Vector buttons; #define DEPTH_MUL 160 #define Y_MUL 130 - void update_buttons(ColorRect *canvas) - { - assert(current_layout.length() > 0); - BuildingLayoutGraph::get_singleton()->recalculate_size( - current_layout); - BuildingLayoutGraph::get_singleton()->update_button_positions( - current_layout); - flecs::entity layout_e = get_current_layout(); - List queue; - bool layout_empty = true; - queue.push_back(layout_e); - while (!queue.empty()) { - buttons.clear(); - layout_empty = false; - flecs::entity e = queue.front()->get(); - queue.pop_front(); - if (!e.has()) { - assert(false); - } - int depth = e.get() - ->depth; - int y = e.get() - ->y_pos; - /* make button here */ - Vector2 pt(depth * DEPTH_MUL + 40, y * Y_MUL + 40); - if (pt.x + 16.0f >= canvas->get_size().x || - pt.y + 16.0f >= canvas->get_size().y) { - Vector2 old_size = canvas->get_size(); - Vector2 new_size(MAX(pt.x + 200.0f, old_size.x), - MAX(pt.y + 200.0f, - old_size.y)); - Object::cast_to(canvas->get_parent()) - ->set_custom_minimum_size(new_size); - canvas->set_size(new_size); - } - { - VBoxContainer *box = memnew(VBoxContainer); - MenuButton *button = memnew(MenuButton); - button->set_text(""); - box->add_child(button); - canvas->add_child(box); - button->set_size(Vector2(80, 40)); - box->set_position(pt); - box->update(); - List > menu_options; - List >::Element *pe; - BuildingLayoutGraph::get_singleton() - ->get_menu_entries(e, &menu_options); - pe = menu_options.front(); - while (pe) { - if (pe->get().first < 0) - button->get_popup() - ->add_separator(); - else - button->get_popup()->add_item( - pe->get().second, - pe->get().first); - pe = pe->next(); - } - - if (e.has()) { - button->set_text(button->get_text() + - "Floor\n" + - String(e.name())); - std::vector args = { - "Floor: ", "floor_index" - }; - HashMap save_data; - int floor_index = - e.get() - ->index; - ui_field::ui_field_builder(canvas, box, - "p{h{lx#$}}", - args.data(), - args.size(), - &save_data); - assert(save_data.has("floor_index")); - Object::cast_to( - save_data["floor_index"]) - ->set_value(floor_index); - save_data["floor_index"]->connect( - "value_changed", this, - "floor_index_entered", - varray(save_data["floor_index"], - String(e.path()))); - } - if (e.has()) { - button->set_text(button->get_text() + - "Unit\n" + - String(e.name())); - if (e.has()) { - float area = - e.get() - ->area; - std::vector args = { - "Area: " + itos(area) - }; - ui_field::ui_field_builder( - canvas, box, "l", - args.data(), - args.size()); - } - } - if (e.has()) { - int zone_type = - e.get() - ->type; - if (zone_type == 0) { - int i; - button->set_text( - button->get_text() + - "Private Zone\n"); - } else if (zone_type == 1) { - int i; - button->set_text( - button->get_text() + - "Public Zone\n"); - } - button->set_text(button->get_text() + - String(e.name())); - if (e.has()) { - float area = - e.get() - ->area; - std::vector args = { - "Area: " + - String::num(area) - }; - ui_field::ui_field_builder( - canvas, box, "l", - args.data(), - args.size()); - } - } - if (e.has()) { - int room_type = - e.get() - ->room_type; - button->set_text( - button->get_text() + "Room\n" + - String(e.name() + " " + - itos(room_type))); - if (e.has()) { - std::vector args = { - "Area: ", "area_value" - }; - HashMap - save_data; - float area = - e.get() - ->area; - ui_field::ui_field_builder( - canvas, box, - "p{h{lx#$}}", - args.data(), - args.size(), - &save_data); - assert(save_data.has( - "area_value")); - Object::cast_to( - save_data["area_value"]) - ->set_value(area); - save_data["area_value"]->connect( - "value_changed", this, - "room_size_entered", - varray(save_data["area_value"], - String(e.path()))); - } - } - if (e.has()) { - int index = - e.get() - ->index; - std::vector args = { - "Floor index: " + itos(index) - }; - ui_field::ui_field_builder(canvas, box, - "l", - args.data(), - args.size()); - } - if (e.has()) { - float area = - e.get() - ->area; - int grid_size = - e.get() - ->grid_size; - std::vector args = { - "Area: " + String::num(area), - "Grid size: " + itos(grid_size) - }; - ui_field::ui_field_builder(canvas, box, - "ll", - args.data(), - args.size()); - } - button->get_popup()->connect( - "id_pressed", this, "menu_pressed", - varray(button, String(e.path()))); - } - e.children([&queue](flecs::entity fe) { - queue.push_back(fe); - }); - } - } + void update_buttons(ColorRect *canvas); void update_graph() { int i; @@ -559,27 +302,7 @@ public: "l_p{v{li.#!lh{e#+!b#!Q}b#!Qb#!Q}}", args_data.data(), args_data.size()); } - static void _bind_methods() - { - ClassDB::bind_method(D_METHOD("menu_pressed", "id", "button", - "path"), - &BuildingLayoutGraphUI::menu_pressed); - ClassDB::bind_method(D_METHOD("tree_entered"), - &BuildingLayoutGraphUI::tree_entered); - ClassDB::bind_method(D_METHOD("draw_graph"), - &BuildingLayoutGraphUI::draw_graph); - ClassDB::bind_method(D_METHOD("select_layout", "id"), - &BuildingLayoutGraphUI::select_layout); - ClassDB::bind_method(D_METHOD("room_size_entered", "value", - "item" - "path"), - &BuildingLayoutGraphUI::room_size_entered); - ClassDB::bind_method( - D_METHOD("floor_index_entered", "value", - "item" - "path"), - &BuildingLayoutGraphUI::floor_index_entered); - } + static void _bind_methods(); }; #endif diff --git a/src/modules/stream/ui/graph_module.cpp b/src/modules/stream/ui/graph_module.cpp index 1c9ad93..8a4659e 100644 --- a/src/modules/stream/ui/graph_module.cpp +++ b/src/modules/stream/ui/graph_module.cpp @@ -4,10 +4,306 @@ #include "editor_event.h" #include "building_layout_graph.h" -BuildingLayoutGraph::graph_module::graph_module(flecs::world ecs) +#define MIN_ROOM_SIZE 16 /* 4 * 4 tiles */ + +BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs) { + ecs.module(); ecs.component(); + ecs.component(); ecs.component(); + ecs.component(); + ecs.component(); + ecs.component(); + ecs.component(); + ecs.component(); + const String &module_name = "::BuildingLayoutGraph::graph_module"; + + ecs.system( + "FloorIndex") + .kind(0) + .write() + .each([](flecs::iter &it, size_t count, + WorldEditor::components::buildings_layout_floor_index + &index, + const WorldEditor::components::buildings_layout_floor + &f) { + flecs::entity e = it.entity(count); + List queue; + e.children([&queue](flecs::entity ec) { + if (!ec.has()) + queue.push_back(ec); + }); + while (!queue.empty()) { + flecs::entity qe = queue.front()->get(); + queue.pop_front(); + if (!qe.has()) { + qe.set( + { index.index }); + print_line(String(qe.path()) + + " index set"); + qe.children([&queue](flecs::entity ec) { + queue.push_back(ec); + }); + } + } + print_line("floor index done"); + }); + + ecs.system("RoomArea") + .kind(0) + .without() + .write() + .each([](flecs::iter &it, size_t count, + WorldEditor::components::buildings_layout_room &f) { + flecs::entity floor_e = it.entity(count); + floor_e.set< + WorldEditor::components::buildings_layout_area>( + { MIN_ROOM_SIZE }); + }); + ecs.system("ZoneArea") + .kind(0) + .without() + .write() + .each([](flecs::iter &it, size_t count, + WorldEditor::components::buildings_layout_zone &f) { + flecs::entity floor_e = it.entity(count); + floor_e.set< + WorldEditor::components::buildings_layout_area>( + { 0.0f }); + }); + ecs.system("UnitArea") + .kind(0) + .without() + .write() + .each([](flecs::iter &it, size_t count, + WorldEditor::components::buildings_layout_unit &f) { + flecs::entity floor_e = it.entity(count); + floor_e.set< + WorldEditor::components::buildings_layout_area>( + { 0.0f }); + }); + ecs.system("FloorArea") + .kind(0) + .without() + .write() + .each([](flecs::iter &it, size_t count, + WorldEditor::components::buildings_layout_floor &f) { + flecs::entity floor_e = it.entity(count); + floor_e.set< + WorldEditor::components::buildings_layout_area>( + { 0.0f }); + }); + ecs.system( + "ZoneAreaSum") + .kind(0) + .write() + .each([](flecs::iter &it, size_t count, + WorldEditor::components::buildings_layout_zone &f, + WorldEditor::components::buildings_layout_area &area, + WorldEditor::components::buildings_layout_floor_index + &index) { + flecs::entity zone_e = it.entity(count); + flecs::query< + WorldEditor::components::buildings_layout_room, + const WorldEditor::components:: + buildings_layout_area, + const WorldEditor::components:: + buildings_layout_floor_index> + q = zone_e.world() + .query_builder< + WorldEditor::components:: + buildings_layout_room, + const WorldEditor::components:: + buildings_layout_area, + const WorldEditor::components:: + buildings_layout_floor_index>() + .with(flecs::ChildOf, zone_e) + .build(); + float sum = 0.0f; + q.each([&sum, + index](flecs::entity e, + WorldEditor::components:: + buildings_layout_room &r, + const WorldEditor::components:: + buildings_layout_area &rarea, + const WorldEditor::components:: + buildings_layout_floor_index + &rindex) { + if (index.index == rindex.index) + sum += MAX(rarea.area, MIN_ROOM_SIZE); + }); + zone_e.set< + WorldEditor::components::buildings_layout_area>( + { sum }); + print_line("area: " + String(zone_e.path()) + ": " + + String::num(sum)); + }); + ecs.system( + "UnitAreaSum") + .kind(0) + .write() + .each([](flecs::iter &it, size_t count, + const WorldEditor::components::buildings_layout_unit &f, + WorldEditor::components::buildings_layout_area &area, + WorldEditor::components::buildings_layout_floor_index + &index) { + flecs::entity unit_e = it.entity(count); + flecs::query< + WorldEditor::components::buildings_layout_zone, + const WorldEditor::components:: + buildings_layout_area, + const WorldEditor::components:: + buildings_layout_floor_index> + q = unit_e.world() + .query_builder< + WorldEditor::components:: + buildings_layout_zone, + const WorldEditor::components:: + buildings_layout_area, + const WorldEditor::components:: + buildings_layout_floor_index>() + .with(flecs::ChildOf, unit_e) + .build(); + float sum = 0.0f; + q.each([&sum, + index](flecs::entity e, + WorldEditor::components:: + buildings_layout_zone &r, + const WorldEditor::components:: + buildings_layout_area &rarea, + const WorldEditor::components:: + buildings_layout_floor_index + &rindex) { + if (index.index == rindex.index) + sum += rarea.area; + }); + unit_e.set< + WorldEditor::components::buildings_layout_area>( + { sum }); + print_line("area: " + String(unit_e.path()) + ": " + + String::num(sum)); + }); + ecs.system( + "FloorAreaSum") + .kind(0) + .write() + .each([](flecs::iter &it, size_t count, + const WorldEditor::components::buildings_layout_floor + &f, + WorldEditor::components::buildings_layout_area &area, + WorldEditor::components::buildings_layout_floor_index + &index) { + flecs::entity floor_e = it.entity(count); + flecs::query + q = floor_e.world() + .query_builder< + const WorldEditor::components:: + buildings_layout_area, + const WorldEditor::components:: + buildings_layout_floor_index>() + .scope_open() + .with() + .or_() + .with() + .scope_close() + .with(flecs::ChildOf, floor_e) + .build(); + float sum = 0.0f; + q.each([&sum, + index](flecs::entity e, + const WorldEditor::components:: + buildings_layout_area &rarea, + const WorldEditor::components:: + buildings_layout_floor_index + &rindex) { + if (index.index == rindex.index) + sum += rarea.area; + }); + floor_e.set< + WorldEditor::components::buildings_layout_area>( + { sum }); + print_line("area: " + String(floor_e.path()) + ": " + + String::num(sum)); + }); + ecs.system( + "CreateFloorData") + .kind(0) + .write() + .each([](flecs::iter &it, size_t count, + const WorldEditor::components::buildings_layout_graph + &p, + const WorldEditor::components:: + buildings_layout_floor_index &idx, + const WorldEditor::components::buildings_layout_area + &area) { + flecs::entity e = it.entity(count); + flecs::query q = + e.world() + .query_builder< + WorldEditor::components:: + buildings_layout_area>() + .with() + .with(flecs::ChildOf, e) + .build(); + float max_area = area.area; + q.each([&max_area](flecs::entity ec, + WorldEditor::components:: + buildings_layout_area &r) { + if (max_area < r.area) + max_area = r.area; + }); + float grid_size = Math::ceil( + Math::ceil(Math::sqrt(max_area)) / 4.0f); + e.set( + { (int)Math::ceil(grid_size) }); + print_line("grid size: " + + itos((int)Math::ceil(grid_size))); + }); + ecs.system( + "FloorCompleteArea") + .kind(0) + .each([module_name]( + flecs::iter &it, size_t count, + WorldEditor::components::buildings_layout_floor + &f) { + flecs::entity floor_e = it.entity(count); + flecs::world w = floor_e.world(); + std::vector systems = { + "RoomArea", "ZoneArea", "UnitArea", + "FloorArea", "ZoneAreaSum", "UnitAreaSum", + "FloorAreaSum", "CreateFloorData" + }; + int i; + for (i = 0; i < (int)systems.size(); i++) { + w.system(w.lookup((module_name + + "::" + systems[i]) + .ascii() + .ptr())) + .run(); + } + print_line("floor complete"); + }); +#if 0 ecs.system("FloorArea") .kind(flecs::OnUpdate) .without() @@ -595,14 +891,29 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world ecs) e.remove(); }); +#endif ecs.observer() .event(flecs::OnSet) .with() .write() - .each([](flecs::entity e, - const WorldEditor::components::buildings_layout_area - &r) { + .each([module_name]( + flecs::entity e, + const WorldEditor::components::buildings_layout_area + &r) { + flecs::world w = e.world(); + // create indices + w.system(w.lookup((module_name + "::FloorIndex") + .ascii() + .ptr())) + .run(); + w.system(w.lookup((module_name + "::FloorCompleteArea") + .ascii() + .ptr())) + .run(); + EditorEvent::get_singleton()->event.emit( + "update_layout_view", varray()); +#if 0 /* if set for room make zone dirty */ flecs::entity parent_e = e.parent(); while (parent_e.is_valid()) { @@ -617,6 +928,7 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world ecs) } parent_e = parent_e.parent(); } +#endif #if 0 if (parent_e.has()) @@ -639,38 +951,22 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world ecs) ecs.observer() .event(flecs::OnSet) .with() - .each([](flecs::entity e, - const WorldEditor::components:: - buildings_layout_floor_index &r) { - // e.add(); - if (e.has()) { - List queue; - e.children([&queue](flecs::entity ec) { - queue.push_back(ec); - }); - while (!queue.empty()) { - flecs::entity qe = queue.front()->get(); - queue.pop_front(); - if (!qe.has()) { - qe.set( - { r.index }); - print_line(String(qe.path()) + - " index set"); - qe.children( - [&queue](flecs::entity - ec) { - queue.push_back( - ec); - }); - } - } - } + .each([module_name](flecs::entity e, + const WorldEditor::components:: + buildings_layout_floor_index &r) { + flecs::world w = e.world(); + // create indices + w.system(w.lookup((module_name + "::FloorIndex") + .ascii() + .ptr())) + .run(); + w.system(w.lookup((module_name + "::FloorCompleteArea") + .ascii() + .ptr())) + .run(); EditorEvent::get_singleton()->event.emit( "update_layout_view", varray()); - print_line("floor set"); + print_line(String(e.path()) + ": set floor"); #if 0 List queue; queue.push_back(e); @@ -690,4 +986,67 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world ecs) buildings_layout_dirty>(); #endif }); -} \ No newline at end of file +} + +/* These functions are used from GUI */ + +flecs::entity +BuildingLayoutGraph::create_graph_entity(const String &base_path, + const String &entity_type) +{ + flecs::world ecs = BaseData::get_singleton()->get(); + flecs::entity base_e = ecs.lookup(base_path.ascii().ptr()); + int count = 0; + const String &type_name = entity_type; + base_e.children([type_name, &count](flecs::entity e) { + String name(e.name()); + if (name.begins_with(type_name + "_" + itos(count))) + count++; + }); + String ename = type_name + "_" + itos(count); + flecs::entity new_e = ecs.entity(ename.ascii().ptr()).child_of(base_e); + return new_e; +} + +void BuildingLayoutGraph::create_zone(const String &base_path, int zone_type) +{ + flecs::entity new_e = create_graph_entity(base_path, "zone"); + new_e.set( + { zone_type }); + new_e.set( + { 0, 0, 0 }); +} + +void BuildingLayoutGraph::create_unit(const String &base_path) +{ + flecs::entity new_e = create_graph_entity(base_path, "unit"); + new_e.set({ 0 }); + new_e.set( + { 0, 0, 0 }); +} + +void BuildingLayoutGraph::create_floor(const String &base_path) +{ + flecs::entity new_e = create_graph_entity(base_path, "floor"); + new_e.set({ 0 }); + new_e.set( + { 0, 0, 0 }); +} + +void BuildingLayoutGraph::create_room(const String &base_path, int id) +{ + Array room; + BuildingLayoutGraph::get_singleton()->get_room_data(id, room); + assert(!room.empty()); + String type_name = room[1]; + Dictionary room_options = room[2]; + bool window = room_options.get("window", false); + type_name = type_name.replace(" ", "_").to_lower(); + flecs::entity new_e = create_graph_entity(base_path, type_name); + new_e.set( + { id, window }); + new_e.set( + { 0, 0, 0 }); + new_e.set({ 0.0f }); + assert(new_e.has()); +} diff --git a/src/modules/stream/ui/ui_field_builder.cpp b/src/modules/stream/ui/ui_field_builder.cpp index f0abc7e..dc63b9b 100644 --- a/src/modules/stream/ui/ui_field_builder.cpp +++ b/src/modules/stream/ui/ui_field_builder.cpp @@ -42,7 +42,7 @@ void ui_field::ui_field_builder(Node *owner, Node *parent, const String format, for (i = 0; i < format.length(); i++) { const char *fmt = format.ascii().ptr(); int c = fmt[i]; - printf("character: %c : argp: %d\n", (char)c, argp); + // printf("character: %c : argp: %d\n", (char)c, argp); switch (c) { case '{': stack.push_front(parent); @@ -57,7 +57,7 @@ void ui_field::ui_field_builder(Node *owner, Node *parent, const String format, String name = args[argp++]; assert(name.length() > 0); last_created->set_name(name); - printf("set name: %s\n", name.ascii().ptr()); + // printf("set name: %s\n", name.ascii().ptr()); } break; case '$': { if (saved_nodes)