visual_script_property_selector.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_script_property_selector.h"
  31. #include "core/os/keyboard.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/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. StringName base = base_type;
  75. List<StringName> base_list;
  76. while (base) {
  77. base_list.push_back(base);
  78. base = ClassDB::get_parent_class_nocheck(base);
  79. }
  80. for (List<StringName>::Element *E = base_list.front(); E; E = E->next()) {
  81. List<MethodInfo> methods;
  82. List<PropertyInfo> props;
  83. TreeItem *category = NULL;
  84. Ref<Texture2D> type_icons[Variant::VARIANT_MAX] = {
  85. Control::get_icon("Variant", "EditorIcons"),
  86. Control::get_icon("bool", "EditorIcons"),
  87. Control::get_icon("int", "EditorIcons"),
  88. Control::get_icon("float", "EditorIcons"),
  89. Control::get_icon("String", "EditorIcons"),
  90. Control::get_icon("Vector2", "EditorIcons"),
  91. Control::get_icon("Rect2", "EditorIcons"),
  92. Control::get_icon("Vector3", "EditorIcons"),
  93. Control::get_icon("Transform2D", "EditorIcons"),
  94. Control::get_icon("Plane", "EditorIcons"),
  95. Control::get_icon("Quat", "EditorIcons"),
  96. Control::get_icon("AABB", "EditorIcons"),
  97. Control::get_icon("Basis", "EditorIcons"),
  98. Control::get_icon("Transform", "EditorIcons"),
  99. Control::get_icon("Color", "EditorIcons"),
  100. Control::get_icon("Path", "EditorIcons"),
  101. Control::get_icon("RID", "EditorIcons"),
  102. Control::get_icon("Object", "EditorIcons"),
  103. Control::get_icon("Dictionary", "EditorIcons"),
  104. Control::get_icon("Array", "EditorIcons"),
  105. Control::get_icon("PackedByteArray", "EditorIcons"),
  106. Control::get_icon("PackedInt32Array", "EditorIcons"),
  107. Control::get_icon("PackedFloat32Array", "EditorIcons"),
  108. Control::get_icon("PackedInt64Array", "EditorIcons"),
  109. Control::get_icon("PackedFloat64Array", "EditorIcons"),
  110. Control::get_icon("PackedStringArray", "EditorIcons"),
  111. Control::get_icon("PackedVector2Array", "EditorIcons"),
  112. Control::get_icon("PackedVector3Array", "EditorIcons"),
  113. Control::get_icon("PackedColorArray", "EditorIcons")
  114. };
  115. {
  116. String b = String(E->get());
  117. category = search_options->create_item(root);
  118. if (category) {
  119. category->set_text(0, b.replace_first("*", ""));
  120. category->set_selectable(0, false);
  121. Ref<Texture2D> icon;
  122. String rep = b.replace("*", "");
  123. icon = EditorNode::get_singleton()->get_class_icon(rep);
  124. category->set_icon(0, icon);
  125. }
  126. }
  127. if (properties || seq_connect) {
  128. if (instance) {
  129. instance->get_property_list(&props, true);
  130. } else {
  131. Object *obj = ObjectDB::get_instance(script);
  132. if (Object::cast_to<Script>(obj)) {
  133. Object::cast_to<Script>(obj)->get_script_property_list(&props);
  134. } else {
  135. ClassDB::get_property_list(E->get(), &props, true);
  136. }
  137. }
  138. for (List<PropertyInfo>::Element *F = props.front(); F; F = F->next()) {
  139. if (!(F->get().usage & PROPERTY_USAGE_EDITOR) && !(F->get().usage & PROPERTY_USAGE_SCRIPT_VARIABLE))
  140. continue;
  141. if (type_filter.size() && type_filter.find(F->get().type) == -1)
  142. continue;
  143. // capitalize() also converts underscore to space, we'll match again both possible styles
  144. String get_text_raw = String(vformat(TTR("Get %s"), F->get().name));
  145. String get_text = get_text_raw.capitalize();
  146. String set_text_raw = String(vformat(TTR("Set %s"), F->get().name));
  147. String set_text = set_text_raw.capitalize();
  148. String input = search_box->get_text().capitalize();
  149. if (input == String() || get_text_raw.findn(input) != -1 || get_text.findn(input) != -1) {
  150. TreeItem *item = search_options->create_item(category ? category : root);
  151. item->set_text(0, get_text);
  152. item->set_metadata(0, F->get().name);
  153. item->set_icon(0, type_icons[F->get().type]);
  154. item->set_metadata(1, "get");
  155. item->set_collapsed(1);
  156. item->set_selectable(0, true);
  157. item->set_selectable(1, false);
  158. item->set_selectable(2, false);
  159. item->set_metadata(2, connecting);
  160. }
  161. if (input == String() || set_text_raw.findn(input) != -1 || set_text.findn(input) != -1) {
  162. TreeItem *item = search_options->create_item(category ? category : root);
  163. item->set_text(0, set_text);
  164. item->set_metadata(0, F->get().name);
  165. item->set_icon(0, type_icons[F->get().type]);
  166. item->set_metadata(1, "set");
  167. item->set_selectable(0, true);
  168. item->set_selectable(1, false);
  169. item->set_selectable(2, false);
  170. item->set_metadata(2, connecting);
  171. }
  172. }
  173. }
  174. {
  175. if (type != Variant::NIL) {
  176. Variant v;
  177. Callable::CallError ce;
  178. v = Variant::construct(type, NULL, 0, ce);
  179. v.get_method_list(&methods);
  180. } else {
  181. Object *obj = ObjectDB::get_instance(script);
  182. if (Object::cast_to<Script>(obj)) {
  183. Object::cast_to<Script>(obj)->get_script_method_list(&methods);
  184. }
  185. ClassDB::get_method_list(E->get(), &methods, true, true);
  186. }
  187. }
  188. for (List<MethodInfo>::Element *M = methods.front(); M; M = M->next()) {
  189. String name = M->get().name.get_slice(":", 0);
  190. if (name.begins_with("_") && !(M->get().flags & METHOD_FLAG_VIRTUAL))
  191. continue;
  192. if (virtuals_only && !(M->get().flags & METHOD_FLAG_VIRTUAL))
  193. continue;
  194. if (!virtuals_only && (M->get().flags & METHOD_FLAG_VIRTUAL))
  195. continue;
  196. MethodInfo mi = M->get();
  197. String desc_arguments;
  198. if (mi.arguments.size() > 0) {
  199. desc_arguments = "(";
  200. for (int i = 0; i < mi.arguments.size(); i++) {
  201. if (i > 0) {
  202. desc_arguments += ", ";
  203. }
  204. if (mi.arguments[i].type == Variant::NIL) {
  205. desc_arguments += "var";
  206. } else if (mi.arguments[i].name.find(":") != -1) {
  207. desc_arguments += mi.arguments[i].name.get_slice(":", 1);
  208. mi.arguments[i].name = mi.arguments[i].name.get_slice(":", 0);
  209. } else {
  210. desc_arguments += Variant::get_type_name(mi.arguments[i].type);
  211. }
  212. }
  213. desc_arguments += ")";
  214. }
  215. String desc_raw = mi.name + desc_arguments;
  216. String desc = desc_raw.capitalize().replace("( ", "(");
  217. if (search_box->get_text() != String() &&
  218. name.findn(search_box->get_text()) == -1 &&
  219. desc.findn(search_box->get_text()) == -1 &&
  220. desc_raw.findn(search_box->get_text()) == -1) {
  221. continue;
  222. }
  223. TreeItem *item = search_options->create_item(category ? category : root);
  224. item->set_text(0, desc);
  225. item->set_icon(0, get_icon("MemberMethod", "EditorIcons"));
  226. item->set_metadata(0, name);
  227. item->set_selectable(0, true);
  228. item->set_metadata(1, "method");
  229. item->set_collapsed(1);
  230. item->set_selectable(1, false);
  231. item->set_selectable(2, false);
  232. item->set_metadata(2, connecting);
  233. }
  234. if (category && category->get_children() == NULL) {
  235. memdelete(category); //old category was unused
  236. }
  237. }
  238. if (properties) {
  239. if (!seq_connect && !visual_script_generic) {
  240. get_visual_node_names("flow_control/type_cast", Set<String>(), found, root, search_box);
  241. get_visual_node_names("functions/built_in/print", Set<String>(), found, root, search_box);
  242. get_visual_node_names("functions/by_type/" + Variant::get_type_name(type), Set<String>(), found, root, search_box);
  243. get_visual_node_names("functions/deconstruct/" + Variant::get_type_name(type), Set<String>(), found, root, search_box);
  244. get_visual_node_names("operators/compare/", Set<String>(), found, root, search_box);
  245. if (type == Variant::INT) {
  246. get_visual_node_names("operators/bitwise/", Set<String>(), found, root, search_box);
  247. }
  248. if (type == Variant::BOOL) {
  249. get_visual_node_names("operators/logic/", Set<String>(), found, root, search_box);
  250. }
  251. if (type == Variant::BOOL || type == Variant::INT || type == Variant::FLOAT || type == Variant::VECTOR2 || type == Variant::VECTOR3) {
  252. get_visual_node_names("operators/math/", Set<String>(), found, root, search_box);
  253. }
  254. }
  255. }
  256. if (seq_connect && !visual_script_generic) {
  257. String text = search_box->get_text();
  258. create_visualscript_item(String("VisualScriptCondition"), root, text, String("Condition"));
  259. create_visualscript_item(String("VisualScriptSwitch"), root, text, String("Switch"));
  260. create_visualscript_item(String("VisualScriptSequence"), root, text, String("Sequence"));
  261. create_visualscript_item(String("VisualScriptIterator"), root, text, String("Iterator"));
  262. create_visualscript_item(String("VisualScriptWhile"), root, text, String("While"));
  263. create_visualscript_item(String("VisualScriptReturn"), root, text, String("Return"));
  264. get_visual_node_names("flow_control/type_cast", Set<String>(), found, root, search_box);
  265. get_visual_node_names("functions/built_in/print", Set<String>(), found, root, search_box);
  266. }
  267. if ((properties || seq_connect) && visual_script_generic) {
  268. get_visual_node_names("", Set<String>(), found, root, search_box);
  269. }
  270. TreeItem *selected_item = search_options->search_item_text(search_box->get_text());
  271. if (!found && selected_item != NULL) {
  272. selected_item->select(0);
  273. found = true;
  274. }
  275. get_ok()->set_disabled(root->get_children() == NULL);
  276. }
  277. void VisualScriptPropertySelector::create_visualscript_item(const String &name, TreeItem *const root, const String &search_input, const String &text) {
  278. if (search_input == String() || text.findn(search_input) != -1) {
  279. TreeItem *item = search_options->create_item(root);
  280. item->set_text(0, text);
  281. item->set_icon(0, get_icon("VisualScript", "EditorIcons"));
  282. item->set_metadata(0, name);
  283. item->set_metadata(1, "action");
  284. item->set_selectable(0, true);
  285. item->set_collapsed(1);
  286. item->set_selectable(1, false);
  287. item->set_selectable(2, false);
  288. item->set_metadata(2, connecting);
  289. }
  290. }
  291. void VisualScriptPropertySelector::get_visual_node_names(const String &root_filter, const Set<String> &p_modifiers, bool &found, TreeItem *const root, LineEdit *const search_box) {
  292. Map<String, TreeItem *> path_cache;
  293. List<String> fnodes;
  294. VisualScriptLanguage::singleton->get_registered_node_names(&fnodes);
  295. for (List<String>::Element *E = fnodes.front(); E; E = E->next()) {
  296. if (!E->get().begins_with(root_filter)) {
  297. continue;
  298. }
  299. Vector<String> path = E->get().split("/");
  300. // check if the name has the filter
  301. bool in_filter = false;
  302. Vector<String> tx_filters = search_box->get_text().split(" ");
  303. for (int i = 0; i < tx_filters.size(); i++) {
  304. if (tx_filters[i] == "") {
  305. in_filter = true;
  306. } else {
  307. in_filter = false;
  308. }
  309. if (E->get().findn(tx_filters[i]) != -1) {
  310. in_filter = true;
  311. break;
  312. }
  313. }
  314. if (!in_filter) {
  315. continue;
  316. }
  317. bool in_modifier = p_modifiers.empty();
  318. for (Set<String>::Element *F = p_modifiers.front(); F && in_modifier; F = F->next()) {
  319. if (E->get().findn(F->get()) != -1)
  320. in_modifier = true;
  321. }
  322. if (!in_modifier) {
  323. continue;
  324. }
  325. TreeItem *item = search_options->create_item(root);
  326. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(E->get());
  327. Ref<VisualScriptOperator> vnode_operator = vnode;
  328. String type_name;
  329. if (vnode_operator.is_valid()) {
  330. String type;
  331. if (path.size() >= 2) {
  332. type = path[1];
  333. }
  334. type_name = type.capitalize() + " ";
  335. }
  336. Ref<VisualScriptFunctionCall> vnode_function_call = vnode;
  337. if (vnode_function_call.is_valid()) {
  338. String basic_type = Variant::get_type_name(vnode_function_call->get_basic_type());
  339. type_name = basic_type.capitalize() + " ";
  340. }
  341. Ref<VisualScriptConstructor> vnode_constructor = vnode;
  342. if (vnode_constructor.is_valid()) {
  343. type_name = "Construct ";
  344. }
  345. Ref<VisualScriptDeconstruct> vnode_deconstruct = vnode;
  346. if (vnode_deconstruct.is_valid()) {
  347. type_name = "Deconstruct ";
  348. }
  349. Vector<String> desc = path[path.size() - 1].replace("(", " ").replace(")", " ").replace(",", " ").split(" ");
  350. for (int i = 0; i < desc.size(); i++) {
  351. desc.write[i] = desc[i].capitalize();
  352. if (desc[i].ends_with(",")) {
  353. desc.write[i] = desc[i].replace(",", ", ");
  354. }
  355. }
  356. item->set_text(0, type_name + String("").join(desc));
  357. item->set_icon(0, get_icon("VisualScript", "EditorIcons"));
  358. item->set_selectable(0, true);
  359. item->set_metadata(0, E->get());
  360. item->set_selectable(0, true);
  361. item->set_metadata(1, "visualscript");
  362. item->set_selectable(1, false);
  363. item->set_selectable(2, false);
  364. item->set_metadata(2, connecting);
  365. }
  366. }
  367. void VisualScriptPropertySelector::_confirmed() {
  368. TreeItem *ti = search_options->get_selected();
  369. if (!ti)
  370. return;
  371. emit_signal("selected", ti->get_metadata(0), ti->get_metadata(1), ti->get_metadata(2));
  372. hide();
  373. }
  374. void VisualScriptPropertySelector::_item_selected() {
  375. help_bit->set_text("");
  376. TreeItem *item = search_options->get_selected();
  377. if (!item)
  378. return;
  379. String name = item->get_metadata(0);
  380. String class_type;
  381. if (type != Variant::NIL) {
  382. class_type = Variant::get_type_name(type);
  383. } else {
  384. class_type = base_type;
  385. }
  386. DocData *dd = EditorHelp::get_doc_data();
  387. String text;
  388. String at_class = class_type;
  389. while (at_class != String()) {
  390. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(at_class);
  391. if (E) {
  392. for (int i = 0; i < E->get().properties.size(); i++) {
  393. if (E->get().properties[i].name == name) {
  394. text = E->get().properties[i].description;
  395. }
  396. }
  397. }
  398. at_class = ClassDB::get_parent_class_nocheck(at_class);
  399. }
  400. at_class = class_type;
  401. while (at_class != String()) {
  402. Map<String, DocData::ClassDoc>::Element *C = dd->class_list.find(at_class);
  403. if (C) {
  404. for (int i = 0; i < C->get().methods.size(); i++) {
  405. if (C->get().methods[i].name == name) {
  406. text = C->get().methods[i].description;
  407. }
  408. }
  409. }
  410. at_class = ClassDB::get_parent_class_nocheck(at_class);
  411. }
  412. Map<String, DocData::ClassDoc>::Element *T = dd->class_list.find(class_type);
  413. if (T) {
  414. for (int i = 0; i < T->get().methods.size(); i++) {
  415. Vector<String> functions = name.rsplit("/", false, 1);
  416. if (T->get().methods[i].name == functions[functions.size() - 1]) {
  417. text = T->get().methods[i].description;
  418. }
  419. }
  420. }
  421. List<String> *names = memnew(List<String>);
  422. VisualScriptLanguage::singleton->get_registered_node_names(names);
  423. if (names->find(name) != NULL) {
  424. Ref<VisualScriptOperator> operator_node = VisualScriptLanguage::singleton->create_node_from_name(name);
  425. if (operator_node.is_valid()) {
  426. Map<String, DocData::ClassDoc>::Element *F = dd->class_list.find(operator_node->get_class_name());
  427. if (F) {
  428. text = Variant::get_operator_name(operator_node->get_operator());
  429. }
  430. }
  431. Ref<VisualScriptTypeCast> typecast_node = VisualScriptLanguage::singleton->create_node_from_name(name);
  432. if (typecast_node.is_valid()) {
  433. Map<String, DocData::ClassDoc>::Element *F = dd->class_list.find(typecast_node->get_class_name());
  434. if (F) {
  435. text = F->get().description;
  436. }
  437. }
  438. Ref<VisualScriptBuiltinFunc> builtin_node = VisualScriptLanguage::singleton->create_node_from_name(name);
  439. if (builtin_node.is_valid()) {
  440. Map<String, DocData::ClassDoc>::Element *F = dd->class_list.find(builtin_node->get_class_name());
  441. if (F) {
  442. for (int i = 0; i < F->get().constants.size(); i++) {
  443. if (F->get().constants[i].value.to_int() == int(builtin_node->get_func())) {
  444. text = F->get().constants[i].description;
  445. }
  446. }
  447. }
  448. }
  449. }
  450. memdelete(names);
  451. if (text == String())
  452. return;
  453. help_bit->set_text(text);
  454. }
  455. void VisualScriptPropertySelector::_hide_requested() {
  456. _closed(); // From WindowDialog.
  457. }
  458. void VisualScriptPropertySelector::_notification(int p_what) {
  459. if (p_what == NOTIFICATION_ENTER_TREE) {
  460. connect("confirmed", callable_mp(this, &VisualScriptPropertySelector::_confirmed));
  461. }
  462. }
  463. void VisualScriptPropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, const bool p_virtuals_only, const bool p_connecting, bool clear_text) {
  464. base_type = p_base;
  465. selected = p_current;
  466. type = Variant::NIL;
  467. properties = false;
  468. instance = NULL;
  469. virtuals_only = p_virtuals_only;
  470. show_window(.5f);
  471. if (clear_text)
  472. search_box->set_text("");
  473. else
  474. search_box->select_all();
  475. search_box->grab_focus();
  476. connecting = p_connecting;
  477. _update_search();
  478. }
  479. void VisualScriptPropertySelector::set_type_filter(const Vector<Variant::Type> &p_type_filter) {
  480. type_filter = p_type_filter;
  481. }
  482. 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, bool clear_text) {
  483. base_type = p_base;
  484. selected = p_current;
  485. type = Variant::NIL;
  486. properties = true;
  487. visual_script_generic = false;
  488. instance = NULL;
  489. virtuals_only = p_virtuals_only;
  490. show_window(.5f);
  491. if (clear_text)
  492. search_box->set_text("");
  493. else
  494. search_box->select_all();
  495. search_box->grab_focus();
  496. seq_connect = p_seq_connect;
  497. connecting = p_connecting;
  498. _update_search();
  499. }
  500. void VisualScriptPropertySelector::select_from_script(const Ref<Script> &p_script, const String &p_current, const bool p_connecting, bool clear_text) {
  501. ERR_FAIL_COND(p_script.is_null());
  502. base_type = p_script->get_instance_base_type();
  503. selected = p_current;
  504. type = Variant::NIL;
  505. script = p_script->get_instance_id();
  506. properties = true;
  507. visual_script_generic = false;
  508. instance = NULL;
  509. virtuals_only = false;
  510. show_window(.5f);
  511. if (clear_text)
  512. search_box->set_text("");
  513. else
  514. search_box->select_all();
  515. search_box->grab_focus();
  516. seq_connect = false;
  517. connecting = p_connecting;
  518. _update_search();
  519. }
  520. void VisualScriptPropertySelector::select_from_basic_type(Variant::Type p_type, const String &p_current, const bool p_connecting, bool clear_text) {
  521. ERR_FAIL_COND(p_type == Variant::NIL);
  522. base_type = "";
  523. selected = p_current;
  524. type = p_type;
  525. properties = true;
  526. visual_script_generic = false;
  527. instance = NULL;
  528. virtuals_only = false;
  529. show_window(.5f);
  530. if (clear_text)
  531. search_box->set_text("");
  532. else
  533. search_box->select_all();
  534. search_box->grab_focus();
  535. seq_connect = false;
  536. connecting = p_connecting;
  537. _update_search();
  538. }
  539. void VisualScriptPropertySelector::select_from_action(const String &p_type, const String &p_current, const bool p_connecting, bool clear_text) {
  540. base_type = p_type;
  541. selected = p_current;
  542. type = Variant::NIL;
  543. properties = false;
  544. visual_script_generic = false;
  545. instance = NULL;
  546. virtuals_only = false;
  547. show_window(.5f);
  548. if (clear_text)
  549. search_box->set_text("");
  550. else
  551. search_box->select_all();
  552. search_box->grab_focus();
  553. seq_connect = true;
  554. connecting = p_connecting;
  555. _update_search();
  556. }
  557. void VisualScriptPropertySelector::select_from_instance(Object *p_instance, const String &p_current, const bool p_connecting, const String &p_basetype, bool clear_text) {
  558. base_type = p_basetype;
  559. selected = p_current;
  560. type = Variant::NIL;
  561. properties = true;
  562. visual_script_generic = false;
  563. instance = p_instance;
  564. virtuals_only = false;
  565. show_window(.5f);
  566. if (clear_text)
  567. search_box->set_text("");
  568. else
  569. search_box->select_all();
  570. search_box->grab_focus();
  571. seq_connect = false;
  572. connecting = p_connecting;
  573. _update_search();
  574. }
  575. void VisualScriptPropertySelector::select_from_visual_script(const String &p_base, const bool p_connecting, bool clear_text) {
  576. base_type = p_base;
  577. selected = "";
  578. type = Variant::NIL;
  579. properties = true;
  580. visual_script_generic = true;
  581. instance = NULL;
  582. virtuals_only = false;
  583. show_window(.5f);
  584. if (clear_text)
  585. search_box->set_text("");
  586. else
  587. search_box->select_all();
  588. search_box->grab_focus();
  589. connecting = p_connecting;
  590. _update_search();
  591. }
  592. void VisualScriptPropertySelector::show_window(float p_screen_ratio) {
  593. Rect2 rect;
  594. Point2 window_size = get_viewport_rect().size;
  595. rect.size = (window_size * p_screen_ratio).floor();
  596. rect.size.x = rect.size.x / 2.2f;
  597. rect.position = ((window_size - rect.size) / 2.0f).floor();
  598. popup(rect);
  599. }
  600. void VisualScriptPropertySelector::_bind_methods() {
  601. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::STRING, "category"), PropertyInfo(Variant::BOOL, "connecting")));
  602. }
  603. VisualScriptPropertySelector::VisualScriptPropertySelector() {
  604. VBoxContainer *vbc = memnew(VBoxContainer);
  605. add_child(vbc);
  606. //set_child_rect(vbc);
  607. search_box = memnew(LineEdit);
  608. vbc->add_margin_child(TTR("Search:"), search_box);
  609. search_box->connect("text_changed", callable_mp(this, &VisualScriptPropertySelector::_text_changed));
  610. search_box->connect("gui_input", callable_mp(this, &VisualScriptPropertySelector::_sbox_input));
  611. search_options = memnew(Tree);
  612. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  613. get_ok()->set_text(TTR("Open"));
  614. get_ok()->set_disabled(true);
  615. register_text_enter(search_box);
  616. set_hide_on_ok(false);
  617. search_options->connect("item_activated", callable_mp(this, &VisualScriptPropertySelector::_confirmed));
  618. search_options->connect("cell_selected", callable_mp(this, &VisualScriptPropertySelector::_item_selected));
  619. search_options->set_hide_root(true);
  620. search_options->set_hide_folding(true);
  621. virtuals_only = false;
  622. seq_connect = false;
  623. help_bit = memnew(EditorHelpBit);
  624. vbc->add_margin_child(TTR("Description:"), help_bit);
  625. help_bit->connect("request_hide", callable_mp(this, &VisualScriptPropertySelector::_hide_requested));
  626. search_options->set_columns(3);
  627. search_options->set_column_expand(1, false);
  628. search_options->set_column_expand(2, false);
  629. }