/*************************************************************************/ /* property_editor.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ /* */ /* 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 "property_editor.h" #include "scene/gui/label.h" #include "io/resource_loader.h" #include "io/image_loader.h" #include "object_type_db.h" #include "print_string.h" #include "globals.h" #include "scene/resources/font.h" #include "pair.h" #include "scene/scene_string_names.h" #include "editor_settings.h" #include "editor_import_export.h" #include "editor_node.h" #include "multi_node_edit.h" #include "array_property_edit.h" #include "editor_help.h" #include "scene/resources/packed_scene.h" #include "scene/main/viewport.h" #include "editor_file_system.h" #include "create_dialog.h" #include "property_selector.h" void CustomPropertyEditor::_notification(int p_what) { if (p_what==NOTIFICATION_DRAW) { RID ci = get_canvas_item(); get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size())); /* if (v.get_type()==Variant::COLOR) { VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2( 10,10,60, get_size().height-20 ), v ); }*/ } } void CustomPropertyEditor::_menu_option(int p_which) { switch(type) { case Variant::INT: { if (hint==PROPERTY_HINT_FLAGS) { int val = v; if (val&(1<set_mode(EditorFileDialog::MODE_OPEN_FILE); String type=(hint==PROPERTY_HINT_RESOURCE_TYPE)?hint_text:String(); List extensions; for (int i=0;i valid_extensions; for (List::Element *E=extensions.front();E;E=E->next()) { valid_extensions.insert(E->get()); } file->clear_filters(); for (Set::Element *E=valid_extensions.front();E;E=E->next()) { file->add_filter("*."+E->get()+" ; "+E->get().to_upper() ); } file->popup_centered_ratio(); } break; case OBJ_MENU_EDIT: { RefPtr RefPtr=v; if (!RefPtr.is_null()) { emit_signal("resource_edit_request"); hide(); } } break; case OBJ_MENU_CLEAR: { v=Variant(); emit_signal("variant_changed"); hide(); } break; case OBJ_MENU_MAKE_UNIQUE: { RefPtr RefPtr=v; Ref res_orig = RefPtr; if (res_orig.is_null()) return; List property_list; res_orig->get_property_list(&property_list); List< Pair > propvalues; for(List::Element *E=property_list.front();E;E=E->next()) { Pair p; PropertyInfo &pi = E->get(); if (pi.usage&PROPERTY_USAGE_STORAGE) { p.first=pi.name; p.second=res_orig->get(pi.name); } propvalues.push_back(p); } String orig_type = res_orig->get_type(); Object *inst = ObjectTypeDB::instance( orig_type ); Ref res = Ref( inst->cast_to() ); ERR_FAIL_COND(res.is_null()); for(List< Pair >::Element *E=propvalues.front();E;E=E->next()) { Pair &p=E->get(); res->set(p.first,p.second); } v=res.get_ref_ptr(); emit_signal("variant_changed"); hide(); } break; case OBJ_MENU_COPY: { EditorSettings::get_singleton()->set_resource_clipboard(v); } break; case OBJ_MENU_PASTE: { v=EditorSettings::get_singleton()->get_resource_clipboard(); emit_signal("variant_changed"); } break; case OBJ_MENU_REIMPORT: { RES r=v; if (r.is_valid() && r->get_import_metadata().is_valid()) { Ref rimd = r->get_import_metadata(); Ref eip = EditorImportExport::get_singleton()->get_import_plugin_by_name(rimd->get_editor()); if (eip.is_valid()) { eip->import_dialog(r->get_path()); } } } break; case OBJ_MENU_NEW_SCRIPT: { if (owner->cast_to()) EditorNode::get_singleton()->get_scene_tree_dock()->open_script_dialog(owner->cast_to()); } break; default: { ERR_FAIL_COND( inheritors_array.empty() ); String intype=inheritors_array[p_which-TYPE_BASE_ID]; Object *obj = ObjectTypeDB::instance(intype); ERR_BREAK( !obj ); Resource *res=obj->cast_to(); ERR_BREAK( !res ); if (owner && hint==PROPERTY_HINT_RESOURCE_TYPE && hint_text=="Script") { //make visual script the right type res->call("set_instance_base_type",owner->get_type()); } v=Ref(res).get_ref_ptr(); emit_signal("variant_changed"); } break; } } break; default:{} } } void CustomPropertyEditor::hide_menu() { menu->hide(); } Variant CustomPropertyEditor::get_variant() const { return v; } String CustomPropertyEditor::get_name() const { return name; } bool CustomPropertyEditor::edit(Object* p_owner,const String& p_name,Variant::Type p_type, const Variant& p_variant,int p_hint,String p_hint_text) { owner=p_owner; updating=true; name=p_name; v=p_variant; hint=p_hint; hint_text=p_hint_text; type_button->hide(); if (color_picker) color_picker->hide(); texture_preview->hide(); inheritors_array.clear(); text_edit->hide(); easing_draw->hide(); spinbox->hide(); slider->hide(); for (int i=0;ihide(); value_label[i]->hide(); if (i<4) scroll[i]->hide(); } for (int i=0;ihide(); } for(int i=0;i<20;i++) checks20[i]->hide(); type = (p_variant.get_type()!=Variant::NIL && p_variant.get_type()!=Variant::_RID && p_type!=Variant::OBJECT)? p_variant.get_type() : p_type; switch(type) { case Variant::BOOL: { CheckBox *c=checks20[0]; c->set_text("True"); c->set_pos(Vector2(4,4)); c->set_pressed(v); c->show(); set_size(checks20[0]->get_pos()+checks20[0]->get_size()+Vector2(4,4)*EDSCALE); } break; case Variant::INT: case Variant::REAL: { if (hint==PROPERTY_HINT_RANGE) { int c = hint_text.get_slice_count(","); float min=0,max=100,step=1; if (c>=1) { if (!hint_text.get_slice(",",0).empty()) min=hint_text.get_slice(",",0).to_double(); } if (c>=2) { if (!hint_text.get_slice(",",1).empty()) max=hint_text.get_slice(",",1).to_double(); } if (c>=3) { if (!hint_text.get_slice(",",2).empty()) step= hint_text.get_slice(",",2).to_double(); } if (c>=4 && hint_text.get_slice(",",3)=="slider") { slider->set_min(min); slider->set_max(max); slider->set_step(step); slider->set_val(v); slider->show(); set_size(Size2(110,30)*EDSCALE); } else { spinbox->set_min(min); spinbox->set_max(max); spinbox->set_step(step); spinbox->set_val(v); spinbox->show(); set_size(Size2(70,35)*EDSCALE); } } else if (hint==PROPERTY_HINT_ENUM) { menu->clear(); Vector options = hint_text.split(","); for(int i=0;iadd_item(options[i],i); } menu->set_pos(get_pos()); menu->popup(); hide(); updating=false; return false; } else if (hint==PROPERTY_HINT_ALL_FLAGS) { checks20[0]->set_text(""); uint32_t flgs = v; for(int i=0;i<2;i++) { Point2 ofs(4,4); ofs.y+=22*i; for(int j=0;j<10;j++) { CheckBox *c=checks20[i*10+j]; Point2 o=ofs; o.x+=j*22; if (j>=5) o.x+=4; c->set_pos(o); c->set_pressed( flgs & (1<<(i*10+j)) ); c->show(); } } set_size(checks20[19]->get_pos()+Size2(20,25)*EDSCALE); } else if (hint==PROPERTY_HINT_EXP_EASING) { easing_draw->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,5); easing_draw->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,5); easing_draw->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,5); easing_draw->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,30); type_button->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,3); type_button->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,3); type_button->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,25); type_button->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,7); type_button->set_text(TTR("Preset..")); type_button->get_popup()->clear(); type_button->get_popup()->add_item(TTR("Linear"),EASING_LINEAR); type_button->get_popup()->add_item(TTR("Ease In"),EASING_EASE_IN); type_button->get_popup()->add_item(TTR("Ease Out"),EASING_EASE_OUT); if (hint_text!="attenuation") { type_button->get_popup()->add_item(TTR("Zero"),EASING_ZERO); type_button->get_popup()->add_item(TTR("Easing In-Out"),EASING_IN_OUT); type_button->get_popup()->add_item(TTR("Easing Out-In"),EASING_OUT_IN); } type_button->show(); easing_draw->show(); set_size(Size2(200,150)*EDSCALE); } else if (hint==PROPERTY_HINT_FLAGS) { menu->clear(); Vector flags = hint_text.split(","); for(int i=0;iadd_check_item(flag,i); int f = v; if (f&(1<set_item_checked(menu->get_item_index(i),true); } menu->set_pos(get_pos()); menu->popup(); hide(); updating=false; return false; } else { List names; names.push_back("value:"); config_value_editors(1,1,50,names); value_editor[0]->set_text( String::num(v) ); } } break; case Variant::STRING: { if (hint==PROPERTY_HINT_FILE || hint==PROPERTY_HINT_GLOBAL_FILE) { List names; names.push_back(TTR("File..")); names.push_back(TTR("Clear")); config_action_buttons(names); } else if (hint==PROPERTY_HINT_DIR || hint==PROPERTY_HINT_GLOBAL_DIR) { List names; names.push_back(TTR("Dir..")); names.push_back(TTR("Clear")); config_action_buttons(names); } else if (hint==PROPERTY_HINT_ENUM) { menu->clear(); Vector options = hint_text.split(","); for(int i=0;iadd_item(options[i],i); } menu->set_pos(get_pos()); menu->popup(); hide(); updating=false; return false; } else if (hint==PROPERTY_HINT_MULTILINE_TEXT) { text_edit->show(); text_edit->set_text(v); //action_buttons[0]; int button_margin = get_constant("button_margin","Dialogs"); int margin = get_constant("margin","Dialogs"); action_buttons[0]->set_anchor( MARGIN_LEFT, ANCHOR_END ); action_buttons[0]->set_anchor( MARGIN_TOP, ANCHOR_END ); action_buttons[0]->set_anchor( MARGIN_RIGHT, ANCHOR_END ); action_buttons[0]->set_anchor( MARGIN_BOTTOM, ANCHOR_END ); action_buttons[0]->set_begin( Point2( 70, button_margin-5 ) ); action_buttons[0]->set_end( Point2( margin, margin ) ); action_buttons[0]->set_text(TTR("Close")); action_buttons[0]->show(); } else if (hint==PROPERTY_HINT_TYPE_STRING) { if (!create_dialog) { create_dialog = memnew( CreateDialog ); create_dialog->connect("create",this,"_create_dialog_callback"); add_child(create_dialog); } if (hint_text!=String()) { create_dialog->set_base_type(hint_text); } else { create_dialog->set_base_type("Object"); } create_dialog->popup(false); hide(); updating=false; return false; } else if (hint==PROPERTY_HINT_METHOD_OF_VARIANT_TYPE) { #define MAKE_PROPSELECT if (!property_select) { property_select = memnew(PropertySelector); property_select->connect("selected",this,"_create_selected_property"); add_child(property_select); } hide(); MAKE_PROPSELECT; Variant::Type type=Variant::NIL; for(int i=0;iselect_method_from_basic_type(type,v); updating=false; return false; } else if (hint==PROPERTY_HINT_METHOD_OF_BASE_TYPE) { MAKE_PROPSELECT property_select->select_method_from_base_type(hint_text,v); updating=false; return false; } else if (hint==PROPERTY_HINT_METHOD_OF_INSTANCE) { MAKE_PROPSELECT Object *instance = ObjectDB::get_instance(hint_text.to_int64()); if (instance) property_select->select_method_from_instance(instance,v); updating=false; return false; } else if (hint==PROPERTY_HINT_METHOD_OF_SCRIPT) { MAKE_PROPSELECT Object *obj = ObjectDB::get_instance(hint_text.to_int64()); if (obj && obj->cast_to