visual_script_property_selector.cpp 24 KB

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