property_selector.cpp 17 KB

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