visual_script_property_selector.cpp 24 KB

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