visual_script_property_selector.cpp 24 KB

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