Grid 2D display
This commit is contained in:
@@ -38,7 +38,6 @@ void BuildingsEditor::activate()
|
|||||||
Error result = config.load("res://config/stream.conf");
|
Error result = config.load("res://config/stream.conf");
|
||||||
ERR_FAIL_COND_MSG(result != OK, "Failed to load config");
|
ERR_FAIL_COND_MSG(result != OK, "Failed to load config");
|
||||||
print_line("BuildingsEditor ACTIVE");
|
print_line("BuildingsEditor ACTIVE");
|
||||||
int i;
|
|
||||||
/* TODO: make separate function for this */
|
/* TODO: make separate function for this */
|
||||||
EditorEvent::get_singleton()->event.add_listener(
|
EditorEvent::get_singleton()->event.add_listener(
|
||||||
this, &BuildingsEditor::event_handler);
|
this, &BuildingsEditor::event_handler);
|
||||||
@@ -368,7 +367,6 @@ void BuildingsEditor::remove_buildings_by_prefix(const String &prefix)
|
|||||||
void BuildingsEditor::select_building(const Transform &xform, const String &key,
|
void BuildingsEditor::select_building(const Transform &xform, const String &key,
|
||||||
const String &mid)
|
const String &mid)
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
assert(scene());
|
assert(scene());
|
||||||
selected_building_xform = xform;
|
selected_building_xform = xform;
|
||||||
selected_building = key;
|
selected_building = key;
|
||||||
|
|||||||
@@ -125,7 +125,16 @@ flecs::entity BuildingLayoutGraph::get_layout(const String &layout_name) const
|
|||||||
assert(layout_e.is_valid());
|
assert(layout_e.is_valid());
|
||||||
assert(layout_e.has<WorldEditor::components::buildings_layout_graph>());
|
assert(layout_e.has<WorldEditor::components::buildings_layout_graph>());
|
||||||
return layout_e;
|
return layout_e;
|
||||||
return flecs::entity();
|
}
|
||||||
|
|
||||||
|
/* this one can fail if no grid exists yet */
|
||||||
|
flecs::entity
|
||||||
|
BuildingLayoutGraph::get_grid_layout(const String &layout_name) const
|
||||||
|
{
|
||||||
|
assert(layout_name.length() > 0);
|
||||||
|
flecs::entity base = get_layout_grid_base();
|
||||||
|
flecs::entity layout_e = base.lookup(layout_name.ascii().ptr());
|
||||||
|
return layout_e;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildingLayoutGraph::get_layout_entity_children(
|
void BuildingLayoutGraph::get_layout_entity_children(
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ public:
|
|||||||
void get_layout_list(List<String> *keys) const;
|
void get_layout_list(List<String> *keys) const;
|
||||||
void create_new_layout(const String &layout_name);
|
void create_new_layout(const String &layout_name);
|
||||||
flecs::entity get_layout(const String &layout_name) const;
|
flecs::entity get_layout(const String &layout_name) const;
|
||||||
|
flecs::entity get_grid_layout(const String &layout_name) const;
|
||||||
void get_layout_entity_children(flecs::entity layout_e,
|
void get_layout_entity_children(flecs::entity layout_e,
|
||||||
List<flecs::entity> *keys) const;
|
List<flecs::entity> *keys) const;
|
||||||
void recalculate_size(const String &layout_name);
|
void recalculate_size(const String &layout_name);
|
||||||
@@ -45,6 +46,7 @@ public:
|
|||||||
int get_layout_count() const;
|
int get_layout_count() const;
|
||||||
int get_children_count(flecs::entity base_e) const;
|
int get_children_count(flecs::entity base_e) const;
|
||||||
struct graph_module {
|
struct graph_module {
|
||||||
|
flecs::entity get_layout_grid_base();
|
||||||
graph_module(flecs::world &ecs);
|
graph_module(flecs::world &ecs);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -684,6 +684,50 @@ void BuildingLayoutGraphUI::handle_event(const String &event,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void BuildingLayoutGraphUI::tree_entered()
|
||||||
|
{
|
||||||
|
if (!Engine::get_singleton()->is_editor_hint()) {
|
||||||
|
/* move this elsewhere */
|
||||||
|
std::vector<Variant> args_data2 = {
|
||||||
|
/* clang-format off */
|
||||||
|
MARGIN_RIGHT, 1.0f, MARGIN_BOTTOM, 1.0f,
|
||||||
|
"Layout Graph",
|
||||||
|
MARGIN_RIGHT, 1.0f, MARGIN_BOTTOM, 1.0f,
|
||||||
|
"buildings_layout_graph",
|
||||||
|
MARGIN_RIGHT, 1.0f, MARGIN_BOTTOM, 1.0f,
|
||||||
|
"Interior", Control::SIZE_EXPAND_FILL, Color(1, 0, 0, 1),
|
||||||
|
MARGIN_RIGHT, 1.0f, MARGIN_BOTTOM, 1.0f,
|
||||||
|
"color_view_grid",
|
||||||
|
"Exterior",
|
||||||
|
/* clang-format on */
|
||||||
|
};
|
||||||
|
HashMap<String, Object *> save_obj;
|
||||||
|
ui_field::ui_field_builder(
|
||||||
|
dlg, dlg,
|
||||||
|
// "p+={t+={v#{s#!+={}}v#{}v#{}}}",
|
||||||
|
"t**{v#{s**{p#!**}}v#{p={c**#$}}v#{}}",
|
||||||
|
args_data2.data(), args_data2.size(), &save_obj);
|
||||||
|
assert(save_obj.has("color_view_grid"));
|
||||||
|
ColorRect *grid_view =
|
||||||
|
Object::cast_to<ColorRect>(save_obj["color_view_grid"]);
|
||||||
|
grid_view->connect("draw", this, "draw_2d_grid_view",
|
||||||
|
varray(grid_view));
|
||||||
|
dlg->update();
|
||||||
|
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);
|
||||||
|
ItemList *item_list =
|
||||||
|
gui->get_as_node<ItemList>("%building_layout_list");
|
||||||
|
if (!item_list->is_connected("item_selected", this,
|
||||||
|
"select_layout"))
|
||||||
|
item_list->connect("item_selected", this,
|
||||||
|
"select_layout");
|
||||||
|
}
|
||||||
|
}
|
||||||
void BuildingLayoutGraphUI::setup_layout_tab(Control *tab, const String &header)
|
void BuildingLayoutGraphUI::setup_layout_tab(Control *tab, const String &header)
|
||||||
{
|
{
|
||||||
assert(gui);
|
assert(gui);
|
||||||
@@ -737,6 +781,83 @@ void BuildingLayoutGraphUI::draw_building_gen_display(Control *draw)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void BuildingLayoutGraphUI::draw_2d_grid_view(Control *draw)
|
||||||
|
{
|
||||||
|
flecs::entity e = get_current_layout();
|
||||||
|
Vector2 size = draw->get_size();
|
||||||
|
int grid_size =
|
||||||
|
e.get<WorldEditor::components::buildings_layout_grid_size>()
|
||||||
|
->grid_size;
|
||||||
|
float disp_size = MIN(size.x, size.y);
|
||||||
|
float multiplier = disp_size / (float)grid_size;
|
||||||
|
print_line(String::num(disp_size) + " " + itos(grid_size));
|
||||||
|
flecs::entity grid_e = get_current_grid_layout();
|
||||||
|
if (!grid_e.is_valid())
|
||||||
|
return;
|
||||||
|
print_line(String(grid_e.path()));
|
||||||
|
flecs::query<const WorldEditor::components::buildings_layout_grid_floor>
|
||||||
|
floor_q = grid_e.world()
|
||||||
|
.query_builder<
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_grid_floor>()
|
||||||
|
.with(flecs::ChildOf, grid_e)
|
||||||
|
.build();
|
||||||
|
int count = 0;
|
||||||
|
floor_q.each([&count](flecs::entity fe,
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_grid_floor) {
|
||||||
|
print_line(String(fe.path()));
|
||||||
|
count++;
|
||||||
|
});
|
||||||
|
if (count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int pos = 0;
|
||||||
|
draw->draw_rect(Rect2(0, 0, disp_size, disp_size), Color(1, 1, 0, 1));
|
||||||
|
|
||||||
|
int cur_floor = current_floor;
|
||||||
|
|
||||||
|
floor_q.each([count, disp_size, grid_size, &pos, &draw,
|
||||||
|
cur_floor](flecs::entity fe,
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_grid_floor) {
|
||||||
|
print_line(String(fe.path()));
|
||||||
|
if (pos == cur_floor) {
|
||||||
|
draw->draw_rect(Rect2(0, 0, disp_size, disp_size),
|
||||||
|
Color(0, 1, 1, 1), false);
|
||||||
|
// TODO: display children cells
|
||||||
|
flecs::query<const WorldEditor::components::
|
||||||
|
buildings_layout_grid_cell>
|
||||||
|
q2 = fe.world()
|
||||||
|
.query_builder<
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_grid_cell>()
|
||||||
|
.with(flecs::ChildOf, fe)
|
||||||
|
.build();
|
||||||
|
HashMap<String, Color> colors;
|
||||||
|
q2.each([&draw, grid_size, disp_size,
|
||||||
|
&colors](flecs::entity fc,
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_grid_cell
|
||||||
|
&cell) {
|
||||||
|
int index = cell.index;
|
||||||
|
int x = index % grid_size;
|
||||||
|
int y = index / grid_size;
|
||||||
|
float dx = x * disp_size / grid_size;
|
||||||
|
float dy = y * disp_size / grid_size;
|
||||||
|
float dsize = disp_size / grid_size;
|
||||||
|
if (!colors.has(cell.type))
|
||||||
|
colors[cell.type] = Color(Math::randf(),
|
||||||
|
Math::randf(),
|
||||||
|
Math::randf(),
|
||||||
|
1);
|
||||||
|
draw->draw_rect(Rect2(dx, dy, dsize, dsize),
|
||||||
|
colors[cell.type]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
pos++;
|
||||||
|
});
|
||||||
|
}
|
||||||
void BuildingLayoutGraphUI::_bind_methods()
|
void BuildingLayoutGraphUI::_bind_methods()
|
||||||
{
|
{
|
||||||
ClassDB::bind_method(D_METHOD("menu_pressed", "id", "button", "path"),
|
ClassDB::bind_method(D_METHOD("menu_pressed", "id", "button", "path"),
|
||||||
@@ -770,4 +891,6 @@ void BuildingLayoutGraphUI::_bind_methods()
|
|||||||
&BuildingLayoutGraphUI::show_command_editor);
|
&BuildingLayoutGraphUI::show_command_editor);
|
||||||
ClassDB::bind_method(D_METHOD("draw_building_gen_display", "draw"),
|
ClassDB::bind_method(D_METHOD("draw_building_gen_display", "draw"),
|
||||||
&BuildingLayoutGraphUI::draw_building_gen_display);
|
&BuildingLayoutGraphUI::draw_building_gen_display);
|
||||||
|
ClassDB::bind_method(D_METHOD("draw_2d_grid_view", "draw"),
|
||||||
|
&BuildingLayoutGraphUI::draw_2d_grid_view);
|
||||||
}
|
}
|
||||||
@@ -24,6 +24,7 @@ class BuildingLayoutGraphUI : public Object {
|
|||||||
Control *canvas_item;
|
Control *canvas_item;
|
||||||
Button *button;
|
Button *button;
|
||||||
String current_layout;
|
String current_layout;
|
||||||
|
int current_floor;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BuildingLayoutGraphUI(MainTabs *gui, WindowDialog *dlg)
|
BuildingLayoutGraphUI(MainTabs *gui, WindowDialog *dlg)
|
||||||
@@ -32,6 +33,7 @@ public:
|
|||||||
, dlg(dlg)
|
, dlg(dlg)
|
||||||
, canvas_item(nullptr)
|
, canvas_item(nullptr)
|
||||||
, button(nullptr)
|
, button(nullptr)
|
||||||
|
, current_floor(0)
|
||||||
{
|
{
|
||||||
dlg->connect("tree_entered", this, "tree_entered");
|
dlg->connect("tree_entered", this, "tree_entered");
|
||||||
}
|
}
|
||||||
@@ -55,6 +57,12 @@ public:
|
|||||||
return BuildingLayoutGraph::get_singleton()->get_layout(
|
return BuildingLayoutGraph::get_singleton()->get_layout(
|
||||||
current_layout);
|
current_layout);
|
||||||
}
|
}
|
||||||
|
flecs::entity get_current_grid_layout()
|
||||||
|
{
|
||||||
|
assert(current_layout.length() > 0);
|
||||||
|
return BuildingLayoutGraph::get_singleton()->get_grid_layout(
|
||||||
|
current_layout);
|
||||||
|
}
|
||||||
void select_layout(int id)
|
void select_layout(int id)
|
||||||
{
|
{
|
||||||
ItemList *item_list =
|
ItemList *item_list =
|
||||||
@@ -66,42 +74,7 @@ public:
|
|||||||
print_line("select_layout: " + itos(id));
|
print_line("select_layout: " + itos(id));
|
||||||
}
|
}
|
||||||
void handle_event(const String &event, const Vector<Variant> &args);
|
void handle_event(const String &event, const Vector<Variant> &args);
|
||||||
void tree_entered()
|
void tree_entered();
|
||||||
{
|
|
||||||
if (!Engine::get_singleton()->is_editor_hint()) {
|
|
||||||
/* move this elsewhere */
|
|
||||||
std::vector<Variant> args_data2 = {
|
|
||||||
/* clang-format off */
|
|
||||||
MARGIN_RIGHT, 1.0f, MARGIN_BOTTOM, 1.0f,
|
|
||||||
"Layout Graph",
|
|
||||||
MARGIN_RIGHT, 1.0f, MARGIN_BOTTOM, 1.0f,
|
|
||||||
"buildings_layout_graph",
|
|
||||||
MARGIN_RIGHT, 1.0f, MARGIN_BOTTOM, 1.0f,
|
|
||||||
"Interior",
|
|
||||||
"Exterior",
|
|
||||||
/* clang-format on */
|
|
||||||
};
|
|
||||||
ui_field::ui_field_builder(
|
|
||||||
dlg, dlg,
|
|
||||||
// "p+={t+={v#{s#!+={}}v#{}v#{}}}",
|
|
||||||
"t**{v#{s**{p#!**}}v#{}v#{}}",
|
|
||||||
args_data2.data(), args_data2.size());
|
|
||||||
dlg->update();
|
|
||||||
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);
|
|
||||||
ItemList *item_list = gui->get_as_node<ItemList>(
|
|
||||||
"%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)
|
void room_size_entered(float value, Control *item, const String &path)
|
||||||
{
|
{
|
||||||
print_line(String::num(value));
|
print_line(String::num(value));
|
||||||
@@ -197,6 +170,7 @@ public:
|
|||||||
}
|
}
|
||||||
void setup_layout_tab(Control *tab, const String &header);
|
void setup_layout_tab(Control *tab, const String &header);
|
||||||
void draw_building_gen_display(Control *draw);
|
void draw_building_gen_display(Control *draw);
|
||||||
|
void draw_2d_grid_view(Control *draw);
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -136,6 +136,27 @@ struct state {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
flecs::entity BuildingLayoutGraph::graph_module::get_layout_grid_base()
|
||||||
|
{
|
||||||
|
const String &layout_grid_name = "buildings_layout_grid_v2";
|
||||||
|
flecs::world ecs = BaseData::get_singleton()->get();
|
||||||
|
flecs::entity layout_base_e =
|
||||||
|
ecs.lookup(layout_grid_name.ascii().ptr());
|
||||||
|
if (layout_base_e.is_valid()) {
|
||||||
|
assert(layout_base_e.has<
|
||||||
|
WorldEditor::components::buildings_layout_grid_base>());
|
||||||
|
return layout_base_e;
|
||||||
|
} else {
|
||||||
|
layout_base_e = ecs.entity(layout_grid_name.ascii().ptr());
|
||||||
|
assert(layout_base_e.is_valid());
|
||||||
|
layout_base_e.add<
|
||||||
|
WorldEditor::components::buildings_layout_grid_base>();
|
||||||
|
assert(layout_base_e.has<
|
||||||
|
WorldEditor::components::buildings_layout_grid_base>());
|
||||||
|
return layout_base_e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
||||||
{
|
{
|
||||||
ecs.module<BuildingLayoutGraph::graph_module>();
|
ecs.module<BuildingLayoutGraph::graph_module>();
|
||||||
@@ -152,12 +173,14 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
|||||||
.member<int>("index");
|
.member<int>("index");
|
||||||
ecs.component<WorldEditor::components::buildings_layout_grid>();
|
ecs.component<WorldEditor::components::buildings_layout_grid>();
|
||||||
ecs.component<WorldEditor::components::buildings_layout_grid_cell>();
|
ecs.component<WorldEditor::components::buildings_layout_grid_cell>();
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_grid_floor>();
|
||||||
ecs.component<WorldEditor::components::buildings_layout_grid_size>()
|
ecs.component<WorldEditor::components::buildings_layout_grid_size>()
|
||||||
.member<int>("grid_size");
|
.member<int>("grid_size");
|
||||||
ecs.component<WorldEditor::components::buildings_layout_area>()
|
ecs.component<WorldEditor::components::buildings_layout_area>()
|
||||||
.member<float>("area");
|
.member<float>("area");
|
||||||
ecs.component<WorldEditor::components::buildings_layout_order>()
|
ecs.component<WorldEditor::components::buildings_layout_order>()
|
||||||
.member<int>("index");
|
.member<int>("index");
|
||||||
|
ecs.component<WorldEditor::components::belongs>();
|
||||||
#if 0
|
#if 0
|
||||||
ecs.component<
|
ecs.component<
|
||||||
WorldEditor::components::buildings_layout_commands::command>()
|
WorldEditor::components::buildings_layout_commands::command>()
|
||||||
@@ -777,24 +800,27 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
|||||||
&area) {
|
&area) {
|
||||||
int grid_size = (int)(Math::sqrt(area.area) * 1.5) + 1;
|
int grid_size = (int)(Math::sqrt(area.area) * 1.5) + 1;
|
||||||
flecs::entity graph_e = it.entity(count);
|
flecs::entity graph_e = it.entity(count);
|
||||||
List<flecs::entity> query;
|
List<flecs::entity> queue;
|
||||||
query.push_back(graph_e);
|
queue.push_back(graph_e);
|
||||||
|
|
||||||
List<int> floors;
|
List<Pair<int, flecs::entity_t> > floors;
|
||||||
while (!query.empty()) {
|
Set<int> tmp;
|
||||||
flecs::entity fe = query.front()->get();
|
while (!queue.empty()) {
|
||||||
query.pop_front();
|
flecs::entity fe = queue.front()->get();
|
||||||
|
queue.pop_front();
|
||||||
if (fe.has<WorldEditor::components::
|
if (fe.has<WorldEditor::components::
|
||||||
buildings_layout_floor>()) {
|
buildings_layout_floor>()) {
|
||||||
int index =
|
int index =
|
||||||
fe.get<WorldEditor::components::
|
fe.get<WorldEditor::components::
|
||||||
buildings_layout_floor_index>()
|
buildings_layout_floor_index>()
|
||||||
->index;
|
->index;
|
||||||
if (!floors.find(index))
|
Pair<int, flecs::entity_t> m(index,
|
||||||
floors.push_back(index);
|
fe.id());
|
||||||
|
if (!floors.find(m))
|
||||||
|
floors.push_back(m);
|
||||||
}
|
}
|
||||||
fe.children([&query](flecs::entity ce) {
|
fe.children([&queue](flecs::entity ce) {
|
||||||
query.push_back(ce);
|
queue.push_back(ce);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
graph_e.set<WorldEditor::components::
|
graph_e.set<WorldEditor::components::
|
||||||
@@ -806,18 +832,22 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
|||||||
.kind(GraphSolve)
|
.kind(GraphSolve)
|
||||||
.immediate()
|
.immediate()
|
||||||
.read<WorldEditor::components::buildings_layout_grid_base>()
|
.read<WorldEditor::components::buildings_layout_grid_base>()
|
||||||
|
.read<WorldEditor::components::buildings_layout_grid_size>()
|
||||||
.write<WorldEditor::components::buildings_layout_grid_base>()
|
.write<WorldEditor::components::buildings_layout_grid_base>()
|
||||||
.each([](flecs::iter &it, size_t count,
|
.each([](flecs::iter &it, size_t count,
|
||||||
const WorldEditor::components::buildings_layout_grid_size
|
const WorldEditor::components::buildings_layout_grid_size
|
||||||
&size) {
|
&size) {
|
||||||
flecs::entity graph_e = it.entity(count);
|
flecs::entity graph_e = it.entity(count);
|
||||||
|
it.world().defer_suspend();
|
||||||
flecs::entity grid_base_e =
|
flecs::entity grid_base_e =
|
||||||
BuildingLayoutGraph::get_singleton()
|
BuildingLayoutGraph::get_singleton()
|
||||||
->get_layout_grid_base();
|
->get_layout_grid_base();
|
||||||
flecs::entity grid_e =
|
flecs::entity grid_e =
|
||||||
grid_base_e.lookup(graph_e.name());
|
grid_base_e.lookup(graph_e.name());
|
||||||
|
flecs::log::warn("deleting entity %s", grid_e.path());
|
||||||
if (grid_e.is_valid())
|
if (grid_e.is_valid())
|
||||||
grid_e.destruct();
|
grid_e.destruct();
|
||||||
|
it.world().defer_resume();
|
||||||
});
|
});
|
||||||
ecs.system<const WorldEditor::components::buildings_layout_grid_size>(
|
ecs.system<const WorldEditor::components::buildings_layout_grid_size>(
|
||||||
"CreateGridEntity")
|
"CreateGridEntity")
|
||||||
@@ -843,15 +873,64 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
|||||||
"SetupGrid")
|
"SetupGrid")
|
||||||
.kind(GraphSolve)
|
.kind(GraphSolve)
|
||||||
.write<WorldEditor::components::buildings_layout_grid>()
|
.write<WorldEditor::components::buildings_layout_grid>()
|
||||||
.each([](flecs::iter &it, size_t count,
|
.write<WorldEditor::components::buildings_layout_grid_floor>()
|
||||||
const WorldEditor::components::buildings_layout_grid_size
|
.read<WorldEditor::components::buildings_layout_floor_index>()
|
||||||
&size) {
|
.each([this](flecs::iter &it, size_t count,
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_grid_size &size) {
|
||||||
/* TODO: use it or delete it
|
/* TODO: use it or delete it
|
||||||
flecs::entity graph_e = it.entity(count);
|
flecs::entity graph_e = it.entity(count);
|
||||||
graph_e.add<
|
graph_e.add<
|
||||||
WorldEditor::components::buildings_layout_grid>();
|
WorldEditor::components::buildings_layout_grid>();
|
||||||
*/
|
*/
|
||||||
|
flecs::entity graph_e = it.entity(count);
|
||||||
|
it.world().defer_suspend();
|
||||||
|
flecs::entity grid_base_e = get_layout_grid_base();
|
||||||
|
flecs::entity grid_e =
|
||||||
|
grid_base_e.lookup(graph_e.name());
|
||||||
|
assert(grid_e.is_valid());
|
||||||
|
const List<Pair<int, flecs::entity_t> >::Element *me =
|
||||||
|
size.floors.front();
|
||||||
|
while (me) {
|
||||||
|
flecs::entity base_floor_e =
|
||||||
|
grid_e.world().entity(me->get().second);
|
||||||
|
int floor_index =
|
||||||
|
base_floor_e
|
||||||
|
.get<WorldEditor::components::
|
||||||
|
buildings_layout_floor_index>()
|
||||||
|
->index;
|
||||||
|
String floor_name =
|
||||||
|
"floor_" + itos(floor_index);
|
||||||
|
flecs::entity floor_e =
|
||||||
|
grid_e.world()
|
||||||
|
.entity(floor_name.ascii().ptr())
|
||||||
|
.child_of(grid_e);
|
||||||
|
assert(floor_e.is_valid());
|
||||||
|
assert(grid_e.is_valid());
|
||||||
|
floor_e.add<
|
||||||
|
WorldEditor::components::
|
||||||
|
buildings_layout_grid_floor>();
|
||||||
|
flecs::log::warn("created floor: %s::%s",
|
||||||
|
grid_e.path().c_str(),
|
||||||
|
floor_name.ascii().ptr());
|
||||||
|
me = me->next();
|
||||||
|
}
|
||||||
|
it.world().defer_resume();
|
||||||
});
|
});
|
||||||
|
struct make_random {
|
||||||
|
int seed;
|
||||||
|
int next;
|
||||||
|
make_random(int seed)
|
||||||
|
: seed(seed)
|
||||||
|
, next(seed)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
int get()
|
||||||
|
{
|
||||||
|
next = next * 1103515245 + 12345;
|
||||||
|
return (int)((unsigned)next >> 16) % 32768;
|
||||||
|
}
|
||||||
|
};
|
||||||
ecs.system<const WorldEditor::components::buildings_layout_grid_size>(
|
ecs.system<const WorldEditor::components::buildings_layout_grid_size>(
|
||||||
"CreateGrid")
|
"CreateGrid")
|
||||||
.kind(GraphSolve)
|
.kind(GraphSolve)
|
||||||
@@ -861,9 +940,7 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
|||||||
&size) {
|
&size) {
|
||||||
int i;
|
int i;
|
||||||
flecs::entity graph_e = it.entity(count);
|
flecs::entity graph_e = it.entity(count);
|
||||||
flecs::log::err("CreateGrid: %s",
|
flecs::log::warn("creating grid for: %s",
|
||||||
graph_e.path().c_str());
|
|
||||||
flecs::log::dbg("CreateGrid: %s",
|
|
||||||
graph_e.path().c_str());
|
graph_e.path().c_str());
|
||||||
flecs::entity grid_base_e =
|
flecs::entity grid_base_e =
|
||||||
BuildingLayoutGraph::get_singleton()
|
BuildingLayoutGraph::get_singleton()
|
||||||
@@ -871,76 +948,266 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
|||||||
flecs::entity grid_e =
|
flecs::entity grid_e =
|
||||||
grid_base_e.lookup(graph_e.name());
|
grid_base_e.lookup(graph_e.name());
|
||||||
assert(grid_e.is_valid());
|
assert(grid_e.is_valid());
|
||||||
const List<int>::Element *me = size.floors.front();
|
flecs::log::warn("creating grid for: %s: %s",
|
||||||
while (me) {
|
|
||||||
String floor_name = "floor_" + itos(me->get());
|
|
||||||
flecs::entity floor_e =
|
|
||||||
grid_e.world()
|
|
||||||
.entity(floor_name.ascii().ptr())
|
|
||||||
.child_of(grid_e);
|
|
||||||
assert(floor_e.is_valid());
|
|
||||||
flecs::log::dbg(
|
|
||||||
"CreateGrid: %s floor %d grid_size %d",
|
|
||||||
graph_e.path().c_str(), me->get(),
|
|
||||||
size.grid_size);
|
|
||||||
for (i = 0; i < size.grid_size * size.grid_size;
|
|
||||||
i++) {
|
|
||||||
String pname = "cell_" + itos(i);
|
|
||||||
flecs::entity cell_e =
|
|
||||||
floor_e.world()
|
|
||||||
.entity(pname.ascii()
|
|
||||||
.ptr())
|
|
||||||
.child_of(floor_e);
|
|
||||||
assert(cell_e.is_valid());
|
|
||||||
cell_e.set<
|
|
||||||
WorldEditor::components::
|
|
||||||
buildings_layout_grid_cell>(
|
|
||||||
{ "empty" });
|
|
||||||
flecs::log::dbg(
|
|
||||||
"CreateGrid: %s floor %d cell %d %s",
|
|
||||||
graph_e.path().c_str(),
|
graph_e.path().c_str(),
|
||||||
me->get(), i,
|
grid_e.path().c_str());
|
||||||
cell_e.path().c_str());
|
const List<Pair<int, flecs::entity_t> >::Element *me =
|
||||||
|
size.floors.front();
|
||||||
|
graph_e.world().defer_suspend();
|
||||||
|
while (me) {
|
||||||
|
LocalVector<Pair<flecs::entity_t, Vector2i> >
|
||||||
|
positions;
|
||||||
|
/* starting at grid center */
|
||||||
|
struct make_random r(100);
|
||||||
|
flecs::query<const WorldEditor::components::
|
||||||
|
buildings_layout_area>
|
||||||
|
q = graph_e.world()
|
||||||
|
.query_builder<
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_area>()
|
||||||
|
.with(flecs::ChildOf,
|
||||||
|
me->get().second)
|
||||||
|
.scope_open()
|
||||||
|
.with<WorldEditor::components::
|
||||||
|
buildings_layout_zone>()
|
||||||
|
.or_()
|
||||||
|
.with<WorldEditor::components::
|
||||||
|
buildings_layout_unit>()
|
||||||
|
.scope_close()
|
||||||
|
.build();
|
||||||
|
q.each([size, &positions,
|
||||||
|
&r](flecs::entity ce,
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_area &area) {
|
||||||
|
flecs::log::warn(
|
||||||
|
"generating positions for: %s",
|
||||||
|
ce.path().c_str());
|
||||||
|
if (positions.empty()) {
|
||||||
|
/* starting at grid center */
|
||||||
|
Vector2i start_pos(
|
||||||
|
size.grid_size / 2,
|
||||||
|
size.grid_size / 2);
|
||||||
|
positions.push_back(
|
||||||
|
Pair<flecs::entity_t,
|
||||||
|
Vector2i>(
|
||||||
|
ce.id(),
|
||||||
|
start_pos));
|
||||||
|
} else {
|
||||||
|
int which = r.get() %
|
||||||
|
positions.size();
|
||||||
|
assert(which <
|
||||||
|
(int)positions.size());
|
||||||
|
while (1) {
|
||||||
|
int j, k;
|
||||||
|
assert(which <
|
||||||
|
(int)positions
|
||||||
|
.size());
|
||||||
|
const Vector2i &base =
|
||||||
|
positions[which]
|
||||||
|
.second;
|
||||||
|
flecs::entity_t base_et =
|
||||||
|
positions[which]
|
||||||
|
.first;
|
||||||
|
float base_area =
|
||||||
|
ce.world()
|
||||||
|
.entity(base_et)
|
||||||
|
.get<WorldEditor::components::
|
||||||
|
buildings_layout_area>()
|
||||||
|
->area;
|
||||||
|
int base_radius =
|
||||||
|
(int)((Math::sqrt(
|
||||||
|
base_area) *
|
||||||
|
1.5f) /
|
||||||
|
2.0f) /
|
||||||
|
4; /* grid conversion */
|
||||||
|
base_radius = MAX(
|
||||||
|
1, base_radius);
|
||||||
|
int local_radius =
|
||||||
|
(int)((Math::sqrt(
|
||||||
|
area.area) *
|
||||||
|
1.5f) /
|
||||||
|
2.0f) /
|
||||||
|
4; /* grid conversion */
|
||||||
|
local_radius = MAX(
|
||||||
|
1,
|
||||||
|
local_radius);
|
||||||
|
int dim = base_radius +
|
||||||
|
local_radius;
|
||||||
|
dim = MAX(1, dim);
|
||||||
|
/* grid coordinates */
|
||||||
|
std::vector<Vector2i> candidates = {
|
||||||
|
/* clang-format off */
|
||||||
|
{ base.x + dim, base.y },
|
||||||
|
{ base.x + dim, base.y + dim },
|
||||||
|
{ base.x, base.y + dim },
|
||||||
|
{ base.x - dim, base.y + dim },
|
||||||
|
{ base.x - dim, base.y },
|
||||||
|
{ base.x - dim, base.y - dim },
|
||||||
|
{ base.x, base.y - dim },
|
||||||
|
/* clang-format on */
|
||||||
|
};
|
||||||
|
LocalVector<Vector2i>
|
||||||
|
accepted;
|
||||||
|
for (j = 0;
|
||||||
|
j <
|
||||||
|
(int)candidates
|
||||||
|
.size();
|
||||||
|
j++) {
|
||||||
|
print_line(
|
||||||
|
"candidate: " +
|
||||||
|
itos(candidates[j]
|
||||||
|
.x) +
|
||||||
|
", " +
|
||||||
|
itos(candidates[j]
|
||||||
|
.y));
|
||||||
|
if (candidates[j].x <
|
||||||
|
0 ||
|
||||||
|
candidates[j].x >=
|
||||||
|
size.grid_size)
|
||||||
|
continue;
|
||||||
|
if (candidates[j].y <
|
||||||
|
0 ||
|
||||||
|
candidates[j].y >=
|
||||||
|
size.grid_size)
|
||||||
|
continue;
|
||||||
|
{
|
||||||
|
bool ok =
|
||||||
|
true;
|
||||||
|
for (k = 0;
|
||||||
|
k <
|
||||||
|
(int)positions
|
||||||
|
.size();
|
||||||
|
k++) {
|
||||||
|
assert(k <
|
||||||
|
(int)positions
|
||||||
|
.size());
|
||||||
|
const Vector2i &pos =
|
||||||
|
positions[k]
|
||||||
|
.second;
|
||||||
|
if (candidates
|
||||||
|
[j] ==
|
||||||
|
pos) {
|
||||||
|
ok = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
me = me->next();
|
|
||||||
}
|
}
|
||||||
#if 0
|
if (!ok)
|
||||||
int i;
|
continue;
|
||||||
flecs::entity graph_e = it.entity(count);
|
}
|
||||||
List<flecs::entity> query;
|
for (k = 0;
|
||||||
query.push_back(graph_e);
|
k <
|
||||||
HashMap<int, String> floor_list;
|
(int)positions
|
||||||
while (!query.empty()) {
|
.size();
|
||||||
flecs::entity fe = query.front()->get();
|
k++) {
|
||||||
query.pop_front();
|
assert(k <
|
||||||
if (fe.has<WorldEditor::components::
|
(int)positions
|
||||||
buildings_layout_floor>()) {
|
.size());
|
||||||
int index =
|
flecs::entity_t pbase_et =
|
||||||
fe.get<WorldEditor::components::
|
positions[k]
|
||||||
|
.first;
|
||||||
|
const Vector2i &check =
|
||||||
|
positions[k]
|
||||||
|
.second;
|
||||||
|
float parea =
|
||||||
|
ce.world()
|
||||||
|
.entity(pbase_et)
|
||||||
|
.get<WorldEditor::components::
|
||||||
|
buildings_layout_area>()
|
||||||
|
->area;
|
||||||
|
int pdim =
|
||||||
|
(int)((Math::sqrt(
|
||||||
|
parea) *
|
||||||
|
1.5f) /
|
||||||
|
2.0f) /
|
||||||
|
4; /* radius converted to grid 4x4*/
|
||||||
|
int radius =
|
||||||
|
pdim +
|
||||||
|
local_radius;
|
||||||
|
int radius_sq =
|
||||||
|
radius *
|
||||||
|
radius;
|
||||||
|
int lx =
|
||||||
|
check.x -
|
||||||
|
candidates[j]
|
||||||
|
.x;
|
||||||
|
int ly =
|
||||||
|
check.y -
|
||||||
|
candidates[j]
|
||||||
|
.y;
|
||||||
|
if (lx * lx +
|
||||||
|
ly * ly <
|
||||||
|
radius_sq)
|
||||||
|
continue;
|
||||||
|
accepted.push_back(
|
||||||
|
candidates
|
||||||
|
[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (accepted.size() ==
|
||||||
|
0) {
|
||||||
|
which = r.get() %
|
||||||
|
positions
|
||||||
|
.size();
|
||||||
|
print_line(
|
||||||
|
"reset choice: " +
|
||||||
|
itos(which) +
|
||||||
|
" " +
|
||||||
|
itos(positions
|
||||||
|
.size()));
|
||||||
|
assert(which <
|
||||||
|
(int)positions
|
||||||
|
.size());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
assert(accepted.size() >
|
||||||
|
0);
|
||||||
|
const Vector2i &selected = accepted
|
||||||
|
[r.get() %
|
||||||
|
accepted.size()];
|
||||||
|
Pair<flecs::entity_t,
|
||||||
|
Vector2i>
|
||||||
|
m(ce.id(),
|
||||||
|
selected);
|
||||||
|
positions.push_back(m);
|
||||||
|
flecs::log::warn(
|
||||||
|
"add position: %d, %d",
|
||||||
|
selected.x,
|
||||||
|
selected.y);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
flecs::entity base_floor_e =
|
||||||
|
grid_e.world().entity(me->get().second);
|
||||||
|
flecs::log::warn("base_floor: %s",
|
||||||
|
base_floor_e.path().c_str());
|
||||||
|
int floor_index =
|
||||||
|
base_floor_e
|
||||||
|
.get<WorldEditor::components::
|
||||||
buildings_layout_floor_index>()
|
buildings_layout_floor_index>()
|
||||||
->index;
|
->index;
|
||||||
String floor_name =
|
String floor_name =
|
||||||
"floor_" + itos(index);
|
"floor_" + itos(floor_index);
|
||||||
floor_list[index] = floor_name;
|
|
||||||
}
|
|
||||||
fe.children([&query](flecs::entity ce) {
|
|
||||||
query.push_back(ce);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
List<int> floors;
|
|
||||||
floor_list.get_key_list(&floors);
|
|
||||||
flecs::entity grid_e = graph_e.lookup("grid");
|
|
||||||
assert(graph_e.is_valid());
|
|
||||||
List<int>::Element *me = floors.front();
|
|
||||||
while (me) {
|
|
||||||
String floor_name = floor_list[me->get()];
|
|
||||||
flecs::entity floor_e =
|
flecs::entity floor_e =
|
||||||
grid_e.world()
|
grid_e.lookup(floor_name.ascii().ptr());
|
||||||
.entity(floor_name.ascii().ptr())
|
print_line("grid: " + String(grid_e.path()));
|
||||||
.child_of(grid_e);
|
print_line("floor: " + floor_name);
|
||||||
for (i = 0; i < size.grid_size * size.grid_size;
|
assert(floor_e.is_valid());
|
||||||
i++) {
|
assert(grid_e.is_valid());
|
||||||
String pname = "cell_" + itos(i);
|
floor_e.add<
|
||||||
|
WorldEditor::components::
|
||||||
|
buildings_layout_grid_floor>();
|
||||||
|
flecs::log::warn("grid floor: %s",
|
||||||
|
floor_e.path().c_str());
|
||||||
|
for (i = 0; i < (int)positions.size(); i++) {
|
||||||
|
int cell_id =
|
||||||
|
positions[i].second.x +
|
||||||
|
size.grid_size *
|
||||||
|
positions[i].second.y;
|
||||||
|
flecs::entity region_e =
|
||||||
|
grid_e.world().entity(
|
||||||
|
positions[i].first);
|
||||||
|
String pname = "cell_" + itos(cell_id);
|
||||||
flecs::entity cell_e =
|
flecs::entity cell_e =
|
||||||
floor_e.world()
|
floor_e.world()
|
||||||
.entity(pname.ascii()
|
.entity(pname.ascii()
|
||||||
@@ -950,13 +1217,18 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
|||||||
cell_e.set<
|
cell_e.set<
|
||||||
WorldEditor::components::
|
WorldEditor::components::
|
||||||
buildings_layout_grid_cell>(
|
buildings_layout_grid_cell>(
|
||||||
{ "empty" });
|
{ String(region_e.name()),
|
||||||
|
cell_id });
|
||||||
|
cell_e.add<
|
||||||
|
WorldEditor::components::belongs>(
|
||||||
|
region_e);
|
||||||
|
flecs::log::warn("grid cell: %s",
|
||||||
|
cell_e.path().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
me = me->next();
|
me = me->next();
|
||||||
}
|
}
|
||||||
String pjson(grid_e.world().to_json());
|
graph_e.world().defer_resume();
|
||||||
print_line(pjson);
|
|
||||||
#endif
|
|
||||||
});
|
});
|
||||||
ecs.system("AssembleSkeletonEnd")
|
ecs.system("AssembleSkeletonEnd")
|
||||||
.kind(GraphSolve)
|
.kind(GraphSolve)
|
||||||
@@ -969,6 +1241,7 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
|||||||
.disable();
|
.disable();
|
||||||
EditorEvent::get_singleton()->event.emit(
|
EditorEvent::get_singleton()->event.emit(
|
||||||
"update_layout_view", varray());
|
"update_layout_view", varray());
|
||||||
|
// assert(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
ecs.system<WorldEditor::components::buildings_layout_floor>(
|
ecs.system<WorldEditor::components::buildings_layout_floor>(
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <list>
|
#include <list>
|
||||||
#include <scene/3d/spatial.h>
|
#include <scene/3d/spatial.h>
|
||||||
#include "stream.h"
|
#include "stream.h"
|
||||||
|
#include "base_data.h"
|
||||||
class RoadLinesEditor;
|
class RoadLinesEditor;
|
||||||
class BuildingsEditor;
|
class BuildingsEditor;
|
||||||
class WorldEditor : public Spatial {
|
class WorldEditor : public Spatial {
|
||||||
@@ -72,6 +73,7 @@ public:
|
|||||||
struct buildings_layout_grid {};
|
struct buildings_layout_grid {};
|
||||||
struct buildings_layout_grid_cell {
|
struct buildings_layout_grid_cell {
|
||||||
String type;
|
String type;
|
||||||
|
int index;
|
||||||
};
|
};
|
||||||
struct buildings_layout_graph {};
|
struct buildings_layout_graph {};
|
||||||
struct buildings_layout_graph_node {
|
struct buildings_layout_graph_node {
|
||||||
@@ -113,8 +115,10 @@ public:
|
|||||||
};
|
};
|
||||||
struct buildings_layout_grid_size {
|
struct buildings_layout_grid_size {
|
||||||
int grid_size;
|
int grid_size;
|
||||||
List<int> floors;
|
List<Pair<int, flecs::entity_t> > floors;
|
||||||
};
|
};
|
||||||
|
struct belongs {};
|
||||||
|
struct buildings_layout_grid_floor {};
|
||||||
struct buildings_layout_commands {
|
struct buildings_layout_commands {
|
||||||
struct command {
|
struct command {
|
||||||
int command;
|
int command;
|
||||||
|
|||||||
Reference in New Issue
Block a user