property_selector.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. methods.push_back(MethodInfo("*Script Methods"));
  198. if (script_ref->is_built_in()) {
  199. script_ref->reload(true);
  200. }
  201. script_ref->get_script_method_list(&methods);
  202. }
  203. StringName base = base_type;
  204. while (base) {
  205. methods.push_back(MethodInfo("*" + String(base)));
  206. ClassDB::get_method_list(base, &methods, true, true);
  207. base = ClassDB::get_parent_class(base);
  208. }
  209. }
  210. TreeItem *category = nullptr;
  211. bool found = false;
  212. bool script_methods = false;
  213. for (MethodInfo &mi : methods) {
  214. if (mi.name.begins_with("*")) {
  215. if (category && category->get_first_child() == nullptr) {
  216. memdelete(category); //old category was unused
  217. }
  218. category = search_options->create_item(root);
  219. category->set_text(0, mi.name.replace_first("*", ""));
  220. category->set_selectable(0, false);
  221. Ref<Texture2D> icon;
  222. script_methods = false;
  223. String rep = mi.name.replace("*", "");
  224. if (mi.name == "*Script Methods") {
  225. icon = search_options->get_editor_theme_icon(SNAME("Script"));
  226. script_methods = true;
  227. } else {
  228. icon = EditorNode::get_singleton()->get_class_icon(rep);
  229. }
  230. category->set_icon(0, icon);
  231. continue;
  232. }
  233. String name = mi.name.get_slice(":", 0);
  234. if (!script_methods && name.begins_with("_") && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  235. continue;
  236. }
  237. if (virtuals_only && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  238. continue;
  239. }
  240. if (!virtuals_only && (mi.flags & METHOD_FLAG_VIRTUAL)) {
  241. continue;
  242. }
  243. if (!search_box->get_text().is_empty() && !name.containsn(search_text)) {
  244. continue;
  245. }
  246. TreeItem *item = search_options->create_item(category ? category : root);
  247. String desc;
  248. if (mi.name.contains(":")) {
  249. desc = mi.name.get_slice(":", 1) + " ";
  250. mi.name = mi.name.get_slice(":", 0);
  251. } else if (mi.return_val.type != Variant::NIL) {
  252. desc = Variant::get_type_name(mi.return_val.type);
  253. } else {
  254. desc = "void";
  255. }
  256. desc += vformat(" %s(", mi.name);
  257. for (List<PropertyInfo>::Iterator arg_itr = mi.arguments.begin(); arg_itr != mi.arguments.end(); ++arg_itr) {
  258. if (arg_itr != mi.arguments.begin()) {
  259. desc += ", ";
  260. }
  261. desc += arg_itr->name;
  262. if (arg_itr->type == Variant::NIL) {
  263. desc += ": Variant";
  264. } else if (arg_itr->name.contains(":")) {
  265. desc += vformat(": %s", arg_itr->name.get_slice(":", 1));
  266. arg_itr->name = arg_itr->name.get_slice(":", 0);
  267. } else {
  268. desc += vformat(": %s", Variant::get_type_name(arg_itr->type));
  269. }
  270. }
  271. desc += ")";
  272. if (mi.flags & METHOD_FLAG_CONST) {
  273. desc += " const";
  274. }
  275. if (mi.flags & METHOD_FLAG_VIRTUAL) {
  276. desc += " virtual";
  277. }
  278. item->set_text(0, desc);
  279. item->set_metadata(0, name);
  280. item->set_selectable(0, true);
  281. if (!found && !search_box->get_text().is_empty() && name.containsn(search_text)) {
  282. item->select(0);
  283. found = true;
  284. }
  285. }
  286. if (category && category->get_first_child() == nullptr) {
  287. memdelete(category); //old category was unused
  288. }
  289. }
  290. get_ok_button()->set_disabled(root->get_first_child() == nullptr);
  291. }
  292. void PropertySelector::_confirmed() {
  293. TreeItem *ti = search_options->get_selected();
  294. if (!ti) {
  295. return;
  296. }
  297. emit_signal(SNAME("selected"), ti->get_metadata(0));
  298. hide();
  299. }
  300. void PropertySelector::_item_selected() {
  301. help_bit->set_custom_text(String(), String(), String());
  302. TreeItem *item = search_options->get_selected();
  303. if (!item) {
  304. return;
  305. }
  306. String name = item->get_metadata(0);
  307. String class_type;
  308. if (type != Variant::NIL) {
  309. class_type = Variant::get_type_name(type);
  310. } else if (!base_type.is_empty()) {
  311. class_type = base_type;
  312. } else if (instance) {
  313. class_type = instance->get_class();
  314. }
  315. String text;
  316. while (!class_type.is_empty()) {
  317. if (properties) {
  318. if (ClassDB::has_property(class_type, name, true)) {
  319. help_bit->parse_symbol("property|" + class_type + "|" + name);
  320. break;
  321. }
  322. } else {
  323. if (ClassDB::has_method(class_type, name, true)) {
  324. help_bit->parse_symbol("method|" + class_type + "|" + name);
  325. break;
  326. }
  327. }
  328. // It may be from a parent class, keep looking.
  329. class_type = ClassDB::get_parent_class(class_type);
  330. }
  331. }
  332. void PropertySelector::_hide_requested() {
  333. _cancel_pressed(); // From AcceptDialog.
  334. }
  335. void PropertySelector::_notification(int p_what) {
  336. switch (p_what) {
  337. case NOTIFICATION_ENTER_TREE: {
  338. connect("confirmed", callable_mp(this, &PropertySelector::_confirmed));
  339. } break;
  340. case NOTIFICATION_EXIT_TREE: {
  341. disconnect("confirmed", callable_mp(this, &PropertySelector::_confirmed));
  342. } break;
  343. }
  344. }
  345. void PropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only) {
  346. base_type = p_base;
  347. selected = p_current;
  348. type = Variant::NIL;
  349. script = ObjectID();
  350. properties = false;
  351. instance = nullptr;
  352. virtuals_only = p_virtuals_only;
  353. popup_centered_ratio(0.6);
  354. search_box->set_text("");
  355. search_box->grab_focus();
  356. _update_search();
  357. }
  358. void PropertySelector::select_method_from_script(const Ref<Script> &p_script, const String &p_current) {
  359. ERR_FAIL_COND(p_script.is_null());
  360. base_type = p_script->get_instance_base_type();
  361. selected = p_current;
  362. type = Variant::NIL;
  363. script = p_script->get_instance_id();
  364. properties = false;
  365. instance = nullptr;
  366. virtuals_only = false;
  367. popup_centered_ratio(0.6);
  368. search_box->set_text("");
  369. search_box->grab_focus();
  370. _update_search();
  371. }
  372. void PropertySelector::select_method_from_basic_type(Variant::Type p_type, const String &p_current) {
  373. ERR_FAIL_COND(p_type == Variant::NIL);
  374. base_type = "";
  375. selected = p_current;
  376. type = p_type;
  377. script = ObjectID();
  378. properties = false;
  379. instance = nullptr;
  380. virtuals_only = false;
  381. popup_centered_ratio(0.6);
  382. search_box->set_text("");
  383. search_box->grab_focus();
  384. _update_search();
  385. }
  386. void PropertySelector::select_method_from_instance(Object *p_instance, const String &p_current) {
  387. base_type = p_instance->get_class();
  388. selected = p_current;
  389. type = Variant::NIL;
  390. script = ObjectID();
  391. {
  392. Ref<Script> scr = p_instance->get_script();
  393. if (scr.is_valid()) {
  394. script = scr->get_instance_id();
  395. }
  396. }
  397. properties = false;
  398. instance = nullptr;
  399. virtuals_only = false;
  400. popup_centered_ratio(0.6);
  401. search_box->set_text("");
  402. search_box->grab_focus();
  403. _update_search();
  404. }
  405. void PropertySelector::select_property_from_base_type(const String &p_base, const String &p_current) {
  406. base_type = p_base;
  407. selected = p_current;
  408. type = Variant::NIL;
  409. script = ObjectID();
  410. properties = true;
  411. instance = nullptr;
  412. virtuals_only = false;
  413. popup_centered_ratio(0.6);
  414. search_box->set_text("");
  415. search_box->grab_focus();
  416. _update_search();
  417. }
  418. void PropertySelector::select_property_from_script(const Ref<Script> &p_script, const String &p_current) {
  419. ERR_FAIL_COND(p_script.is_null());
  420. base_type = p_script->get_instance_base_type();
  421. selected = p_current;
  422. type = Variant::NIL;
  423. script = p_script->get_instance_id();
  424. properties = true;
  425. instance = nullptr;
  426. virtuals_only = false;
  427. popup_centered_ratio(0.6);
  428. search_box->set_text("");
  429. search_box->grab_focus();
  430. _update_search();
  431. }
  432. void PropertySelector::select_property_from_basic_type(Variant::Type p_type, const String &p_current) {
  433. ERR_FAIL_COND(p_type == Variant::NIL);
  434. base_type = "";
  435. selected = p_current;
  436. type = p_type;
  437. script = ObjectID();
  438. properties = true;
  439. instance = nullptr;
  440. virtuals_only = false;
  441. popup_centered_ratio(0.6);
  442. search_box->set_text("");
  443. search_box->grab_focus();
  444. _update_search();
  445. }
  446. void PropertySelector::select_property_from_instance(Object *p_instance, const String &p_current) {
  447. base_type = "";
  448. selected = p_current;
  449. type = Variant::NIL;
  450. script = ObjectID();
  451. properties = true;
  452. instance = p_instance;
  453. virtuals_only = false;
  454. popup_centered_ratio(0.6);
  455. search_box->set_text("");
  456. search_box->grab_focus();
  457. _update_search();
  458. }
  459. void PropertySelector::set_type_filter(const Vector<Variant::Type> &p_type_filter) {
  460. type_filter = p_type_filter;
  461. }
  462. void PropertySelector::_bind_methods() {
  463. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name")));
  464. }
  465. PropertySelector::PropertySelector() {
  466. VBoxContainer *vbc = memnew(VBoxContainer);
  467. add_child(vbc);
  468. //set_child_rect(vbc);
  469. search_box = memnew(LineEdit);
  470. vbc->add_margin_child(TTR("Search:"), search_box);
  471. search_box->connect("text_changed", callable_mp(this, &PropertySelector::_text_changed));
  472. search_box->connect(SceneStringName(gui_input), callable_mp(this, &PropertySelector::_sbox_input));
  473. search_options = memnew(Tree);
  474. search_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  475. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  476. set_ok_button_text(TTR("Open"));
  477. get_ok_button()->set_disabled(true);
  478. register_text_enter(search_box);
  479. set_hide_on_ok(false);
  480. search_options->connect("item_activated", callable_mp(this, &PropertySelector::_confirmed));
  481. search_options->connect("cell_selected", callable_mp(this, &PropertySelector::_item_selected));
  482. search_options->set_hide_root(true);
  483. search_options->set_hide_folding(true);
  484. help_bit = memnew(EditorHelpBit);
  485. help_bit->set_content_height_limits(80 * EDSCALE, 80 * EDSCALE);
  486. help_bit->connect("request_hide", callable_mp(this, &PropertySelector::_hide_requested));
  487. vbc->add_margin_child(TTR("Description:"), help_bit);
  488. }