property_selector.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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/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_text("");
  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[Variant::VARIANT_MAX] = {
  107. search_options->get_theme_icon(SNAME("Variant"), SNAME("EditorIcons")),
  108. search_options->get_theme_icon(SNAME("bool"), SNAME("EditorIcons")),
  109. search_options->get_theme_icon(SNAME("int"), SNAME("EditorIcons")),
  110. search_options->get_theme_icon(SNAME("float"), SNAME("EditorIcons")),
  111. search_options->get_theme_icon(SNAME("String"), SNAME("EditorIcons")),
  112. search_options->get_theme_icon(SNAME("Vector2"), SNAME("EditorIcons")),
  113. search_options->get_theme_icon(SNAME("Vector2i"), SNAME("EditorIcons")),
  114. search_options->get_theme_icon(SNAME("Rect2"), SNAME("EditorIcons")),
  115. search_options->get_theme_icon(SNAME("Rect2i"), SNAME("EditorIcons")),
  116. search_options->get_theme_icon(SNAME("Vector3"), SNAME("EditorIcons")),
  117. search_options->get_theme_icon(SNAME("Vector3i"), SNAME("EditorIcons")),
  118. search_options->get_theme_icon(SNAME("Transform2D"), SNAME("EditorIcons")),
  119. search_options->get_theme_icon(SNAME("Plane"), SNAME("EditorIcons")),
  120. search_options->get_theme_icon(SNAME("Quaternion"), SNAME("EditorIcons")),
  121. search_options->get_theme_icon(SNAME("AABB"), SNAME("EditorIcons")),
  122. search_options->get_theme_icon(SNAME("Basis"), SNAME("EditorIcons")),
  123. search_options->get_theme_icon(SNAME("Transform3D"), SNAME("EditorIcons")),
  124. search_options->get_theme_icon(SNAME("Color"), SNAME("EditorIcons")),
  125. search_options->get_theme_icon(SNAME("StringName"), SNAME("EditorIcons")),
  126. search_options->get_theme_icon(SNAME("NodePath"), SNAME("EditorIcons")),
  127. search_options->get_theme_icon(SNAME("RID"), SNAME("EditorIcons")),
  128. search_options->get_theme_icon(SNAME("MiniObject"), SNAME("EditorIcons")),
  129. search_options->get_theme_icon(SNAME("Callable"), SNAME("EditorIcons")),
  130. search_options->get_theme_icon(SNAME("Signal"), SNAME("EditorIcons")),
  131. search_options->get_theme_icon(SNAME("Dictionary"), SNAME("EditorIcons")),
  132. search_options->get_theme_icon(SNAME("Array"), SNAME("EditorIcons")),
  133. search_options->get_theme_icon(SNAME("PackedByteArray"), SNAME("EditorIcons")),
  134. search_options->get_theme_icon(SNAME("PackedInt32Array"), SNAME("EditorIcons")),
  135. search_options->get_theme_icon(SNAME("PackedInt64Array"), SNAME("EditorIcons")),
  136. search_options->get_theme_icon(SNAME("PackedFloat32Array"), SNAME("EditorIcons")),
  137. search_options->get_theme_icon(SNAME("PackedFloat64Array"), SNAME("EditorIcons")),
  138. search_options->get_theme_icon(SNAME("PackedStringArray"), SNAME("EditorIcons")),
  139. search_options->get_theme_icon(SNAME("PackedVector2Array"), SNAME("EditorIcons")),
  140. search_options->get_theme_icon(SNAME("PackedVector3Array"), SNAME("EditorIcons")),
  141. search_options->get_theme_icon(SNAME("PackedColorArray"), SNAME("EditorIcons"))
  142. };
  143. for (const PropertyInfo &E : props) {
  144. if (E.usage == PROPERTY_USAGE_CATEGORY) {
  145. if (category && category->get_first_child() == nullptr) {
  146. memdelete(category); //old category was unused
  147. }
  148. category = search_options->create_item(root);
  149. category->set_text(0, E.name);
  150. category->set_selectable(0, false);
  151. Ref<Texture2D> icon;
  152. if (E.name == "Script Variables") {
  153. icon = search_options->get_theme_icon(SNAME("Script"), SNAME("EditorIcons"));
  154. } else {
  155. icon = EditorNode::get_singleton()->get_class_icon(E.name);
  156. }
  157. category->set_icon(0, icon);
  158. continue;
  159. }
  160. if (!(E.usage & PROPERTY_USAGE_EDITOR) && !(E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE)) {
  161. continue;
  162. }
  163. if (!search_box->get_text().is_empty() && E.name.findn(search_text) == -1) {
  164. continue;
  165. }
  166. if (type_filter.size() && !type_filter.has(E.type)) {
  167. continue;
  168. }
  169. TreeItem *item = search_options->create_item(category ? category : root);
  170. item->set_text(0, E.name);
  171. item->set_metadata(0, E.name);
  172. item->set_icon(0, type_icons[E.type]);
  173. if (!found && !search_box->get_text().is_empty() && E.name.findn(search_text) != -1) {
  174. item->select(0);
  175. found = true;
  176. }
  177. item->set_selectable(0, true);
  178. }
  179. if (category && category->get_first_child() == nullptr) {
  180. memdelete(category); //old category was unused
  181. }
  182. } else {
  183. List<MethodInfo> methods;
  184. if (type != Variant::NIL) {
  185. Variant v;
  186. Callable::CallError ce;
  187. Variant::construct(type, v, nullptr, 0, ce);
  188. v.get_method_list(&methods);
  189. } else {
  190. Ref<Script> script_ref = Object::cast_to<Script>(ObjectDB::get_instance(script));
  191. if (script_ref.is_valid()) {
  192. methods.push_back(MethodInfo("*Script Methods"));
  193. if (script_ref->is_built_in()) {
  194. script_ref->reload(true);
  195. }
  196. script_ref->get_script_method_list(&methods);
  197. }
  198. StringName base = base_type;
  199. while (base) {
  200. methods.push_back(MethodInfo("*" + String(base)));
  201. ClassDB::get_method_list(base, &methods, true, true);
  202. base = ClassDB::get_parent_class(base);
  203. }
  204. }
  205. TreeItem *category = nullptr;
  206. bool found = false;
  207. bool script_methods = false;
  208. for (MethodInfo &mi : methods) {
  209. if (mi.name.begins_with("*")) {
  210. if (category && category->get_first_child() == nullptr) {
  211. memdelete(category); //old category was unused
  212. }
  213. category = search_options->create_item(root);
  214. category->set_text(0, mi.name.replace_first("*", ""));
  215. category->set_selectable(0, false);
  216. Ref<Texture2D> icon;
  217. script_methods = false;
  218. String rep = mi.name.replace("*", "");
  219. if (mi.name == "*Script Methods") {
  220. icon = search_options->get_theme_icon(SNAME("Script"), SNAME("EditorIcons"));
  221. script_methods = true;
  222. } else {
  223. icon = EditorNode::get_singleton()->get_class_icon(rep);
  224. }
  225. category->set_icon(0, icon);
  226. continue;
  227. }
  228. String name = mi.name.get_slice(":", 0);
  229. if (!script_methods && name.begins_with("_") && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  230. continue;
  231. }
  232. if (virtuals_only && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  233. continue;
  234. }
  235. if (!virtuals_only && (mi.flags & METHOD_FLAG_VIRTUAL)) {
  236. continue;
  237. }
  238. if (!search_box->get_text().is_empty() && name.findn(search_text) == -1) {
  239. continue;
  240. }
  241. TreeItem *item = search_options->create_item(category ? category : root);
  242. String desc;
  243. if (mi.name.contains(":")) {
  244. desc = mi.name.get_slice(":", 1) + " ";
  245. mi.name = mi.name.get_slice(":", 0);
  246. } else if (mi.return_val.type != Variant::NIL) {
  247. desc = Variant::get_type_name(mi.return_val.type);
  248. } else {
  249. desc = "void";
  250. }
  251. desc += vformat(" %s(", mi.name);
  252. for (int i = 0; i < mi.arguments.size(); i++) {
  253. if (i > 0) {
  254. desc += ", ";
  255. }
  256. desc += mi.arguments[i].name;
  257. if (mi.arguments[i].type == Variant::NIL) {
  258. desc += ": Variant";
  259. } else if (mi.arguments[i].name.contains(":")) {
  260. desc += vformat(": %s", mi.arguments[i].name.get_slice(":", 1));
  261. mi.arguments[i].name = mi.arguments[i].name.get_slice(":", 0);
  262. } else {
  263. desc += vformat(": %s", Variant::get_type_name(mi.arguments[i].type));
  264. }
  265. }
  266. desc += ")";
  267. if (mi.flags & METHOD_FLAG_CONST) {
  268. desc += " const";
  269. }
  270. if (mi.flags & METHOD_FLAG_VIRTUAL) {
  271. desc += " virtual";
  272. }
  273. item->set_text(0, desc);
  274. item->set_metadata(0, name);
  275. item->set_selectable(0, true);
  276. if (!found && !search_box->get_text().is_empty() && name.findn(search_text) != -1) {
  277. item->select(0);
  278. found = true;
  279. }
  280. }
  281. if (category && category->get_first_child() == nullptr) {
  282. memdelete(category); //old category was unused
  283. }
  284. }
  285. get_ok_button()->set_disabled(root->get_first_child() == nullptr);
  286. }
  287. void PropertySelector::_confirmed() {
  288. TreeItem *ti = search_options->get_selected();
  289. if (!ti) {
  290. return;
  291. }
  292. emit_signal(SNAME("selected"), ti->get_metadata(0));
  293. hide();
  294. }
  295. void PropertySelector::_item_selected() {
  296. help_bit->set_text("");
  297. TreeItem *item = search_options->get_selected();
  298. if (!item) {
  299. return;
  300. }
  301. String name = item->get_metadata(0);
  302. String class_type;
  303. if (type != Variant::NIL) {
  304. class_type = Variant::get_type_name(type);
  305. } else if (!base_type.is_empty()) {
  306. class_type = base_type;
  307. } else if (instance) {
  308. class_type = instance->get_class();
  309. }
  310. DocTools *dd = EditorHelp::get_doc_data();
  311. String text;
  312. if (properties) {
  313. while (!class_type.is_empty()) {
  314. HashMap<String, DocData::ClassDoc>::Iterator E = dd->class_list.find(class_type);
  315. if (E) {
  316. for (int i = 0; i < E->value.properties.size(); i++) {
  317. if (E->value.properties[i].name == name) {
  318. text = DTR(E->value.properties[i].description);
  319. break;
  320. }
  321. }
  322. }
  323. if (!text.is_empty()) {
  324. break;
  325. }
  326. // The property may be from a parent class, keep looking.
  327. class_type = ClassDB::get_parent_class(class_type);
  328. }
  329. } else {
  330. while (!class_type.is_empty()) {
  331. HashMap<String, DocData::ClassDoc>::Iterator E = dd->class_list.find(class_type);
  332. if (E) {
  333. for (int i = 0; i < E->value.methods.size(); i++) {
  334. if (E->value.methods[i].name == name) {
  335. text = DTR(E->value.methods[i].description);
  336. break;
  337. }
  338. }
  339. }
  340. if (!text.is_empty()) {
  341. break;
  342. }
  343. // The method may be from a parent class, keep looking.
  344. class_type = ClassDB::get_parent_class(class_type);
  345. }
  346. }
  347. if (!text.is_empty()) {
  348. // Display both property name and description, since the help bit may be displayed
  349. // far away from the location (especially if the dialog was resized to be taller).
  350. help_bit->set_text(vformat("[b]%s[/b]: %s", name, text));
  351. help_bit->get_rich_text()->set_self_modulate(Color(1, 1, 1, 1));
  352. } else {
  353. // Use nested `vformat()` as translators shouldn't interfere with BBCode tags.
  354. help_bit->set_text(vformat(TTR("No description available for %s."), vformat("[b]%s[/b]", name)));
  355. help_bit->get_rich_text()->set_self_modulate(Color(1, 1, 1, 0.5));
  356. }
  357. }
  358. void PropertySelector::_hide_requested() {
  359. _cancel_pressed(); // From AcceptDialog.
  360. }
  361. void PropertySelector::_notification(int p_what) {
  362. switch (p_what) {
  363. case NOTIFICATION_ENTER_TREE: {
  364. connect("confirmed", callable_mp(this, &PropertySelector::_confirmed));
  365. } break;
  366. case NOTIFICATION_EXIT_TREE: {
  367. disconnect("confirmed", callable_mp(this, &PropertySelector::_confirmed));
  368. } break;
  369. }
  370. }
  371. void PropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only) {
  372. base_type = p_base;
  373. selected = p_current;
  374. type = Variant::NIL;
  375. script = ObjectID();
  376. properties = false;
  377. instance = nullptr;
  378. virtuals_only = p_virtuals_only;
  379. popup_centered_ratio(0.6);
  380. search_box->set_text("");
  381. search_box->grab_focus();
  382. _update_search();
  383. }
  384. void PropertySelector::select_method_from_script(const Ref<Script> &p_script, const String &p_current) {
  385. ERR_FAIL_COND(p_script.is_null());
  386. base_type = p_script->get_instance_base_type();
  387. selected = p_current;
  388. type = Variant::NIL;
  389. script = p_script->get_instance_id();
  390. properties = false;
  391. instance = nullptr;
  392. virtuals_only = false;
  393. popup_centered_ratio(0.6);
  394. search_box->set_text("");
  395. search_box->grab_focus();
  396. _update_search();
  397. }
  398. void PropertySelector::select_method_from_basic_type(Variant::Type p_type, const String &p_current) {
  399. ERR_FAIL_COND(p_type == Variant::NIL);
  400. base_type = "";
  401. selected = p_current;
  402. type = p_type;
  403. script = ObjectID();
  404. properties = false;
  405. instance = nullptr;
  406. virtuals_only = false;
  407. popup_centered_ratio(0.6);
  408. search_box->set_text("");
  409. search_box->grab_focus();
  410. _update_search();
  411. }
  412. void PropertySelector::select_method_from_instance(Object *p_instance, const String &p_current) {
  413. base_type = p_instance->get_class();
  414. selected = p_current;
  415. type = Variant::NIL;
  416. script = ObjectID();
  417. {
  418. Ref<Script> scr = p_instance->get_script();
  419. if (scr.is_valid()) {
  420. script = scr->get_instance_id();
  421. }
  422. }
  423. properties = false;
  424. instance = nullptr;
  425. virtuals_only = false;
  426. popup_centered_ratio(0.6);
  427. search_box->set_text("");
  428. search_box->grab_focus();
  429. _update_search();
  430. }
  431. void PropertySelector::select_property_from_base_type(const String &p_base, const String &p_current) {
  432. base_type = p_base;
  433. selected = p_current;
  434. type = Variant::NIL;
  435. script = ObjectID();
  436. properties = true;
  437. instance = nullptr;
  438. virtuals_only = false;
  439. popup_centered_ratio(0.6);
  440. search_box->set_text("");
  441. search_box->grab_focus();
  442. _update_search();
  443. }
  444. void PropertySelector::select_property_from_script(const Ref<Script> &p_script, const String &p_current) {
  445. ERR_FAIL_COND(p_script.is_null());
  446. base_type = p_script->get_instance_base_type();
  447. selected = p_current;
  448. type = Variant::NIL;
  449. script = p_script->get_instance_id();
  450. properties = true;
  451. instance = nullptr;
  452. virtuals_only = false;
  453. popup_centered_ratio(0.6);
  454. search_box->set_text("");
  455. search_box->grab_focus();
  456. _update_search();
  457. }
  458. void PropertySelector::select_property_from_basic_type(Variant::Type p_type, const String &p_current) {
  459. ERR_FAIL_COND(p_type == Variant::NIL);
  460. base_type = "";
  461. selected = p_current;
  462. type = p_type;
  463. script = ObjectID();
  464. properties = true;
  465. instance = nullptr;
  466. virtuals_only = false;
  467. popup_centered_ratio(0.6);
  468. search_box->set_text("");
  469. search_box->grab_focus();
  470. _update_search();
  471. }
  472. void PropertySelector::select_property_from_instance(Object *p_instance, const String &p_current) {
  473. base_type = "";
  474. selected = p_current;
  475. type = Variant::NIL;
  476. script = ObjectID();
  477. properties = true;
  478. instance = p_instance;
  479. virtuals_only = false;
  480. popup_centered_ratio(0.6);
  481. search_box->set_text("");
  482. search_box->grab_focus();
  483. _update_search();
  484. }
  485. void PropertySelector::set_type_filter(const Vector<Variant::Type> &p_type_filter) {
  486. type_filter = p_type_filter;
  487. }
  488. void PropertySelector::_bind_methods() {
  489. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name")));
  490. }
  491. PropertySelector::PropertySelector() {
  492. VBoxContainer *vbc = memnew(VBoxContainer);
  493. add_child(vbc);
  494. //set_child_rect(vbc);
  495. search_box = memnew(LineEdit);
  496. vbc->add_margin_child(TTR("Search:"), search_box);
  497. search_box->connect("text_changed", callable_mp(this, &PropertySelector::_text_changed));
  498. search_box->connect("gui_input", callable_mp(this, &PropertySelector::_sbox_input));
  499. search_options = memnew(Tree);
  500. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  501. set_ok_button_text(TTR("Open"));
  502. get_ok_button()->set_disabled(true);
  503. register_text_enter(search_box);
  504. set_hide_on_ok(false);
  505. search_options->connect("item_activated", callable_mp(this, &PropertySelector::_confirmed));
  506. search_options->connect("cell_selected", callable_mp(this, &PropertySelector::_item_selected));
  507. search_options->set_hide_root(true);
  508. search_options->set_hide_folding(true);
  509. help_bit = memnew(EditorHelpBit);
  510. vbc->add_margin_child(TTR("Description:"), help_bit);
  511. help_bit->connect("request_hide", callable_mp(this, &PropertySelector::_hide_requested));
  512. }