Continued work on layout graph

This commit is contained in:
2024-10-27 17:50:48 +03:00
parent 6eb584f73c
commit ae3d86d391
5 changed files with 194 additions and 71 deletions

View File

@@ -0,0 +1,101 @@
#include <cassert>
#include "base_data.h"
#include "world_editor.h"
#include "building_layout_graph.h"
BuildingLayoutGraph *BuildingLayoutGraph::singleton = nullptr;
BuildingLayoutGraph::BuildingLayoutGraph()
{
Error err = config.load("res://astream/building_layout.conf");
assert(err == OK);
public_rooms = config.get_value("rooms", "public", Array());
private_rooms = config.get_value("rooms", "private", Array());
}
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);
int index = -1;
for (i = 0; i < rooms.size(); i++) {
Array room_data = rooms[i];
int room_id = room_data[0];
if (room_id == id) {
index = i;
break;
}
}
Array room = rooms[index];
result = room;
}
const Array &BuildingLayoutGraph::get_private_rooms() const
{
return private_rooms;
}
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<WorldEditor::components::buildings_layout_zone>(
{ zone_type });
}
void BuildingLayoutGraph::create_unit(const String &base_path)
{
flecs::entity new_e = create_graph_entity(base_path, "unit");
new_e.set<WorldEditor::components::buildings_layout_unit>({ 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<WorldEditor::components::buildings_layout_room>(
{ id, window });
}
void BuildingLayoutGraph::destroy_graph_entity(const String &path)
{
flecs::world ecs = BaseData::get_singleton()->get();
flecs::entity e = ecs.lookup(path.ascii().ptr());
if (e.is_valid())
e.destruct();
}

View File

@@ -0,0 +1,35 @@
#include <core/os/memory.h>
#include <core/io/config_file.h>
class BuildingLayoutGraph {
static BuildingLayoutGraph *singleton;
BuildingLayoutGraph();
ConfigFile config;
Array public_rooms;
Array private_rooms;
flecs::entity create_graph_entity(const String &base_path,
const String &entity_type);
public:
virtual ~BuildingLayoutGraph();
static BuildingLayoutGraph *get_singleton()
{
if (!singleton)
singleton = memnew(BuildingLayoutGraph());
return singleton;
}
static void cleanup()
{
memdelete(singleton);
singleton = nullptr;
}
void get_room_data(int id, Array &result);
const Array &get_private_rooms() const;
const Array &get_public_rooms() const;
void create_zone(const String &base_path, int zone_type);
void create_unit(const String &base_path);
void create_room(const String &base_path, int id);
void destroy_graph_entity(const String &path);
};

View File

@@ -15,6 +15,7 @@
#include "base_data.h"
#include "editor_event.h"
#include "world_editor.h"
#include "building_layout_graph.h"
class BuildingLayoutGraphUI : public Object {
GDCLASS(BuildingLayoutGraphUI, Object)
@@ -23,9 +24,6 @@ class BuildingLayoutGraphUI : public Object {
Control *canvas_item;
Button *button;
String current_layout;
ConfigFile config;
Array public_rooms;
Array private_rooms;
public:
BuildingLayoutGraphUI(MainTabs *gui, WindowDialog *dlg)
@@ -36,89 +34,41 @@ public:
, button(nullptr)
{
dlg->connect("tree_entered", this, "tree_entered");
Error err = config.load("res://astream/building_layout.conf");
assert(err == OK);
public_rooms = config.get_value("rooms", "public", Array());
private_rooms = config.get_value("rooms", "private", Array());
}
void menu_pressed(int id, Control *button, const String &path)
{
assert(button);
print_line(itos(id));
print_line(path);
flecs::world ecs = BaseData::get_singleton()->get();
flecs::entity base_e = ecs.lookup(path.ascii().ptr());
switch (id) {
case 100:
case 101:
case 102:
case 103: {
int count = 0;
String type_name;
switch (id) {
case 100:
type_name = "unit";
BuildingLayoutGraph::get_singleton()
->create_unit(path);
break;
case 101:
BuildingLayoutGraph::get_singleton()
->create_zone(path, 0);
break;
case 102:
type_name = "zone";
BuildingLayoutGraph::get_singleton()
->create_zone(path, 1);
break;
}
if (type_name.length() == 0)
return;
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);
if (id == 100) {
new_e.set<WorldEditor::components::
buildings_layout_unit>({ 0 });
} else if (id == 101) {
new_e.set<WorldEditor::components::
buildings_layout_zone>({ 0 });
} else if (id == 102) {
new_e.set<WorldEditor::components::
buildings_layout_zone>({ 1 });
}
} break;
default: {
int i;
int count = 0;
Array rooms;
rooms.append_array(private_rooms);
rooms.append_array(public_rooms);
int index = -1;
for (i = 0; i < rooms.size(); i++) {
Array room_data = rooms[i];
int room_id = room_data[0];
if (room_id == id) {
index = i;
break;
}
}
Array room = rooms[index];
String type_name = room[1];
Dictionary room_options = room[2];
bool window = room_options.get("window", false);
type_name = type_name.replace(" ", "_").to_lower();
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);
new_e.set<WorldEditor::components::buildings_layout_room>(
{ id, window });
} break;
case 1000:
BuildingLayoutGraph::get_singleton()
->destroy_graph_entity(path);
break;
default:
BuildingLayoutGraph::get_singleton()->create_room(path,
id);
break;
}
update_graph();
}
@@ -263,6 +213,10 @@ public:
this, &BuildingLayoutGraphUI::handle_event);
}
}
void room_size_entered(float value, Control *item, const String &path)
{
print_line(String::num(value));
}
Vector<Vector2> buttons;
void update_buttons(ColorRect *canvas)
{
@@ -369,6 +323,9 @@ public:
button->set_text(
button->get_text() +
"Private Zone\n");
const Array &private_rooms =
BuildingLayoutGraph::get_singleton()
->get_private_rooms();
for (i = 0;
i < private_rooms.size();
i++) {
@@ -387,6 +344,9 @@ public:
button->set_text(
button->get_text() +
"Public Zone\n");
const Array &public_rooms =
BuildingLayoutGraph::get_singleton()
->get_public_rooms();
for (i = 0;
i < public_rooms.size();
i++) {
@@ -424,7 +384,21 @@ public:
"Room\n" +
String(e.name()));
PopupMenu *mp = memnew(PopupMenu);
mp->add_child(memnew(LineEdit));
std::vector<Variant> args = {
"Room Area: ", "area_value"
};
HashMap<String, Object *> save_data;
ui_field::ui_field_builder(canvas, mp,
"p{h{lx#$}}",
args.data(),
args.size(),
&save_data);
assert(save_data.has("area_value"));
save_data["area_value"]->connect(
"value_changed", this,
"room_size_entered",
varray(save_data["area_value"],
String(e.path())));
button->get_popup()->add_child(mp);
mp->set_name("subedit");
button->get_popup()->add_submenu_item(
@@ -437,6 +411,8 @@ public:
buildings_layout_graph_node>()
->size));
#endif
button->get_popup()->add_separator();
button->get_popup()->add_item("Delete", 1000);
button->get_popup()->connect(
"id_pressed", this, "menu_pressed",
varray(button, String(e.path())));
@@ -601,6 +577,10 @@ public:
&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);
}
};

View File

@@ -21,7 +21,8 @@
Vector<Object *> ui_field::c_handlers;
void ui_field::ui_field_builder(Node *owner, Node *parent, const String format,
const Variant *args, int args_size)
const Variant *args, int args_size,
HashMap<String, Object *> *saved_nodes)
{
int argp = 0, i;
List<Node *> stack;
@@ -58,6 +59,11 @@ void ui_field::ui_field_builder(Node *owner, Node *parent, const String format,
last_created->set_name(name);
printf("set name: %s\n", name.ascii().ptr());
} break;
case '$': {
if (saved_nodes)
(*saved_nodes)[last_created->get_name()] =
last_created;
} break;
case '.':
assert(argp < args_size);
last_created->set_custom_minimum_size(args[argp++]);

View File

@@ -5,9 +5,10 @@
class ui_field {
public:
static Vector<Object *> c_handlers;
static void ui_field_builder(Node *owner, Node *parent,
const String format, const Variant *args,
int args_size);
static void
ui_field_builder(Node *owner, Node *parent, const String format,
const Variant *args, int args_size,
HashMap<String, Object *> *saved_nodes = nullptr);
};
#endif