property_selector.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*************************************************************************/
  2. /* property_selector.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor_scale.h"
  32. #include "os/keyboard.h"
  33. void PropertySelector::_text_changed(const String &p_newtext) {
  34. _update_search();
  35. }
  36. void PropertySelector::_sbox_input(const Ref<InputEvent> &p_ie) {
  37. Ref<InputEventKey> k = p_ie;
  38. if (k.is_valid()) {
  39. switch (k->get_scancode()) {
  40. case KEY_UP:
  41. case KEY_DOWN:
  42. case KEY_PAGEUP:
  43. case KEY_PAGEDOWN: {
  44. search_options->call("_gui_input", k);
  45. search_box->accept_event();
  46. TreeItem *root = search_options->get_root();
  47. if (!root->get_children())
  48. break;
  49. TreeItem *current = search_options->get_selected();
  50. TreeItem *item = search_options->get_next_selected(root);
  51. while (item) {
  52. item->deselect(0);
  53. item = search_options->get_next_selected(item);
  54. }
  55. current->select(0);
  56. } break;
  57. }
  58. }
  59. }
  60. void PropertySelector::_update_search() {
  61. if (properties)
  62. set_title(TTR("Select Property"));
  63. else if (virtuals_only)
  64. set_title(TTR("Select Virtual Method"));
  65. else
  66. set_title(TTR("Select Method"));
  67. search_options->clear();
  68. help_bit->set_text("");
  69. TreeItem *root = search_options->create_item();
  70. if (properties) {
  71. List<PropertyInfo> props;
  72. if (instance) {
  73. instance->get_property_list(&props, true);
  74. } else if (type != Variant::NIL) {
  75. Variant v;
  76. Variant::CallError ce;
  77. v = Variant::construct(type, NULL, 0, ce);
  78. v.get_property_list(&props);
  79. } else {
  80. Object *obj = ObjectDB::get_instance(script);
  81. if (Object::cast_to<Script>(obj)) {
  82. props.push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CATEGORY));
  83. Object::cast_to<Script>(obj)->get_script_property_list(&props);
  84. }
  85. StringName base = base_type;
  86. while (base) {
  87. props.push_back(PropertyInfo(Variant::NIL, base, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CATEGORY));
  88. ClassDB::get_property_list(base, &props, true);
  89. base = ClassDB::get_parent_class(base);
  90. }
  91. }
  92. TreeItem *category = NULL;
  93. bool found = false;
  94. Ref<Texture> type_icons[Variant::VARIANT_MAX] = {
  95. Control::get_icon("MiniVariant", "EditorIcons"),
  96. Control::get_icon("MiniBoolean", "EditorIcons"),
  97. Control::get_icon("MiniInteger", "EditorIcons"),
  98. Control::get_icon("MiniFloat", "EditorIcons"),
  99. Control::get_icon("MiniString", "EditorIcons"),
  100. Control::get_icon("MiniVector2", "EditorIcons"),
  101. Control::get_icon("MiniRect2", "EditorIcons"),
  102. Control::get_icon("MiniVector3", "EditorIcons"),
  103. Control::get_icon("MiniMatrix2", "EditorIcons"),
  104. Control::get_icon("MiniPlane", "EditorIcons"),
  105. Control::get_icon("MiniQuat", "EditorIcons"),
  106. Control::get_icon("MiniAabb", "EditorIcons"),
  107. Control::get_icon("MiniMatrix3", "EditorIcons"),
  108. Control::get_icon("MiniTransform", "EditorIcons"),
  109. Control::get_icon("MiniColor", "EditorIcons"),
  110. Control::get_icon("MiniPath", "EditorIcons"),
  111. Control::get_icon("MiniRid", "EditorIcons"),
  112. Control::get_icon("MiniObject", "EditorIcons"),
  113. Control::get_icon("MiniDictionary", "EditorIcons"),
  114. Control::get_icon("MiniArray", "EditorIcons"),
  115. Control::get_icon("MiniRawArray", "EditorIcons"),
  116. Control::get_icon("MiniIntArray", "EditorIcons"),
  117. Control::get_icon("MiniFloatArray", "EditorIcons"),
  118. Control::get_icon("MiniStringArray", "EditorIcons"),
  119. Control::get_icon("MiniVector2Array", "EditorIcons"),
  120. Control::get_icon("MiniVector3Array", "EditorIcons"),
  121. Control::get_icon("MiniColorArray", "EditorIcons")
  122. };
  123. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  124. if (E->get().usage == PROPERTY_USAGE_CATEGORY) {
  125. if (category && category->get_children() == NULL) {
  126. memdelete(category); //old category was unused
  127. }
  128. category = search_options->create_item(root);
  129. category->set_text(0, E->get().name);
  130. category->set_selectable(0, false);
  131. Ref<Texture> icon;
  132. if (E->get().name == "Script Variables") {
  133. icon = get_icon("Script", "EditorIcons");
  134. } else if (has_icon(E->get().name, "EditorIcons")) {
  135. icon = get_icon(E->get().name, "EditorIcons");
  136. } else {
  137. icon = get_icon("Object", "EditorIcons");
  138. }
  139. category->set_icon(0, icon);
  140. continue;
  141. }
  142. if (!(E->get().usage & PROPERTY_USAGE_EDITOR) && !(E->get().usage & PROPERTY_USAGE_SCRIPT_VARIABLE))
  143. continue;
  144. if (search_box->get_text() != String() && E->get().name.find(search_box->get_text()) == -1)
  145. continue;
  146. TreeItem *item = search_options->create_item(category ? category : root);
  147. item->set_text(0, E->get().name);
  148. item->set_metadata(0, E->get().name);
  149. item->set_icon(0, type_icons[E->get().type]);
  150. if (!found && search_box->get_text() != String() && E->get().name.find(search_box->get_text()) != -1) {
  151. item->select(0);
  152. found = true;
  153. }
  154. item->set_selectable(0, true);
  155. }
  156. if (category && category->get_children() == NULL) {
  157. memdelete(category); //old category was unused
  158. }
  159. } else {
  160. List<MethodInfo> methods;
  161. if (type != Variant::NIL) {
  162. Variant v;
  163. Variant::CallError ce;
  164. v = Variant::construct(type, NULL, 0, ce);
  165. v.get_method_list(&methods);
  166. } else {
  167. Object *obj = ObjectDB::get_instance(script);
  168. if (Object::cast_to<Script>(obj)) {
  169. methods.push_back(MethodInfo("*Script Methods"));
  170. Object::cast_to<Script>(obj)->get_script_method_list(&methods);
  171. }
  172. StringName base = base_type;
  173. while (base) {
  174. methods.push_back(MethodInfo("*" + String(base)));
  175. ClassDB::get_method_list(base, &methods, true, true);
  176. base = ClassDB::get_parent_class(base);
  177. }
  178. }
  179. TreeItem *category = NULL;
  180. bool found = false;
  181. bool script_methods = false;
  182. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  183. if (E->get().name.begins_with("*")) {
  184. if (category && category->get_children() == NULL) {
  185. memdelete(category); //old category was unused
  186. }
  187. category = search_options->create_item(root);
  188. category->set_text(0, E->get().name.replace_first("*", ""));
  189. category->set_selectable(0, false);
  190. Ref<Texture> icon;
  191. script_methods = false;
  192. print_line("name: " + E->get().name);
  193. String rep = E->get().name.replace("*", "");
  194. if (E->get().name == "*Script Methods") {
  195. icon = get_icon("Script", "EditorIcons");
  196. script_methods = true;
  197. } else if (has_icon(rep, "EditorIcons")) {
  198. icon = get_icon(rep, "EditorIcons");
  199. } else {
  200. icon = get_icon("Object", "EditorIcons");
  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.find(search_box->get_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. desc += " " + mi.name + " ( ";
  225. for (int i = 0; i < mi.arguments.size(); i++) {
  226. if (i > 0)
  227. desc += ", ";
  228. if (mi.arguments[i].type == Variant::NIL)
  229. desc += "var ";
  230. else if (mi.arguments[i].name.find(":") != -1) {
  231. desc += mi.arguments[i].name.get_slice(":", 1) + " ";
  232. mi.arguments[i].name = mi.arguments[i].name.get_slice(":", 0);
  233. } else
  234. desc += Variant::get_type_name(mi.arguments[i].type) + " ";
  235. desc += mi.arguments[i].name;
  236. }
  237. desc += " )";
  238. if (E->get().flags & METHOD_FLAG_CONST)
  239. desc += " const";
  240. if (E->get().flags & METHOD_FLAG_VIRTUAL)
  241. desc += " virtual";
  242. item->set_text(0, desc);
  243. item->set_metadata(0, name);
  244. item->set_selectable(0, true);
  245. if (!found && search_box->get_text() != String() && name.find(search_box->get_text()) != -1) {
  246. item->select(0);
  247. found = true;
  248. }
  249. }
  250. if (category && category->get_children() == NULL) {
  251. memdelete(category); //old category was unused
  252. }
  253. }
  254. get_ok()->set_disabled(root->get_children() == NULL);
  255. }
  256. void PropertySelector::_confirmed() {
  257. TreeItem *ti = search_options->get_selected();
  258. if (!ti)
  259. return;
  260. emit_signal("selected", ti->get_metadata(0));
  261. hide();
  262. }
  263. void PropertySelector::_item_selected() {
  264. help_bit->set_text("");
  265. TreeItem *item = search_options->get_selected();
  266. if (!item)
  267. return;
  268. String name = item->get_metadata(0);
  269. String class_type;
  270. if (type) {
  271. class_type = Variant::get_type_name(type);
  272. } else {
  273. class_type = base_type;
  274. }
  275. DocData *dd = EditorHelp::get_doc_data();
  276. String text;
  277. if (properties) {
  278. String at_class = class_type;
  279. while (at_class != String()) {
  280. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(at_class);
  281. if (E) {
  282. for (int i = 0; i < E->get().properties.size(); i++) {
  283. if (E->get().properties[i].name == name) {
  284. text = E->get().properties[i].description;
  285. }
  286. }
  287. }
  288. at_class = ClassDB::get_parent_class(at_class);
  289. }
  290. if (text == String()) {
  291. StringName setter;
  292. StringName type;
  293. if (ClassDB::get_setter_and_type_for_property(class_type, name, type, setter)) {
  294. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(type);
  295. if (E) {
  296. for (int i = 0; i < E->get().methods.size(); i++) {
  297. if (E->get().methods[i].name == setter.operator String()) {
  298. text = E->get().methods[i].description;
  299. }
  300. }
  301. }
  302. }
  303. }
  304. } else {
  305. String at_class = class_type;
  306. while (at_class != String()) {
  307. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(at_class);
  308. if (E) {
  309. for (int i = 0; i < E->get().methods.size(); i++) {
  310. if (E->get().methods[i].name == name) {
  311. text = E->get().methods[i].description;
  312. }
  313. }
  314. }
  315. at_class = ClassDB::get_parent_class(at_class);
  316. }
  317. }
  318. if (text == String())
  319. return;
  320. help_bit->set_text(text);
  321. }
  322. void PropertySelector::_notification(int p_what) {
  323. if (p_what == NOTIFICATION_ENTER_TREE) {
  324. connect("confirmed", this, "_confirmed");
  325. }
  326. }
  327. void PropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only) {
  328. base_type = p_base;
  329. selected = p_current;
  330. type = Variant::NIL;
  331. script = 0;
  332. properties = false;
  333. instance = NULL;
  334. virtuals_only = p_virtuals_only;
  335. popup_centered_ratio(0.6);
  336. search_box->set_text("");
  337. search_box->grab_focus();
  338. _update_search();
  339. }
  340. void PropertySelector::select_method_from_script(const Ref<Script> &p_script, const String &p_current) {
  341. ERR_FAIL_COND(p_script.is_null());
  342. base_type = p_script->get_instance_base_type();
  343. selected = p_current;
  344. type = Variant::NIL;
  345. script = p_script->get_instance_id();
  346. properties = false;
  347. instance = NULL;
  348. virtuals_only = false;
  349. popup_centered_ratio(0.6);
  350. search_box->set_text("");
  351. search_box->grab_focus();
  352. _update_search();
  353. }
  354. void PropertySelector::select_method_from_basic_type(Variant::Type p_type, const String &p_current) {
  355. ERR_FAIL_COND(p_type == Variant::NIL);
  356. base_type = "";
  357. selected = p_current;
  358. type = p_type;
  359. script = 0;
  360. properties = false;
  361. instance = NULL;
  362. virtuals_only = false;
  363. popup_centered_ratio(0.6);
  364. search_box->set_text("");
  365. search_box->grab_focus();
  366. _update_search();
  367. }
  368. void PropertySelector::select_method_from_instance(Object *p_instance, const String &p_current) {
  369. base_type = p_instance->get_class();
  370. selected = p_current;
  371. type = Variant::NIL;
  372. script = 0;
  373. {
  374. Ref<Script> scr = p_instance->get_script();
  375. if (scr.is_valid())
  376. script = scr->get_instance_id();
  377. }
  378. properties = false;
  379. instance = NULL;
  380. virtuals_only = false;
  381. popup_centered_ratio(0.6);
  382. search_box->set_text("");
  383. search_box->grab_focus();
  384. _update_search();
  385. }
  386. void PropertySelector::select_property_from_base_type(const String &p_base, const String &p_current) {
  387. base_type = p_base;
  388. selected = p_current;
  389. type = Variant::NIL;
  390. script = 0;
  391. properties = true;
  392. instance = NULL;
  393. virtuals_only = false;
  394. popup_centered_ratio(0.6);
  395. search_box->set_text("");
  396. search_box->grab_focus();
  397. _update_search();
  398. }
  399. void PropertySelector::select_property_from_script(const Ref<Script> &p_script, const String &p_current) {
  400. ERR_FAIL_COND(p_script.is_null());
  401. base_type = p_script->get_instance_base_type();
  402. selected = p_current;
  403. type = Variant::NIL;
  404. script = p_script->get_instance_id();
  405. properties = true;
  406. instance = NULL;
  407. virtuals_only = false;
  408. popup_centered_ratio(0.6);
  409. search_box->set_text("");
  410. search_box->grab_focus();
  411. _update_search();
  412. }
  413. void PropertySelector::select_property_from_basic_type(Variant::Type p_type, const String &p_current) {
  414. ERR_FAIL_COND(p_type == Variant::NIL);
  415. base_type = "";
  416. selected = p_current;
  417. type = p_type;
  418. script = 0;
  419. properties = true;
  420. instance = NULL;
  421. virtuals_only = false;
  422. popup_centered_ratio(0.6);
  423. search_box->set_text("");
  424. search_box->grab_focus();
  425. _update_search();
  426. }
  427. void PropertySelector::select_property_from_instance(Object *p_instance, const String &p_current) {
  428. base_type = "";
  429. selected = p_current;
  430. type = Variant::NIL;
  431. script = 0;
  432. properties = true;
  433. instance = p_instance;
  434. virtuals_only = false;
  435. popup_centered_ratio(0.6);
  436. search_box->set_text("");
  437. search_box->grab_focus();
  438. _update_search();
  439. }
  440. void PropertySelector::_bind_methods() {
  441. ClassDB::bind_method(D_METHOD("_text_changed"), &PropertySelector::_text_changed);
  442. ClassDB::bind_method(D_METHOD("_confirmed"), &PropertySelector::_confirmed);
  443. ClassDB::bind_method(D_METHOD("_sbox_input"), &PropertySelector::_sbox_input);
  444. ClassDB::bind_method(D_METHOD("_item_selected"), &PropertySelector::_item_selected);
  445. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name")));
  446. }
  447. PropertySelector::PropertySelector() {
  448. VBoxContainer *vbc = memnew(VBoxContainer);
  449. add_child(vbc);
  450. //set_child_rect(vbc);
  451. search_box = memnew(LineEdit);
  452. vbc->add_margin_child(TTR("Search:"), search_box);
  453. search_box->connect("text_changed", this, "_text_changed");
  454. search_box->connect("gui_input", this, "_sbox_input");
  455. search_options = memnew(Tree);
  456. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  457. get_ok()->set_text(TTR("Open"));
  458. get_ok()->set_disabled(true);
  459. register_text_enter(search_box);
  460. set_hide_on_ok(false);
  461. search_options->connect("item_activated", this, "_confirmed");
  462. search_options->connect("cell_selected", this, "_item_selected");
  463. search_options->set_hide_root(true);
  464. search_options->set_hide_folding(true);
  465. virtuals_only = false;
  466. help_bit = memnew(EditorHelpBit);
  467. vbc->add_margin_child(TTR("Description:"), help_bit);
  468. help_bit->connect("request_hide", this, "_closed");
  469. }