visual_script_property_selector.cpp 25 KB

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