Separated dlayout data code
This commit is contained in:
@@ -99,3 +99,100 @@ void BuildingLayoutGraph::destroy_graph_entity(const String &path)
|
|||||||
if (e.is_valid())
|
if (e.is_valid())
|
||||||
e.destruct();
|
e.destruct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flecs::entity BuildingLayoutGraph::get_layout_base() const
|
||||||
|
{
|
||||||
|
const String &layout_base_name = "buildings_layout_graph";
|
||||||
|
flecs::world ecs = BaseData::get_singleton()->get();
|
||||||
|
flecs::entity layout_base_e =
|
||||||
|
ecs.lookup(layout_base_name.ascii().ptr());
|
||||||
|
if (layout_base_e.is_valid())
|
||||||
|
return layout_base_e;
|
||||||
|
else {
|
||||||
|
layout_base_e = ecs.entity(layout_base_name.ascii().ptr());
|
||||||
|
assert(layout_base_e.is_valid());
|
||||||
|
return layout_base_e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BuildingLayoutGraph::get_layout_list(List<String> *keys) const
|
||||||
|
{
|
||||||
|
flecs::entity base = get_layout_base();
|
||||||
|
base.children([keys](flecs::entity e) {
|
||||||
|
if (e.has<WorldEditor::components::buildings_layout_graph>())
|
||||||
|
keys->push_back(String(e.name()));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void BuildingLayoutGraph::create_new_layout(const String &layout_name)
|
||||||
|
{
|
||||||
|
flecs::world ecs = BaseData::get_singleton()->get();
|
||||||
|
flecs::entity base = get_layout_base();
|
||||||
|
flecs::entity e = base.lookup(layout_name.ascii().ptr());
|
||||||
|
if (e.is_valid())
|
||||||
|
return;
|
||||||
|
e = ecs.entity(layout_name.ascii().ptr()).child_of(base);
|
||||||
|
if (e.is_valid()) {
|
||||||
|
e.add<WorldEditor::components::buildings_layout_graph>();
|
||||||
|
e.set<WorldEditor::components::buildings_layout_floor>({ 0 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flecs::entity BuildingLayoutGraph::get_layout(const String &layout_name) const
|
||||||
|
{
|
||||||
|
assert(layout_name.length() > 0);
|
||||||
|
flecs::entity base = get_layout_base();
|
||||||
|
flecs::entity layout_e = base.lookup(layout_name.ascii().ptr());
|
||||||
|
assert(layout_e.is_valid());
|
||||||
|
assert(layout_e.has<WorldEditor::components::buildings_layout_graph>());
|
||||||
|
return layout_e;
|
||||||
|
return flecs::entity();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BuildingLayoutGraph::get_layout_entity_children(
|
||||||
|
flecs::entity layout_e, List<flecs::entity> *keys) const
|
||||||
|
{
|
||||||
|
layout_e.children([keys](flecs::entity e) { keys->push_back(e); });
|
||||||
|
}
|
||||||
|
|
||||||
|
void BuildingLayoutGraph::recalculate_size(const String &layout_name)
|
||||||
|
{
|
||||||
|
assert(layout_name.length() > 0);
|
||||||
|
flecs::entity layout_e = get_layout(layout_name);
|
||||||
|
assert(layout_e.is_valid());
|
||||||
|
List<flecs::entity> queue;
|
||||||
|
layout_e.set<WorldEditor::components::buildings_layout_graph_node>(
|
||||||
|
{ 0, 0, 0 });
|
||||||
|
queue.push_back(layout_e);
|
||||||
|
while (!queue.empty()) {
|
||||||
|
flecs::entity e = queue.front()->get();
|
||||||
|
queue.pop_front();
|
||||||
|
int size = 1;
|
||||||
|
bool readd = false;
|
||||||
|
e.children([&readd, &size, &queue](flecs::entity fe) {
|
||||||
|
if (!fe.has<WorldEditor::components::
|
||||||
|
buildings_layout_graph_node>()) {
|
||||||
|
fe.set<WorldEditor::components::
|
||||||
|
buildings_layout_graph_node>(
|
||||||
|
{ 0, 0, 0 });
|
||||||
|
readd = true;
|
||||||
|
}
|
||||||
|
size += fe.get<WorldEditor::components::
|
||||||
|
buildings_layout_graph_node>()
|
||||||
|
->size;
|
||||||
|
queue.push_back(fe);
|
||||||
|
});
|
||||||
|
if (readd)
|
||||||
|
queue.push_back(e);
|
||||||
|
if (e.get<WorldEditor::components::buildings_layout_graph_node>()
|
||||||
|
->size != size) {
|
||||||
|
e.get_mut<WorldEditor::components::
|
||||||
|
buildings_layout_graph_node>()
|
||||||
|
->size = size;
|
||||||
|
if (e.parent()
|
||||||
|
.has<WorldEditor::components::
|
||||||
|
buildings_layout_graph_node>())
|
||||||
|
queue.push_back(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -32,4 +32,11 @@ public:
|
|||||||
void create_unit(const String &base_path);
|
void create_unit(const String &base_path);
|
||||||
void create_room(const String &base_path, int id);
|
void create_room(const String &base_path, int id);
|
||||||
void destroy_graph_entity(const String &path);
|
void destroy_graph_entity(const String &path);
|
||||||
|
flecs::entity get_layout_base() const;
|
||||||
|
void get_layout_list(List<String> *keys) const;
|
||||||
|
void create_new_layout(const String &layout_name);
|
||||||
|
flecs::entity get_layout(const String &layout_name) const;
|
||||||
|
void get_layout_entity_children(flecs::entity layout_e,
|
||||||
|
List<flecs::entity> *keys) const;
|
||||||
|
void recalculate_size(const String &layout_name);
|
||||||
};
|
};
|
||||||
@@ -72,36 +72,12 @@ public:
|
|||||||
}
|
}
|
||||||
update_graph();
|
update_graph();
|
||||||
}
|
}
|
||||||
flecs::entity get_layout_base()
|
|
||||||
{
|
|
||||||
const String &layout_base_name = "buildings_layout_graph";
|
|
||||||
flecs::world ecs = BaseData::get_singleton()->get();
|
|
||||||
flecs::entity layout_base_e =
|
|
||||||
ecs.lookup(layout_base_name.ascii().ptr());
|
|
||||||
if (layout_base_e.is_valid())
|
|
||||||
return layout_base_e;
|
|
||||||
else {
|
|
||||||
layout_base_e =
|
|
||||||
ecs.entity(layout_base_name.ascii().ptr());
|
|
||||||
assert(layout_base_e.is_valid());
|
|
||||||
return layout_base_e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void get_layout_list(List<String> *keys)
|
|
||||||
{
|
|
||||||
flecs::entity base = get_layout_base();
|
|
||||||
base.children([keys](flecs::entity e) {
|
|
||||||
if (e.has<WorldEditor::components::
|
|
||||||
buildings_layout_graph>())
|
|
||||||
keys->push_back(String(e.name()));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
void update_layout_item_list()
|
void update_layout_item_list()
|
||||||
{
|
{
|
||||||
ItemList *item_list =
|
ItemList *item_list =
|
||||||
gui->get_as_node<ItemList>("%building_layout_list");
|
gui->get_as_node<ItemList>("%building_layout_list");
|
||||||
List<String> items;
|
List<String> items;
|
||||||
get_layout_list(&items);
|
BuildingLayoutGraph::get_singleton()->get_layout_list(&items);
|
||||||
List<String>::Element *e = items.front();
|
List<String>::Element *e = items.front();
|
||||||
item_list->clear();
|
item_list->clear();
|
||||||
while (e) {
|
while (e) {
|
||||||
@@ -109,35 +85,11 @@ public:
|
|||||||
e = e->next();
|
e = e->next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void create_new_layout(const String &layout_name)
|
|
||||||
{
|
|
||||||
flecs::world ecs = BaseData::get_singleton()->get();
|
|
||||||
flecs::entity base = get_layout_base();
|
|
||||||
flecs::entity e = base.lookup(layout_name.ascii().ptr());
|
|
||||||
if (e.is_valid())
|
|
||||||
return;
|
|
||||||
e = ecs.entity(layout_name.ascii().ptr()).child_of(base);
|
|
||||||
if (e.is_valid()) {
|
|
||||||
e.add<WorldEditor::components::buildings_layout_graph>();
|
|
||||||
e.set<WorldEditor::components::buildings_layout_floor>(
|
|
||||||
{ 0 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
flecs::entity get_current_layout()
|
flecs::entity get_current_layout()
|
||||||
{
|
{
|
||||||
assert(current_layout.length() > 0);
|
assert(current_layout.length() > 0);
|
||||||
flecs::entity base = get_layout_base();
|
return BuildingLayoutGraph::get_singleton()->get_layout(
|
||||||
flecs::entity layout_e =
|
current_layout);
|
||||||
base.lookup(current_layout.ascii().ptr());
|
|
||||||
assert(layout_e.has<
|
|
||||||
WorldEditor::components::buildings_layout_graph>());
|
|
||||||
return layout_e;
|
|
||||||
}
|
|
||||||
void get_layout_entity_children(flecs::entity layout_e,
|
|
||||||
List<flecs::entity> *keys)
|
|
||||||
{
|
|
||||||
layout_e.children(
|
|
||||||
[keys](flecs::entity e) { keys->push_back(e); });
|
|
||||||
}
|
}
|
||||||
void select_layout(int id)
|
void select_layout(int id)
|
||||||
{
|
{
|
||||||
@@ -161,7 +113,8 @@ public:
|
|||||||
item_list->disconnect("item_selected", this,
|
item_list->disconnect("item_selected", this,
|
||||||
"select_layout");
|
"select_layout");
|
||||||
String new_name = line_edit->get_text().strip_edges();
|
String new_name = line_edit->get_text().strip_edges();
|
||||||
create_new_layout(new_name);
|
BuildingLayoutGraph::get_singleton()->create_new_layout(
|
||||||
|
new_name);
|
||||||
update_layout_item_list();
|
update_layout_item_list();
|
||||||
int selected = -1;
|
int selected = -1;
|
||||||
for (i = 0; i < item_list->get_item_count(); i++)
|
for (i = 0; i < item_list->get_item_count(); i++)
|
||||||
@@ -221,49 +174,12 @@ public:
|
|||||||
void update_buttons(ColorRect *canvas)
|
void update_buttons(ColorRect *canvas)
|
||||||
{
|
{
|
||||||
assert(current_layout.length() > 0);
|
assert(current_layout.length() > 0);
|
||||||
|
BuildingLayoutGraph::get_singleton()->recalculate_size(
|
||||||
|
current_layout);
|
||||||
flecs::entity layout_e = get_current_layout();
|
flecs::entity layout_e = get_current_layout();
|
||||||
List<flecs::entity> queue;
|
List<flecs::entity> queue;
|
||||||
bool layout_empty = true;
|
bool layout_empty = true;
|
||||||
queue.push_back(layout_e);
|
queue.push_back(layout_e);
|
||||||
layout_e.set<
|
|
||||||
WorldEditor::components::buildings_layout_graph_node>(
|
|
||||||
{ 0, 0, 0 });
|
|
||||||
queue.clear();
|
|
||||||
queue.push_back(layout_e);
|
|
||||||
while (!queue.empty()) {
|
|
||||||
flecs::entity e = queue.front()->get();
|
|
||||||
queue.pop_front();
|
|
||||||
int size = 1;
|
|
||||||
bool readd = false;
|
|
||||||
e.children([&readd, &size, &queue](flecs::entity fe) {
|
|
||||||
if (!fe.has<WorldEditor::components::
|
|
||||||
buildings_layout_graph_node>()) {
|
|
||||||
fe.set<WorldEditor::components::
|
|
||||||
buildings_layout_graph_node>(
|
|
||||||
{ 0, 0, 0 });
|
|
||||||
readd = true;
|
|
||||||
}
|
|
||||||
size += fe.get<WorldEditor::components::
|
|
||||||
buildings_layout_graph_node>()
|
|
||||||
->size;
|
|
||||||
queue.push_back(fe);
|
|
||||||
});
|
|
||||||
if (readd)
|
|
||||||
queue.push_back(e);
|
|
||||||
if (e.get<WorldEditor::components::
|
|
||||||
buildings_layout_graph_node>()
|
|
||||||
->size != size) {
|
|
||||||
e.get_mut<WorldEditor::components::
|
|
||||||
buildings_layout_graph_node>()
|
|
||||||
->size = size;
|
|
||||||
if (e.parent()
|
|
||||||
.has<WorldEditor::components::
|
|
||||||
buildings_layout_graph_node>())
|
|
||||||
queue.push_back(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
queue.clear();
|
|
||||||
queue.push_back(layout_e);
|
|
||||||
while (!queue.empty()) {
|
while (!queue.empty()) {
|
||||||
buttons.clear();
|
buttons.clear();
|
||||||
layout_empty = false;
|
layout_empty = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user