property_selector.cpp 17 KB

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