visual_script_property_selector.cpp 24 KB

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