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) 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 {
  275. class_type = base_type;
  276. }
  277. DocData *dd = EditorHelp::get_doc_data();
  278. String text;
  279. if (properties) {
  280. String at_class = class_type;
  281. while (at_class != String()) {
  282. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(at_class);
  283. if (E) {
  284. for (int i = 0; i < E->get().properties.size(); i++) {
  285. if (E->get().properties[i].name == name) {
  286. text = E->get().properties[i].description;
  287. }
  288. }
  289. }
  290. at_class = ClassDB::get_parent_class(at_class);
  291. }
  292. } else {
  293. String at_class = class_type;
  294. while (at_class != String()) {
  295. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(at_class);
  296. if (E) {
  297. for (int i = 0; i < E->get().methods.size(); i++) {
  298. if (E->get().methods[i].name == name) {
  299. text = E->get().methods[i].description;
  300. }
  301. }
  302. }
  303. at_class = ClassDB::get_parent_class(at_class);
  304. }
  305. }
  306. if (text == String())
  307. return;
  308. help_bit->set_text(text);
  309. }
  310. void PropertySelector::_notification(int p_what) {
  311. if (p_what == NOTIFICATION_ENTER_TREE) {
  312. connect("confirmed", this, "_confirmed");
  313. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  314. disconnect("confirmed", this, "_confirmed");
  315. }
  316. }
  317. void PropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only) {
  318. base_type = p_base;
  319. selected = p_current;
  320. type = Variant::NIL;
  321. script = 0;
  322. properties = false;
  323. instance = NULL;
  324. virtuals_only = p_virtuals_only;
  325. popup_centered_ratio(0.6);
  326. search_box->set_text("");
  327. search_box->grab_focus();
  328. _update_search();
  329. }
  330. void PropertySelector::select_method_from_script(const Ref<Script> &p_script, const String &p_current) {
  331. ERR_FAIL_COND(p_script.is_null());
  332. base_type = p_script->get_instance_base_type();
  333. selected = p_current;
  334. type = Variant::NIL;
  335. script = p_script->get_instance_id();
  336. properties = false;
  337. instance = NULL;
  338. virtuals_only = false;
  339. popup_centered_ratio(0.6);
  340. search_box->set_text("");
  341. search_box->grab_focus();
  342. _update_search();
  343. }
  344. void PropertySelector::select_method_from_basic_type(Variant::Type p_type, const String &p_current) {
  345. ERR_FAIL_COND(p_type == Variant::NIL);
  346. base_type = "";
  347. selected = p_current;
  348. type = p_type;
  349. script = 0;
  350. properties = false;
  351. instance = NULL;
  352. virtuals_only = false;
  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_instance(Object *p_instance, const String &p_current) {
  359. base_type = p_instance->get_class();
  360. selected = p_current;
  361. type = Variant::NIL;
  362. script = 0;
  363. {
  364. Ref<Script> scr = p_instance->get_script();
  365. if (scr.is_valid())
  366. script = scr->get_instance_id();
  367. }
  368. properties = false;
  369. instance = NULL;
  370. virtuals_only = false;
  371. popup_centered_ratio(0.6);
  372. search_box->set_text("");
  373. search_box->grab_focus();
  374. _update_search();
  375. }
  376. void PropertySelector::select_property_from_base_type(const String &p_base, const String &p_current) {
  377. base_type = p_base;
  378. selected = p_current;
  379. type = Variant::NIL;
  380. script = 0;
  381. properties = true;
  382. instance = NULL;
  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_property_from_script(const Ref<Script> &p_script, const String &p_current) {
  390. ERR_FAIL_COND(p_script.is_null());
  391. base_type = p_script->get_instance_base_type();
  392. selected = p_current;
  393. type = Variant::NIL;
  394. script = p_script->get_instance_id();
  395. properties = true;
  396. instance = NULL;
  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_basic_type(Variant::Type p_type, const String &p_current) {
  404. ERR_FAIL_COND(p_type == Variant::NIL);
  405. base_type = "";
  406. selected = p_current;
  407. type = p_type;
  408. script = 0;
  409. properties = true;
  410. instance = NULL;
  411. virtuals_only = false;
  412. popup_centered_ratio(0.6);
  413. search_box->set_text("");
  414. search_box->grab_focus();
  415. _update_search();
  416. }
  417. void PropertySelector::select_property_from_instance(Object *p_instance, const String &p_current) {
  418. base_type = "";
  419. selected = p_current;
  420. type = Variant::NIL;
  421. script = 0;
  422. properties = true;
  423. instance = p_instance;
  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::set_type_filter(const Vector<Variant::Type> &p_type_filter) {
  431. type_filter = p_type_filter;
  432. }
  433. void PropertySelector::_bind_methods() {
  434. ClassDB::bind_method(D_METHOD("_text_changed"), &PropertySelector::_text_changed);
  435. ClassDB::bind_method(D_METHOD("_confirmed"), &PropertySelector::_confirmed);
  436. ClassDB::bind_method(D_METHOD("_sbox_input"), &PropertySelector::_sbox_input);
  437. ClassDB::bind_method(D_METHOD("_item_selected"), &PropertySelector::_item_selected);
  438. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name")));
  439. }
  440. PropertySelector::PropertySelector() {
  441. VBoxContainer *vbc = memnew(VBoxContainer);
  442. add_child(vbc);
  443. //set_child_rect(vbc);
  444. search_box = memnew(LineEdit);
  445. vbc->add_margin_child(TTR("Search:"), search_box);
  446. search_box->connect("text_changed", this, "_text_changed");
  447. search_box->connect("gui_input", this, "_sbox_input");
  448. search_options = memnew(Tree);
  449. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  450. get_ok()->set_text(TTR("Open"));
  451. get_ok()->set_disabled(true);
  452. register_text_enter(search_box);
  453. set_hide_on_ok(false);
  454. search_options->connect("item_activated", this, "_confirmed");
  455. search_options->connect("cell_selected", this, "_item_selected");
  456. search_options->set_hide_root(true);
  457. search_options->set_hide_folding(true);
  458. virtuals_only = false;
  459. help_bit = memnew(EditorHelpBit);
  460. vbc->add_margin_child(TTR("Description:"), help_bit);
  461. help_bit->connect("request_hide", this, "_closed");
  462. }