property_selector.cpp 17 KB

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