| 1 | /**************************************************************************/ |
| 2 | /* property_selector.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "property_selector.h" |
| 32 | |
| 33 | #include "editor/doc/editor_help.h" |
| 34 | #include "editor/editor_node.h" |
| 35 | #include "editor/themes/editor_scale.h" |
| 36 | #include "scene/gui/line_edit.h" |
| 37 | #include "scene/gui/margin_container.h" |
| 38 | #include "scene/gui/tree.h" |
| 39 | |
| 40 | void PropertySelector::_text_changed(const String &p_newtext) { |
| 41 | _update_search(); |
| 42 | } |
| 43 | |
| 44 | void PropertySelector::_sbox_input(const Ref<InputEvent> &p_event) { |
| 45 | // Redirect navigational key events to the tree. |
| 46 | Ref<InputEventKey> key = p_event; |
| 47 | if (key.is_valid()) { |
| 48 | if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) { |
| 49 | search_options->gui_input(key); |
| 50 | search_box->accept_event(); |
| 51 | |
| 52 | TreeItem *root = search_options->get_root(); |
| 53 | if (!root->get_first_child()) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | TreeItem *current = search_options->get_selected(); |
| 58 | |
| 59 | TreeItem *item = search_options->get_next_selected(root); |
| 60 | while (item) { |
| 61 | item->deselect(0); |
| 62 | item = search_options->get_next_selected(item); |
| 63 | } |
| 64 | |
| 65 | current->select(0); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void PropertySelector::_update_search() { |
| 71 | if (properties) { |
| 72 | set_title(TTRC("Select Property")); |
| 73 | } else if (virtuals_only) { |
| 74 | set_title(TTRC("Select Virtual Method")); |
| 75 | } else { |
| 76 | set_title(TTRC("Select Method")); |
| 77 | } |
| 78 | |
| 79 | search_options->clear(); |
| 80 | help_bit->set_custom_text(String(), String(), String()); |
| 81 | |
| 82 | TreeItem *root = search_options->create_item(); |
| 83 | |
| 84 | // Allow using spaces in place of underscores in the search string (makes the search more fault-tolerant). |
| 85 | const String search_text = search_box->get_text().replace_char(' ', '_'); |
| 86 | |
| 87 | if (properties) { |
| 88 | List<PropertyInfo> props; |
| 89 | |
| 90 | if (instance) { |
| 91 | instance->get_property_list(&props, true); |
| 92 | } else if (type != Variant::NIL) { |
| 93 | Variant v; |
| 94 | Callable::CallError ce; |
| 95 | Variant::construct(type, v, nullptr, 0, ce); |
| 96 | |
| 97 | v.get_property_list(&props); |
| 98 | } else { |
| 99 | Object *obj = ObjectDB::get_instance(script); |
| 100 | if (Object::cast_to<Script>(obj)) { |
| 101 | props.push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CATEGORY)); |
| 102 | Object::cast_to<Script>(obj)->get_script_property_list(&props); |
| 103 | } |
| 104 | |
| 105 | StringName base = base_type; |
| 106 | while (base) { |
| 107 | props.push_back(PropertyInfo(Variant::NIL, base, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CATEGORY)); |
| 108 | ClassDB::get_property_list(base, &props, true); |
| 109 | base = ClassDB::get_parent_class(base); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | TreeItem *category = nullptr; |
| 114 | |
| 115 | bool found = false; |
| 116 | for (const PropertyInfo &E : props) { |
| 117 | if (E.usage == PROPERTY_USAGE_CATEGORY) { |
| 118 | if (category && category->get_first_child() == nullptr) { |
| 119 | memdelete(category); //old category was unused |
| 120 | } |
| 121 | category = search_options->create_item(root); |
| 122 | category->set_text(0, E.name); |
| 123 | category->set_selectable(0, false); |
| 124 | |
| 125 | Ref<Texture2D> icon; |
| 126 | if (E.name == "Script Variables") { |
| 127 | icon = search_options->get_editor_theme_icon(SNAME("Script")); |
| 128 | } else { |
| 129 | icon = EditorNode::get_singleton()->get_class_icon(E.name); |
| 130 | } |
| 131 | category->set_icon(0, icon); |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | if (!(E.usage & PROPERTY_USAGE_EDITOR) && !(E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE)) { |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | if (!search_box->get_text().is_empty() && !E.name.containsn(search_text)) { |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | if (!type_filter.is_empty() && !type_filter.has(E.type)) { |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | TreeItem *item = search_options->create_item(category ? category : root); |
| 148 | item->set_text(0, E.name); |
| 149 | item->set_metadata(0, E.name); |
| 150 | item->set_icon(0, search_options->get_editor_theme_icon(Variant::get_type_name(E.type))); |
| 151 | |
| 152 | if (!found && !search_box->get_text().is_empty() && E.name.containsn(search_text)) { |
| 153 | item->select(0); |
| 154 | found = true; |
| 155 | } else if (!found && search_box->get_text().is_empty() && E.name == selected) { |
| 156 | item->select(0); |
| 157 | found = true; |
| 158 | } |
| 159 | |
| 160 | item->set_selectable(0, true); |
| 161 | |
| 162 | _create_subproperties(item, E.type); |
| 163 | item->set_collapsed(true); |
| 164 | } |
| 165 | |
| 166 | if (category && category->get_first_child() == nullptr) { |
| 167 | memdelete(category); //old category was unused |
| 168 | } |
| 169 | |
| 170 | if (found) { |
| 171 | // As we call this while adding items, defer until list is completely populated. |
| 172 | callable_mp(search_options, &Tree::scroll_to_item).call_deferred(search_options->get_selected(), true); |
| 173 | } |
| 174 | |
| 175 | } else { |
| 176 | List<MethodInfo> methods; |
| 177 | |
| 178 | if (type != Variant::NIL) { |
| 179 | Variant v; |
| 180 | Callable::CallError ce; |
| 181 | Variant::construct(type, v, nullptr, 0, ce); |
| 182 | v.get_method_list(&methods); |
| 183 | } else { |
| 184 | Ref<Script> script_ref = ObjectDB::get_ref<Script>(script); |
| 185 | if (script_ref.is_valid()) { |
| 186 | if (script_ref->is_built_in()) { |
| 187 | script_ref->reload(true); |
| 188 | } |
| 189 | |
| 190 | List<MethodInfo> script_methods; |
| 191 | script_ref->get_script_method_list(&script_methods); |
| 192 | |
| 193 | methods.push_back(MethodInfo("*Script Methods")); // TODO: Split by inheritance. |
| 194 | |
| 195 | for (const MethodInfo &mi : script_methods) { |
| 196 | if (mi.name.begins_with("@")) { |
| 197 | // GH-92782. GDScript inline setters/getters are historically present in `get_method_list()` |
| 198 | // and can be called using `Object.call()`. However, these functions are meant to be internal |
| 199 | // and their names are not valid identifiers, so let's hide them from the user. |
| 200 | continue; |
| 201 | } |
| 202 | methods.push_back(mi); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | StringName base = base_type; |
| 207 | while (base) { |
| 208 | methods.push_back(MethodInfo("*" + String(base))); |
| 209 | ClassDB::get_method_list(base, &methods, true, true); |
| 210 | base = ClassDB::get_parent_class(base); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | TreeItem *category = nullptr; |
| 215 | |
| 216 | bool found = false; |
| 217 | bool script_methods = false; |
| 218 | |
| 219 | for (MethodInfo &mi : methods) { |
| 220 | if (mi.name.begins_with("*")) { |
| 221 | if (category && category->get_first_child() == nullptr) { |
| 222 | memdelete(category); //old category was unused |
| 223 | } |
| 224 | category = search_options->create_item(root); |
| 225 | category->set_text(0, mi.name.replace_first("*", "")); |
| 226 | category->set_selectable(0, false); |
| 227 | |
| 228 | Ref<Texture2D> icon; |
| 229 | script_methods = false; |
| 230 | String rep = mi.name.remove_char('*'); |
| 231 | if (mi.name == "*Script Methods") { |
| 232 | icon = search_options->get_editor_theme_icon(SNAME("Script")); |
| 233 | script_methods = true; |
| 234 | } else { |
| 235 | icon = EditorNode::get_singleton()->get_class_icon(rep); |
| 236 | } |
| 237 | category->set_icon(0, icon); |
| 238 | |
| 239 | continue; |
| 240 | } |
| 241 | |
| 242 | String name = mi.name.get_slicec(':', 0); |
| 243 | if (!script_methods && name.begins_with("_") && !(mi.flags & METHOD_FLAG_VIRTUAL)) { |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | if (virtuals_only && !(mi.flags & METHOD_FLAG_VIRTUAL)) { |
| 248 | continue; |
| 249 | } |
| 250 | |
| 251 | if (!virtuals_only && (mi.flags & METHOD_FLAG_VIRTUAL)) { |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | if (!search_box->get_text().is_empty() && !name.containsn(search_text)) { |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | TreeItem *item = search_options->create_item(category ? category : root); |
| 260 | |
| 261 | String desc; |
| 262 | if (mi.name.contains_char(':')) { |
| 263 | desc = mi.name.get_slicec(':', 1) + " "; |
| 264 | mi.name = mi.name.get_slicec(':', 0); |
| 265 | } else if (mi.return_val.type != Variant::NIL) { |
| 266 | desc = Variant::get_type_name(mi.return_val.type); |
| 267 | } else { |
| 268 | desc = "void"; |
| 269 | } |
| 270 | |
| 271 | desc += vformat(" %s(", mi.name); |
| 272 | |
| 273 | for (int64_t i = 0; i < mi.arguments.size(); ++i) { |
| 274 | PropertyInfo &arg = mi.arguments.write[i]; |
| 275 | if (i > 0) { |
| 276 | desc += ", "; |
| 277 | } |
| 278 | |
| 279 | desc += arg.name; |
| 280 | |
| 281 | if (arg.type == Variant::NIL) { |
| 282 | desc += ": Variant"; |
| 283 | } else if (arg.name.contains_char(':')) { |
| 284 | desc += vformat(": %s", arg.name.get_slicec(':', 1)); |
| 285 | arg.name = arg.name.get_slicec(':', 0); |
| 286 | } else { |
| 287 | desc += vformat(": %s", Variant::get_type_name(arg.type)); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if (mi.flags & METHOD_FLAG_VARARG) { |
| 292 | desc += mi.arguments.is_empty() ? "..." : ", ..."; |
| 293 | } |
| 294 | |
| 295 | desc += ")"; |
| 296 | |
| 297 | if (mi.flags & METHOD_FLAG_VARARG) { |
| 298 | desc += " vararg"; |
| 299 | } |
| 300 | |
| 301 | if (mi.flags & METHOD_FLAG_CONST) { |
| 302 | desc += " const"; |
| 303 | } |
| 304 | |
| 305 | if (mi.flags & METHOD_FLAG_VIRTUAL) { |
| 306 | desc += " virtual"; |
| 307 | } |
| 308 | |
| 309 | item->set_text(0, desc); |
| 310 | item->set_metadata(0, name); |
| 311 | item->set_selectable(0, true); |
| 312 | |
| 313 | if (!found && !search_box->get_text().is_empty() && name.containsn(search_text)) { |
| 314 | item->select(0); |
| 315 | found = true; |
| 316 | } else if (!found && search_box->get_text().is_empty() && name == selected) { |
| 317 | item->select(0); |
| 318 | found = true; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (category && category->get_first_child() == nullptr) { |
| 323 | memdelete(category); //old category was unused |
| 324 | } |
| 325 | |
| 326 | if (found) { |
| 327 | // As we call this while adding items, defer until list is completely populated. |
| 328 | callable_mp(search_options, &Tree::scroll_to_item).call_deferred(search_options->get_selected(), true); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | get_ok_button()->set_disabled(search_options->get_selected() == nullptr); |
| 333 | } |
| 334 | |
| 335 | void PropertySelector::_confirmed() { |
| 336 | TreeItem *ti = search_options->get_selected(); |
| 337 | if (!ti) { |
| 338 | return; |
| 339 | } |
| 340 | emit_signal(SNAME("selected"), ti->get_metadata(0)); |
| 341 | hide(); |
| 342 | } |
| 343 | |
| 344 | void PropertySelector::_item_selected() { |
| 345 | help_bit->set_custom_text(String(), String(), String()); |
| 346 | |
| 347 | TreeItem *item = search_options->get_selected(); |
| 348 | get_ok_button()->set_disabled(item == nullptr); |
| 349 | |
| 350 | if (!item) { |
| 351 | return; |
| 352 | } |
| 353 | String name = item->get_metadata(0); |
| 354 | |
| 355 | String class_type; |
| 356 | if (type != Variant::NIL) { |
| 357 | class_type = Variant::get_type_name(type); |
| 358 | } else if (!base_type.is_empty()) { |
| 359 | class_type = base_type; |
| 360 | } else if (instance) { |
| 361 | class_type = instance->get_class(); |
| 362 | } |
| 363 | |
| 364 | String text; |
| 365 | while (!class_type.is_empty()) { |
| 366 | if (properties) { |
| 367 | if (ClassDB::has_property(class_type, name, true)) { |
| 368 | help_bit->parse_symbol("property|" + class_type + "|" + name); |
| 369 | break; |
| 370 | } |
| 371 | } else { |
| 372 | if (ClassDB::has_method(class_type, name, true)) { |
| 373 | help_bit->parse_symbol("method|" + class_type + "|" + name); |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // It may be from a parent class, keep looking. |
| 379 | class_type = ClassDB::get_parent_class(class_type); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void PropertySelector::_hide_requested() { |
| 384 | _cancel_pressed(); // From AcceptDialog. |
| 385 | } |
| 386 | |
| 387 | void PropertySelector::_create_subproperties(TreeItem *p_parent_item, Variant::Type p_type) { |
| 388 | switch (p_type) { |
| 389 | case Variant::VECTOR2: { |
| 390 | _create_subproperty(p_parent_item, "x", Variant::FLOAT); |
| 391 | _create_subproperty(p_parent_item, "y", Variant::FLOAT); |
| 392 | } break; |
| 393 | |
| 394 | case Variant::VECTOR2I: { |
| 395 | _create_subproperty(p_parent_item, "x", Variant::INT); |
| 396 | _create_subproperty(p_parent_item, "y", Variant::INT); |
| 397 | } break; |
| 398 | |
| 399 | case Variant::RECT2: { |
| 400 | _create_subproperty(p_parent_item, "position", Variant::VECTOR2); |
| 401 | _create_subproperty(p_parent_item, "size", Variant::VECTOR2); |
| 402 | _create_subproperty(p_parent_item, "end", Variant::VECTOR2); |
| 403 | } break; |
| 404 | |
| 405 | case Variant::RECT2I: { |
| 406 | _create_subproperty(p_parent_item, "position", Variant::VECTOR2I); |
| 407 | _create_subproperty(p_parent_item, "size", Variant::VECTOR2I); |
| 408 | _create_subproperty(p_parent_item, "end", Variant::VECTOR2I); |
| 409 | } break; |
| 410 | |
| 411 | case Variant::VECTOR3: { |
| 412 | _create_subproperty(p_parent_item, "x", Variant::FLOAT); |
| 413 | _create_subproperty(p_parent_item, "y", Variant::FLOAT); |
| 414 | _create_subproperty(p_parent_item, "z", Variant::FLOAT); |
| 415 | } break; |
| 416 | |
| 417 | case Variant::VECTOR3I: { |
| 418 | _create_subproperty(p_parent_item, "x", Variant::INT); |
| 419 | _create_subproperty(p_parent_item, "y", Variant::INT); |
| 420 | _create_subproperty(p_parent_item, "z", Variant::INT); |
| 421 | } break; |
| 422 | |
| 423 | case Variant::TRANSFORM2D: { |
| 424 | _create_subproperty(p_parent_item, "origin", Variant::VECTOR2); |
| 425 | _create_subproperty(p_parent_item, "x", Variant::VECTOR2); |
| 426 | _create_subproperty(p_parent_item, "y", Variant::VECTOR2); |
| 427 | } break; |
| 428 | |
| 429 | case Variant::VECTOR4: { |
| 430 | _create_subproperty(p_parent_item, "x", Variant::FLOAT); |
| 431 | _create_subproperty(p_parent_item, "y", Variant::FLOAT); |
| 432 | _create_subproperty(p_parent_item, "z", Variant::FLOAT); |
| 433 | _create_subproperty(p_parent_item, "w", Variant::FLOAT); |
| 434 | } break; |
| 435 | |
| 436 | case Variant::VECTOR4I: { |
| 437 | _create_subproperty(p_parent_item, "x", Variant::INT); |
| 438 | _create_subproperty(p_parent_item, "y", Variant::INT); |
| 439 | _create_subproperty(p_parent_item, "z", Variant::INT); |
| 440 | _create_subproperty(p_parent_item, "w", Variant::INT); |
| 441 | } break; |
| 442 | |
| 443 | case Variant::PLANE: { |
| 444 | _create_subproperty(p_parent_item, "x", Variant::FLOAT); |
| 445 | _create_subproperty(p_parent_item, "y", Variant::FLOAT); |
| 446 | _create_subproperty(p_parent_item, "z", Variant::FLOAT); |
| 447 | _create_subproperty(p_parent_item, "normal", Variant::VECTOR3); |
| 448 | _create_subproperty(p_parent_item, "d", Variant::FLOAT); |
| 449 | } break; |
| 450 | |
| 451 | case Variant::QUATERNION: { |
| 452 | _create_subproperty(p_parent_item, "x", Variant::FLOAT); |
| 453 | _create_subproperty(p_parent_item, "y", Variant::FLOAT); |
| 454 | _create_subproperty(p_parent_item, "z", Variant::FLOAT); |
| 455 | _create_subproperty(p_parent_item, "w", Variant::FLOAT); |
| 456 | } break; |
| 457 | |
| 458 | case Variant::AABB: { |
| 459 | _create_subproperty(p_parent_item, "position", Variant::VECTOR3); |
| 460 | _create_subproperty(p_parent_item, "size", Variant::VECTOR3); |
| 461 | _create_subproperty(p_parent_item, "end", Variant::VECTOR3); |
| 462 | } break; |
| 463 | |
| 464 | case Variant::BASIS: { |
| 465 | _create_subproperty(p_parent_item, "x", Variant::VECTOR3); |
| 466 | _create_subproperty(p_parent_item, "y", Variant::VECTOR3); |
| 467 | _create_subproperty(p_parent_item, "z", Variant::VECTOR3); |
| 468 | } break; |
| 469 | |
| 470 | case Variant::TRANSFORM3D: { |
| 471 | _create_subproperty(p_parent_item, "basis", Variant::BASIS); |
| 472 | _create_subproperty(p_parent_item, "origin", Variant::VECTOR3); |
| 473 | } break; |
| 474 | |
| 475 | case Variant::PROJECTION: { |
| 476 | _create_subproperty(p_parent_item, "x", Variant::VECTOR4); |
| 477 | _create_subproperty(p_parent_item, "y", Variant::VECTOR4); |
| 478 | _create_subproperty(p_parent_item, "z", Variant::VECTOR4); |
| 479 | _create_subproperty(p_parent_item, "w", Variant::VECTOR4); |
| 480 | } break; |
| 481 | |
| 482 | case Variant::COLOR: { |
| 483 | _create_subproperty(p_parent_item, "r", Variant::FLOAT); |
| 484 | _create_subproperty(p_parent_item, "g", Variant::FLOAT); |
| 485 | _create_subproperty(p_parent_item, "b", Variant::FLOAT); |
| 486 | _create_subproperty(p_parent_item, "a", Variant::FLOAT); |
| 487 | _create_subproperty(p_parent_item, "r8", Variant::INT); |
| 488 | _create_subproperty(p_parent_item, "g8", Variant::INT); |
| 489 | _create_subproperty(p_parent_item, "b8", Variant::INT); |
| 490 | _create_subproperty(p_parent_item, "a8", Variant::INT); |
| 491 | _create_subproperty(p_parent_item, "h", Variant::FLOAT); |
| 492 | _create_subproperty(p_parent_item, "s", Variant::FLOAT); |
| 493 | _create_subproperty(p_parent_item, "v", Variant::FLOAT); |
| 494 | } break; |
| 495 | |
| 496 | default: { |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void PropertySelector::_create_subproperty(TreeItem *p_parent_item, const String &p_name, Variant::Type p_type) { |
| 502 | if (!type_filter.is_empty() && !type_filter.has(p_type)) { |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | TreeItem *item = search_options->create_item(p_parent_item); |
| 507 | item->set_text(0, p_name); |
| 508 | item->set_metadata(0, String(p_parent_item->get_metadata(0)) + ":" + p_name); |
| 509 | item->set_icon(0, search_options->get_editor_theme_icon(Variant::get_type_name(p_type))); |
| 510 | |
| 511 | _create_subproperties(item, p_type); |
| 512 | } |
| 513 | |
| 514 | void PropertySelector::_notification(int p_what) { |
| 515 | switch (p_what) { |
| 516 | case NOTIFICATION_ENTER_TREE: { |
| 517 | connect(SceneStringName(confirmed), callable_mp(this, &PropertySelector::_confirmed)); |
| 518 | } break; |
| 519 | |
| 520 | case NOTIFICATION_EXIT_TREE: { |
| 521 | disconnect(SceneStringName(confirmed), callable_mp(this, &PropertySelector::_confirmed)); |
| 522 | } break; |
| 523 | |
| 524 | case NOTIFICATION_THEME_CHANGED: { |
| 525 | search_box->set_right_icon(get_editor_theme_icon(SNAME("Search"))); |
| 526 | } break; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | void PropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only) { |
| 531 | base_type = p_base; |
| 532 | selected = p_current; |
| 533 | type = Variant::NIL; |
| 534 | script = ObjectID(); |
| 535 | properties = false; |
| 536 | instance = nullptr; |
| 537 | virtuals_only = p_virtuals_only; |
| 538 | |
| 539 | popup_centered_ratio(0.6); |
| 540 | search_box->set_text(""); |
| 541 | search_box->grab_focus(); |
| 542 | _update_search(); |
| 543 | } |
| 544 | |
| 545 | void PropertySelector::select_method_from_script(const Ref<Script> &p_script, const String &p_current) { |
| 546 | ERR_FAIL_COND(p_script.is_null()); |
| 547 | base_type = p_script->get_instance_base_type(); |
| 548 | selected = p_current; |
| 549 | type = Variant::NIL; |
| 550 | script = p_script->get_instance_id(); |
| 551 | properties = false; |
| 552 | instance = nullptr; |
| 553 | virtuals_only = false; |
| 554 | |
| 555 | popup_centered_ratio(0.6); |
| 556 | search_box->set_text(""); |
| 557 | search_box->grab_focus(); |
| 558 | _update_search(); |
| 559 | } |
| 560 | |
| 561 | void PropertySelector::select_method_from_basic_type(Variant::Type p_type, const String &p_current) { |
| 562 | ERR_FAIL_COND(p_type == Variant::NIL); |
| 563 | base_type = ""; |
| 564 | selected = p_current; |
| 565 | type = p_type; |
| 566 | script = ObjectID(); |
| 567 | properties = false; |
| 568 | instance = nullptr; |
| 569 | virtuals_only = false; |
| 570 | |
| 571 | popup_centered_ratio(0.6); |
| 572 | search_box->set_text(""); |
| 573 | search_box->grab_focus(); |
| 574 | _update_search(); |
| 575 | } |
| 576 | |
| 577 | void PropertySelector::select_method_from_instance(Object *p_instance, const String &p_current) { |
| 578 | base_type = p_instance->get_class(); |
| 579 | selected = p_current; |
| 580 | type = Variant::NIL; |
| 581 | script = ObjectID(); |
| 582 | { |
| 583 | Ref<Script> scr = p_instance->get_script(); |
| 584 | if (scr.is_valid()) { |
| 585 | script = scr->get_instance_id(); |
| 586 | } |
| 587 | } |
| 588 | properties = false; |
| 589 | instance = nullptr; |
| 590 | virtuals_only = false; |
| 591 | |
| 592 | popup_centered_ratio(0.6); |
| 593 | search_box->set_text(""); |
| 594 | search_box->grab_focus(); |
| 595 | _update_search(); |
| 596 | } |
| 597 | |
| 598 | void PropertySelector::select_property_from_base_type(const String &p_base, const String &p_current) { |
| 599 | base_type = p_base; |
| 600 | selected = p_current; |
| 601 | type = Variant::NIL; |
| 602 | script = ObjectID(); |
| 603 | properties = true; |
| 604 | instance = nullptr; |
| 605 | virtuals_only = false; |
| 606 | |
| 607 | popup_centered_ratio(0.6); |
| 608 | search_box->set_text(""); |
| 609 | search_box->grab_focus(); |
| 610 | _update_search(); |
| 611 | } |
| 612 | |
| 613 | void PropertySelector::select_property_from_script(const Ref<Script> &p_script, const String &p_current) { |
| 614 | ERR_FAIL_COND(p_script.is_null()); |
| 615 | |
| 616 | base_type = p_script->get_instance_base_type(); |
| 617 | selected = p_current; |
| 618 | type = Variant::NIL; |
| 619 | script = p_script->get_instance_id(); |
| 620 | properties = true; |
| 621 | instance = nullptr; |
| 622 | virtuals_only = false; |
| 623 | |
| 624 | popup_centered_ratio(0.6); |
| 625 | search_box->set_text(""); |
| 626 | search_box->grab_focus(); |
| 627 | _update_search(); |
| 628 | } |
| 629 | |
| 630 | void PropertySelector::select_property_from_basic_type(Variant::Type p_type, const String &p_current) { |
| 631 | ERR_FAIL_COND(p_type == Variant::NIL); |
| 632 | base_type = ""; |
| 633 | selected = p_current; |
| 634 | type = p_type; |
| 635 | script = ObjectID(); |
| 636 | properties = true; |
| 637 | instance = nullptr; |
| 638 | virtuals_only = false; |
| 639 | |
| 640 | popup_centered_ratio(0.6); |
| 641 | search_box->set_text(""); |
| 642 | search_box->grab_focus(); |
| 643 | _update_search(); |
| 644 | } |
| 645 | |
| 646 | void PropertySelector::select_property_from_instance(Object *p_instance, const String &p_current) { |
| 647 | base_type = ""; |
| 648 | selected = p_current; |
| 649 | type = Variant::NIL; |
| 650 | script = ObjectID(); |
| 651 | properties = true; |
| 652 | instance = p_instance; |
| 653 | virtuals_only = false; |
| 654 | |
| 655 | popup_centered_ratio(0.6); |
| 656 | search_box->set_text(""); |
| 657 | search_box->grab_focus(); |
| 658 | _update_search(); |
| 659 | } |
| 660 | |
| 661 | void PropertySelector::set_type_filter(const Vector<Variant::Type> &p_type_filter) { |
| 662 | type_filter = p_type_filter; |
| 663 | } |
| 664 | |
| 665 | void PropertySelector::_bind_methods() { |
| 666 | ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name"))); |
| 667 | } |
| 668 | |
| 669 | PropertySelector::PropertySelector() { |
| 670 | VBoxContainer *vbc = memnew(VBoxContainer); |
| 671 | add_child(vbc); |
| 672 | |
| 673 | search_box = memnew(LineEdit); |
| 674 | search_box->set_accessibility_name(TTRC("Search:")); |
| 675 | search_box->set_clear_button_enabled(true); |
| 676 | search_box->connect(SceneStringName(text_changed), callable_mp(this, &PropertySelector::_text_changed)); |
| 677 | search_box->connect(SceneStringName(gui_input), callable_mp(this, &PropertySelector::_sbox_input)); |
| 678 | vbc->add_margin_child(TTRC("Search:"), search_box); |
| 679 | |
| 680 | search_options = memnew(Tree); |
| 681 | search_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 682 | search_options->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTH); |
| 683 | MarginContainer *mc = vbc->add_margin_child(TTRC("Matches:"), search_options, true); |
| 684 | mc->set_theme_type_variation("NoBorderHorizontalWindow"); |
| 685 | |
| 686 | search_options->connect("item_activated", callable_mp(this, &PropertySelector::_confirmed)); |
| 687 | search_options->connect("cell_selected", callable_mp(this, &PropertySelector::_item_selected)); |
| 688 | search_options->set_hide_root(true); |
| 689 | |
| 690 | help_bit = memnew(EditorHelpBit); |
| 691 | help_bit->set_content_height_limits(80 * EDSCALE, 80 * EDSCALE); |
| 692 | help_bit->connect("request_hide", callable_mp(this, &PropertySelector::_hide_requested)); |
| 693 | vbc->add_margin_child(TTRC("Description:"), help_bit); |
| 694 | |
| 695 | set_ok_button_text(TTRC("Open")); |
| 696 | get_ok_button()->set_disabled(true); |
| 697 | register_text_enter(search_box); |
| 698 | set_hide_on_ok(false); |
| 699 | } |