property_selector.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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->call("_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. }
  61. }
  62. }
  63. void PropertySelector::_update_search() {
  64. if (properties) {
  65. set_title(TTR("Select Property"));
  66. } else if (virtuals_only) {
  67. set_title(TTR("Select Virtual Method"));
  68. } else {
  69. set_title(TTR("Select Method"));
  70. }
  71. search_options->clear();
  72. help_bit->set_text("");
  73. TreeItem *root = search_options->create_item();
  74. // Allow using spaces in place of underscores in the search string (makes the search more fault-tolerant).
  75. const String search_text = search_box->get_text().replace(" ", "_");
  76. if (properties) {
  77. List<PropertyInfo> props;
  78. if (instance) {
  79. instance->get_property_list(&props, true);
  80. } else if (type != Variant::NIL) {
  81. Variant v;
  82. Callable::CallError ce;
  83. Variant::construct(type, v, nullptr, 0, ce);
  84. v.get_property_list(&props);
  85. } else {
  86. Object *obj = ObjectDB::get_instance(script);
  87. if (Object::cast_to<Script>(obj)) {
  88. props.push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CATEGORY));
  89. Object::cast_to<Script>(obj)->get_script_property_list(&props);
  90. }
  91. StringName base = base_type;
  92. while (base) {
  93. props.push_back(PropertyInfo(Variant::NIL, base, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CATEGORY));
  94. ClassDB::get_property_list(base, &props, true);
  95. base = ClassDB::get_parent_class(base);
  96. }
  97. }
  98. TreeItem *category = nullptr;
  99. bool found = false;
  100. Ref<Texture2D> type_icons[Variant::VARIANT_MAX] = {
  101. search_options->get_theme_icon(SNAME("Variant"), SNAME("EditorIcons")),
  102. search_options->get_theme_icon(SNAME("bool"), SNAME("EditorIcons")),
  103. search_options->get_theme_icon(SNAME("int"), SNAME("EditorIcons")),
  104. search_options->get_theme_icon(SNAME("float"), SNAME("EditorIcons")),
  105. search_options->get_theme_icon(SNAME("String"), SNAME("EditorIcons")),
  106. search_options->get_theme_icon(SNAME("Vector2"), SNAME("EditorIcons")),
  107. search_options->get_theme_icon(SNAME("Vector2i"), SNAME("EditorIcons")),
  108. search_options->get_theme_icon(SNAME("Rect2"), SNAME("EditorIcons")),
  109. search_options->get_theme_icon(SNAME("Rect2i"), SNAME("EditorIcons")),
  110. search_options->get_theme_icon(SNAME("Vector3"), SNAME("EditorIcons")),
  111. search_options->get_theme_icon(SNAME("Vector3i"), SNAME("EditorIcons")),
  112. search_options->get_theme_icon(SNAME("Transform2D"), SNAME("EditorIcons")),
  113. search_options->get_theme_icon(SNAME("Plane"), SNAME("EditorIcons")),
  114. search_options->get_theme_icon(SNAME("Quaternion"), SNAME("EditorIcons")),
  115. search_options->get_theme_icon(SNAME("AABB"), SNAME("EditorIcons")),
  116. search_options->get_theme_icon(SNAME("Basis"), SNAME("EditorIcons")),
  117. search_options->get_theme_icon(SNAME("Transform3D"), SNAME("EditorIcons")),
  118. search_options->get_theme_icon(SNAME("Color"), SNAME("EditorIcons")),
  119. search_options->get_theme_icon(SNAME("StringName"), SNAME("EditorIcons")),
  120. search_options->get_theme_icon(SNAME("NodePath"), SNAME("EditorIcons")),
  121. search_options->get_theme_icon(SNAME("RID"), SNAME("EditorIcons")),
  122. search_options->get_theme_icon(SNAME("MiniObject"), SNAME("EditorIcons")),
  123. search_options->get_theme_icon(SNAME("Callable"), SNAME("EditorIcons")),
  124. search_options->get_theme_icon(SNAME("Signal"), SNAME("EditorIcons")),
  125. search_options->get_theme_icon(SNAME("Dictionary"), SNAME("EditorIcons")),
  126. search_options->get_theme_icon(SNAME("Array"), SNAME("EditorIcons")),
  127. search_options->get_theme_icon(SNAME("PackedByteArray"), SNAME("EditorIcons")),
  128. search_options->get_theme_icon(SNAME("PackedInt32Array"), SNAME("EditorIcons")),
  129. search_options->get_theme_icon(SNAME("PackedInt64Array"), SNAME("EditorIcons")),
  130. search_options->get_theme_icon(SNAME("PackedFloat32Array"), SNAME("EditorIcons")),
  131. search_options->get_theme_icon(SNAME("PackedFloat64Array"), SNAME("EditorIcons")),
  132. search_options->get_theme_icon(SNAME("PackedStringArray"), SNAME("EditorIcons")),
  133. search_options->get_theme_icon(SNAME("PackedVector2Array"), SNAME("EditorIcons")),
  134. search_options->get_theme_icon(SNAME("PackedVector3Array"), SNAME("EditorIcons")),
  135. search_options->get_theme_icon(SNAME("PackedColorArray"), SNAME("EditorIcons"))
  136. };
  137. for (const PropertyInfo &E : props) {
  138. if (E.usage == PROPERTY_USAGE_CATEGORY) {
  139. if (category && category->get_first_child() == nullptr) {
  140. memdelete(category); //old category was unused
  141. }
  142. category = search_options->create_item(root);
  143. category->set_text(0, E.name);
  144. category->set_selectable(0, false);
  145. Ref<Texture2D> icon;
  146. if (E.name == "Script Variables") {
  147. icon = search_options->get_theme_icon(SNAME("Script"), SNAME("EditorIcons"));
  148. } else {
  149. icon = EditorNode::get_singleton()->get_class_icon(E.name);
  150. }
  151. category->set_icon(0, icon);
  152. continue;
  153. }
  154. if (!(E.usage & PROPERTY_USAGE_EDITOR) && !(E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE)) {
  155. continue;
  156. }
  157. if (search_box->get_text() != String() && E.name.findn(search_text) == -1) {
  158. continue;
  159. }
  160. if (type_filter.size() && type_filter.find(E.type) == -1) {
  161. continue;
  162. }
  163. TreeItem *item = search_options->create_item(category ? category : root);
  164. item->set_text(0, E.name);
  165. item->set_metadata(0, E.name);
  166. item->set_icon(0, type_icons[E.type]);
  167. if (!found && search_box->get_text() != String() && E.name.findn(search_text) != -1) {
  168. item->select(0);
  169. found = true;
  170. }
  171. item->set_selectable(0, true);
  172. }
  173. if (category && category->get_first_child() == nullptr) {
  174. memdelete(category); //old category was unused
  175. }
  176. } else {
  177. List<MethodInfo> methods;
  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. Object *obj = ObjectDB::get_instance(script);
  185. if (Object::cast_to<Script>(obj)) {
  186. methods.push_back(MethodInfo("*Script Methods"));
  187. Object::cast_to<Script>(obj)->get_script_method_list(&methods);
  188. }
  189. StringName base = base_type;
  190. while (base) {
  191. methods.push_back(MethodInfo("*" + String(base)));
  192. ClassDB::get_method_list(base, &methods, true, true);
  193. base = ClassDB::get_parent_class(base);
  194. }
  195. }
  196. TreeItem *category = nullptr;
  197. bool found = false;
  198. bool script_methods = false;
  199. for (MethodInfo &mi : methods) {
  200. if (mi.name.begins_with("*")) {
  201. if (category && category->get_first_child() == nullptr) {
  202. memdelete(category); //old category was unused
  203. }
  204. category = search_options->create_item(root);
  205. category->set_text(0, mi.name.replace_first("*", ""));
  206. category->set_selectable(0, false);
  207. Ref<Texture2D> icon;
  208. script_methods = false;
  209. String rep = mi.name.replace("*", "");
  210. if (mi.name == "*Script Methods") {
  211. icon = search_options->get_theme_icon(SNAME("Script"), SNAME("EditorIcons"));
  212. script_methods = true;
  213. } else {
  214. icon = EditorNode::get_singleton()->get_class_icon(rep);
  215. }
  216. category->set_icon(0, icon);
  217. continue;
  218. }
  219. String name = mi.name.get_slice(":", 0);
  220. if (!script_methods && name.begins_with("_") && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  221. continue;
  222. }
  223. if (virtuals_only && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  224. continue;
  225. }
  226. if (!virtuals_only && (mi.flags & METHOD_FLAG_VIRTUAL)) {
  227. continue;
  228. }
  229. if (search_box->get_text() != String() && name.findn(search_text) == -1) {
  230. continue;
  231. }
  232. TreeItem *item = search_options->create_item(category ? category : root);
  233. String desc;
  234. if (mi.name.find(":") != -1) {
  235. desc = mi.name.get_slice(":", 1) + " ";
  236. mi.name = mi.name.get_slice(":", 0);
  237. } else if (mi.return_val.type != Variant::NIL) {
  238. desc = Variant::get_type_name(mi.return_val.type);
  239. } else {
  240. desc = "void";
  241. }
  242. desc += vformat(" %s(", mi.name);
  243. for (int i = 0; i < mi.arguments.size(); i++) {
  244. if (i > 0) {
  245. desc += ", ";
  246. }
  247. desc += mi.arguments[i].name;
  248. if (mi.arguments[i].type == Variant::NIL) {
  249. desc += ": Variant";
  250. } else if (mi.arguments[i].name.find(":") != -1) {
  251. desc += vformat(": %s", mi.arguments[i].name.get_slice(":", 1));
  252. mi.arguments[i].name = mi.arguments[i].name.get_slice(":", 0);
  253. } else {
  254. desc += vformat(": %s", Variant::get_type_name(mi.arguments[i].type));
  255. }
  256. }
  257. desc += ")";
  258. if (mi.flags & METHOD_FLAG_CONST) {
  259. desc += " const";
  260. }
  261. if (mi.flags & METHOD_FLAG_VIRTUAL) {
  262. desc += " virtual";
  263. }
  264. item->set_text(0, desc);
  265. item->set_metadata(0, name);
  266. item->set_selectable(0, true);
  267. if (!found && search_box->get_text() != String() && name.findn(search_text) != -1) {
  268. item->select(0);
  269. found = true;
  270. }
  271. }
  272. if (category && category->get_first_child() == nullptr) {
  273. memdelete(category); //old category was unused
  274. }
  275. }
  276. get_ok_button()->set_disabled(root->get_first_child() == nullptr);
  277. }
  278. void PropertySelector::_confirmed() {
  279. TreeItem *ti = search_options->get_selected();
  280. if (!ti) {
  281. return;
  282. }
  283. emit_signal(SNAME("selected"), ti->get_metadata(0));
  284. hide();
  285. }
  286. void PropertySelector::_item_selected() {
  287. help_bit->set_text("");
  288. TreeItem *item = search_options->get_selected();
  289. if (!item) {
  290. return;
  291. }
  292. String name = item->get_metadata(0);
  293. String class_type;
  294. if (type != Variant::NIL) {
  295. class_type = Variant::get_type_name(type);
  296. } else {
  297. class_type = base_type;
  298. }
  299. DocTools *dd = EditorHelp::get_doc_data();
  300. String text;
  301. if (properties) {
  302. String at_class = class_type;
  303. while (at_class != String()) {
  304. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(at_class);
  305. if (E) {
  306. for (int i = 0; i < E->get().properties.size(); i++) {
  307. if (E->get().properties[i].name == name) {
  308. text = DTR(E->get().properties[i].description);
  309. }
  310. }
  311. }
  312. at_class = ClassDB::get_parent_class(at_class);
  313. }
  314. } else {
  315. String at_class = class_type;
  316. while (at_class != String()) {
  317. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(at_class);
  318. if (E) {
  319. for (int i = 0; i < E->get().methods.size(); i++) {
  320. if (E->get().methods[i].name == name) {
  321. text = DTR(E->get().methods[i].description);
  322. }
  323. }
  324. }
  325. at_class = ClassDB::get_parent_class(at_class);
  326. }
  327. }
  328. if (text == String()) {
  329. return;
  330. }
  331. help_bit->set_text(text);
  332. }
  333. void PropertySelector::_hide_requested() {
  334. _cancel_pressed(); // From AcceptDialog.
  335. }
  336. void PropertySelector::_notification(int p_what) {
  337. if (p_what == NOTIFICATION_ENTER_TREE) {
  338. connect("confirmed", callable_mp(this, &PropertySelector::_confirmed));
  339. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  340. disconnect("confirmed", callable_mp(this, &PropertySelector::_confirmed));
  341. }
  342. }
  343. void PropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only) {
  344. base_type = p_base;
  345. selected = p_current;
  346. type = Variant::NIL;
  347. script = ObjectID();
  348. properties = false;
  349. instance = nullptr;
  350. virtuals_only = p_virtuals_only;
  351. popup_centered_ratio(0.6);
  352. search_box->set_text("");
  353. search_box->grab_focus();
  354. _update_search();
  355. }
  356. void PropertySelector::select_method_from_script(const Ref<Script> &p_script, const String &p_current) {
  357. ERR_FAIL_COND(p_script.is_null());
  358. base_type = p_script->get_instance_base_type();
  359. selected = p_current;
  360. type = Variant::NIL;
  361. script = p_script->get_instance_id();
  362. properties = false;
  363. instance = nullptr;
  364. virtuals_only = false;
  365. popup_centered_ratio(0.6);
  366. search_box->set_text("");
  367. search_box->grab_focus();
  368. _update_search();
  369. }
  370. void PropertySelector::select_method_from_basic_type(Variant::Type p_type, const String &p_current) {
  371. ERR_FAIL_COND(p_type == Variant::NIL);
  372. base_type = "";
  373. selected = p_current;
  374. type = p_type;
  375. script = ObjectID();
  376. properties = false;
  377. instance = nullptr;
  378. virtuals_only = false;
  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_instance(Object *p_instance, const String &p_current) {
  385. base_type = p_instance->get_class();
  386. selected = p_current;
  387. type = Variant::NIL;
  388. script = ObjectID();
  389. {
  390. Ref<Script> scr = p_instance->get_script();
  391. if (scr.is_valid()) {
  392. script = scr->get_instance_id();
  393. }
  394. }
  395. properties = false;
  396. instance = nullptr;
  397. virtuals_only = false;
  398. popup_centered_ratio(0.6);
  399. search_box->set_text("");
  400. search_box->grab_focus();
  401. _update_search();
  402. }
  403. void PropertySelector::select_property_from_base_type(const String &p_base, const String &p_current) {
  404. base_type = p_base;
  405. selected = p_current;
  406. type = Variant::NIL;
  407. script = ObjectID();
  408. properties = true;
  409. instance = nullptr;
  410. virtuals_only = false;
  411. popup_centered_ratio(0.6);
  412. search_box->set_text("");
  413. search_box->grab_focus();
  414. _update_search();
  415. }
  416. void PropertySelector::select_property_from_script(const Ref<Script> &p_script, const String &p_current) {
  417. ERR_FAIL_COND(p_script.is_null());
  418. base_type = p_script->get_instance_base_type();
  419. selected = p_current;
  420. type = Variant::NIL;
  421. script = p_script->get_instance_id();
  422. properties = true;
  423. instance = nullptr;
  424. virtuals_only = false;
  425. popup_centered_ratio(0.6);
  426. search_box->set_text("");
  427. search_box->grab_focus();
  428. _update_search();
  429. }
  430. void PropertySelector::select_property_from_basic_type(Variant::Type p_type, const String &p_current) {
  431. ERR_FAIL_COND(p_type == Variant::NIL);
  432. base_type = "";
  433. selected = p_current;
  434. type = p_type;
  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_instance(Object *p_instance, const String &p_current) {
  445. base_type = "";
  446. selected = p_current;
  447. type = Variant::NIL;
  448. script = ObjectID();
  449. properties = true;
  450. instance = p_instance;
  451. virtuals_only = false;
  452. popup_centered_ratio(0.6);
  453. search_box->set_text("");
  454. search_box->grab_focus();
  455. _update_search();
  456. }
  457. void PropertySelector::set_type_filter(const Vector<Variant::Type> &p_type_filter) {
  458. type_filter = p_type_filter;
  459. }
  460. void PropertySelector::_bind_methods() {
  461. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name")));
  462. }
  463. PropertySelector::PropertySelector() {
  464. VBoxContainer *vbc = memnew(VBoxContainer);
  465. add_child(vbc);
  466. //set_child_rect(vbc);
  467. search_box = memnew(LineEdit);
  468. vbc->add_margin_child(TTR("Search:"), search_box);
  469. search_box->connect("text_changed", callable_mp(this, &PropertySelector::_text_changed));
  470. search_box->connect("gui_input", callable_mp(this, &PropertySelector::_sbox_input));
  471. search_options = memnew(Tree);
  472. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  473. get_ok_button()->set_text(TTR("Open"));
  474. get_ok_button()->set_disabled(true);
  475. register_text_enter(search_box);
  476. set_hide_on_ok(false);
  477. search_options->connect("item_activated", callable_mp(this, &PropertySelector::_confirmed));
  478. search_options->connect("cell_selected", callable_mp(this, &PropertySelector::_item_selected));
  479. search_options->set_hide_root(true);
  480. search_options->set_hide_folding(true);
  481. virtuals_only = false;
  482. help_bit = memnew(EditorHelpBit);
  483. vbc->add_margin_child(TTR("Description:"), help_bit);
  484. help_bit->connect("request_hide", callable_mp(this, &PropertySelector::_hide_requested));
  485. }