Merge pull request #8202 from williamd1k0/add-script-button

Add Attach and Clear Script buttons (2.1)
This commit is contained in:
Rémi Verschelde 2017-04-07 08:54:39 +02:00 committed by GitHub
commit 2ee9e033ec
9 changed files with 264 additions and 29 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 541 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 341 B

View File

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="16"
height="16"
viewBox="0 0 16 16"
id="svg2"
version="1.1"
inkscape:version="0.91 r13725"
inkscape:export-filename="/home/godotengine/godot/tools/editor/icons/con_script_create.png"
inkscape:export-xdpi="90"
inkscape:export-ydpi="90"
sodipodi:docname="icon_script_create.svg">
<defs
id="defs4" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="29.966667"
inkscape:cx="5.495872"
inkscape:cy="2.1206692"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="true"
units="px"
inkscape:snap-bbox="true"
inkscape:bbox-paths="true"
inkscape:bbox-nodes="true"
inkscape:snap-bbox-edge-midpoints="true"
inkscape:snap-bbox-midpoints="true"
inkscape:snap-object-midpoints="true"
inkscape:snap-center="true"
inkscape:window-width="1920"
inkscape:window-height="1119"
inkscape:window-x="0"
inkscape:window-y="26"
inkscape:window-maximized="1"
showguides="false">
<inkscape:grid
type="xygrid"
id="grid3336"
empspacing="4" />
</sodipodi:namedview>
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-1036.3622)">
<path
style="opacity:1;fill:#e0e0e0;fill-opacity:1;stroke:none;stroke-width:2;stroke-linecap:butt;stroke-linejoin:bevel;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:0.99607843"
d="M 6 1 L 6 2 A 1 1 0 0 0 5 3 L 5 13 L 4 13 L 4 11 L 2 11 L 2 13 A 1 1 0 0 0 2.5 13.865234 A 1 1 0 0 0 3 14 L 3 15 L 10 15 L 10 14 L 7 14 L 7 10 L 10 10 L 10 7 L 12 7 L 12 5 L 15 5 L 15 3 A 2 2 0 0 0 13 1 L 6 1 z M 11 8 L 11 11 L 8 11 L 8 13 L 11 13 L 11 14.730469 A 2 2 0 0 0 12 13 L 12 8 L 11 8 z "
transform="translate(0,1036.3622)"
id="rect4255" />
<path
style="opacity:1;fill:#b4b4b4;fill-opacity:1;stroke:none;stroke-width:2;stroke-linecap:butt;stroke-linejoin:bevel;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:0.99607843"
d="M 6 1 C 4.8954305 1 4 1.8954305 4 3 L 4 10 L 2 10 L 1 10 L 1 11 L 1 13 C 1 14.104569 1.8954305 15 3 15 C 4.1045695 15 5 14.104569 5 13 L 5 3 C 5 2.4477153 5.4477153 2 6 2 C 6.5522847 2 7 2.4477153 7 3 L 7 4 L 7 5 L 7 6 L 8 6 L 12 6 L 12 5 L 8 5 L 8 4 L 8 3 C 8 1.8954305 7.1045695 1 6 1 z M 2 11 L 4 11 L 4 13 C 4 13.552285 3.5522847 14 3 14 C 2.4477153 14 2 13.552285 2 13 L 2 11 z "
transform="translate(0,1036.3622)"
id="path4265" />
<circle
cy="1048.3622"
cx="3"
id="ellipse4234"
style="opacity:1;fill:#e0e0e0;fill-opacity:1;stroke:none;stroke-width:2;stroke-linecap:butt;stroke-linejoin:bevel;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:0.99607843"
ry="1.0000174"
rx="1" />
<path
style="fill:#e0e0e0;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;fill-opacity:1"
d="m 13,1049.3622 3,0 0,-2 -3,0 0,-3 -2,0 0,3 -3,0 0,2 3,0 0,3 2,0 z"
id="path8069"
inkscape:connector-curvature="0" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 4.0 KiB

View File

