property_selector.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*************************************************************************/
  2. /* property_selector.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. 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. Variant::CallError ce;
  78. v = Variant::construct(type, NULL, 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 = NULL;
  94. bool found = false;
  95. Ref<Texture> type_icons[Variant::VARIANT_MAX] = {
  96. Control::get_icon("Variant", "EditorIcons"),
  97. Control::get_icon("bool", "EditorIcons"),
  98. Control::get_icon("int", "EditorIcons"),
  99. Control::get_icon("float", "EditorIcons"),
  100. Control::get_icon("String", "EditorIcons"),
  101. Control::get_icon("Vector2", "EditorIcons"),
  102. Control::get_icon("Rect2", "EditorIcons"),
  103. Control::get_icon("Vector3", "EditorIcons"),
  104. Control::get_icon("Transform2D", "EditorIcons"),
  105. Control::get_icon("Plane", "EditorIcons"),
  106. Control::get_icon("Quat", "EditorIcons"),
  107. Control::get_icon("AABB", "EditorIcons"),
  108. Control::get_icon("Basis", "EditorIcons"),
  109. Control::get_icon("Transform", "EditorIcons"),
  110. Control::get_icon("Color", "EditorIcons"),
  111. Control::get_icon("Path", "EditorIcons"),
  112. Control::get_icon("RID", "EditorIcons"),
  113. Control::get_icon("Object", "EditorIcons"),
  114. Control::get_icon("Dictionary", "EditorIcons"),
  115. Control::get_icon("Array", "EditorIcons"),
  116. Control::get_icon("PoolByteArray", "EditorIcons"),
  117. Control::get_icon("PoolIntArray", "EditorIcons"),
  118. Control::get_icon("PoolRealArray", "EditorIcons"),
  119. Control::get_icon("PoolStringArray", "EditorIcons"),
  120. Control::get_icon("PoolVector2Array", "EditorIcons"),
  121. Control::get_icon("PoolVector3Array", "EditorIcons"),
  122. Control::get_icon("PoolColorArray", "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() == NULL) {
  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<Texture> icon;
  133. if (E->get().name == "Script Variables") {
  134. icon = get_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() == NULL) {
  158. memdelete(category); //old category was unused
  159. }
  160. } else {
  161. List<MethodInfo> methods;
  162. if (type != Variant::NIL) {
  163. Variant v;
  164. Variant::CallError ce;
  165. v = Variant::construct(type, NULL, 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 = NULL;
  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() == NULL) {
  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<Texture> icon;
  192. script_methods = false;
  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 {
  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() == NULL) {
  249. memdelete(category); //old category was unused
  250. }
  251. }
  252. get_ok()->set_disabled(root->get_children() == NULL);
  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) {
  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::_notification(int p_what) {
  307. if (p_what == NOTIFICATION_ENTER_TREE) {
  308. connect("confirmed", this, "_confirmed");
  309. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  310. disconnect("confirmed", this, "_confirmed");
  311. }
  312. }
  313. void PropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only) {
  314. base_type = p_base;
  315. selected = p_current;
  316. type = Variant::NIL;
  317. script = 0;
  318. properties = false;
  319. instance = NULL;
  320. virtuals_only = p_virtuals_only;
  321. popup_centered_ratio(0.6);
  322. search_box->set_text("");
  323. search_box->grab_focus();
  324. _update_search();
  325. }
  326. void PropertySelector::select_method_from_script(const Ref<Script> &p_script, const String &p_current) {
  327. ERR_FAIL_COND(p_script.is_null());
  328. base_type = p_script->get_instance_base_type();
  329. selected = p_current;
  330. type = Variant::NIL;
  331. script = p_script->get_instance_id();
  332. properties = false;
  333. instance = NULL;
  334. virtuals_only = false;
  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_basic_type(Variant::Type p_type, const String &p_current) {
  341. ERR_FAIL_COND(p_type == Variant::NIL);
  342. base_type = "";
  343. selected = p_current;
  344. type = p_type;
  345. script = 0;
  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_instance(Object *p_instance, const String &p_current) {
  355. base_type = p_instance->get_class();
  356. selected = p_current;
  357. type = Variant::NIL;
  358. script = 0;
  359. {
  360. Ref<Script> scr = p_instance->get_script();
  361. if (scr.is_valid())
  362. script = scr->get_instance_id();
  363. }
  364. properties = false;
  365. instance = NULL;
  366. virtuals_only = false;
  367. popup_centered_ratio(0.6);
  368. search_box->set_text("");
  369. search_box->grab_focus();
  370. _update_search();
  371. }
  372. void PropertySelector::select_property_from_base_type(const String &p_base, const String &p_current) {
  373. base_type = p_base;
  374. selected = p_current;
  375. type = Variant::NIL;
  376. script = 0;
  377. properties = true;
  378. instance = NULL;
  379. virtuals_only = false;
  380. popup_centered_ratio(0.6);
  381. search_box->set_text("");
  382. search_box->grab_focus();
  383. _update_search();
  384. }
  385. void PropertySelector::select_property_from_script(const Ref<Script> &p_script, const String &p_current) {
  386. ERR_FAIL_COND(p_script.is_null());
  387. base_type = p_script->get_instance_base_type();
  388. selected = p_current;
  389. type = Variant::NIL;
  390. script = p_script->get_instance_id();
  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_basic_type(Variant::Type p_type, const String &p_current) {
  400. ERR_FAIL_COND(p_type == Variant::NIL);
  401. base_type = "";
  402. selected = p_current;
  403. type = p_type;
  404. script = 0;
  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_instance(Object *p_instance, const String &p_current) {
  414. base_type = "";
  415. selected = p_current;
  416. type = Variant::NIL;
  417. script = 0;
  418. properties = true;
  419. instance = p_instance;
  420. virtuals_only = false;
  421. popup_centered_ratio(0.6);
  422. search_box->set_text("");
  423. search_box->grab_focus();
  424. _update_search();
  425. }
  426. void PropertySelector::set_type_filter(const Vector<Variant::Type> &p_type_filter) {
  427. type_filter = p_type_filter;
  428. }
  429. void PropertySelector::_bind_methods() {
  430. ClassDB::bind_method(D_METHOD("_text_changed"), &PropertySelector::_text_changed);
  431. ClassDB::bind_method(D_METHOD("_confirmed"), &PropertySelector::_confirmed);
  432. ClassDB::bind_method(D_METHOD("_sbox_input"), &PropertySelector::_sbox_input);
  433. ClassDB::bind_method(D_METHOD("_item_selected"), &PropertySelector::_item_selected);
  434. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name")));
  435. }
  436. PropertySelector::PropertySelector() {
  437. VBoxContainer *vbc = memnew(VBoxContainer);
  438. add_child(vbc);
  439. //set_child_rect(vbc);
  440. search_box = memnew(LineEdit);
  441. vbc->add_margin_child(TTR("Search:"), search_box);
  442. search_box->connect("text_changed", this, "_text_changed");
  443. search_box->connect("gui_input", this, "_sbox_input");
  444. search_options = memnew(Tree);
  445. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  446. get_ok()->set_text(TTR("Open"));
  447. get_ok()->set_disabled(true);
  448. register_text_enter(search_box);
  449. set_hide_on_ok(false);
  450. search_options->connect("item_activated", this, "_confirmed");
  451. search_options->connect("cell_selected", this, "_item_selected");
  452. search_options->set_hide_root(true);
  453. search_options->set_hide_folding(true);
  454. virtuals_only = false;
  455. help_bit = memnew(EditorHelpBit);
  456. vbc->add_margin_child(TTR("Description:"), help_bit);
  457. help_bit->connect("request_hide", this, "_closed");
  458. }