visual_script_property_selector.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*************************************************************************/
  2. /* visual_script_property_selector.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "visual_script_property_selector.h"
  31. #include "editor_scale.h"
  32. #include "modules/visual_script/visual_script.h"
  33. #include "modules/visual_script/visual_script_builtin_funcs.h"
  34. #include "modules/visual_script/visual_script_flow_control.h"
  35. #include "modules/visual_script/visual_script_func_nodes.h"
  36. #include "modules/visual_script/visual_script_nodes.h"
  37. #include "os/keyboard.h"
  38. #include "scene/main/node.h"
  39. #include "scene/main/viewport.h"
  40. void VisualScriptPropertySelector::_text_changed(const String &p_newtext) {
  41. _update_search();
  42. }
  43. void VisualScriptPropertySelector::_sbox_input(const Ref<InputEvent> &p_ie) {
  44. Ref<InputEventKey> k = p_ie;
  45. if (k.is_valid()) {
  46. switch (k->get_scancode()) {
  47. case KEY_UP:
  48. case KEY_DOWN:
  49. case KEY_PAGEUP:
  50. case KEY_PAGEDOWN: {
  51. search_options->call("_gui_input", k);
  52. search_box->accept_event();
  53. TreeItem *root = search_options->get_root();
  54. if (!root->get_children())
  55. break;
  56. TreeItem *current = search_options->get_selected();
  57. TreeItem *item = search_options->get_next_selected(root);
  58. while (item) {
  59. item->deselect(0);
  60. item = search_options->get_next_selected(item);
  61. }
  62. current->select(0);
  63. } break;
  64. }
  65. }
  66. }
  67. void VisualScriptPropertySelector::_update_search() {
  68. set_title(TTR("Search VisualScript"));
  69. search_options->clear();
  70. help_bit->set_text("");
  71. TreeItem *root = search_options->create_item();
  72. bool found = false;
  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_nocheck(base);
  93. }
  94. }
  95. TreeItem *category = NULL;
  96. Ref<Texture> type_icons[Variant::VARIANT_MAX] = {
  97. Control::get_icon("Variant", "EditorIcons"),
  98. Control::get_icon("bool", "EditorIcons"),
  99. Control::get_icon("int", "EditorIcons"),
  100. Control::get_icon("float", "EditorIcons"),
  101. Control::get_icon("String", "EditorIcons"),
  102. Control::get_icon("Vector2", "EditorIcons"),
  103. Control::get_icon("Rect2", "EditorIcons"),
  104. Control::get_icon("Vector3", "EditorIcons"),
  105. Control::get_icon("Transform2D", "EditorIcons"),
  106. Control::get_icon("Plane", "EditorIcons"),
  107. Control::get_icon("Quat", "EditorIcons"),
  108. Control::get_icon("AABB", "EditorIcons"),
  109. Control::get_icon("Basis", "EditorIcons"),
  110. Control::get_icon("Transform", "EditorIcons"),
  111. Control::get_icon("Color", "EditorIcons"),
  112. Control::get_icon("Path", "EditorIcons"),
  113. Control::get_icon("RID", "EditorIcons"),
  114. Control::get_icon("Object", "EditorIcons"),
  115. Control::get_icon("Dictionary", "EditorIcons"),
  116. Control::get_icon("Array", "EditorIcons"),
  117. Control::get_icon("PoolByteArray", "EditorIcons"),
  118. Control::get_icon("PoolIntArray", "EditorIcons"),
  119. Control::get_icon("PoolRealArray", "EditorIcons"),
  120. Control::get_icon("PoolStringArray", "EditorIcons"),
  121. Control::get_icon("PoolVector2Array", "EditorIcons"),
  122. Control::get_icon("PoolVector3Array", "EditorIcons"),
  123. Control::get_icon("PoolColorArray", "EditorIcons")
  124. };
  125. if (!seq_connect && visual_script_generic == false) {
  126. get_visual_node_names("flow_control/type_cast", Set<String>(), found, root, search_box);
  127. get_visual_node_names("functions/built_in/print", Set<String>(), found, root, search_box);
  128. get_visual_node_names("functions/by_type/" + Variant::get_type_name(type), Set<String>(), found, root, search_box);
  129. get_visual_node_names("operators/compare/", Set<String>(), found, root, search_box);
  130. if (type == Variant::INT) {
  131. get_visual_node_names("operators/bitwise/", Set<String>(), found, root, search_box);
  132. }
  133. if (type == Variant::BOOL) {
  134. get_visual_node_names("operators/logic/", Set<String>(), found, root, search_box);
  135. }
  136. if (type == Variant::BOOL || type == Variant::INT || type == Variant::REAL || type == Variant::VECTOR2 || type == Variant::VECTOR3) {
  137. get_visual_node_names("operators/math/", Set<String>(), found, root, search_box);
  138. }
  139. }
  140. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  141. if (E->get().usage == PROPERTY_USAGE_CATEGORY) {
  142. if (category && category->get_children() == NULL) {
  143. memdelete(category); //old category was unused
  144. }
  145. category = search_options->create_item(root);
  146. category->set_text(0, E->get().name);
  147. category->set_selectable(0, false);
  148. Ref<Texture> icon;
  149. if (E->get().name == "Script Variables") {
  150. icon = get_icon("Script", "EditorIcons");
  151. } else if (has_icon(E->get().name, "EditorIcons")) {
  152. icon = get_icon(E->get().name, "EditorIcons");
  153. } else {
  154. icon = get_icon("Object", "EditorIcons");
  155. }
  156. category->set_icon(0, icon);
  157. continue;
  158. }
  159. if (!(E->get().usage & PROPERTY_USAGE_EDITOR) && !(E->get().usage & PROPERTY_USAGE_SCRIPT_VARIABLE))
  160. continue;
  161. if (type_filter.size() && type_filter.find(E->get().type) == -1)
  162. continue;
  163. String get_text_raw = String(TTR("Get")) + String(" ") + E->get().name;
  164. String get_text = get_text_raw.capitalize();
  165. String set_text_raw = String(TTR("Set ")) + String(" ") + E->get().name;
  166. String set_text = set_text_raw.capitalize();
  167. String input = search_box->get_text().capitalize();
  168. if (input == String() ||
  169. get_text_raw.findn(input) != -1 ||
  170. get_text.findn(input) != -1) {
  171. TreeItem *item = search_options->create_item(category ? category : root);
  172. item->set_text(0, get_text);
  173. item->set_metadata(0, E->get().name);
  174. item->set_icon(0, type_icons[E->get().type]);
  175. item->set_metadata(1, "get");
  176. item->set_collapsed(1);
  177. item->set_selectable(0, true);
  178. item->set_selectable(1, false);
  179. item->set_selectable(2, false);
  180. item->set_metadata(2, connecting);
  181. }
  182. if (input == String() ||
  183. set_text_raw.findn(input) != -1 &&
  184. set_text.findn(input) != -1) {
  185. TreeItem *item = search_options->create_item(category ? category : root);
  186. item->set_text(0, set_text);
  187. item->set_metadata(0, E->get().name);
  188. item->set_icon(0, type_icons[E->get().type]);
  189. item->set_metadata(1, "set");
  190. item->set_selectable(0, true);
  191. item->set_selectable(1, false);
  192. item->set_selectable(2, false);
  193. item->set_metadata(2, connecting);
  194. }
  195. }
  196. if (category && category->get_children() == NULL) {
  197. memdelete(category); //old category was unused
  198. }
  199. }
  200. if (seq_connect == true && visual_script_generic == false) {
  201. String text = search_box->get_text();
  202. create_visualscript_item(String("VisualScriptCondition"), root, text, String("Condition"));
  203. create_visualscript_item(String("VisualScriptSwitch"), root, text, String("Switch"));
  204. create_visualscript_item(String("VisualScriptSequence"), root, text, String("Sequence"));
  205. create_visualscript_item(String("VisualScriptIterator"), root, text, String("Iterator"));
  206. create_visualscript_item(String("VisualScriptWhile"), root, text, String("While"));
  207. create_visualscript_item(String("VisualScriptReturn"), root, text, String("Return"));
  208. get_visual_node_names("flow_control/type_cast", Set<String>(), found, root, search_box);
  209. get_visual_node_names("functions/built_in/print", Set<String>(), found, root, search_box);
  210. }
  211. if (visual_script_generic) {
  212. get_visual_node_names("", Set<String>(), found, root, search_box);
  213. }
  214. List<MethodInfo> methods;
  215. if (type != Variant::NIL) {
  216. Variant v;
  217. Variant::CallError ce;
  218. v = Variant::construct(type, NULL, 0, ce);
  219. v.get_method_list(&methods);
  220. } else {
  221. Object *obj = ObjectDB::get_instance(script);
  222. if (Object::cast_to<Script>(obj)) {
  223. methods.push_back(MethodInfo("*Script Methods"));
  224. Object::cast_to<Script>(obj)->get_script_method_list(&methods);
  225. }
  226. StringName base = base_type;
  227. while (base) {
  228. methods.push_back(MethodInfo("*" + String(base)));
  229. ClassDB::get_method_list(base, &methods, true, true);
  230. base = ClassDB::get_parent_class_nocheck(base);
  231. }
  232. }
  233. TreeItem *category = NULL;
  234. bool script_methods = false;
  235. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  236. if (E->get().name.begins_with("*")) {
  237. if (category && category->get_children() == NULL) {
  238. memdelete(category); //old category was unused
  239. }
  240. category = search_options->create_item(root);
  241. category->set_text(0, E->get().name.replace_first("*", ""));
  242. category->set_selectable(0, false);
  243. Ref<Texture> icon;
  244. script_methods = false;
  245. print_line("name: " + E->get().name);
  246. String rep = E->get().name.replace("*", "");
  247. if (E->get().name == "*Script Methods") {
  248. icon = get_icon("Script", "EditorIcons");
  249. script_methods = true;
  250. } else if (has_icon(rep, "EditorIcons")) {
  251. icon = get_icon(rep, "EditorIcons");
  252. } else {
  253. icon = get_icon("Object", "EditorIcons");
  254. }
  255. category->set_icon(0, icon);
  256. continue;
  257. }
  258. String name = E->get().name.get_slice(":", 0);
  259. if (!script_methods && name.begins_with("_") && !(E->get().flags & METHOD_FLAG_VIRTUAL))
  260. continue;
  261. if (virtuals_only && !(E->get().flags & METHOD_FLAG_VIRTUAL))
  262. continue;
  263. if (!virtuals_only && (E->get().flags & METHOD_FLAG_VIRTUAL))
  264. continue;
  265. MethodInfo mi = E->get();
  266. String desc = mi.name.capitalize() + " (";
  267. if (search_box->get_text() != String() &&
  268. name.findn(search_box->get_text()) == -1 &&
  269. desc.findn(search_box->get_text()) == -1)
  270. continue;
  271. TreeItem *item = search_options->create_item(category ? category : root);
  272. for (int i = 0; i < mi.arguments.size(); i++) {
  273. if (i > 0)
  274. desc += ", ";
  275. if (mi.arguments[i].type == Variant::NIL)
  276. desc += "var";
  277. else if (mi.arguments[i].name.find(":") != -1) {
  278. desc += mi.arguments[i].name.get_slice(":", 1);
  279. mi.arguments[i].name = mi.arguments[i].name.get_slice(":", 0);
  280. } else
  281. desc += Variant::get_type_name(mi.arguments[i].type);
  282. }
  283. desc += ")";
  284. item->set_text(0, desc);
  285. item->set_icon(0, get_icon("MemberMethod", "EditorIcons"));
  286. item->set_metadata(0, name);
  287. item->set_selectable(0, true);
  288. item->set_metadata(1, "method");
  289. item->set_collapsed(1);
  290. item->set_selectable(1, false);
  291. item->set_selectable(2, false);
  292. item->set_metadata(2, connecting);
  293. if (category && category->get_children() == NULL) {
  294. memdelete(category); //old category was unused
  295. }
  296. }
  297. TreeItem *selected_item = search_options->search_item_text(search_box->get_text());
  298. if (!found && selected_item != NULL) {
  299. selected_item->select(0);
  300. found = true;
  301. }
  302. if (category && category->get_children() == NULL) {
  303. memdelete(category); //old category was unused
  304. }
  305. get_ok()->set_disabled(root->get_children() == NULL);
  306. }
  307. void VisualScriptPropertySelector::create_visualscript_item(const String &name, TreeItem *const root, const String &search_input, const String &text) {
  308. if (search_input == String() || text.findn(search_input) != -1) {
  309. TreeItem *item = search_options->create_item(root);
  310. item->set_text(0, text);
  311. item->set_icon(0, get_icon("VisualScript", "EditorIcons"));
  312. item->set_metadata(0, name);
  313. item->set_metadata(1, "action");
  314. item->set_selectable(0, true);
  315. item->set_collapsed(1);
  316. item->set_selectable(1, false);
  317. item->set_selectable(2, false);
  318. item->set_metadata(2, connecting);
  319. }
  320. }
  321. void VisualScriptPropertySelector::get_visual_node_names(const String &root_filter, const Set<String> &filter, bool &found, TreeItem *const root, LineEdit *const search_box) {
  322. Map<String, TreeItem *> path_cache;
  323. List<String> fnodes;
  324. VisualScriptLanguage::singleton->get_registered_node_names(&fnodes);
  325. for (List<String>::Element *E = fnodes.front(); E; E = E->next()) {
  326. if (!E->get().begins_with(root_filter)) {
  327. continue;
  328. }
  329. Vector<String> path = E->get().split("/");
  330. bool is_filter = false;
  331. for (Set<String>::Element *E = filter.front(); E; E = E->next()) {
  332. if (path.size() >= 2 && path[1].findn(E->get()) != -1) {
  333. is_filter = true;
  334. break;
  335. }
  336. }
  337. if (is_filter == true) {
  338. continue;
  339. }
  340. if (search_box->get_text() != String() && E->get().findn(search_box->get_text()) == -1) {
  341. continue;
  342. }
  343. TreeItem *item = search_options->create_item(root);
  344. VisualScriptOperator *vnode_operator = Object::cast_to<VisualScriptOperator>(*VisualScriptLanguage::singleton->create_node_from_name(E->get()));
  345. String type_name;
  346. if (vnode_operator != NULL) {
  347. String type;
  348. if (path.size() >= 2) {
  349. type = path[1];
  350. }
  351. type_name = type.capitalize() + " ";
  352. }
  353. VisualScriptFunctionCall *vnode_function_call = Object::cast_to<VisualScriptFunctionCall>(*VisualScriptLanguage::singleton->create_node_from_name(E->get()));
  354. if (vnode_function_call != NULL) {
  355. String basic_type = Variant::get_type_name(vnode_function_call->get_basic_type());
  356. type_name = basic_type.capitalize() + " ";
  357. }
  358. VisualScriptBuiltinFunc *vnode_builtin_function_call = Object::cast_to<VisualScriptBuiltinFunc>(*VisualScriptLanguage::singleton->create_node_from_name(E->get()));
  359. if (vnode_builtin_function_call != NULL) {
  360. type_name = "Builtin ";
  361. }
  362. item->set_text(0, type_name + path[path.size() - 1].capitalize());
  363. item->set_icon(0, get_icon("VisualScript", "EditorIcons"));
  364. item->set_selectable(0, true);
  365. item->set_metadata(0, E->get());
  366. item->set_selectable(0, true);
  367. item->set_metadata(1, "visualscript");
  368. item->set_selectable(1, false);
  369. item->set_selectable(2, false);
  370. item->set_metadata(2, connecting);
  371. }
  372. }
  373. void VisualScriptPropertySelector::_confirmed() {
  374. TreeItem *ti = search_options->get_selected();
  375. if (!ti)
  376. return;
  377. emit_signal("selected", ti->get_metadata(0), ti->get_metadata(1), ti->get_metadata(2));
  378. hide();
  379. }
  380. void VisualScriptPropertySelector::_item_selected() {
  381. help_bit->set_text("");
  382. TreeItem *item = search_options->get_selected();
  383. if (!item)
  384. return;
  385. String name = item->get_metadata(0);
  386. String class_type;
  387. if (type) {
  388. class_type = Variant::get_type_name(type);
  389. } else {
  390. class_type = base_type;
  391. }
  392. DocData *dd = EditorHelp::get_doc_data();
  393. String text;
  394. String at_class = class_type;
  395. while (at_class != String()) {
  396. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(at_class);
  397. if (E) {
  398. for (int i = 0; i < E->get().properties.size(); i++) {
  399. if (E->get().properties[i].name == name) {
  400. text = E->get().properties[i].description;
  401. }
  402. }
  403. }
  404. at_class = ClassDB::get_parent_class_nocheck(at_class);
  405. }
  406. at_class = class_type;
  407. while (at_class != String()) {
  408. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(at_class);
  409. if (E) {
  410. for (int i = 0; i < E->get().methods.size(); i++) {
  411. if (E->get().methods[i].name == name) {
  412. text = E->get().methods[i].description;
  413. }
  414. }
  415. }
  416. at_class = ClassDB::get_parent_class_nocheck(at_class);
  417. }
  418. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(class_type);
  419. if (E) {
  420. for (int i = 0; i < E->get().methods.size(); i++) {
  421. Vector<String> functions = name.rsplit("/", false, 1);
  422. if (E->get().methods[i].name == functions[functions.size() - 1]) {
  423. text = E->get().methods[i].description;
  424. }
  425. }
  426. }
  427. List<String> *names = memnew(List<String>);
  428. VisualScriptLanguage::singleton->get_registered_node_names(names);
  429. if (names->find(name) != NULL) {
  430. Ref<VisualScriptOperator> operator_node = VisualScriptLanguage::singleton->create_node_from_name(name);
  431. if (operator_node.is_valid()) {
  432. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(operator_node->get_class_name());
  433. if (E) {
  434. text = Variant::get_operator_name(operator_node->get_operator());
  435. }
  436. }
  437. Ref<VisualScriptTypeCast> typecast_node = VisualScriptLanguage::singleton->create_node_from_name(name);
  438. if (typecast_node.is_valid()) {
  439. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(typecast_node->get_class_name());
  440. if (E) {
  441. text = E->get().description;
  442. }
  443. }
  444. Ref<VisualScriptBuiltinFunc> builtin_node = VisualScriptLanguage::singleton->create_node_from_name(name);
  445. if (builtin_node.is_valid()) {
  446. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(builtin_node->get_class_name());
  447. if (E) {
  448. for (int i = 0; i < E->get().constants.size(); i++) {
  449. if (E->get().constants[i].value.to_int() == int(builtin_node->get_func())) {
  450. text = E->get().constants[i].description;
  451. }
  452. }
  453. }
  454. }
  455. }
  456. memdelete(names);
  457. if (text == String())
  458. return;
  459. help_bit->set_text(text);
  460. }
  461. void VisualScriptPropertySelector::_notification(int p_what) {
  462. if (p_what == NOTIFICATION_ENTER_TREE) {
  463. connect("confirmed", this, "_confirmed");
  464. }
  465. }
  466. void VisualScriptPropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, const bool p_virtuals_only, const bool p_connecting) {
  467. base_type = p_base;
  468. selected = p_current;
  469. type = Variant::NIL;
  470. script = 0;
  471. properties = false;
  472. instance = NULL;
  473. virtuals_only = p_virtuals_only;
  474. show_window(.5f);
  475. search_box->set_text("");
  476. search_box->grab_focus();
  477. connecting = p_connecting;
  478. _update_search();
  479. }
  480. void VisualScriptPropertySelector::set_type_filter(const Vector<Variant::Type> &p_type_filter) {
  481. type_filter = p_type_filter;
  482. }
  483. void VisualScriptPropertySelector::select_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only, bool p_seq_connect, const bool p_connecting) {
  484. base_type = p_base;
  485. selected = p_current;
  486. type = Variant::NIL;
  487. script = 0;
  488. properties = true;
  489. instance = NULL;
  490. virtuals_only = p_virtuals_only;
  491. show_window(.5f);
  492. search_box->set_text("");
  493. search_box->grab_focus();
  494. seq_connect = p_seq_connect;
  495. connecting = p_connecting;
  496. _update_search();
  497. }
  498. void VisualScriptPropertySelector::select_from_script(const Ref<Script> &p_script, const String &p_current, const bool p_connecting) {
  499. ERR_FAIL_COND(p_script.is_null());
  500. base_type = p_script->get_instance_base_type();
  501. selected = p_current;
  502. type = Variant::NIL;
  503. script = p_script->get_instance_id();
  504. properties = true;
  505. instance = NULL;
  506. virtuals_only = false;
  507. show_window(.5f);
  508. search_box->set_text("");
  509. search_box->grab_focus();
  510. seq_connect = false;
  511. connecting = p_connecting;
  512. _update_search();
  513. }
  514. void VisualScriptPropertySelector::select_from_basic_type(Variant::Type p_type, const String &p_current, const bool p_connecting) {
  515. ERR_FAIL_COND(p_type == Variant::NIL);
  516. base_type = "";
  517. selected = p_current;
  518. type = p_type;
  519. script = 0;
  520. properties = true;
  521. instance = NULL;
  522. virtuals_only = false;
  523. show_window(.5f);
  524. search_box->set_text("");
  525. search_box->grab_focus();
  526. seq_connect = false;
  527. connecting = p_connecting;
  528. _update_search();
  529. }
  530. void VisualScriptPropertySelector::select_from_action(const String &p_type, const String &p_current, const bool p_connecting) {
  531. base_type = p_type;
  532. selected = p_current;
  533. type = Variant::NIL;
  534. script = 0;
  535. properties = false;
  536. instance = NULL;
  537. virtuals_only = false;
  538. show_window(.5f);
  539. search_box->set_text("");
  540. search_box->grab_focus();
  541. seq_connect = true;
  542. connecting = p_connecting;
  543. _update_search();
  544. }
  545. void VisualScriptPropertySelector::select_from_instance(Object *p_instance, const String &p_current, const bool p_connecting) {
  546. base_type = "";
  547. selected = p_current;
  548. type = Variant::NIL;
  549. script = 0;
  550. properties = true;
  551. instance = p_instance;
  552. virtuals_only = false;
  553. show_window(.5f);
  554. search_box->set_text("");
  555. search_box->grab_focus();
  556. seq_connect = false;
  557. connecting = p_connecting;
  558. _update_search();
  559. }
  560. void VisualScriptPropertySelector::select_from_visual_script(const String &p_base, const bool p_connecting) {
  561. base_type = p_base;
  562. selected = "";
  563. type = Variant::NIL;
  564. script = 0;
  565. properties = true;
  566. visual_script_generic = true;
  567. instance = NULL;
  568. virtuals_only = false;
  569. show_window(.5f);
  570. search_box->set_text("");
  571. search_box->grab_focus();
  572. connecting = p_connecting;
  573. _update_search();
  574. }
  575. void VisualScriptPropertySelector::show_window(float p_screen_ratio) {
  576. Rect2 rect;
  577. Point2 window_size = get_viewport_rect().size;
  578. rect.size = (window_size * p_screen_ratio).floor();
  579. rect.size.x = rect.size.x / 1.25f;
  580. rect.position = ((window_size - rect.size) / 2.0f).floor();
  581. popup(rect);
  582. }
  583. void VisualScriptPropertySelector::_bind_methods() {
  584. ClassDB::bind_method(D_METHOD("_text_changed"), &VisualScriptPropertySelector::_text_changed);
  585. ClassDB::bind_method(D_METHOD("_confirmed"), &VisualScriptPropertySelector::_confirmed);
  586. ClassDB::bind_method(D_METHOD("_sbox_input"), &VisualScriptPropertySelector::_sbox_input);
  587. ClassDB::bind_method(D_METHOD("_item_selected"), &VisualScriptPropertySelector::_item_selected);
  588. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::STRING, "category"), PropertyInfo(Variant::BOOL, "connecting")));
  589. }
  590. VisualScriptPropertySelector::VisualScriptPropertySelector() {
  591. VBoxContainer *vbc = memnew(VBoxContainer);
  592. add_child(vbc);
  593. //set_child_rect(vbc);
  594. search_box = memnew(LineEdit);
  595. vbc->add_margin_child(TTR("Search:"), search_box);
  596. search_box->connect("text_changed", this, "_text_changed");
  597. search_box->connect("gui_input", this, "_sbox_input");
  598. search_options = memnew(Tree);
  599. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  600. get_ok()->set_text(TTR("Open"));
  601. get_ok()->set_disabled(true);
  602. register_text_enter(search_box);
  603. set_hide_on_ok(false);
  604. search_options->connect("item_activated", this, "_confirmed");
  605. search_options->connect("cell_selected", this, "_item_selected");
  606. search_options->set_hide_root(true);
  607. search_options->set_hide_folding(true);
  608. virtuals_only = false;
  609. help_bit = memnew(EditorHelpBit);
  610. vbc->add_margin_child(TTR("Description:"), help_bit);
  611. help_bit->connect("request_hide", this, "_closed");
  612. search_options->set_columns(3);
  613. search_options->set_column_expand(1, false);
  614. search_options->set_column_expand(2, false);
  615. }