Building creation done

This commit is contained in:
2024-09-16 01:37:41 +03:00
parent d5ad5bac8c
commit e971020cb3
4 changed files with 104 additions and 24 deletions

View File

@@ -31,6 +31,15 @@ func _ready():
$"%line_cursor".hide()
# $"%building_type".connect("item_selected", self, "change_building_type")
var ignore_events = [
"result:get_closest_building",
"mouse_drag",
"mouse_press",
"mouse_drag_on",
"mouse_drag_off",
"line_cursor_motion",
"button:create_building"
]
func editor_event(evname: String, args: Array):
print(evname, args)
if evname == "mode_change_pre":
@@ -48,10 +57,6 @@ func editor_event(evname: String, args: Array):
$WorldEditor.editor_command("get_building_types", [])
elif mode_next == 6:
$WorldEditor.editor_command("get_lines_list", [])
elif evname == "result:get_closest_building":
pass
# print(evname, args)
# select_building(args[0], args[3], args[4])
elif evname == "result:get_building_types":
print(evname, args)
var btypes = args[0]
@@ -63,17 +68,7 @@ func editor_event(evname: String, args: Array):
$Area.global_transform.origin.z = $Camera.global_transform.origin.z
# elif evname == "edit_update_building":
# check_edit_building()
elif evname == "mouse_press":
pass
# mouse_press(args[0])
elif evname == "mouse_drag":
pass
# mouse_drag(args[0])
elif evname == "mouse_drag_on":
pass
elif evname == "mouse_drag_off":
pass
elif evname == "line_cursor_motion":
elif evname in ignore_events:
pass
else:
breakpoint

View File

@@ -6,6 +6,7 @@
#include <scene/3d/camera.h>
#include <modules/voxel/terrain/voxel_lod_terrain.h>
#include <modules/imgmapper/voxel_generator_imgmapper.h>
#include "from_string.h"
#include "world_editor.h"
#include "buildings_editor.h"
@@ -53,6 +54,51 @@ protected:
&HandleChangeBuildingType::change_building_type);
}
};
class HandleButton : public Object {
GDCLASS(HandleButton, Object)
BuildingsEditor *editor;
String button_path;
String event_string;
Array event_args;
Button *get_button()
{
Button *button = editor->get_as_node<Button>(button_path);
assert(button);
return button;
}
void button_handler()
{
editor->emit("button:" + event_string, event_args);
}
public:
HandleButton(BuildingsEditor *editor, const String &button_path,
const String &event_string,
const Array &event_args = Array())
: Object()
, editor(editor)
, button_path(button_path)
, event_string(event_string)
, event_args(event_args)
{
if (!get_button()->is_connected("pressed", this,
"button_handler"))
get_button()->connect("pressed", this,
"button_handler");
}
virtual ~HandleButton()
{
if (get_button()->is_connected("pressed", this,
"button_handler"))
get_button()->disconnect("pressed", this,
"button_handler");
}
static void _bind_methods()
{
ClassDB::bind_method(D_METHOD("button_handler"),
&HandleButton::button_handler);
}
};
class HandleDeleteButton : public Object {
GDCLASS(HandleDeleteButton, Object)
@@ -92,8 +138,8 @@ public:
&HandleDeleteButton::delete_building_handler);
}
};
static HandleChangeBuildingType *change_building_type_handler = nullptr;
static HandleDeleteButton *delete_button_handler = nullptr;
static std::vector<Object *> ui_handlers;
BuildingsEditor::BuildingsEditor(WorldEditor *editor)
: editor(editor)
@@ -111,26 +157,30 @@ void BuildingsEditor::activate()
{
assert(!active);
editor->event.add_listener(this, &BuildingsEditor::event_handler);
if (!change_building_type_handler)
change_building_type_handler =
memnew(HandleChangeBuildingType(this));
Array args;
ui_handlers.push_back(memnew(HandleButton(
this, "%buildings_create_building", "create_building", args)));
ui_handlers.push_back(memnew(HandleChangeBuildingType(this)));
int i;
for (i = 0; i < (int)ui_handlers.size(); i++)
assert(ui_handlers[i]);
if (!delete_button_handler)
delete_button_handler = memnew(HandleDeleteButton(this));
assert(change_building_type_handler && delete_button_handler);
assert(delete_button_handler);
active = true;
}
void BuildingsEditor::deactivate()
{
assert(active);
if (change_building_type_handler) {
memdelete(change_building_type_handler);
change_building_type_handler = nullptr;
}
if (delete_button_handler) {
memdelete(delete_button_handler);
delete_button_handler = nullptr;
}
int i;
for (i = 0; i < (int)ui_handlers.size(); i++)
memdelete(ui_handlers[i]);
ui_handlers.clear();
editor->event.remove_listener(this, &BuildingsEditor::event_handler);
active = false;
}
@@ -141,6 +191,8 @@ void BuildingsEditor::event_handler(const String &event, const Array &args)
mouse_drag(args[0]);
else if (event == "mouse_press")
mouse_press(args[0]);
else if (event == "button:create_building")
handle_create_building();
else if (event == "result:get_closest_building") {
select_building(args[0], args[3], args[4]);
if (get_buildings_editor_mode() == 2 /* rotate */) {
@@ -152,6 +204,29 @@ void BuildingsEditor::event_handler(const String &event, const Array &args)
print_line("buildings::" + event);
}
void BuildingsEditor::handle_create_building()
{
print_line("create_building");
int bmode = get_buildings_editor_mode();
assert(bmode == 3);
OptionButton *building_type =
get_as_node<OptionButton>("%building_type");
int index = building_type->get_selected();
if (index >= 0) {
const String &item = building_type->get_item_text(index);
Array args;
Transform xform = get_as_node<Spatial>("%building_cursor")
->get_global_transform();
Dictionary building_data;
/* FIXME: calculate AABBs as in previous editor */
building_data["id"] = item;
String building_key = to_string<Transform>(xform);
args.push_back(building_data);
args.push_back(building_key);
editor->editor_command("create_building", args);
}
}
template <class T>
inline void BuildingsEditor::mode_visibility(int mode, const String &path)
{
@@ -372,6 +447,11 @@ void BuildingsEditor::change_building_type(const String &type_name)
editor->editor_command("change_building_type", args);
}
void BuildingsEditor::emit(const String &event_string, const Array &event_args)
{
editor->event.emit(event_string, event_args);
}
void BuildingsEditor::remove_buildings_by_prefix(const String &prefix)
{
Array args;

View File

@@ -11,6 +11,7 @@ class BuildingsEditor {
void deactivate();
void event_handler(const String &event, const Array &args);
template <class T> void mode_visibility(int mode, const String &path);
void handle_create_building();
public:
BuildingsEditor(WorldEditor *editor);
@@ -27,6 +28,7 @@ public:
void editor_command(const String &command, const Array &args);
void delete_building_handler();
void change_building_type(const String &type_name);
void emit(const String &event_string, const Array &event_args);
template <class T> T *get_as_node(const String &path);
template <class T> const T *get_as_node(const String &path) const;
Node *scene();

View File

@@ -245,6 +245,9 @@ void WorldEditor::editor_command(const String &command, const Array &args)
} else if (command == "remove_buildings_by_prefix") {
if (stream_world)
stream_world->run_command(command, args);
} else if (command == "create_building") {
if (stream_world)
stream_world->run_command(command, args);
} else {
if (road_lines_editor)
road_lines_editor->editor_command(command, args);