2020-01-31 19:52:37 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2019-2020 Péter Magyar
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-01-25 22:06:43 +01:00
|
|
|
#include "prop_tool_editor_plugin.h"
|
|
|
|
|
2020-02-29 00:21:59 +01:00
|
|
|
#include "core/os/dir_access.h"
|
|
|
|
#include "core/os/file_access.h"
|
2020-01-25 22:06:43 +01:00
|
|
|
#include "editor/editor_scale.h"
|
2020-02-29 00:21:59 +01:00
|
|
|
#include "scene/resources/packed_scene.h"
|
2020-01-25 22:06:43 +01:00
|
|
|
|
2020-02-29 16:23:14 +01:00
|
|
|
#include "prop_tool.h"
|
|
|
|
#include "prop_tool_entity.h"
|
|
|
|
#include "prop_tool_light.h"
|
|
|
|
#include "prop_tool_mesh.h"
|
|
|
|
#include "prop_tool_scene.h"
|
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
void PropToolEditorPlugin::edit(Object *p_object) {
|
2020-02-29 16:23:14 +01:00
|
|
|
Ref<PropData> pedited_prop(Object::cast_to<PropData>(p_object));
|
2020-01-25 22:06:43 +01:00
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
String p = create_or_get_scene_path(pedited_prop);
|
2020-01-25 22:06:43 +01:00
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
get_editor_interface()->open_scene_from_path(p);
|
2020-01-25 22:06:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
bool PropToolEditorPlugin::handles(Object *p_object) const {
|
2020-01-25 22:06:43 +01:00
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
bool handles = p_object->is_class("PropData");
|
2020-01-25 22:06:43 +01:00
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
return handles;
|
2020-01-25 22:06:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
void PropToolEditorPlugin::make_visible(bool p_visible) {
|
2020-01-25 22:06:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
String PropToolEditorPlugin::create_or_get_scene_path(const Ref<PropData> &data) {
|
2020-02-29 16:23:14 +01:00
|
|
|
ERR_FAIL_COND_V(!data.is_valid(), "");
|
|
|
|
|
|
|
|
String temp_path = EditorSettings::get_singleton()->get("editors/prop_tool/temp_path");
|
2020-01-25 22:06:43 +01:00
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
String path = temp_path + data->get_path().get_file().get_basename() + ".tscn";
|
2020-01-25 22:06:43 +01:00
|
|
|
|
2020-02-29 00:21:59 +01:00
|
|
|
if (!FileAccess::exists(path))
|
|
|
|
create_scene(data);
|
2020-01-25 22:06:43 +01:00
|
|
|
|
2020-02-29 00:21:59 +01:00
|
|
|
return path;
|
2020-01-25 22:06:43 +01:00
|
|
|
}
|
2020-02-27 23:58:23 +01:00
|
|
|
PropTool *PropToolEditorPlugin::create_or_get_scene(const Ref<PropData> &data) {
|
2020-02-29 00:21:59 +01:00
|
|
|
ERR_FAIL_COND_V(!data.is_valid(), NULL);
|
2020-02-27 23:58:23 +01:00
|
|
|
|
2020-02-29 16:23:14 +01:00
|
|
|
String temp_path = EditorSettings::get_singleton()->get("editors/prop_tool/temp_path");
|
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
String path = temp_path + data->get_path().get_file().get_basename() + ".tscn";
|
2020-02-29 00:21:59 +01:00
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
Ref<PackedScene> ps;
|
2020-02-29 00:21:59 +01:00
|
|
|
|
|
|
|
if (!FileAccess::exists(path))
|
2020-02-27 23:58:23 +01:00
|
|
|
ps = create_scene(data);
|
2020-02-29 00:21:59 +01:00
|
|
|
else
|
|
|
|
ps = ResourceLoader::load(path, "PackedScene");
|
|
|
|
|
|
|
|
if (!ps.is_valid())
|
|
|
|
return NULL;
|
2020-02-27 23:58:23 +01:00
|
|
|
|
2020-02-29 00:21:59 +01:00
|
|
|
PropTool *pt = Object::cast_to<PropTool>(ps->instance());
|
|
|
|
pt->set_plugin(this);
|
|
|
|
return pt;
|
2020-02-27 23:58:23 +01:00
|
|
|
}
|
|
|
|
Ref<PackedScene> PropToolEditorPlugin::create_scene(const Ref<PropData> &data) {
|
2020-02-29 00:21:59 +01:00
|
|
|
PropTool *pt = memnew(PropTool);
|
2020-02-27 23:58:23 +01:00
|
|
|
|
2020-02-29 00:21:59 +01:00
|
|
|
pt->set_plugin(this);
|
|
|
|
pt->set_target_prop(data);
|
|
|
|
|
|
|
|
Ref<PackedScene> ps;
|
|
|
|
ps.instance();
|
|
|
|
ps->pack(pt);
|
|
|
|
|
2020-02-29 16:23:14 +01:00
|
|
|
String temp_path = EditorSettings::get_singleton()->get("editors/prop_tool/temp_path");
|
|
|
|
|
2020-02-29 00:21:59 +01:00
|
|
|
Error err = ResourceSaver::save(temp_path + data->get_path().get_file().get_basename() + ".tscn", ps);
|
2020-01-25 22:06:43 +01:00
|
|
|
|
2020-02-29 16:23:14 +01:00
|
|
|
if (err)
|
|
|
|
print_error("PropTool: create_scene failed! Error_code:" + String::num(err));
|
|
|
|
|
2020-02-29 00:21:59 +01:00
|
|
|
pt->queue_delete();
|
|
|
|
return ps;
|
2020-01-25 22:06:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 16:23:14 +01:00
|
|
|
void PropToolEditorPlugin::on_scene_changed(Node *scene) {
|
|
|
|
PropTool *pt = Object::cast_to<PropTool>(scene);
|
|
|
|
|
|
|
|
if (ObjectDB::instance_validate(pt)) {
|
|
|
|
pt->set_plugin(this);
|
|
|
|
|
|
|
|
light_button->show();
|
|
|
|
mesh_button->show();
|
|
|
|
prop_button->show();
|
|
|
|
scene_button->show();
|
|
|
|
entity_button->show();
|
|
|
|
} else {
|
|
|
|
light_button->hide();
|
|
|
|
mesh_button->hide();
|
|
|
|
prop_button->hide();
|
|
|
|
scene_button->hide();
|
|
|
|
entity_button->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropToolEditorPlugin::apply_changes() {
|
|
|
|
Node *scene = get_editor_interface()->get_edited_scene_root();
|
|
|
|
PropTool *pt = Object::cast_to<PropTool>(scene);
|
|
|
|
|
|
|
|
if (ObjectDB::instance_validate(pt)) {
|
|
|
|
pt->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropToolEditorPlugin::add_light() {
|
|
|
|
Array selection = get_editor_interface()->get_selection()->get_selected_nodes();
|
|
|
|
|
|
|
|
Node *selected;
|
|
|
|
|
|
|
|
if (selection.size() != 1)
|
|
|
|
selected = get_editor_interface()->get_edited_scene_root();
|
|
|
|
else
|
|
|
|
selected = selection[0];
|
|
|
|
|
|
|
|
Node *s = selected;
|
|
|
|
PropToolLight *n = memnew(PropToolLight);
|
|
|
|
|
|
|
|
UndoRedo u = get_undo_redo();
|
|
|
|
u.create_action("Add Light");
|
|
|
|
u.add_do_method(s, "add_child", n);
|
|
|
|
u.add_do_property(n, "owner", get_editor_interface()->get_edited_scene_root());
|
|
|
|
u.add_undo_method(s, "remove_child", n);
|
|
|
|
u.commit_action();
|
|
|
|
|
|
|
|
get_editor_interface()->get_selection()->clear();
|
|
|
|
get_editor_interface()->get_selection()->add_node(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropToolEditorPlugin::add_mesh() {
|
|
|
|
Array selected = get_editor_interface()->get_selection()->get_selected_nodes();
|
|
|
|
|
|
|
|
if (selected.size() != 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Node *s = selected[0];
|
|
|
|
PropToolMesh *n = memnew(PropToolMesh);
|
|
|
|
|
|
|
|
UndoRedo u = get_undo_redo();
|
|
|
|
u.create_action("Add Mesh");
|
|
|
|
u.add_do_method(s, "add_child", n);
|
|
|
|
u.add_do_property(n, "owner", get_editor_interface()->get_edited_scene_root());
|
|
|
|
u.add_undo_method(s, "remove_child", n);
|
|
|
|
u.commit_action();
|
|
|
|
|
|
|
|
get_editor_interface()->get_selection()->clear();
|
|
|
|
get_editor_interface()->get_selection()->add_node(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropToolEditorPlugin::add_prop() {
|
|
|
|
|
|
|
|
Array selected = get_editor_interface()->get_selection()->get_selected_nodes();
|
|
|
|
|
|
|
|
if (selected.size() != 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Node *s = selected[0];
|
|
|
|
PropTool *n = memnew(PropTool);
|
|
|
|
|
|
|
|
UndoRedo u = get_undo_redo();
|
|
|
|
u.create_action("Add Prop");
|
|
|
|
u.add_do_method(s, "add_child", n);
|
|
|
|
u.add_do_property(n, "owner", get_editor_interface()->get_edited_scene_root());
|
|
|
|
u.add_undo_method(s, "remove_child", n);
|
|
|
|
u.commit_action();
|
|
|
|
|
|
|
|
get_editor_interface()->get_selection()->clear();
|
|
|
|
get_editor_interface()->get_selection()->add_node(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropToolEditorPlugin::add_scene() {
|
|
|
|
Array selected = get_editor_interface()->get_selection()->get_selected_nodes();
|
|
|
|
|
|
|
|
if (selected.size() != 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Node *s = selected[0];
|
|
|
|
PropToolScene *n = memnew(PropToolScene);
|
|
|
|
|
|
|
|
UndoRedo u = get_undo_redo();
|
|
|
|
u.create_action("Add Scene");
|
|
|
|
u.add_do_method(s, "add_child", n);
|
|
|
|
u.add_do_property(n, "owner", get_editor_interface()->get_edited_scene_root());
|
|
|
|
u.add_undo_method(s, "remove_child", n);
|
|
|
|
u.commit_action();
|
|
|
|
|
|
|
|
get_editor_interface()->get_selection()->clear();
|
|
|
|
get_editor_interface()->get_selection()->add_node(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropToolEditorPlugin::add_entity() {
|
|
|
|
Array selected = get_editor_interface()->get_selection()->get_selected_nodes();
|
|
|
|
|
|
|
|
if (selected.size() != 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Node *s = selected[0];
|
|
|
|
PropToolEntity *n = memnew(PropToolEntity);
|
|
|
|
|
|
|
|
UndoRedo u = get_undo_redo();
|
|
|
|
u.create_action("Add Entity");
|
|
|
|
u.add_do_method(s, "add_child", n);
|
|
|
|
u.add_do_property(n, "owner", get_editor_interface()->get_edited_scene_root());
|
|
|
|
u.add_undo_method(s, "remove_child", n);
|
|
|
|
u.commit_action();
|
|
|
|
|
|
|
|
get_editor_interface()->get_selection()->clear();
|
|
|
|
get_editor_interface()->get_selection()->add_node(n);
|
|
|
|
}
|
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
PropToolEditorPlugin::PropToolEditorPlugin(EditorNode *p_node) {
|
2020-02-29 16:44:42 +01:00
|
|
|
EDITOR_DEF("editors/prop_tool/temp_path", "res://.prop_tool_temp/");
|
2020-02-29 16:23:14 +01:00
|
|
|
EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "editors/prop_tool/temp_path"));
|
|
|
|
|
|
|
|
String tp = EditorSettings::get_singleton()->get("editors/prop_tool/temp_path");
|
|
|
|
|
|
|
|
if (!tp.ends_with("/")) {
|
|
|
|
tp += "/";
|
|
|
|
EditorSettings::get_singleton()->set("editors/prop_tool/temp_path", tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DirAccess::exists(tp)) {
|
|
|
|
DirAccess *d = DirAccess::create_for_path(tp);
|
|
|
|
|
|
|
|
Error err = d->make_dir_recursive(tp);
|
|
|
|
|
|
|
|
memdelete(d);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
print_error("PropTool: Temporary directory creation failed: error code: " + String::num(err));
|
|
|
|
}
|
2020-01-25 22:06:43 +01:00
|
|
|
|
|
|
|
editor = p_node;
|
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
light_button = memnew(ToolButton);
|
|
|
|
light_button->set_text("Light");
|
|
|
|
light_button->connect("pressed", this, "add_light");
|
2020-01-25 22:06:43 +01:00
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
mesh_button = memnew(ToolButton);
|
|
|
|
mesh_button->set_text("Mesh");
|
|
|
|
mesh_button->connect("pressed", this, "add_mesh");
|
2020-01-25 22:06:43 +01:00
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
prop_button = memnew(ToolButton);
|
|
|
|
prop_button->set_text("Prop");
|
|
|
|
prop_button->connect("pressed", this, "add_prop");
|
|
|
|
|
|
|
|
scene_button = memnew(ToolButton);
|
|
|
|
scene_button->set_text("Scene");
|
|
|
|
scene_button->connect("pressed", this, "add_scene");
|
|
|
|
|
|
|
|
entity_button = memnew(ToolButton);
|
|
|
|
entity_button->set_text("Entity");
|
|
|
|
entity_button->connect("pressed", this, "add_entity");
|
|
|
|
|
|
|
|
add_control_to_container(EditorPlugin::CONTAINER_SPATIAL_EDITOR_MENU, light_button);
|
|
|
|
add_control_to_container(EditorPlugin::CONTAINER_SPATIAL_EDITOR_MENU, mesh_button);
|
|
|
|
add_control_to_container(EditorPlugin::CONTAINER_SPATIAL_EDITOR_MENU, prop_button);
|
|
|
|
add_control_to_container(EditorPlugin::CONTAINER_SPATIAL_EDITOR_MENU, scene_button);
|
|
|
|
add_control_to_container(EditorPlugin::CONTAINER_SPATIAL_EDITOR_MENU, entity_button);
|
|
|
|
|
|
|
|
light_button->hide();
|
|
|
|
mesh_button->hide();
|
|
|
|
prop_button->hide();
|
|
|
|
scene_button->hide();
|
|
|
|
entity_button->hide();
|
|
|
|
|
2020-02-29 16:23:14 +01:00
|
|
|
call_deferred("connect", "scene_changed", this, "on_scene_changed");
|
2020-01-25 22:06:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 23:58:23 +01:00
|
|
|
PropToolEditorPlugin::~PropToolEditorPlugin() {
|
|
|
|
_edited_prop.unref();
|
2020-01-25 22:06:43 +01:00
|
|
|
}
|
2020-02-27 23:58:23 +01:00
|
|
|
|
2020-02-29 16:23:14 +01:00
|
|
|
void PropToolEditorPlugin::_notification(int p_what) {
|
|
|
|
if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
|
|
|
|
|
|
|
|
String tp = EditorSettings::get_singleton()->get("editors/prop_tool/temp_path");
|
|
|
|
|
|
|
|
if (!tp.ends_with("/")) {
|
|
|
|
tp += "/";
|
|
|
|
EditorSettings::get_singleton()->set("editors/prop_tool/temp_path", tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DirAccess::exists(tp)) {
|
|
|
|
DirAccess *d = DirAccess::create_for_path(tp);
|
|
|
|
|
|
|
|
Error err = d->make_dir_recursive(tp);
|
2020-02-27 23:58:23 +01:00
|
|
|
|
2020-02-29 16:23:14 +01:00
|
|
|
memdelete(d);
|
|
|
|
|
|
|
|
ERR_FAIL_COND_MSG(err, "PropTool: Temporary directory creation failed: error code: " + String::num(err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropToolEditorPlugin::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("on_scene_changed", "scene_root"), &PropToolEditorPlugin::on_scene_changed);
|
|
|
|
ClassDB::bind_method(D_METHOD("apply_changes"), &PropToolEditorPlugin::apply_changes);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_light"), &PropToolEditorPlugin::add_light);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_mesh"), &PropToolEditorPlugin::add_mesh);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_prop"), &PropToolEditorPlugin::add_prop);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_scene"), &PropToolEditorPlugin::add_scene);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_entity"), &PropToolEditorPlugin::add_entity);
|
|
|
|
}
|