property_selector.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. #include "property_selector.h"
  31. #include "core/os/keyboard.h"
  32. #include "editor/doc_tools.h"
  33. #include "editor/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/rich_text_label.h"
  38. #include "scene/gui/tree.h"
  39. void PropertySelector::_text_changed(const String &p_newtext) {
  40. _update_search();
  41. }
  42. void PropertySelector::_sbox_input(const Ref<InputEvent> &p_ie) {
  43. Ref<InputEventKey> k = p_ie;
  44. if (k.is_valid()) {
  45. switch (k->get_keycode()) {
  46. case Key::UP:
  47. case Key::DOWN:
  48. case Key::PAGEUP:
  49. case Key::PAGEDOWN: {
  50. search_options->gui_input(k);
  51. search_box->accept_event();
  52. TreeItem *root = search_options->get_root();
  53. if (!root->get_first_child()) {
  54. break;
  55. }
  56. TreeItem *current = search_options->get_selected();
  57. TreeItem *item = search_options->get_next_selected(root);
  58. while (item) {
  59. item->deselect(0);
  60. item = search_options->get_next_selected(item);
  61. }
  62. current->select(0);
  63. } break;
  64. default:
  65. break;
  66. }
  67. }
  68. }
  69. void PropertySelector::_update_search() {
  70. if (properties) {
  71. set_title(TTR("Select Property"));
  72. } else if (virtuals_only) {
  73. set_title(TTR("Select Virtual Method"));
  74. } else {
  75. set_title(TTR("Select Method"));
  76. }
  77. search_options->clear();
  78. help_bit->set_custom_text(String(), String(), String());
  79. TreeItem *root = search_options->create_item();
  80. // Allow using spaces in place of underscores in the search string (makes the search more fault-tolerant).
  81. const String search_text = search_box->get_text().replace(" ", "_");
  82. if (properties) {
  83. List<PropertyInfo> props;
  84. if (instance) {
  85. instance->get_property_list(&props, true);
  86. } else if (type != Variant::NIL) {
  87. Variant v;
  88. Callable::CallError ce;
  89. Variant::construct(type, v, nullptr, 0, ce);
  90. v.get_property_list(&props);
  91. } else {
  92. Object *obj = ObjectDB::get_instance(script);
  93. if (Object::cast_to<Script>(obj)) {
  94. props.push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CATEGORY));
  95. Object::cast_to<Script>(obj)->get_script_property_list(&props);
  96. }
  97. StringName base = base_type;
  98. while (base) {
  99. props.push_back(PropertyInfo(Variant::NIL, base, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CATEGORY));
  100. ClassDB::get_property_list(base, &props, true);
  101. base = ClassDB::get_parent_class(base);
  102. }
  103. }
  104. TreeItem *category = nullptr;
  105. bool found = false;
  106. Ref<Texture2D> type_icons[] = {
  107. search_options->get_editor_theme_icon(SNAME("Variant")),
  108. search_options->get_editor_theme_icon(SNAME("bool")),
  109. search_options->get_editor_theme_icon(SNAME("int")),
  110. search_options->get_editor_theme_icon(SNAME("float")),
  111. search_options->get_editor_theme_icon(SNAME("String")),
  112. search_options->get_editor_theme_icon(SNAME("Vector2")),
  113. search_options->get_editor_theme_icon(SNAME("Vector2i")),
  114. search_options->get_editor_theme_icon(SNAME("Rect2")),
  115. search_options->get_editor_theme_icon(SNAME("Rect2i")),
  116. search_options->get_editor_theme_icon(SNAME("Vector3")),
  117. search_options->get_editor_theme_icon(SNAME("Vector3i")),
  118. search_options->get_editor_theme_icon(SNAME("Transform2D")),
  119. search_options->get_editor_theme_icon(SNAME("Vector4")),
  120. search_options->get_editor_theme_icon(SNAME("Vector4i")),
  121. search_options->get_editor_theme_icon(SNAME("Plane")),
  122. search_options->get_editor_theme_icon(SNAME("Quaternion")),
  123. search_options->get_editor_theme_icon(SNAME("AABB")),
  124. search_options->get_editor_theme_icon(SNAME("Basis")),
  125. search_options->get_editor_theme_icon(SNAME("Transform3D")),
  126. search_options->get_editor_theme_icon(SNAME("Projection")),
  127. search_options->get_editor_theme_icon(SNAME("Color")),
  128. search_options->get_editor_theme_icon(SNAME("StringName")),
  129. search_options->get_editor_theme_icon(SNAME("NodePath")),
  130. search_options->get_editor_theme_icon(SNAME("RID")),
  131. search_options->get_editor_theme_icon(SNAME("MiniObject")),
  132. search_options->get_editor_theme_icon(SNAME("Callable")),
  133. search_options->get_editor_theme_icon(SNAME("Signal")),
  134. search_options->get_editor_theme_icon(SNAME("Dictionary")),
  135. search_options->get_editor_theme_icon(SNAME("Array")),
  136. search_options->get_editor_theme_icon(SNAME("PackedByteArray")),
  137. search_options->get_editor_theme_icon(SNAME("PackedInt32Array")),
  138. search_options->get_editor_theme_icon(SNAME("PackedInt64Array")),
  139. search_options->get_editor_theme_icon(SNAME("PackedFloat32Array")),
  140. search_options->get_editor_theme_icon(SNAME("PackedFloat64Array")),
  141. search_options->get_editor_theme_icon(SNAME("PackedStringArray")),
  142. search_options->get_editor_theme_icon(SNAME("PackedVector2Array")),
  143. search_options->get_editor_theme_icon(SNAME("PackedVector3Array")),
  144. search_options->get_editor_theme_icon(SNAME("PackedColorArray")),
  145. search_options->get_editor_theme_icon(SNAME("PackedVector4Array")),
  146. };
  147. static_assert((sizeof(type_icons) / sizeof(type_icons[0])) == Variant::VARIANT_MAX, "Number of type icons doesn't match the number of Variant types.");
  148. for (const PropertyInfo &E : props) {
  149. if (E.usage == PROPERTY_USAGE_CATEGORY) {
  150. if (category && category->get_first_child() == nullptr) {
  151. memdelete(category); //old category was unused
  152. }
  153. category = search_options->create_item(root);
  154. category->set_text(0, E.name);
  155. category->set_selectable(0, false);
  156. Ref<Texture2D> icon;
  157. if (E.name == "Script Variables") {
  158. icon = search_options->get_editor_theme_icon(SNAME("Script"));
  159. } else {
  160. icon = EditorNode::get_singleton()->get_class_icon(E.name);
  161. }
  162. category->set_icon(0, icon);
  163. continue;
  164. }
  165. if (!(E.usage & PROPERTY_USAGE_EDITOR) && !(E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE)) {
  166. continue;
  167. }
  168. if (!search_box->get_text().is_empty() && !E.name.containsn(search_text)) {
  169. continue;
  170. }
  171. if (type_filter.size() && !type_filter.has(E.type)) {
  172. continue;
  173. }
  174. TreeItem *item = search_options->create_item(category ? category : root);
  175. item->set_text(0, E.name);
  176. item->set_metadata(0, E.name);
  177. item->set_icon(0, type_icons[E.type]);
  178. if (!found && !search_box->get_text().is_empty() && E.name.containsn(search_text)) {
  179. item->select(0);
  180. found = true;
  181. }
  182. item->set_selectable(0, true);
  183. }
  184. if (category && category->get_first_child() == nullptr) {
  185. memdelete(category); //old category was unused
  186. }
  187. } else {
  188. List<MethodInfo> methods;
  189. if (type != Variant::NIL) {
  190. Variant v;
  191. Callable::CallError ce;
  192. Variant::construct(type, v, nullptr, 0, ce);
  193. v.get_method_list(&methods);
  194. } else {
  195. Ref<Script> script_ref = Object::cast_to<Script>(ObjectDB::get_instance(script));
  196. if (script_ref.is_valid()) {
  197. if (script_ref->is_built_in()) {
  198. script_ref->reload(true);
  199. }
  200. List<MethodInfo> script_methods;
  201. script_ref->get_script_method_list(&script_methods);
  202. methods.push_back(MethodInfo("*Script Methods")); // TODO: Split by inheritance.
  203. for (const MethodInfo &mi : script_methods) {
  204. if (mi.name.begins_with("@")) {
  205. // GH-92782. GDScript inline setters/getters are historically present in `get_method_list()`
  206. // and can be called using `Object.call()`. However, these functions are meant to be internal
  207. // and their names are not valid identifiers, so let's hide them from the user.
  208. continue;
  209. }
  210. methods.push_back(mi);
  211. }
  212. }
  213. StringName base = base_type;
  214. while (base) {
  215. methods.push_back(MethodInfo("*" + String(base)));
  216. ClassDB::get_method_list(base, &methods, true, true);
  217. base = ClassDB::get_parent_class(base);
  218. }
  219. }
  220. TreeItem *category = nullptr;
  221. bool found = false;
  222. bool script_methods = false;
  223. for (MethodInfo &mi : methods) {
  224. if (mi.name.begins_with("*")) {
  225. if (category && category->get_first_child() == nullptr) {
  226. memdelete(category); //old category was unused
  227. }
  228. category = search_options->create_item(root);
  229. category->set_text(0, mi.name.replace_first("*", ""));
  230. category->set_selectable(0, false);
  231. Ref<Texture2D> icon;
  232. script_methods = false;
  233. String rep = mi.name.replace("*", "");
  234. if (mi.name == "*Script Methods") {
  235. icon = search_options->get_editor_theme_icon(SNAME("Script"));
  236. script_methods = true;
  237. } else {
  238. icon = EditorNode::get_singleton()->get_class_icon(rep);
  239. }
  240. category->set_icon(0, icon);
  241. continue;
  242. }
  243. String name = mi.name.get_slice(":", 0);
  244. if (!script_methods && name.begins_with("_") && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  245. continue;
  246. }
  247. if (virtuals_only && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  248. continue;
  249. }
  250. if (!virtuals_only && (mi.flags & METHOD_FLAG_VIRTUAL)) {
  251. continue;
  252. }
  253. if (!search_box->get_text().is_empty() && !name.containsn(search_text)) {
  254. continue;
  255. }
  256. TreeItem *item = search_options->create_item(category ? category : root);
  257. String desc;
  258. if (mi.name.contains(":")) {
  259. desc = mi.name.get_slice(":", 1) + " ";
  260. mi.name = mi.name.get_slice(":", 0);
  261. } else if (mi.return_val.type != Variant::NIL) {
  262. desc = Variant::get_type_name(mi.return_val.type);
  263. } else {
  264. desc = "void";
  265. }
  266. desc += vformat(" %s(", mi.name);
  267. for (List<PropertyInfo>::Iterator arg_itr = mi.arguments.begin(); arg_itr != mi.arguments.end(); ++arg_itr) {
  268. if (arg_itr != mi.arguments.begin()) {
  269. desc += ", ";
  270. }
  271. desc += arg_itr->name;
  272. if (arg_itr->type == Variant::NIL) {
  273. desc += ": Variant";
  274. } else if (arg_itr->name.contains(":")) {
  275. desc += vformat(": %s", arg_itr->name.get_slice(":", 1));
  276. arg_itr->name = arg_itr->name.get_slice(":", 0);
  277. } else {
  278. desc += vformat(": %s", Variant::get_type_name(arg_itr->type));
  279. }
  280. }
  281. desc += ")";
  282. if (mi.flags & METHOD_FLAG_CONST) {
  283. desc += " const";
  284. }
  285. if (mi.flags & METHOD_FLAG_VIRTUAL) {
  286. desc += " virtual";
  287. }
  288. item->set_text(0, desc);
  289. item->set_metadata(0, name);
  290. item->set_selectable(0, true);
  291. if (!found && !search_box->get_text().is_empty() && name.containsn(search_text)) {
  292. item->select(0);
  293. found = true;
  294. }
  295. }
  296. if (category && category->get_first_child() == nullptr) {
  297. memdelete(category); //old category was unused
  298. }
  299. }
  300. get_ok_button()->set_disabled(root->get_first_child() == nullptr);
  301. }
  302. void PropertySelector::_confirmed() {
  303. TreeItem *ti = search_options->get_selected();
  304. if (!ti) {
  305. return;
  306. }
  307. emit_signal(SNAME("selected"), ti->get_metadata(0));
  308. hide();
  309. }
  310. void PropertySelector::_item_selected() {
  311. help_bit->set_custom_text(String(), String(), String());
  312. TreeItem *item = search_options->get_selected();
  313. if (!item) {
  314. return;
  315. }
  316. String name = item->get_metadata(0);
  317. String class_type;
  318. if (type != Variant::NIL) {
  319. class_type = Variant::get_type_name(type);
  320. } else if (!base_type.is_empty()) {
  321. class_type = base_type;
  322. } else if (instance) {
  323. class_type = instance->get_class();
  324. }
  325. String text;
  326. while (!class_type.is_empty()) {
  327. if (properties) {
  328. if (ClassDB::has_property(class_type, name, true)) {
  329. help_bit->parse_symbol("property|" + class_type + "|" + name);
  330. break;
  331. }
  332. } else {
  333. if (ClassDB::has_method(class_type, name, true)) {
  334. help_bit->parse_symbol("method|" + class_type + "|" + name);
  335. break;
  336. }
  337. }
  338. // It may be from a parent class, keep looking.
  339. class_type = ClassDB::get_parent_class(class_type);
  340. }
  341. }
  342. void PropertySelector::_hide_requested() {
  343. _cancel_pressed(); // From AcceptDialog.
  344. }
  345. void PropertySelector::_notification(int p_what) {
  346. switch (p_what) {
  347. case NOTIFICATION_ENTER_TREE: {
  348. connect(SceneStringName(confirmed), callable_mp(this, &PropertySelector::_confirmed));
  349. } break;
  350. case NOTIFICATION_EXIT_TREE: {
  351. disconnect(SceneStringName(confirmed), callable_mp(this, &PropertySelector::_confirmed));
  352. } break;
  353. }
  354. }
  355. void PropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only) {
  356. base_type = p_base;
  357. selected = p_current;
  358. type = Variant::NIL;
  359. script = ObjectID();
  360. properties = false;
  361. instance = nullptr;
  362. virtuals_only = p_virtuals_only;
  363. popup_centered_ratio(0.6);
  364. search_box->set_text("");
  365. search_box->grab_focus();
  366. _update_search();
  367. }
  368. void PropertySelector::select_method_from_script(const Ref<Script> &p_script, const String &p_current) {
  369. ERR_FAIL_COND(p_script.is_null());
  370. base_type = p_script->get_instance_base_type();
  371. selected = p_current;
  372. type = Variant::NIL;
  373. script = p_script->get_instance_id();
  374. properties = false;
  375. instance = nullptr;
  376. virtuals_only = false;
  377. popup_centered_ratio(0.6);
  378. search_box->set_text("");
  379. search_box->grab_focus();
  380. _update_search();
  381. }
  382. void PropertySelector::select_method_from_basic_type(Variant::Type p_type, const String &p_current) {
  383. ERR_FAIL_COND(p_type == Variant::NIL);
  384. base_type = "";
  385. selected = p_current;
  386. type = p_type;
  387. script = ObjectID();
  388. properties = false;
  389. instance = nullptr;
  390. virtuals_only = false;
  391. popup_centered_ratio(0.6);
  392. search_box->set_text("");
  393. search_box->grab_focus();
  394. _update_search();
  395. }
  396. void PropertySelector::select_method_from_instance(Object *p_instance, const String &p_current) {
  397. base_type = p_instance->get_class();
  398. selected = p_current;
  399. type = Variant::NIL;
  400. script = ObjectID();
  401. {
  402. Ref<Script> scr = p_instance->get_script();
  403. if (scr.is_valid()) {
  404. script = scr->get_instance_id();
  405. }
  406. }
  407. properties = false;
  408. instance = nullptr;
  409. virtuals_only = false;
  410. popup_centered_ratio(0.6);
  411. search_box->set_text("");
  412. search_box->grab_focus();
  413. _update_search();
  414. }
  415. void PropertySelector::select_property_from_base_type(const String &p_base, const String &p_current) {
  416. base_type = p_base;
  417. selected = p_current;
  418. type = Variant::NIL;
  419. script = ObjectID();
  420. properties = true;
  421. instance = nullptr;
  422. virtuals_only = false;
  423. popup_centered_ratio(0.6);
  424. search_box->set_text("");
  425. search_box->grab_focus();
  426. _update_search();
  427. }
  428. void PropertySelector::select_property_from_script(const Ref<Script> &p_script, const String &p_current) {
  429. ERR_FAIL_COND(p_script.is_null());
  430. base_type = p_script->get_instance_base_type();
  431. selected = p_current;
  432. type = Variant::NIL;
  433. script = p_script->get_instance_id();
  434. properties = true;
  435. instance = nullptr;
  436. virtuals_only = false;
  437. popup_centered_ratio(0.6);
  438. search_box->set_text("");
  439. search_box->grab_focus();
  440. _update_search();
  441. }
  442. void PropertySelector::select_property_from_basic_type(Variant::Type p_type, const String &p_current) {
  443. ERR_FAIL_COND(p_type == Variant::NIL);
  444. base_type = "";
  445. selected = p_current;
  446. type = p_type;
  447. script = ObjectID();
  448. properties = true;
  449. instance = nullptr;
  450. virtuals_only = false;
  451. popup_centered_ratio(0.6);
  452. search_box->set_text("");
  453. search_box->grab_focus();
  454. _update_search();
  455. }
  456. void PropertySelector::select_property_from_instance(Object *p_instance, const String &p_current) {
  457. base_type = "";
  458. selected = p_current;
  459. type = Variant::NIL;
  460. script = ObjectID();
  461. properties = true;
  462. instance = p_instance;
  463. virtuals_only = false;
  464. popup_centered_ratio(0.6);
  465. search_box->set_text("");
  466. search_box->grab_focus();
  467. _update_search();
  468. }
  469. void PropertySelector::set_type_filter(const Vector<Variant::Type> &p_type_filter) {
  470. type_filter = p_type_filter;
  471. }
  472. void PropertySelector::_bind_methods() {
  473. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name")));
  474. }
  475. PropertySelector::PropertySelector() {
  476. VBoxContainer *vbc = memnew(VBoxContainer);
  477. add_child(vbc);
  478. //set_child_rect(vbc);
  479. search_box = memnew(LineEdit);
  480. vbc->add_margin_child(TTR("Search:"), search_box);
  481. search_box->connect(SceneStringName(text_changed), callable_mp(this, &PropertySelector::_text_changed));
  482. search_box->connect(SceneStringName(gui_input), callable_mp(this, &PropertySelector::_sbox_input));
  483. search_options = memnew(Tree);
  484. search_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  485. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  486. set_ok_button_text(TTR("Open"));
  487. get_ok_button()->set_disabled(true);
  488. register_text_enter(search_box);
  489. set_hide_on_ok(false);
  490. search_options->connect("item_activated", callable_mp(this, &PropertySelector::_confirmed));
  491. search_options->connect("cell_selected", callable_mp(this, &PropertySelector::_item_selected));
  492. search_options->set_hide_root(true);
  493. search_options->set_hide_folding(true);
  494. help_bit = memnew(EditorHelpBit);
  495. help_bit->set_content_height_limits(80 * EDSCALE, 80 * EDSCALE);
  496. help_bit->connect("request_hide", callable_mp(this, &PropertySelector::_hide_requested));
  497. vbc->add_margin_child(TTR("Description:"), help_bit);
  498. }