189 lines
4.0 KiB
C++
189 lines
4.0 KiB
C++
#include <core/object.h>
|
|
#include <core/engine.h>
|
|
#include <scene/gui/control.h>
|
|
#include <scene/gui/box_container.h>
|
|
#include <scene/gui/button.h>
|
|
#include <scene/scene_string_names.h>
|
|
#include "world_editor.h"
|
|
|
|
WorldEditor::WorldEditor()
|
|
: Spatial()
|
|
, stream_world(nullptr)
|
|
, editor_menu(nullptr)
|
|
, current_mode(-1)
|
|
{
|
|
}
|
|
|
|
WorldEditor::~WorldEditor()
|
|
{
|
|
}
|
|
|
|
void WorldEditor::disable_all()
|
|
{
|
|
}
|
|
|
|
void WorldEditor::mode_buildings()
|
|
{
|
|
disable_all();
|
|
print_line("BUILDINGS");
|
|
}
|
|
|
|
void WorldEditor::mode_navigation()
|
|
{
|
|
disable_all();
|
|
print_line("NAVIGATION");
|
|
}
|
|
|
|
void WorldEditor::mode_poi()
|
|
{
|
|
disable_all();
|
|
print_line("POI");
|
|
}
|
|
|
|
void WorldEditor::mode_road_lines()
|
|
{
|
|
disable_all();
|
|
print_line("ROAD_LINES");
|
|
}
|
|
|
|
void WorldEditor::mode_npc()
|
|
{
|
|
disable_all();
|
|
print_line("NPC");
|
|
}
|
|
enum {
|
|
MODE_BUILDINGS = 2,
|
|
MODE_NAVIGATION = 3,
|
|
MODE_POI = 5,
|
|
MODE_ROAD_LINES = 6,
|
|
MODE_NPC = 7,
|
|
};
|
|
struct StringHasher {
|
|
std::size_t operator()(const String &s) const
|
|
{
|
|
return (std::size_t)s.hash64();
|
|
}
|
|
};
|
|
static std::unordered_map<String, int, StringHasher> modes = {
|
|
{ "select_buildings", MODE_BUILDINGS },
|
|
{ "select_navigation", MODE_NAVIGATION },
|
|
{ "select_poi", MODE_POI },
|
|
{ "select_road_lines", MODE_ROAD_LINES },
|
|
{ "select_npc", MODE_NPC }
|
|
};
|
|
void WorldEditor::tools_button(const String &button)
|
|
{
|
|
Array change;
|
|
change.resize(2);
|
|
print_line("tools_button: " + button);
|
|
if (modes.find(button) == modes.end())
|
|
goto end;
|
|
if (current_mode == modes[button])
|
|
goto end;
|
|
change[0] = current_mode;
|
|
change[1] = modes[button];
|
|
emit_signal("editor_event", "mode_change_pre", change);
|
|
switch (modes[button]) {
|
|
case MODE_BUILDINGS:
|
|
mode_buildings();
|
|
break;
|
|
case MODE_NAVIGATION:
|
|
mode_navigation();
|
|
break;
|
|
case MODE_POI:
|
|
mode_poi();
|
|
break;
|
|
case MODE_ROAD_LINES:
|
|
mode_road_lines();
|
|
break;
|
|
case MODE_NPC:
|
|
mode_npc();
|
|
break;
|
|
}
|
|
emit_signal("editor_event", "mode_change_post", change);
|
|
current_mode = modes[button];
|
|
end:;
|
|
}
|
|
|
|
void WorldEditor::editor_command(const String &command, const Array &args)
|
|
{
|
|
print_line("running command: " + command);
|
|
if (command.begins_with("select_") &&
|
|
modes.find(command) != modes.end()) {
|
|
tools_button(command);
|
|
return;
|
|
} else if (command == "get_closest_building") {
|
|
if (stream_world) {
|
|
stream_world->run_command(command, args);
|
|
}
|
|
} else if (command == "update_building_transform") {
|
|
if (stream_world) {
|
|
stream_world->run_command(command, args);
|
|
}
|
|
}
|
|
}
|
|
|
|
int WorldEditor::get_current_mode() const
|
|
{
|
|
return current_mode;
|
|
}
|
|
|
|
StreamWorld *WorldEditor::get_stream_world()
|
|
{
|
|
return stream_world;
|
|
}
|
|
|
|
void WorldEditor::world_command_result(const String &what, const Array &data)
|
|
{
|
|
print_line("what: " + what);
|
|
emit_signal("editor_event", "result:" + what, data);
|
|
}
|
|
|
|
void WorldEditor::_notification(int which)
|
|
{
|
|
switch (which) {
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
Node *base = get_parent();
|
|
int count = base->get_child_count();
|
|
int i;
|
|
for (i = 0; i < count; i++) {
|
|
Node *node = base->get_child(i);
|
|
StreamWorld *sw = Object::cast_to<StreamWorld>(node);
|
|
if (sw && !stream_world &&
|
|
!Engine::get_singleton()->is_editor_hint()) {
|
|
stream_world = sw;
|
|
sw->connect(SceneStringNames::get_singleton()
|
|
->tree_exiting,
|
|
this, "world_exited");
|
|
sw->connect("command_result", this,
|
|
"world_command_result");
|
|
}
|
|
}
|
|
} break;
|
|
case NOTIFICATION_EXIT_TREE:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void WorldEditor::world_exited()
|
|
{
|
|
stream_world = nullptr;
|
|
}
|
|
|
|
void WorldEditor::_bind_methods()
|
|
{
|
|
ClassDB::bind_method(D_METHOD("editor_command", "command", "args"),
|
|
&WorldEditor::editor_command);
|
|
ClassDB::bind_method(D_METHOD("get_current_mode"),
|
|
&WorldEditor::get_current_mode);
|
|
ClassDB::bind_method(D_METHOD("get_stream_world"),
|
|
&WorldEditor::get_stream_world);
|
|
ClassDB::bind_method(D_METHOD("world_exited"),
|
|
&WorldEditor::world_exited);
|
|
ClassDB::bind_method(D_METHOD("world_command_result", "what", "data"),
|
|
&WorldEditor::world_command_result);
|
|
ADD_SIGNAL(MethodInfo("editor_event",
|
|
PropertyInfo(Variant::STRING, "event_name"),
|
|
PropertyInfo(Variant::ARRAY, "args")));
|
|
}
|