@ -194,6 +194,12 @@ void CustomPropertyEditor::_menu_option(int p_which) {
}
}
} break;
case OBJ_MENU_NEW_SCRIPT: {
if (owner->cast_to<Node>())
EditorNode::get_singleton()->get_scene_tree_dock()->open_script_dialog(owner->cast_to<Node>());
} break;
default: {
ERR_FAIL_COND(inheritors_array.empty());
@ -212,6 +218,7 @@ void CustomPropertyEditor::_menu_option(int p_which) {
}
} break;
default: {}
}
}
@ -647,7 +654,10 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant::
menu->clear();
menu->set_size(Size2(1, 1));
if (hint_text != "") {
if (p_name=="script/script" && hint_text=="Script" && owner->cast_to<Node>()) {
menu->add_icon_item(get_icon("Script","EditorIcons"),TTR("New Script"),OBJ_MENU_NEW_SCRIPT);
menu->add_separator();
} else if (hint_text!="") {
int idx = 0;
for (int i = 0; i < hint_text.get_slice_count(","); i++) {

View File

@ -63,6 +63,7 @@ class CustomPropertyEditor : public Popup {
OBJ_MENU_COPY = 4,
OBJ_MENU_PASTE = 5,
OBJ_MENU_REIMPORT = 6,
OBJ_MENU_NEW_SCRIPT=7,
TYPE_BASE_ID = 100
};

View File

@ -57,8 +57,10 @@ void SceneTreeDock::_unhandled_key_input(InputEvent p_event) {
_tool_selected(TOOL_REPLACE);
} else if (ED_IS_SHORTCUT("scene_tree/duplicate", p_event)) {
_tool_selected(TOOL_DUPLICATE);
} else if (ED_IS_SHORTCUT("scene_tree/add_script", p_event)) {
_tool_selected(TOOL_SCRIPT);
} else if (ED_IS_SHORTCUT("scene_tree/attach_script", p_event)) {
_tool_selected(TOOL_ATTACH_SCRIPT);
} else if(ED_IS_SHORTCUT("scene_tree/clear_script", p_event)) {
_tool_selected(TOOL_CLEAR_SCRIPT);
} else if (ED_IS_SHORTCUT("scene_tree/move_up", p_event)) {
_tool_selected(TOOL_MOVE_UP);
} else if (ED_IS_SHORTCUT("scene_tree/move_down", p_event)) {
@ -328,7 +330,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
//groups_editor->set_current(current);
//groups_editor->popup_centered_ratio();
} break;
case TOOL_SCRIPT: {
case TOOL_ATTACH_SCRIPT: {
Node *selected = scene_tree->get_selected();
if (!selected)
@ -347,6 +349,18 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
//script_create_dialog->popup_centered_minsize();
}
} break;
case TOOL_CLEAR_SCRIPT: {
Node *selected = scene_tree->get_selected();
if(!selected)
break;
Ref<Script> existing = selected->get_script();
if(existing.is_valid()) {
const RefPtr empty;
selected->set_script(empty);
}
} break;
case TOOL_MOVE_UP:
case TOOL_MOVE_DOWN: {
@ -675,6 +689,8 @@ void SceneTreeDock::_notification(int p_what) {
}
button_add->set_icon(get_icon("Add", "EditorIcons"));
button_instance->set_icon(get_icon("Instance", "EditorIcons"));
button_create_script->set_icon(get_icon("ScriptCreate","EditorIcons"));
button_clear_script->set_icon(get_icon("Remove", "EditorIcons"));
filter_icon->set_texture(get_icon("Zoom", "EditorIcons"));
@ -1239,11 +1255,26 @@ void SceneTreeDock::_delete_confirm() {
void SceneTreeDock::_selection_changed() {
if (EditorNode::get_singleton()->get_editor_selection()->get_selection().size() > 1) {
int selection_size = EditorNode::get_singleton()->get_editor_selection()->get_selection().size();
if (selection_size > 1) {
//automatically turn on multi-edit
_tool_selected(TOOL_MULTI_EDIT);
}
if (selection_size==1) {
if(EditorNode::get_singleton()->get_editor_selection()->get_selection().front()->key()->get_script().is_null()) {
button_create_script->show();
button_clear_script->hide();
}
else {
button_create_script->hide();
button_clear_script->show();
}
} else {
button_create_script->hide();
button_clear_script->hide();
}
//tool_buttons[TOOL_MULTI_EDIT]->set_disabled(EditorNode::get_singleton()->get_editor_selection()->get_selection().size()<2);
}
@ -1663,7 +1694,8 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
//menu->add_icon_item(get_icon("Groups","EditorIcons"),TTR("Edit Groups"),TOOL_GROUP);
//menu->add_icon_item(get_icon("Connect","EditorIcons"),TTR("Edit Connections"),TOOL_CONNECT);
menu->add_separator();
menu->add_icon_shortcut(get_icon("Script", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_script"), TOOL_SCRIPT);
menu->add_icon_shortcut(get_icon("ScriptCreate", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/attach_script"), TOOL_ATTACH_SCRIPT);
menu->add_icon_shortcut(get_icon("Remove", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/clear_script"), TOOL_CLEAR_SCRIPT);
menu->add_separator();
}
@ -1702,6 +1734,12 @@ void SceneTreeDock::set_filter(const String &p_filter) {
scene_tree->set_filter(p_filter);
}
void SceneTreeDock::open_script_dialog(Node* p_for_node) {
scene_tree->set_selected(p_for_node,false);
_tool_selected(TOOL_ATTACH_SCRIPT);
}
void SceneTreeDock::_bind_methods() {
ObjectTypeDB::bind_method(_MD("_tool_selected"), &SceneTreeDock::_tool_selected, DEFVAL(false));
@ -1745,7 +1783,8 @@ SceneTreeDock::SceneTreeDock(EditorNode *p_editor, Node *p_scene_root, EditorSel
ED_SHORTCUT("scene_tree/add_child_node", TTR("Add Child Node"), KEY_MASK_CMD | KEY_A);
ED_SHORTCUT("scene_tree/instance_scene", TTR("Instance Child Scene"));
ED_SHORTCUT("scene_tree/change_node_type", TTR("Change Type"));
ED_SHORTCUT("scene_tree/add_script", TTR("Add Script"));
ED_SHORTCUT("scene_tree/attach_script", TTR("Attach Script"));
ED_SHORTCUT("scene_tree/clear_script", TTR("Clear Script"));
ED_SHORTCUT("scene_tree/move_up", TTR("Move Up"), KEY_MASK_CMD | KEY_UP);
ED_SHORTCUT("scene_tree/move_down", TTR("Move Down"), KEY_MASK_CMD | KEY_DOWN);
ED_SHORTCUT("scene_tree/duplicate", TTR("Duplicate"), KEY_MASK_CMD | KEY_D);
@ -1779,6 +1818,20 @@ SceneTreeDock::SceneTreeDock(EditorNode *p_editor, Node *p_scene_root, EditorSel
filter_icon->set_stretch_mode(TextureFrame::STRETCH_KEEP_CENTERED);
filter->connect("text_changed", this, "_filter_changed");
tb = memnew(ToolButton);
tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_ATTACH_SCRIPT, false));
tb->set_tooltip(TTR("Attach a new or existing script for the selected node."));
tb->set_shortcut(ED_GET_SHORTCUT("scene_tree/attach_script"));
filter_hbc->add_child(tb);
button_create_script=tb;
tb = memnew(ToolButton);
tb->connect("pressed", this, "_tool_selected", make_binds(TOOL_CLEAR_SCRIPT, false));
tb->set_tooltip(TTR("Clear a script for the selected node."));
tb->set_shortcut(ED_GET_SHORTCUT("scene_tree/clear_script"));
filter_hbc->add_child(tb);
button_clear_script = tb;
scene_tree = memnew(SceneTreeEditor(false, true, true));
vbc->add_child(scene_tree);
scene_tree->set_v_size_flags(SIZE_EXPAND | SIZE_FILL);

View File

@ -58,7 +58,8 @@ class SceneTreeDock : public VBoxContainer {
TOOL_REPLACE,
TOOL_CONNECT,
TOOL_GROUP,
TOOL_SCRIPT,
TOOL_ATTACH_SCRIPT,
TOOL_CLEAR_SCRIPT,
TOOL_MOVE_UP,
TOOL_MOVE_DOWN,
TOOL_DUPLICATE,
@ -76,6 +77,8 @@ class SceneTreeDock : public VBoxContainer {
ToolButton *button_add;
ToolButton *button_instance;
ToolButton *button_create_script;
ToolButton *button_clear_script;
SceneTreeEditor *scene_tree;
@ -150,6 +153,8 @@ class SceneTreeDock : public VBoxContainer {
void _perform_instance_scenes(const Vector<String> &p_files, Node *parent, int p_pos);
void _replace_with_branch_scene(const String &p_file, Node *base);
void _file_selected(String p_file);
protected:
void _notification(int p_what);
static void _bind_methods();
@ -168,6 +173,7 @@ public:
SceneTreeEditor *get_tree_editor() { return scene_tree; }
EditorData *get_editor_data() { return editor_data; }
void open_script_dialog(Node* p_for_node);
SceneTreeDock(EditorNode *p_editor, Node *p_scene_root, EditorSelection *p_editor_selection, EditorData &p_editor_data);
};

View File

@ -94,6 +94,19 @@ void ScriptCreateDialog::_class_name_changed(const String &p_name) {
void ScriptCreateDialog::ok_pressed() {
if (create_new){
_create_new();
} else {
_load_exist();
}
create_new = true;
_update_controls();
}
void ScriptCreateDialog::_create_new() {
if (class_name->is_editable() && !_validate(class_name->get_text())) {
alert->set_text(TTR("Class name is invalid!"));
@ -103,7 +116,6 @@ void ScriptCreateDialog::ok_pressed() {
if (!_validate(parent_name->get_text())) {
alert->set_text(TTR("Parent class name is invalid!"));
alert->popup_centered_minsize();
return;
}
@ -136,14 +148,27 @@ void ScriptCreateDialog::ok_pressed() {
alert->popup_centered_minsize();
return;
}
scr->set_path(lpath);
//EditorFileSystem::get_singleton()->update_file(lpath,scr->get_type());
}
hide();
emit_signal("script_created", scr);
}
void ScriptCreateDialog::_load_exist() {
String path = file_path->get_text();
RES p_script = ResourceLoader::load(path, "Script");
if (p_script.is_null()) {
alert->get_ok()->set_text(TTR("Ugh"));
alert->set_text(vformat(TTR("Error loading script from %s"), path));
alert->popup_centered_minsize();
return;
}
hide();
emit_signal("script_created", p_script.get_ref_ptr());
}
void ScriptCreateDialog::_lang_changed(int l) {
l = language_menu->get_selected();
@ -152,10 +177,35 @@ void ScriptCreateDialog::_lang_changed(int l) {
} else {
class_name->set_editable(false);
}
if (file_path->get_text().basename() == initial_bp) {
file_path->set_text(initial_bp + "." + ScriptServer::get_language(l)->get_extension());
_path_changed(file_path->get_text());
String selected_ext = "."+ScriptServer::get_language(l)->get_extension();
String path = file_path->get_text();
String extension = "";
if (path.find(".") >= 0) {
extension = path.extension();
}
if (extension.length() == 0) {
// add extension if none
path += selected_ext;
_path_changed(path);
} else {
// change extension by selected language
List<String> extensions;
// get all possible extensions for script
for (int l = 0; l<language_menu->get_item_count(); l++) {
ScriptServer::get_language( l )->get_recognized_extensions(&extensions);
}
for(List<String>::Element *E = extensions.front(); E; E = E->next()) {
if (E->get().nocasecmp_to(extension) == 0) {
path = path.basename()+selected_ext;
_path_changed(path);
break;
}
}
}
file_path->set_text(path);
_class_name_changed(class_name->get_text());
}
@ -175,8 +225,10 @@ void ScriptCreateDialog::_browse_path() {
file_browse->clear_filters();
List<String> extensions;
int l = language_menu->get_selected();
ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
// get all possible extensions for script
for (int l = 0; l < language_menu->get_item_count(); l++) {
ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
}
for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
file_browse->add_filter("*." + E->get());
@ -228,43 +280,54 @@ void ScriptCreateDialog::_path_changed(const String &p_path) {
FileAccess *f = FileAccess::create(FileAccess::ACCESS_RESOURCES);
if (f->file_exists(p)) {
path_error_label->set_text(TTR("File exists"));
path_error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
memdelete(f);
return;
}
create_new = !f->file_exists(p);
memdelete(f);
String extension = p.extension();
List<String> extensions;
int l = language_menu->get_selected();
ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
// get all possible extensions for script
for (int l = 0; l < language_menu->get_item_count(); l++) {
ScriptServer::get_language( l )->get_recognized_extensions(&extensions);
}
bool found = false;
int index = 0;
for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
if (E->get().nocasecmp_to(extension) == 0) {
language_menu->select(index); // change Language option by extension
found = true;
break;
}
index++;
}
if (!found) {
path_error_label->set_text(TTR("Invalid extension"));
path_error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
return;
}
path_error_label->set_text(TTR("Valid path"));
_update_controls();
path_error_label->add_color_override("font_color", Color(0, 1.0, 0.8, 0.8));
path_valid = true;
}
void ScriptCreateDialog::_update_controls() {
if (create_new) {
path_error_label->set_text(TTR("Create new script"));
get_ok()->set_text(TTR("Create"));
} else {
path_error_label->set_text(TTR("Load existing script"));
get_ok()->set_text(TTR("Load"));
}
parent_name->set_editable(create_new);
internal->set_disabled(!create_new);
}
void ScriptCreateDialog::_bind_methods() {
ObjectTypeDB::bind_method("_class_name_changed", &ScriptCreateDialog::_class_name_changed);
@ -339,7 +402,7 @@ ScriptCreateDialog::ScriptCreateDialog() {
set_size(Size2(200, 150));
set_hide_on_ok(false);
set_title(TTR("Create Node Script"));
set_title(TTR("Attach Node Script"));
file_browse = memnew(EditorFileDialog);
file_browse->connect("file_selected", this, "_file_selected");
@ -348,4 +411,6 @@ ScriptCreateDialog::ScriptCreateDialog() {
alert = memnew(AcceptDialog);
add_child(alert);
_lang_changed(0);
create_new = true;
}

View File

@ -49,6 +49,7 @@ class ScriptCreateDialog : public ConfirmationDialog {
VBoxContainer *path_vb;
AcceptDialog *alert;
bool path_valid;
bool create_new;
String initial_bp;
void _path_changed(const String &p_path = String());
@ -59,6 +60,9 @@ class ScriptCreateDialog : public ConfirmationDialog {
void _browse_path();
void _file_selected(const String &p_file);
virtual void ok_pressed();
void _create_new();
void _load_exist();
void _update_controls();
protected:
static void _bind_methods();