virtualx-engine/editor/resources_dock.cpp

379 lines
11 KiB
C++
Raw Normal View History

2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* resources_dock.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 02:10:30 +01:00
/* */
/* 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. */
/*************************************************************************/
#include "resources_dock.h"
#include "editor_file_system.h"
2014-02-10 02:10:30 +01:00
#include "editor_node.h"
#include "editor_settings.h"
2014-02-10 02:10:30 +01:00
#include "io/resource_loader.h"
#include "io/resource_saver.h"
#include "project_settings.h"
#include "project_settings_editor.h"
2014-02-10 02:10:30 +01:00
void ResourcesDock::_tool_selected(int p_tool) {
current_action = p_tool;
2014-02-10 02:10:30 +01:00
switch (p_tool) {
2014-02-10 02:10:30 +01:00
case TOOL_NEW: {
create_dialog->popup_centered_ratio();
} break;
case TOOL_OPEN: {
editor->open_resource();
} break;
case TOOL_SAVE: {
TreeItem *ti = resources->get_selected();
if (!ti)
break;
Ref<Resource> current_res = ti->get_metadata(0);
if (current_res->get_path() != "" && current_res->get_path().find("::") == -1) {
2014-02-10 02:10:30 +01:00
_file_action(current_res->get_path());
break;
};
}; /* fallthrough */
case TOOL_SAVE_AS: {
TreeItem *ti = resources->get_selected();
if (!ti)
break;
2014-09-23 01:12:44 +02:00
save_resource_as(ti->get_metadata(0));
2014-02-10 02:10:30 +01:00
} break;
case TOOL_MAKE_LOCAL: {
TreeItem *ti = resources->get_selected();
if (!ti)
break;
Ref<Resource> current_res = ti->get_metadata(0);
current_res->set_path("");
_update_name(ti);
} break;
case TOOL_COPY: {
TreeItem *ti = resources->get_selected();
if (!ti)
break;
Ref<Resource> current_res = ti->get_metadata(0);
EditorSettings::get_singleton()->set_resource_clipboard(current_res);
} break;
case TOOL_PASTE: {
add_resource(EditorSettings::get_singleton()->get_resource_clipboard());
} break;
}
}
void ResourcesDock::_notification(int p_what) {
switch (p_what) {
2014-02-10 02:10:30 +01:00
case NOTIFICATION_ENTER_TREE: {
2014-02-10 02:10:30 +01:00
button_new->set_icon(get_icon("New", "EditorIcons"));
button_open->set_icon(get_icon("Folder", "EditorIcons"));
button_save->set_icon(get_icon("Save", "EditorIcons"));
button_tools->set_icon(get_icon("Tools", "EditorIcons"));
2014-02-10 02:10:30 +01:00
} break;
}
}
void ResourcesDock::save_resource(const String &p_path, const Ref<Resource> &p_resource) {
2014-02-10 02:10:30 +01:00
2017-01-14 18:03:38 +01:00
editor->get_editor_data().apply_changes_in_editors();
int flg = 0;
2014-02-10 02:10:30 +01:00
if (EditorSettings::get_singleton()->get("on_save/compress_binary_resources"))
flg |= ResourceSaver::FLAG_COMPRESS;
/*
if (EditorSettings::get_singleton()->get("on_save/save_paths_as_relative"))
flg|=ResourceSaver::FLAG_RELATIVE_PATHS;
*/
2014-02-10 02:10:30 +01:00
String path = ProjectSettings::get_singleton()->localize_path(p_path);
Error err = ResourceSaver::save(path, p_resource, flg | ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS);
2014-02-10 02:10:30 +01:00
if (err != OK) {
accept->set_text(TTR("Error saving resource!"));
accept->popup_centered_minsize();
return;
2014-02-10 02:10:30 +01:00
}
//EditorFileSystem::get_singleton()->update_file(path,p_resource->get_type());
2014-02-10 02:10:30 +01:00
((Resource *)p_resource.ptr())->set_path(path);
editor->emit_signal("resource_saved", p_resource);
2014-02-10 02:10:30 +01:00
}
void ResourcesDock::save_resource_as(const Ref<Resource> &p_resource) {
2014-02-10 02:10:30 +01:00
current_action = TOOL_SAVE_AS;
2014-02-10 02:10:30 +01:00
2014-09-23 01:12:44 +02:00
RES res(p_resource);
2014-02-10 02:10:30 +01:00
2014-09-23 01:12:44 +02:00
List<String> extensions;
ResourceSaver::get_recognized_extensions(res, &extensions);
2015-05-31 06:59:42 +02:00
file->set_mode(EditorFileDialog::MODE_SAVE_FILE);
2014-02-10 02:10:30 +01:00
if (p_resource->get_path() != "" && p_resource->get_path().find("::") == -1) {
2014-02-10 02:10:30 +01:00
2014-09-23 01:12:44 +02:00
file->set_current_path(p_resource->get_path());
} else {
2014-02-10 02:10:30 +01:00
2014-09-23 01:12:44 +02:00
String existing;
if (extensions.size()) {
existing = "new_" + res->get_class().to_lower() + "." + extensions.front()->get().to_lower();
2014-09-23 01:12:44 +02:00
}
2014-02-10 02:10:30 +01:00
2014-09-23 01:12:44 +02:00
file->set_current_file(existing);
}
2014-02-10 02:10:30 +01:00
2014-09-23 01:12:44 +02:00
file->clear_filters();
for (int i = 0; i < extensions.size(); i++) {
2014-02-10 02:10:30 +01:00
file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
2014-09-23 01:12:44 +02:00
}
2014-02-10 02:10:30 +01:00
2014-09-23 01:12:44 +02:00
file->popup_centered_ratio();
2014-02-10 02:10:30 +01:00
}
void ResourcesDock::_file_action(const String &p_path) {
2014-02-10 02:10:30 +01:00
switch (current_action) {
2014-02-10 02:10:30 +01:00
case TOOL_OPEN: {
} break;
case TOOL_SAVE:
case TOOL_SAVE_AS: {
TreeItem *ti = resources->get_selected();
if (!ti)
break;
Ref<Resource> current_res = ti->get_metadata(0);
RES res(current_res);
save_resource(p_path, res);
2014-02-10 02:10:30 +01:00
_update_name(ti);
2014-02-10 02:10:30 +01:00
} break;
}
}
void ResourcesDock::_update_name(TreeItem *item) {
Ref<Resource> res = item->get_metadata(0);
if (res->get_name() != "")
item->set_text(0, res->get_name());
else if (res->get_path() != "" && res->get_path().find("::") == -1)
item->set_text(0, res->get_path().get_file());
2014-02-10 02:10:30 +01:00
else
item->set_text(0, res->get_class() + " (" + itos(res->get_instance_id()) + ")");
2014-02-10 02:10:30 +01:00
}
void ResourcesDock::remove_resource(const Ref<Resource> &p_resource) {
2014-02-10 02:10:30 +01:00
TreeItem *root = resources->get_root();
ERR_FAIL_COND(!root);
TreeItem *existing = root->get_children();
2014-02-10 02:10:30 +01:00
while (existing) {
2014-02-10 02:10:30 +01:00
Ref<Resource> r = existing->get_metadata(0);
if (r == p_resource) {
2014-02-10 02:10:30 +01:00
//existing->move_to_top();
memdelete(existing);
return;
}
existing = existing->get_next();
2014-02-10 02:10:30 +01:00
}
}
void ResourcesDock::add_resource(const Ref<Resource> &p_resource) {
2014-02-10 02:10:30 +01:00
if (block_add)
return;
if (!p_resource.is_valid())
return;
TreeItem *root = resources->get_root();
ERR_FAIL_COND(!root);
TreeItem *existing = root->get_children();
2014-02-10 02:10:30 +01:00
while (existing) {
2014-02-10 02:10:30 +01:00
Ref<Resource> r = existing->get_metadata(0);
if (r == p_resource) {
2014-02-10 02:10:30 +01:00
//existing->move_to_top();
existing->select(0);
resources->ensure_cursor_is_visible();
return; // existing
}
existing = existing->get_next();
2014-02-10 02:10:30 +01:00
}
TreeItem *res = resources->create_item(root);
res->set_metadata(0, p_resource);
2014-02-10 02:10:30 +01:00
if (has_icon(p_resource->get_class(), "EditorIcons")) {
res->set_icon(0, get_icon(p_resource->get_class(), "EditorIcons"));
2014-02-10 02:10:30 +01:00
}
_update_name(res);
res->add_button(0, get_icon("Del", "EditorIcons"));
2014-02-10 02:10:30 +01:00
res->move_to_top();
res->select(0);
resources->ensure_cursor_is_visible();
}
void ResourcesDock::_resource_selected() {
TreeItem *sel = resources->get_selected();
ERR_FAIL_COND(!sel);
Ref<Resource> r = sel->get_metadata(0);
if (r.is_null())
return;
block_add = true;
2014-02-10 02:10:30 +01:00
editor->push_item(r.ptr());
block_add = false;
2014-02-10 02:10:30 +01:00
}
void ResourcesDock::_delete(Object *p_item, int p_column, int p_id) {
2014-02-10 02:10:30 +01:00
TreeItem *ti = p_item->cast_to<TreeItem>();
ERR_FAIL_COND(!ti);
call_deferred("remove_resource", ti->get_metadata(0));
2014-02-10 02:10:30 +01:00
}
void ResourcesDock::_create() {
Object *c = create_dialog->instance_selected();
ERR_FAIL_COND(!c);
Resource *r = c->cast_to<Resource>();
ERR_FAIL_COND(!r);
REF res(r);
2014-02-10 02:10:30 +01:00
editor->push_item(c);
}
void ResourcesDock::_bind_methods() {
ClassDB::bind_method(D_METHOD("_tool_selected"), &ResourcesDock::_tool_selected);
ClassDB::bind_method(D_METHOD("_create"), &ResourcesDock::_create);
ClassDB::bind_method(D_METHOD("_resource_selected"), &ResourcesDock::_resource_selected);
ClassDB::bind_method(D_METHOD("_delete"), &ResourcesDock::_delete);
ClassDB::bind_method(D_METHOD("remove_resource"), &ResourcesDock::remove_resource);
ClassDB::bind_method(D_METHOD("_file_action"), &ResourcesDock::_file_action);
2014-02-10 02:10:30 +01:00
}
void ResourcesDock::cleanup() {
resources->clear();
resources->create_item(); //root
}
ResourcesDock::ResourcesDock(EditorNode *p_editor) {
editor = p_editor;
2014-02-10 02:10:30 +01:00
VBoxContainer *vbc = this;
HBoxContainer *hbc = memnew(HBoxContainer);
2014-02-10 02:10:30 +01:00
vbc->add_child(hbc);
Button *b;
b = memnew(ToolButton);
b->set_tooltip(TTR("Create New Resource"));
b->connect("pressed", this, "_tool_selected", make_binds(TOOL_NEW));
hbc->add_child(b);
button_new = b;
2014-02-10 02:10:30 +01:00
b = memnew(ToolButton);
b->set_tooltip(TTR("Open Resource"));
b->connect("pressed", this, "_tool_selected", make_binds(TOOL_OPEN));
hbc->add_child(b);
button_open = b;
2014-02-10 02:10:30 +01:00
MenuButton *mb = memnew(MenuButton);
mb->set_tooltip(TTR("Save Resource"));
mb->get_popup()->add_item(TTR("Save Resource"), TOOL_SAVE);
mb->get_popup()->add_item(TTR("Save Resource As.."), TOOL_SAVE_AS);
mb->get_popup()->connect("id_pressed", this, "_tool_selected");
hbc->add_child(mb);
button_save = mb;
2014-02-10 02:10:30 +01:00
hbc->add_spacer();
mb = memnew(MenuButton);
mb->set_tooltip(TTR("Resource Tools"));
mb->get_popup()->add_item(TTR("Make Local"), TOOL_MAKE_LOCAL);
mb->get_popup()->add_item(TTR("Copy"), TOOL_COPY);
mb->get_popup()->add_item(TTR("Paste"), TOOL_PASTE);
mb->get_popup()->connect("id_pressed", this, "_tool_selected");
hbc->add_child(mb);
button_tools = mb;
resources = memnew(Tree);
2014-02-10 02:10:30 +01:00
vbc->add_child(resources);
resources->set_v_size_flags(SIZE_EXPAND_FILL);
resources->create_item(); //root
resources->set_hide_root(true);
resources->connect("cell_selected", this, "_resource_selected");
resources->connect("button_pressed", this, "_delete");
2014-02-10 02:10:30 +01:00
create_dialog = memnew(CreateDialog);
2014-02-10 02:10:30 +01:00
add_child(create_dialog);
create_dialog->set_base_type("Resource");
create_dialog->connect("create", this, "_create");
accept = memnew(AcceptDialog);
2014-02-10 02:10:30 +01:00
add_child(accept);
file = memnew(EditorFileDialog);
2014-02-10 02:10:30 +01:00
add_child(file);
file->connect("file_selected", this, "_file_action");
2014-02-10 02:10:30 +01:00
block_add = false;
2014-02-10 02:10:30 +01:00
}