visual_script_property_selector.cpp 25 KB

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