property_selector.cpp 17 KB

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