property_selector.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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[] = {
  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. };
  146. static_assert((sizeof(type_icons) / sizeof(type_icons[0])) == Variant::VARIANT_MAX, "Number of type icons doesn't match the number of Variant types.");
  147. for (const PropertyInfo &E : props) {
  148. if (E.usage == PROPERTY_USAGE_CATEGORY) {
  149. if (category && category->get_first_child() == nullptr) {
  150. memdelete(category); //old category was unused
  151. }
  152. category = search_options->create_item(root);
  153. category->set_text(0, E.name);
  154. category->set_selectable(0, false);
  155. Ref<Texture2D> icon;
  156. if (E.name == "Script Variables") {
  157. icon = search_options->get_editor_theme_icon(SNAME("Script"));
  158. } else {
  159. icon = EditorNode::get_singleton()->get_class_icon(E.name);
  160. }
  161. category->set_icon(0, icon);
  162. continue;
  163. }
  164. if (!(E.usage & PROPERTY_USAGE_EDITOR) && !(E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE)) {
  165. continue;
  166. }
  167. if (!search_box->get_text().is_empty() && E.name.findn(search_text) == -1) {
  168. continue;
  169. }
  170. if (type_filter.size() && !type_filter.has(E.type)) {
  171. continue;
  172. }
  173. TreeItem *item = search_options->create_item(category ? category : root);
  174. item->set_text(0, E.name);
  175. item->set_metadata(0, E.name);
  176. item->set_icon(0, type_icons[E.type]);
  177. if (!found && !search_box->get_text().is_empty() && E.name.findn(search_text) != -1) {
  178. item->select(0);
  179. found = true;
  180. }
  181. item->set_selectable(0, true);
  182. }
  183. if (category && category->get_first_child() == nullptr) {
  184. memdelete(category); //old category was unused
  185. }
  186. } else {
  187. List<MethodInfo> methods;
  188. if (type != Variant::NIL) {
  189. Variant v;
  190. Callable::CallError ce;
  191. Variant::construct(type, v, nullptr, 0, ce);
  192. v.get_method_list(&methods);
  193. } else {
  194. Ref<Script> script_ref = Object::cast_to<Script>(ObjectDB::get_instance(script));
  195. if (script_ref.is_valid()) {
  196. methods.push_back(MethodInfo("*Script Methods"));
  197. if (script_ref->is_built_in()) {
  198. script_ref->reload(true);
  199. }
  200. script_ref->get_script_method_list(&methods);
  201. }
  202. StringName base = base_type;
  203. while (base) {
  204. methods.push_back(MethodInfo("*" + String(base)));
  205. ClassDB::get_method_list(base, &methods, true, true);
  206. base = ClassDB::get_parent_class(base);
  207. }
  208. }
  209. TreeItem *category = nullptr;
  210. bool found = false;
  211. bool script_methods = false;
  212. for (MethodInfo &mi : methods) {
  213. if (mi.name.begins_with("*")) {
  214. if (category && category->get_first_child() == nullptr) {
  215. memdelete(category); //old category was unused
  216. }
  217. category = search_options->create_item(root);
  218. category->set_text(0, mi.name.replace_first("*", ""));
  219. category->set_selectable(0, false);
  220. Ref<Texture2D> icon;
  221. script_methods = false;
  222. String rep = mi.name.replace("*", "");
  223. if (mi.name == "*Script Methods") {
  224. icon = search_options->get_editor_theme_icon(SNAME("Script"));
  225. script_methods = true;
  226. } else {
  227. icon = EditorNode::get_singleton()->get_class_icon(rep);
  228. }
  229. category->set_icon(0, icon);
  230. continue;
  231. }
  232. String name = mi.name.get_slice(":", 0);
  233. if (!script_methods && name.begins_with("_") && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  234. continue;
  235. }
  236. if (virtuals_only && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  237. continue;
  238. }
  239. if (!virtuals_only && (mi.flags & METHOD_FLAG_VIRTUAL)) {
  240. continue;
  241. }
  242. if (!search_box->get_text().is_empty() && name.findn(search_text) == -1) {
  243. continue;
  244. }
  245. TreeItem *item = search_options->create_item(category ? category : root);
  246. String desc;
  247. if (mi.name.contains(":")) {
  248. desc = mi.name.get_slice(":", 1) + " ";
  249. mi.name = mi.name.get_slice(":", 0);
  250. } else if (mi.return_val.type != Variant::NIL) {
  251. desc = Variant::get_type_name(mi.return_val.type);
  252. } else {
  253. desc = "void";
  254. }
  255. desc += vformat(" %s(", mi.name);
  256. for (int i = 0; i < mi.arguments.size(); i++) {
  257. if (i > 0) {
  258. desc += ", ";
  259. }
  260. desc += mi.arguments[i].name;
  261. if (mi.arguments[i].type == Variant::NIL) {
  262. desc += ": Variant";
  263. } else if (mi.arguments[i].name.contains(":")) {
  264. desc += vformat(": %s", mi.arguments[i].name.get_slice(":", 1));
  265. mi.arguments[i].name = mi.arguments[i].name.get_slice(":", 0);
  266. } else {
  267. desc += vformat(": %s", Variant::get_type_name(mi.arguments[i].type));
  268. }
  269. }
  270. desc += ")";
  271. if (mi.flags & METHOD_FLAG_CONST) {
  272. desc += " const";
  273. }
  274. if (mi.flags & METHOD_FLAG_VIRTUAL) {
  275. desc += " virtual";
  276. }
  277. item->set_text(0, desc);
  278. item->set_metadata(0, name);
  279. item->set_selectable(0, true);
  280. if (!found && !search_box->get_text().is_empty() && name.findn(search_text) != -1) {
  281. item->select(0);
  282. found = true;
  283. }
  284. }
  285. if (category && category->get_first_child() == nullptr) {
  286. memdelete(category); //old category was unused
  287. }
  288. }
  289. get_ok_button()->set_disabled(root->get_first_child() == nullptr);
  290. }
  291. void PropertySelector::_confirmed() {
  292. TreeItem *ti = search_options->get_selected();
  293. if (!ti) {
  294. return;
  295. }
  296. emit_signal(SNAME("selected"), ti->get_metadata(0));
  297. hide();
  298. }
  299. void PropertySelector::_item_selected() {
  300. help_bit->set_text("");
  301. TreeItem *item = search_options->get_selected();
  302. if (!item) {
  303. return;
  304. }
  305. String name = item->get_metadata(0);
  306. String class_type;
  307. if (type != Variant::NIL) {
  308. class_type = Variant::get_type_name(type);
  309. } else if (!base_type.is_empty()) {
  310. class_type = base_type;
  311. } else if (instance) {
  312. class_type = instance->get_class();
  313. }
  314. String text;
  315. while (!class_type.is_empty()) {
  316. text = properties ? help_bit->get_property_description(class_type, name) : help_bit->get_method_description(class_type, name);
  317. if (!text.is_empty()) {
  318. break;
  319. }
  320. // It may be from a parent class, keep looking.
  321. class_type = ClassDB::get_parent_class(class_type);
  322. }
  323. if (!text.is_empty()) {
  324. // Display both property name and description, since the help bit may be displayed
  325. // far away from the location (especially if the dialog was resized to be taller).
  326. help_bit->set_text(vformat("[b]%s[/b]: %s", name, text));
  327. help_bit->get_rich_text()->set_self_modulate(Color(1, 1, 1, 1));
  328. } else {
  329. // Use nested `vformat()` as translators shouldn't interfere with BBCode tags.
  330. help_bit->set_text(vformat(TTR("No description available for %s."), vformat("[b]%s[/b]", name)));
  331. help_bit->get_rich_text()->set_self_modulate(Color(1, 1, 1, 0.5));
  332. }
  333. }
  334. void PropertySelector::_hide_requested() {
  335. _cancel_pressed(); // From AcceptDialog.
  336. }
  337. void PropertySelector::_notification(int p_what) {
  338. switch (p_what) {
  339. case NOTIFICATION_ENTER_TREE: {
  340. connect("confirmed", callable_mp(this, &PropertySelector::_confirmed));
  341. } break;
  342. case NOTIFICATION_EXIT_TREE: {
  343. disconnect("confirmed", callable_mp(this, &PropertySelector::_confirmed));
  344. } break;
  345. }
  346. }
  347. void PropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only) {
  348. base_type = p_base;
  349. selected = p_current;
  350. type = Variant::NIL;
  351. script = ObjectID();
  352. properties = false;
  353. instance = nullptr;
  354. virtuals_only = p_virtuals_only;
  355. popup_centered_ratio(0.6);
  356. search_box->set_text("");
  357. search_box->grab_focus();
  358. _update_search();
  359. }
  360. void PropertySelector::select_method_from_script(const Ref<Script> &p_script, const String &p_current) {
  361. ERR_FAIL_COND(p_script.is_null());
  362. base_type = p_script->get_instance_base_type();
  363. selected = p_current;
  364. type = Variant::NIL;
  365. script = p_script->get_instance_id();
  366. properties = false;
  367. instance = nullptr;
  368. virtuals_only = false;
  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_basic_type(Variant::Type p_type, const String &p_current) {
  375. ERR_FAIL_COND(p_type == Variant::NIL);
  376. base_type = "";
  377. selected = p_current;
  378. type = p_type;
  379. script = ObjectID();
  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_instance(Object *p_instance, const String &p_current) {
  389. base_type = p_instance->get_class();
  390. selected = p_current;
  391. type = Variant::NIL;
  392. script = ObjectID();
  393. {
  394. Ref<Script> scr = p_instance->get_script();
  395. if (scr.is_valid()) {
  396. script = scr->get_instance_id();
  397. }
  398. }
  399. properties = false;
  400. instance = nullptr;
  401. virtuals_only = false;
  402. popup_centered_ratio(0.6);
  403. search_box->set_text("");
  404. search_box->grab_focus();
  405. _update_search();
  406. }
  407. void PropertySelector::select_property_from_base_type(const String &p_base, const String &p_current) {
  408. base_type = p_base;
  409. selected = p_current;
  410. type = Variant::NIL;
  411. script = ObjectID();
  412. properties = true;
  413. instance = nullptr;
  414. virtuals_only = false;
  415. popup_centered_ratio(0.6);
  416. search_box->set_text("");
  417. search_box->grab_focus();
  418. _update_search();
  419. }
  420. void PropertySelector::select_property_from_script(const Ref<Script> &p_script, const String &p_current) {
  421. ERR_FAIL_COND(p_script.is_null());
  422. base_type = p_script->get_instance_base_type();
  423. selected = p_current;
  424. type = Variant::NIL;
  425. script = p_script->get_instance_id();
  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_basic_type(Variant::Type p_type, const String &p_current) {
  435. ERR_FAIL_COND(p_type == Variant::NIL);
  436. base_type = "";
  437. selected = p_current;
  438. type = p_type;
  439. script = ObjectID();
  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_instance(Object *p_instance, const String &p_current) {
  449. base_type = "";
  450. selected = p_current;
  451. type = Variant::NIL;
  452. script = ObjectID();
  453. properties = true;
  454. instance = p_instance;
  455. virtuals_only = false;
  456. popup_centered_ratio(0.6);
  457. search_box->set_text("");
  458. search_box->grab_focus();
  459. _update_search();
  460. }
  461. void PropertySelector::set_type_filter(const Vector<Variant::Type> &p_type_filter) {
  462. type_filter = p_type_filter;
  463. }
  464. void PropertySelector::_bind_methods() {
  465. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name")));
  466. }
  467. PropertySelector::PropertySelector() {
  468. VBoxContainer *vbc = memnew(VBoxContainer);
  469. add_child(vbc);
  470. //set_child_rect(vbc);
  471. search_box = memnew(LineEdit);
  472. vbc->add_margin_child(TTR("Search:"), search_box);
  473. search_box->connect("text_changed", callable_mp(this, &PropertySelector::_text_changed));
  474. search_box->connect("gui_input", callable_mp(this, &PropertySelector::_sbox_input));
  475. search_options = memnew(Tree);
  476. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  477. set_ok_button_text(TTR("Open"));
  478. get_ok_button()->set_disabled(true);
  479. register_text_enter(search_box);
  480. set_hide_on_ok(false);
  481. search_options->connect("item_activated", callable_mp(this, &PropertySelector::_confirmed));
  482. search_options->connect("cell_selected", callable_mp(this, &PropertySelector::_item_selected));
  483. search_options->set_hide_root(true);
  484. search_options->set_hide_folding(true);
  485. help_bit = memnew(EditorHelpBit);
  486. vbc->add_margin_child(TTR("Description:"), help_bit);
  487. help_bit->get_rich_text()->set_fit_content(false);
  488. help_bit->get_rich_text()->set_custom_minimum_size(Size2(help_bit->get_rich_text()->get_minimum_size().x, 135 * EDSCALE));
  489. help_bit->connect("request_hide", callable_mp(this, &PropertySelector::_hide_requested));
  490. }