property_selector.cpp 19 KB

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