Save/Load works for building layout graphs
This commit is contained in:
@@ -11,6 +11,7 @@ BuildingLayoutGraph::BuildingLayoutGraph()
|
||||
assert(err == OK);
|
||||
public_rooms = config.get_value("rooms", "public", Array());
|
||||
private_rooms = config.get_value("rooms", "private", Array());
|
||||
load_layouts();
|
||||
}
|
||||
|
||||
BuildingLayoutGraph::~BuildingLayoutGraph()
|
||||
@@ -20,7 +21,6 @@ BuildingLayoutGraph::~BuildingLayoutGraph()
|
||||
void BuildingLayoutGraph::get_room_data(int id, Array &result)
|
||||
{
|
||||
int i;
|
||||
int count = 0;
|
||||
Array rooms;
|
||||
rooms.append_array(private_rooms);
|
||||
rooms.append_array(public_rooms);
|
||||
@@ -261,7 +261,8 @@ void BuildingLayoutGraph::get_menu_entries(flecs::entity e,
|
||||
{ 102, "Create public zone" } };
|
||||
list->clear();
|
||||
if (e.has<WorldEditor::components::buildings_layout_floor>()) {
|
||||
for (i = 0; i < sizeof(floor_menu) / sizeof(floor_menu[0]);
|
||||
for (i = 0;
|
||||
i < (int)(sizeof(floor_menu) / sizeof(floor_menu[0]));
|
||||
i++) {
|
||||
Pair<int, String> item(
|
||||
{ floor_menu[i].id, floor_menu[i].item });
|
||||
@@ -271,7 +272,8 @@ void BuildingLayoutGraph::get_menu_entries(flecs::entity e,
|
||||
if (e.has<WorldEditor::components::buildings_layout_unit>()) {
|
||||
if (!list->empty())
|
||||
list->push_back(Pair<int, String>({ -1, "" }));
|
||||
for (i = 0; i < sizeof(unit_menu) / sizeof(unit_menu[0]); i++) {
|
||||
for (i = 0; i < (int)(sizeof(unit_menu) / sizeof(unit_menu[0]));
|
||||
i++) {
|
||||
Pair<int, String> item(
|
||||
{ unit_menu[i].id, unit_menu[i].item });
|
||||
list->push_back(item);
|
||||
@@ -332,21 +334,26 @@ void BuildingLayoutGraph::save_layouts()
|
||||
List<flecs::entity> queue;
|
||||
flecs::entity base = get_layout_base();
|
||||
queue.push_back(base);
|
||||
int index = 0;
|
||||
HashMap<String, int> indices;
|
||||
while (!queue.empty()) {
|
||||
flecs::entity e = queue.front()->get();
|
||||
queue.pop_front();
|
||||
e.children([&queue](flecs::entity ce) { queue.push_back(ce); });
|
||||
if (e.has<WorldEditor::components::buildings_layout_base>())
|
||||
continue;
|
||||
print_line("store: " + String(String(e.path())));
|
||||
indices[String(e.path())] = index++;
|
||||
Array entries;
|
||||
{
|
||||
int index = 0;
|
||||
while (!queue.empty()) {
|
||||
flecs::entity e = queue.front()->get();
|
||||
queue.pop_front();
|
||||
e.children([&queue](flecs::entity ce) {
|
||||
queue.push_back(ce);
|
||||
});
|
||||
if (e.has<WorldEditor::components::
|
||||
buildings_layout_base>())
|
||||
continue;
|
||||
print_line("store: " + String(String(e.path())));
|
||||
indices[String(e.path())] = index++;
|
||||
}
|
||||
entries.resize(index);
|
||||
}
|
||||
queue.clear();
|
||||
queue.push_back(base);
|
||||
Array entries;
|
||||
entries.resize(index);
|
||||
while (!queue.empty()) {
|
||||
flecs::entity e = queue.front()->get();
|
||||
queue.pop_front();
|
||||
@@ -403,3 +410,77 @@ void BuildingLayoutGraph::save_layouts()
|
||||
config_layouts.set_value("layouts", "entries", entries);
|
||||
config_layouts.save("res://astream/building_layout_data.conf");
|
||||
}
|
||||
|
||||
void BuildingLayoutGraph::load_layouts()
|
||||
{
|
||||
List<int> queue;
|
||||
flecs::world ecs = BaseData::get_singleton()->get();
|
||||
flecs::entity base = get_layout_base();
|
||||
ConfigFile config_layouts;
|
||||
config_layouts.load("res://astream/building_layout_data.conf");
|
||||
Array entities =
|
||||
config_layouts.get_value("layouts", "entries", Array());
|
||||
int i, j;
|
||||
HashMap<int, flecs::entity> entity_index;
|
||||
queue.clear();
|
||||
for (i = 0; i < entities.size(); i++) {
|
||||
Dictionary entry = entities[i];
|
||||
String name = entry["name"];
|
||||
String type = entry["type"];
|
||||
if (type == "layout")
|
||||
queue.push_back(i);
|
||||
const Array &children = entry["children"];
|
||||
for (j = 0; j < children.size(); j++) {
|
||||
Dictionary child = entities[children[j]];
|
||||
child["parent"] = i;
|
||||
entities[children[j]] = child;
|
||||
}
|
||||
}
|
||||
while (!queue.empty()) {
|
||||
int index = queue.front()->get();
|
||||
queue.pop_front();
|
||||
const Dictionary &entry = entities[index];
|
||||
String name = entry["name"];
|
||||
String type = entry["type"];
|
||||
print_line("name: " + name + " type: " + type);
|
||||
int parent = entry.get("parent", -1);
|
||||
flecs::entity parent_e = base;
|
||||
if (parent >= 0)
|
||||
parent_e = entity_index[parent];
|
||||
flecs::entity e =
|
||||
ecs.entity(name.ascii().ptr()).child_of(parent_e);
|
||||
if (type == "layout") {
|
||||
e.add<WorldEditor::components::buildings_layout_graph>();
|
||||
e.set<WorldEditor::components::buildings_layout_floor>(
|
||||
{ 0 });
|
||||
} else if (type == "floor") {
|
||||
e.set<WorldEditor::components::buildings_layout_floor>(
|
||||
{ 0 });
|
||||
} else if (type == "zone") {
|
||||
int zone_type = entry["zone_type"];
|
||||
e.set<WorldEditor::components::buildings_layout_zone>(
|
||||
{ zone_type });
|
||||
} else if (type == "unit") {
|
||||
e.set<WorldEditor::components::buildings_layout_unit>(
|
||||
{ 0 });
|
||||
} else if (type == "room") {
|
||||
int room_type = entry["room_type"];
|
||||
bool window = entry["window"];
|
||||
e.set<WorldEditor::components::buildings_layout_room>(
|
||||
{ room_type, window });
|
||||
} else
|
||||
assert(false);
|
||||
entity_index[index] = e;
|
||||
const Array &children = entry["children"];
|
||||
for (i = 0; i < children.size(); i++)
|
||||
queue.push_back(children[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int BuildingLayoutGraph::get_layout_count() const
|
||||
{
|
||||
flecs::entity e = get_layout_base();
|
||||
int count = 0;
|
||||
e.children([&count](flecs::entity e) { count++; });
|
||||
return count;
|
||||
}
|
||||
|
||||
@@ -44,4 +44,6 @@ public:
|
||||
void get_menu_entries(flecs::entity e,
|
||||
List<Pair<int, String> > *list) const;
|
||||
void save_layouts();
|
||||
void load_layouts();
|
||||
int get_layout_count() const;
|
||||
};
|
||||
@@ -167,8 +167,11 @@ public:
|
||||
"t**{v#{s**{p#!**}}v#{}v#{}}",
|
||||
args_data2.data(), args_data2.size());
|
||||
dlg->update();
|
||||
update_graph();
|
||||
update_layout_item_list();
|
||||
if (BuildingLayoutGraph::get_singleton()
|
||||
->get_layout_count() > 0)
|
||||
select_layout(0);
|
||||
update_graph();
|
||||
EditorEvent::get_singleton()->event.add_listener(
|
||||
this, &BuildingLayoutGraphUI::handle_event);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user