visual_script_property_selector.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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 "../visual_script.h"
  32. #include "../visual_script_builtin_funcs.h"
  33. #include "../visual_script_flow_control.h"
  34. #include "../visual_script_func_nodes.h"
  35. #include "../visual_script_nodes.h"
  36. #include "core/os/keyboard.h"
  37. #include "editor/doc_tools.h"
  38. #include "editor/editor_feature_profile.h"
  39. #include "editor/editor_node.h"
  40. #include "editor/editor_scale.h"
  41. #include "scene/main/node.h"
  42. #include "scene/main/window.h"
  43. void VisualScriptPropertySelector::_update_icons() {
  44. search_box->set_right_icon(results_tree->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  45. search_box->set_clear_button_enabled(true);
  46. search_box->add_theme_icon_override("right_icon", results_tree->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  47. search_visual_script_nodes->set_icon(results_tree->get_theme_icon(SNAME("VisualScript"), SNAME("EditorIcons")));
  48. search_classes->set_icon(results_tree->get_theme_icon(SNAME("Object"), SNAME("EditorIcons")));
  49. search_methods->set_icon(results_tree->get_theme_icon(SNAME("MemberMethod"), SNAME("EditorIcons")));
  50. search_operators->set_icon(results_tree->get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  51. search_signals->set_icon(results_tree->get_theme_icon(SNAME("MemberSignal"), SNAME("EditorIcons")));
  52. search_constants->set_icon(results_tree->get_theme_icon(SNAME("MemberConstant"), SNAME("EditorIcons")));
  53. search_properties->set_icon(results_tree->get_theme_icon(SNAME("MemberProperty"), SNAME("EditorIcons")));
  54. search_theme_items->set_icon(results_tree->get_theme_icon(SNAME("MemberTheme"), SNAME("EditorIcons")));
  55. case_sensitive_button->set_icon(results_tree->get_theme_icon(SNAME("MatchCase"), SNAME("EditorIcons")));
  56. hierarchy_button->set_icon(results_tree->get_theme_icon(SNAME("ClassList"), SNAME("EditorIcons")));
  57. }
  58. void VisualScriptPropertySelector::_sbox_input(const Ref<InputEvent> &p_ie) {
  59. Ref<InputEventKey> k = p_ie;
  60. if (k.is_valid()) {
  61. switch (k->get_keycode()) {
  62. case Key::UP:
  63. case Key::DOWN:
  64. case Key::PAGEUP:
  65. case Key::PAGEDOWN: {
  66. results_tree->gui_input(k);
  67. search_box->accept_event();
  68. } break;
  69. default:
  70. break;
  71. }
  72. }
  73. }
  74. void VisualScriptPropertySelector::_update_results_i(int p_int) {
  75. _update_results();
  76. }
  77. void VisualScriptPropertySelector::_update_results_s(String p_string) {
  78. _update_results();
  79. }
  80. void VisualScriptPropertySelector::_update_results() {
  81. _update_icons();
  82. search_runner = Ref<SearchRunner>(memnew(SearchRunner(this, results_tree)));
  83. set_process(true);
  84. }
  85. void VisualScriptPropertySelector::_confirmed() {
  86. TreeItem *ti = results_tree->get_selected();
  87. if (!ti) {
  88. return;
  89. }
  90. emit_signal(SNAME("selected"), ti->get_metadata(0), ti->get_metadata(1), connecting);
  91. set_visible(false);
  92. }
  93. void VisualScriptPropertySelector::_item_selected() {
  94. if (results_tree->get_selected()->has_meta("description")) {
  95. help_bit->set_text(results_tree->get_selected()->get_meta("description"));
  96. } else {
  97. help_bit->set_text("No description available");
  98. }
  99. }
  100. void VisualScriptPropertySelector::_hide_requested() {
  101. _cancel_pressed(); // From AcceptDialog.
  102. }
  103. void VisualScriptPropertySelector::_notification(int p_what) {
  104. switch (p_what) {
  105. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  106. _update_icons();
  107. } break;
  108. case NOTIFICATION_ENTER_TREE: {
  109. connect("confirmed", callable_mp(this, &VisualScriptPropertySelector::_confirmed));
  110. } break;
  111. case NOTIFICATION_PROCESS: {
  112. // Update background search.
  113. if (search_runner.is_valid()) {
  114. if (search_runner->work()) {
  115. // Search done.
  116. get_ok_button()->set_disabled(!results_tree->get_selected());
  117. search_runner = Ref<SearchRunner>();
  118. set_process(false);
  119. }
  120. } else {
  121. // if one is valid
  122. set_process(false);
  123. }
  124. } break;
  125. }
  126. }
  127. void VisualScriptPropertySelector::select_method_from_base_type(const String &p_base, const bool p_virtuals_only, const bool p_connecting, bool clear_text) {
  128. set_title(TTR("Select method from base type"));
  129. base_type = p_base;
  130. base_script = "";
  131. type = Variant::NIL;
  132. connecting = p_connecting;
  133. if (clear_text) {
  134. if (p_virtuals_only) {
  135. search_box->set_text("._"); // show all _methods
  136. search_box->set_caret_column(2);
  137. } else {
  138. search_box->set_text("."); // show all methods
  139. search_box->set_caret_column(1);
  140. }
  141. }
  142. search_visual_script_nodes->set_pressed(false);
  143. search_classes->set_pressed(false);
  144. search_methods->set_pressed(true);
  145. search_operators->set_pressed(false);
  146. search_signals->set_pressed(false);
  147. search_constants->set_pressed(false);
  148. search_properties->set_pressed(false);
  149. search_theme_items->set_pressed(false);
  150. scope_combo->select(2); //id0 = "Search Related" //id2 = "Search Base" //id3 = "Search Inheriters" //id4 = "Search Unrelated"
  151. results_tree->clear();
  152. show_window(.5f);
  153. search_box->grab_focus();
  154. _update_results();
  155. }
  156. void VisualScriptPropertySelector::select_from_base_type(const String &p_base, const String &p_base_script, bool p_virtuals_only, const bool p_connecting, bool clear_text) {
  157. set_title(TTR("Select from base type"));
  158. base_type = p_base;
  159. base_script = p_base_script.lstrip("res://").quote(); // filepath to EditorHelp::get_doc_data().name
  160. type = Variant::NIL;
  161. connecting = p_connecting;
  162. if (clear_text) {
  163. if (p_virtuals_only) {
  164. search_box->set_text("_");
  165. } else {
  166. search_box->set_text(" ");
  167. }
  168. }
  169. search_box->select_all();
  170. search_visual_script_nodes->set_pressed(false);
  171. search_classes->set_pressed(false);
  172. search_methods->set_pressed(true);
  173. search_operators->set_pressed(false);
  174. search_signals->set_pressed(true);
  175. search_constants->set_pressed(false);
  176. search_properties->set_pressed(true);
  177. search_theme_items->set_pressed(false);
  178. // When class is Input only show inheritors
  179. scope_combo->select(0); //id0 = "Search Related" //id2 = "Search Base" //id3 = "Search Inheriters" //id4 = "Search Unrelated"
  180. results_tree->clear();
  181. show_window(.5f);
  182. search_box->grab_focus();
  183. _update_results();
  184. }
  185. void VisualScriptPropertySelector::select_from_script(const Ref<Script> &p_script, const bool p_connecting, bool clear_text) {
  186. set_title(TTR("Select from script"));
  187. ERR_FAIL_COND(p_script.is_null());
  188. base_type = p_script->get_instance_base_type();
  189. base_script = p_script->get_path().lstrip("res://").quote(); // filepath to EditorHelp::get_doc_data().name
  190. type = Variant::NIL;
  191. script = p_script->get_instance_id();
  192. connecting = p_connecting;
  193. if (clear_text) {
  194. search_box->set_text("");
  195. }
  196. search_box->select_all();
  197. search_visual_script_nodes->set_pressed(false);
  198. search_classes->set_pressed(true);
  199. search_methods->set_pressed(true);
  200. search_operators->set_pressed(true);
  201. search_signals->set_pressed(true);
  202. search_constants->set_pressed(true);
  203. search_properties->set_pressed(true);
  204. search_theme_items->set_pressed(false);
  205. scope_combo->select(2); //id0 = "Search Related" //id2 = "Search Base" //id3 = "Search Inheriters" //id4 = "Search Unrelated"
  206. results_tree->clear();
  207. show_window(.5f);
  208. search_box->grab_focus();
  209. _update_results();
  210. }
  211. void VisualScriptPropertySelector::select_from_basic_type(Variant::Type p_type, const bool p_connecting, bool clear_text) {
  212. set_title(TTR("Select from basic type"));
  213. ERR_FAIL_COND(p_type == Variant::NIL);
  214. base_type = Variant::get_type_name(p_type);
  215. base_script = "";
  216. type = p_type;
  217. connecting = p_connecting;
  218. if (clear_text) {
  219. search_box->set_text(" ");
  220. }
  221. search_box->select_all();
  222. search_visual_script_nodes->set_pressed(false);
  223. search_classes->set_pressed(false);
  224. search_methods->set_pressed(true);
  225. search_operators->set_pressed(true);
  226. search_signals->set_pressed(false);
  227. search_constants->set_pressed(true);
  228. search_properties->set_pressed(true);
  229. search_theme_items->set_pressed(false);
  230. scope_combo->select(2); //id0 = "Search Related" //id2 = "Search Base" //id3 = "Search Inheriters" //id4 = "Search Unrelated" //id5 "Search All"
  231. results_tree->clear();
  232. show_window(.5f);
  233. search_box->grab_focus();
  234. _update_results();
  235. }
  236. void VisualScriptPropertySelector::select_from_action(const String &p_type, const bool p_connecting, bool clear_text) {
  237. set_title(TTR("Select from action"));
  238. base_type = p_type;
  239. base_script = "";
  240. type = Variant::NIL;
  241. connecting = p_connecting;
  242. if (clear_text) {
  243. search_box->set_text("");
  244. }
  245. search_box->select_all();
  246. search_visual_script_nodes->set_pressed(true);
  247. search_classes->set_pressed(false);
  248. search_methods->set_pressed(false);
  249. search_operators->set_pressed(false);
  250. search_signals->set_pressed(false);
  251. search_constants->set_pressed(false);
  252. search_properties->set_pressed(false);
  253. search_theme_items->set_pressed(false);
  254. scope_combo->select(0); //id0 = "Search Related" //id2 = "Search Base" //id3 = "Search Inheriters" //id4 = "Search Unrelated" //id5 "Search All"
  255. results_tree->clear();
  256. show_window(.5f);
  257. search_box->grab_focus();
  258. _update_results();
  259. }
  260. void VisualScriptPropertySelector::select_from_instance(Object *p_instance, const bool p_connecting, bool clear_text) {
  261. set_title(TTR("Select from instance"));
  262. base_type = p_instance->get_class();
  263. const Ref<Script> &p_script = p_instance->get_script();
  264. if (p_script == nullptr) {
  265. base_script = "";
  266. } else {
  267. base_script = p_script->get_path().lstrip("res://").quote(); // filepath to EditorHelp::get_doc_data().name
  268. }
  269. type = Variant::NIL;
  270. connecting = p_connecting;
  271. if (clear_text) {
  272. search_box->set_text(" ");
  273. }
  274. search_box->select_all();
  275. search_visual_script_nodes->set_pressed(false);
  276. search_classes->set_pressed(false);
  277. search_methods->set_pressed(true);
  278. search_operators->set_pressed(false);
  279. search_signals->set_pressed(true);
  280. search_constants->set_pressed(true);
  281. search_properties->set_pressed(true);
  282. search_theme_items->set_pressed(false);
  283. scope_combo->select(2); //id0 = "Search Related" //id2 = "Search Base" //id3 = "Search Inheriters" //id4 = "Search Unrelated" //id5 "Search All"
  284. results_tree->clear();
  285. show_window(.5f);
  286. search_box->grab_focus();
  287. _update_results();
  288. }
  289. void VisualScriptPropertySelector::select_from_visual_script(const Ref<Script> &p_script, bool clear_text) {
  290. set_title(TTR("Select from visual script"));
  291. base_type = p_script->get_instance_base_type();
  292. if (p_script == nullptr) {
  293. base_script = "";
  294. } else {
  295. base_script = p_script->get_path().lstrip("res://").quote(); // filepath to EditorHelp::get_doc_data().name
  296. }
  297. type = Variant::NIL;
  298. connecting = false;
  299. if (clear_text) {
  300. search_box->set_text(" ");
  301. }
  302. search_box->select_all();
  303. search_visual_script_nodes->set_pressed(true);
  304. search_classes->set_pressed(false);
  305. search_methods->set_pressed(true);
  306. search_operators->set_pressed(false);
  307. search_signals->set_pressed(true);
  308. search_constants->set_pressed(true);
  309. search_properties->set_pressed(true);
  310. search_theme_items->set_pressed(false);
  311. scope_combo->select(2); //id0 = "Search Related" //id2 = "Search Base" //id3 = "Search Inheriters" //id4 = "Search Unrelated" //id5 "Search All"
  312. results_tree->clear();
  313. show_window(.5f);
  314. search_box->grab_focus();
  315. _update_results();
  316. }
  317. void VisualScriptPropertySelector::show_window(float p_screen_ratio) {
  318. popup_centered_ratio(p_screen_ratio);
  319. }
  320. void VisualScriptPropertySelector::_bind_methods() {
  321. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::STRING, "category"), PropertyInfo(Variant::BOOL, "connecting")));
  322. }
  323. VisualScriptPropertySelector::VisualScriptPropertySelector() {
  324. vbox = memnew(VBoxContainer);
  325. add_child(vbox);
  326. HBoxContainer *hbox = memnew(HBoxContainer);
  327. hbox->set_alignment(hbox->ALIGNMENT_CENTER);
  328. vbox->add_child(hbox);
  329. case_sensitive_button = memnew(Button);
  330. case_sensitive_button->set_flat(true);
  331. case_sensitive_button->set_tooltip(TTR("Case Sensitive"));
  332. case_sensitive_button->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  333. case_sensitive_button->set_toggle_mode(true);
  334. case_sensitive_button->set_focus_mode(Control::FOCUS_NONE);
  335. hbox->add_child(case_sensitive_button);
  336. hierarchy_button = memnew(Button);
  337. hierarchy_button->set_flat(true);
  338. hierarchy_button->set_tooltip(TTR("Show Hierarchy"));
  339. hierarchy_button->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  340. hierarchy_button->set_toggle_mode(true);
  341. hierarchy_button->set_pressed(true);
  342. hierarchy_button->set_focus_mode(Control::FOCUS_NONE);
  343. hbox->add_child(hierarchy_button);
  344. hbox->add_child(memnew(VSeparator));
  345. search_visual_script_nodes = memnew(Button);
  346. search_visual_script_nodes->set_flat(true);
  347. search_visual_script_nodes->set_tooltip(TTR("Search Visual Script Nodes"));
  348. search_visual_script_nodes->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  349. search_visual_script_nodes->set_toggle_mode(true);
  350. search_visual_script_nodes->set_pressed(true);
  351. search_visual_script_nodes->set_focus_mode(Control::FOCUS_NONE);
  352. hbox->add_child(search_visual_script_nodes);
  353. search_classes = memnew(Button);
  354. search_classes->set_flat(true);
  355. search_classes->set_tooltip(TTR("Search Classes"));
  356. search_classes->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  357. search_classes->set_toggle_mode(true);
  358. search_classes->set_pressed(true);
  359. search_classes->set_focus_mode(Control::FOCUS_NONE);
  360. hbox->add_child(search_classes);
  361. search_operators = memnew(Button);
  362. search_operators->set_flat(true);
  363. search_operators->set_tooltip(TTR("Search Operators"));
  364. search_operators->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  365. search_operators->set_toggle_mode(true);
  366. search_operators->set_pressed(true);
  367. search_operators->set_focus_mode(Control::FOCUS_NONE);
  368. hbox->add_child(search_operators);
  369. hbox->add_child(memnew(VSeparator));
  370. search_methods = memnew(Button);
  371. search_methods->set_flat(true);
  372. search_methods->set_tooltip(TTR("Search Methods"));
  373. search_methods->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  374. search_methods->set_toggle_mode(true);
  375. search_methods->set_pressed(true);
  376. search_methods->set_focus_mode(Control::FOCUS_NONE);
  377. hbox->add_child(search_methods);
  378. search_signals = memnew(Button);
  379. search_signals->set_flat(true);
  380. search_signals->set_tooltip(TTR("Search Signals"));
  381. search_signals->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  382. search_signals->set_toggle_mode(true);
  383. search_signals->set_pressed(true);
  384. search_signals->set_focus_mode(Control::FOCUS_NONE);
  385. hbox->add_child(search_signals);
  386. search_constants = memnew(Button);
  387. search_constants->set_flat(true);
  388. search_constants->set_tooltip(TTR("Search Constants"));
  389. search_constants->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  390. search_constants->set_toggle_mode(true);
  391. search_constants->set_pressed(true);
  392. search_constants->set_focus_mode(Control::FOCUS_NONE);
  393. hbox->add_child(search_constants);
  394. search_properties = memnew(Button);
  395. search_properties->set_flat(true);
  396. search_properties->set_tooltip(TTR("Search Properties"));
  397. search_properties->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  398. search_properties->set_toggle_mode(true);
  399. search_properties->set_pressed(true);
  400. search_properties->set_focus_mode(Control::FOCUS_NONE);
  401. hbox->add_child(search_properties);
  402. search_theme_items = memnew(Button);
  403. search_theme_items->set_flat(true);
  404. search_theme_items->set_tooltip(TTR("Search Theme Items"));
  405. search_theme_items->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  406. search_theme_items->set_toggle_mode(true);
  407. search_theme_items->set_pressed(true);
  408. search_theme_items->set_focus_mode(Control::FOCUS_NONE);
  409. hbox->add_child(search_theme_items);
  410. scope_combo = memnew(OptionButton);
  411. scope_combo->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  412. scope_combo->set_tooltip(TTR("Select the search limits"));
  413. scope_combo->set_stretch_ratio(0); // Fixed width.
  414. scope_combo->add_item(TTR("Search Related"), SCOPE_RELATED);
  415. scope_combo->add_separator();
  416. scope_combo->add_item(TTR("Search Base"), SCOPE_BASE);
  417. scope_combo->add_item(TTR("Search Inheriters"), SCOPE_INHERITERS);
  418. scope_combo->add_item(TTR("Search Unrelated"), SCOPE_UNRELATED);
  419. scope_combo->add_item(TTR("Search All"), SCOPE_ALL);
  420. scope_combo->connect("item_selected", callable_mp(this, &VisualScriptPropertySelector::_update_results_i));
  421. hbox->add_child(scope_combo);
  422. search_box = memnew(LineEdit);
  423. search_box->set_tooltip(TTR("Enter \" \" to show all filterd options\nEnter \".\" to show all filterd methods, operators and constructors\nUse CTRL_KEY to drop property setters"));
  424. search_box->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  425. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  426. search_box->connect("text_changed", callable_mp(this, &VisualScriptPropertySelector::_update_results_s));
  427. search_box->connect("gui_input", callable_mp(this, &VisualScriptPropertySelector::_sbox_input));
  428. register_text_enter(search_box);
  429. vbox->add_child(search_box);
  430. results_tree = memnew(Tree);
  431. results_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  432. results_tree->set_hide_root(true);
  433. results_tree->set_hide_folding(false);
  434. results_tree->set_columns(2);
  435. results_tree->set_column_title(0, TTR("Name"));
  436. results_tree->set_column_clip_content(0, true);
  437. results_tree->set_column_title(1, TTR("Member Type"));
  438. results_tree->set_column_expand(1, false);
  439. results_tree->set_column_custom_minimum_width(1, 150 * EDSCALE);
  440. results_tree->set_column_clip_content(1, true);
  441. results_tree->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
  442. results_tree->set_select_mode(Tree::SELECT_ROW);
  443. results_tree->connect("item_activated", callable_mp(this, &VisualScriptPropertySelector::_confirmed));
  444. results_tree->connect("item_selected", callable_mp(this, &VisualScriptPropertySelector::_item_selected));
  445. vbox->add_child(results_tree);
  446. help_bit = memnew(EditorHelpBit);
  447. vbox->add_child(help_bit);
  448. help_bit->connect("request_hide", callable_mp(this, &VisualScriptPropertySelector::_hide_requested));
  449. get_ok_button()->set_text(TTR("Open"));
  450. get_ok_button()->set_disabled(true);
  451. set_hide_on_ok(false);
  452. }
  453. bool VisualScriptPropertySelector::SearchRunner::_is_class_disabled_by_feature_profile(const StringName &p_class) {
  454. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  455. if (profile.is_null()) {
  456. return false;
  457. }
  458. StringName class_name = p_class;
  459. while (class_name != StringName()) {
  460. if (!ClassDB::class_exists(class_name)) {
  461. return false;
  462. }
  463. if (profile->is_class_disabled(class_name)) {
  464. return true;
  465. }
  466. class_name = ClassDB::get_parent_class(class_name);
  467. }
  468. return false;
  469. }
  470. bool VisualScriptPropertySelector::SearchRunner::_is_class_disabled_by_scope(const StringName &p_class) {
  471. bool is_base_script = false;
  472. if (p_class == selector_ui->base_script) {
  473. is_base_script = true;
  474. }
  475. bool is_base = false;
  476. if (selector_ui->base_type == p_class) {
  477. is_base = true;
  478. }
  479. bool is_parent = false;
  480. if ((ClassDB::is_parent_class(selector_ui->base_type, p_class)) && !is_base) {
  481. is_parent = true;
  482. }
  483. bool is_inheriter = false;
  484. List<StringName> inheriters;
  485. ClassDB::get_inheriters_from_class(selector_ui->base_type, &inheriters);
  486. if (inheriters.find(p_class)) {
  487. is_inheriter = true;
  488. }
  489. if (scope_flags & SCOPE_BASE) {
  490. if (is_base_script || is_base || is_parent) {
  491. return false;
  492. }
  493. }
  494. if (scope_flags & SCOPE_INHERITERS) {
  495. if (is_base_script || is_base || is_inheriter) {
  496. return false;
  497. }
  498. }
  499. // if (scope_flags & SCOPE_RELATED) {
  500. // /* code */
  501. // }
  502. if (scope_flags & SCOPE_UNRELATED) {
  503. if (!is_base_script && !is_base && !is_inheriter) {
  504. return false;
  505. }
  506. }
  507. return true;
  508. }
  509. bool VisualScriptPropertySelector::SearchRunner::_slice() {
  510. bool phase_done = false;
  511. switch (phase) {
  512. case PHASE_INIT:
  513. phase_done = _phase_init();
  514. break;
  515. case PHASE_MATCH_CLASSES_INIT:
  516. phase_done = _phase_match_classes_init();
  517. break;
  518. case PHASE_NODE_CLASSES_INIT:
  519. phase_done = _phase_node_classes_init();
  520. break;
  521. case PHASE_NODE_CLASSES_BUILD:
  522. phase_done = _phase_node_classes_build();
  523. break;
  524. case PHASE_MATCH_CLASSES:
  525. phase_done = _phase_match_classes();
  526. break;
  527. case PHASE_CLASS_ITEMS_INIT:
  528. phase_done = _phase_class_items_init();
  529. break;
  530. case PHASE_CLASS_ITEMS:
  531. phase_done = _phase_class_items();
  532. break;
  533. case PHASE_MEMBER_ITEMS_INIT:
  534. phase_done = _phase_member_items_init();
  535. break;
  536. case PHASE_MEMBER_ITEMS:
  537. phase_done = _phase_member_items();
  538. break;
  539. case PHASE_SELECT_MATCH:
  540. phase_done = _phase_select_match();
  541. break;
  542. case PHASE_MAX:
  543. return true;
  544. default:
  545. WARN_PRINT("Invalid or unhandled phase in EditorHelpSearch::Runner, aborting search.");
  546. return true;
  547. };
  548. if (phase_done) {
  549. phase++;
  550. }
  551. return false;
  552. }
  553. bool VisualScriptPropertySelector::SearchRunner::_phase_init() {
  554. search_flags = 0; // selector_ui->filter_combo->get_selected_id();
  555. if (selector_ui->search_visual_script_nodes->is_pressed()) {
  556. search_flags |= SEARCH_VISUAL_SCRIPT_NODES;
  557. }
  558. if (selector_ui->search_classes->is_pressed()) {
  559. search_flags |= SEARCH_CLASSES;
  560. }
  561. // if (selector_ui->search_constructors->is_pressed()) {
  562. search_flags |= SEARCH_CONSTRUCTORS;
  563. // }
  564. if (selector_ui->search_methods->is_pressed()) {
  565. search_flags |= SEARCH_METHODS;
  566. }
  567. if (selector_ui->search_operators->is_pressed()) {
  568. search_flags |= SEARCH_OPERATORS;
  569. }
  570. if (selector_ui->search_signals->is_pressed()) {
  571. search_flags |= SEARCH_SIGNALS;
  572. }
  573. if (selector_ui->search_constants->is_pressed()) {
  574. search_flags |= SEARCH_CONSTANTS;
  575. }
  576. if (selector_ui->search_properties->is_pressed()) {
  577. search_flags |= SEARCH_PROPERTIES;
  578. }
  579. if (selector_ui->search_theme_items->is_pressed()) {
  580. search_flags |= SEARCH_THEME_ITEMS;
  581. }
  582. if (selector_ui->case_sensitive_button->is_pressed()) {
  583. search_flags |= SEARCH_CASE_SENSITIVE;
  584. }
  585. if (selector_ui->hierarchy_button->is_pressed()) {
  586. search_flags |= SEARCH_SHOW_HIERARCHY;
  587. }
  588. scope_flags = selector_ui->scope_combo->get_selected_id();
  589. return true;
  590. }
  591. bool VisualScriptPropertySelector::SearchRunner::_phase_match_classes_init() {
  592. combined_docs = EditorHelp::get_doc_data()->class_list;
  593. matches.clear();
  594. matched_item = nullptr;
  595. match_highest_score = 0;
  596. if (
  597. (selector_ui->base_script.unquote() != "") &&
  598. (selector_ui->base_script.unquote() != ".") &&
  599. !combined_docs.has(selector_ui->base_script)) {
  600. String file_path = "res://" + selector_ui->base_script.unquote(); // EditorHelp::get_doc_data().name to filepath
  601. Ref<Script> script;
  602. script = ResourceLoader::load(file_path);
  603. if (!script.is_null()) {
  604. DocData::ClassDoc class_doc = DocData::ClassDoc();
  605. class_doc.name = selector_ui->base_script;
  606. class_doc.inherits = script->get_instance_base_type();
  607. class_doc.brief_description = ".vs files not suported by EditorHelp::get_doc_data()";
  608. class_doc.description = "";
  609. Object *obj = ObjectDB::get_instance(script->get_instance_id());
  610. if (Object::cast_to<Script>(obj)) {
  611. List<MethodInfo> methods;
  612. Object::cast_to<Script>(obj)->get_script_method_list(&methods);
  613. for (List<MethodInfo>::Element *M = methods.front(); M; M = M->next()) {
  614. class_doc.methods.push_back(_get_method_doc(M->get()));
  615. }
  616. List<MethodInfo> signals;
  617. Object::cast_to<Script>(obj)->get_script_signal_list(&signals);
  618. for (List<MethodInfo>::Element *S = signals.front(); S; S = S->next()) {
  619. class_doc.signals.push_back(_get_method_doc(S->get()));
  620. }
  621. List<PropertyInfo> propertys;
  622. Object::cast_to<Script>(obj)->get_script_property_list(&propertys);
  623. for (List<PropertyInfo>::Element *P = propertys.front(); P; P = P->next()) {
  624. DocData::PropertyDoc pd = DocData::PropertyDoc();
  625. pd.name = P->get().name;
  626. pd.type = Variant::get_type_name(P->get().type);
  627. class_doc.properties.push_back(pd);
  628. }
  629. }
  630. combined_docs.insert(class_doc.name, class_doc);
  631. }
  632. }
  633. iterator_doc = combined_docs.front();
  634. return true;
  635. }
  636. bool VisualScriptPropertySelector::SearchRunner::_phase_node_classes_init() {
  637. VisualScriptLanguage::singleton->get_registered_node_names(&vs_nodes);
  638. _add_class_doc("functions", "", "");
  639. _add_class_doc("operators", "", "");
  640. return true;
  641. }
  642. bool VisualScriptPropertySelector::SearchRunner::_phase_node_classes_build() {
  643. if (vs_nodes.is_empty()) {
  644. return true;
  645. }
  646. String registerd_node_name = vs_nodes[0];
  647. vs_nodes.pop_front();
  648. Vector<String> path = registerd_node_name.split("/");
  649. if (path[0] == "constants") {
  650. _add_class_doc(registerd_node_name, "", "constants");
  651. } else if (path[0] == "custom") {
  652. _add_class_doc(registerd_node_name, "", "custom");
  653. } else if (path[0] == "data") {
  654. _add_class_doc(registerd_node_name, "", "data");
  655. } else if (path[0] == "flow_control") {
  656. _add_class_doc(registerd_node_name, "", "flow_control");
  657. } else if (path[0] == "functions") {
  658. if (path[1] == "built_in") {
  659. _add_class_doc(registerd_node_name, "functions", "built_in");
  660. } else if (path[1] == "by_type") {
  661. if (search_flags & SEARCH_CLASSES) {
  662. _add_class_doc(registerd_node_name, path[2], "by_type_class");
  663. }
  664. } else if (path[1] == "constructors") {
  665. if (search_flags & SEARCH_CLASSES) {
  666. _add_class_doc(registerd_node_name, path[2].substr(0, path[2].find_char('(')), "constructors_class");
  667. }
  668. } else if (path[1] == "deconstruct") {
  669. _add_class_doc(registerd_node_name, "", "deconstruct");
  670. } else if (path[1] == "wait") {
  671. _add_class_doc(registerd_node_name, "functions", "yield");
  672. } else {
  673. _add_class_doc(registerd_node_name, "functions", "");
  674. }
  675. } else if (path[0] == "index") {
  676. _add_class_doc(registerd_node_name, "", "index");
  677. } else if (path[0] == "operators") {
  678. if (path[1] == "bitwise") {
  679. _add_class_doc(registerd_node_name, "operators", "bitwise");
  680. } else if (path[1] == "compare") {
  681. _add_class_doc(registerd_node_name, "operators", "compare");
  682. } else if (path[1] == "logic") {
  683. _add_class_doc(registerd_node_name, "operators", "logic");
  684. } else if (path[1] == "math") {
  685. _add_class_doc(registerd_node_name, "operators", "math");
  686. } else {
  687. _add_class_doc(registerd_node_name, "operators", "");
  688. }
  689. }
  690. return false;
  691. }
  692. bool VisualScriptPropertySelector::SearchRunner::_phase_match_classes() {
  693. DocData::ClassDoc &class_doc = iterator_doc->value();
  694. if (
  695. (!_is_class_disabled_by_feature_profile(class_doc.name) && !_is_class_disabled_by_scope(class_doc.name)) ||
  696. _match_visual_script(class_doc)) {
  697. if (class_doc.inherits == "VisualScriptCustomNode") {
  698. class_doc.script_path = "res://" + class_doc.name.unquote();
  699. Ref<Script> script = ResourceLoader::load(class_doc.script_path);
  700. Ref<VisualScriptCustomNode> vsn;
  701. vsn.instantiate();
  702. vsn->set_script(script);
  703. class_doc.name = vsn->get_caption();
  704. if (combined_docs.has(vsn->get_category())) {
  705. class_doc.inherits = vsn->get_category();
  706. } else if (combined_docs.has("VisualScriptNode/" + vsn->get_category())) {
  707. class_doc.inherits = "VisualScriptNode/" + vsn->get_category();
  708. } else if (combined_docs.has("VisualScriptCustomNode/" + vsn->get_category())) {
  709. class_doc.inherits = "VisualScriptCustomNode/" + vsn->get_category();
  710. } else {
  711. class_doc.inherits = "";
  712. }
  713. class_doc.category = "VisualScriptCustomNode/" + vsn->get_category();
  714. class_doc.brief_description = "";
  715. class_doc.constructors.clear();
  716. class_doc.methods.clear();
  717. class_doc.operators.clear();
  718. class_doc.signals.clear();
  719. class_doc.constants.clear();
  720. class_doc.enums.clear();
  721. class_doc.properties.clear();
  722. class_doc.theme_properties.clear();
  723. }
  724. matches[class_doc.name] = ClassMatch();
  725. ClassMatch &match = matches[class_doc.name];
  726. match.category = class_doc.category;
  727. match.doc = &class_doc;
  728. // Match class name.
  729. if (search_flags & SEARCH_CLASSES || _match_visual_script(class_doc)) {
  730. if (term == "") {
  731. match.name = !_match_is_hidden(class_doc);
  732. } else {
  733. match.name = _match_string(term, class_doc.name);
  734. }
  735. // match.name = term == "" || _match_string(term, class_doc.name);
  736. }
  737. // Match members if the term is long enough.
  738. if (term.length() >= 0) {
  739. if (search_flags & SEARCH_CONSTRUCTORS) {
  740. for (int i = 0; i < class_doc.constructors.size(); i++) {
  741. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.constructors[i].name : class_doc.constructors[i].name.to_lower();
  742. if (method_name.find(term) > -1 ||
  743. term == " " ||
  744. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  745. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  746. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  747. match.constructors.push_back(const_cast<DocData::MethodDoc *>(&class_doc.constructors[i]));
  748. }
  749. }
  750. }
  751. if (search_flags & SEARCH_METHODS) {
  752. for (int i = 0; i < class_doc.methods.size(); i++) {
  753. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.methods[i].name : class_doc.methods[i].name.to_lower();
  754. if (method_name.find(term) > -1 ||
  755. term == " " ||
  756. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  757. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  758. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  759. match.methods.push_back(const_cast<DocData::MethodDoc *>(&class_doc.methods[i]));
  760. }
  761. }
  762. }
  763. if (search_flags & SEARCH_OPERATORS) {
  764. for (int i = 0; i < class_doc.operators.size(); i++) {
  765. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.operators[i].name : class_doc.operators[i].name.to_lower();
  766. if (method_name.find(term) > -1 ||
  767. term == " " ||
  768. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  769. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  770. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  771. match.operators.push_back(const_cast<DocData::MethodDoc *>(&class_doc.operators[i]));
  772. }
  773. }
  774. }
  775. if (search_flags & SEARCH_SIGNALS) {
  776. for (int i = 0; i < class_doc.signals.size(); i++) {
  777. if (_match_string(term, class_doc.signals[i].name) ||
  778. term == " ") {
  779. match.signals.push_back(const_cast<DocData::MethodDoc *>(&class_doc.signals[i]));
  780. }
  781. }
  782. }
  783. if (search_flags & SEARCH_CONSTANTS) {
  784. for (int i = 0; i < class_doc.constants.size(); i++) {
  785. if (_match_string(term, class_doc.constants[i].name) ||
  786. term == " ") {
  787. match.constants.push_back(const_cast<DocData::ConstantDoc *>(&class_doc.constants[i]));
  788. }
  789. }
  790. }
  791. if (search_flags & SEARCH_PROPERTIES) {
  792. for (int i = 0; i < class_doc.properties.size(); i++) {
  793. if (_match_string(term, class_doc.properties[i].name) ||
  794. term == " " ||
  795. _match_string(term, class_doc.properties[i].getter) ||
  796. _match_string(term, class_doc.properties[i].setter)) {
  797. match.properties.push_back(const_cast<DocData::PropertyDoc *>(&class_doc.properties[i]));
  798. }
  799. }
  800. }
  801. if (search_flags & SEARCH_THEME_ITEMS) {
  802. for (int i = 0; i < class_doc.theme_properties.size(); i++) {
  803. if (_match_string(term, class_doc.theme_properties[i].name) ||
  804. term == " ") {
  805. match.theme_properties.push_back(const_cast<DocData::ThemeItemDoc *>(&class_doc.theme_properties[i]));
  806. }
  807. }
  808. }
  809. }
  810. }
  811. iterator_doc = iterator_doc->next();
  812. return !iterator_doc;
  813. }
  814. bool VisualScriptPropertySelector::SearchRunner::_phase_class_items_init() {
  815. results_tree->clear();
  816. iterator_match = matches.front();
  817. root_item = results_tree->create_item();
  818. class_items.clear();
  819. return true;
  820. }
  821. bool VisualScriptPropertySelector::SearchRunner::_phase_class_items() {
  822. if (!iterator_match) {
  823. return true;
  824. }
  825. ClassMatch &match = iterator_match->value();
  826. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  827. if (match.required()) {
  828. _create_class_hierarchy(match);
  829. }
  830. } else {
  831. if (match.name) {
  832. _create_class_item(root_item, match.doc, true);
  833. }
  834. }
  835. iterator_match = iterator_match->next();
  836. return !iterator_match;
  837. }
  838. bool VisualScriptPropertySelector::SearchRunner::_phase_member_items_init() {
  839. iterator_match = matches.front();
  840. return true;
  841. }
  842. bool VisualScriptPropertySelector::SearchRunner::_phase_member_items() {
  843. if (!iterator_match) {
  844. return true;
  845. }
  846. ClassMatch &match = iterator_match->value();
  847. TreeItem *parent = (search_flags & SEARCH_SHOW_HIERARCHY) ? class_items[match.doc->name] : root_item;
  848. bool constructor_created = false;
  849. for (int i = 0; i < match.methods.size(); i++) {
  850. String text = match.methods[i]->name;
  851. if (!constructor_created) {
  852. if (match.doc->name == match.methods[i]->name) {
  853. text += " " + TTR("(constructors)");
  854. constructor_created = true;
  855. }
  856. } else {
  857. if (match.doc->name == match.methods[i]->name) {
  858. continue;
  859. }
  860. }
  861. _create_method_item(parent, match.doc, text, match.methods[i]);
  862. }
  863. for (int i = 0; i < match.signals.size(); i++) {
  864. _create_signal_item(parent, match.doc, match.signals[i]);
  865. }
  866. for (int i = 0; i < match.constants.size(); i++) {
  867. _create_constant_item(parent, match.doc, match.constants[i]);
  868. }
  869. for (int i = 0; i < match.properties.size(); i++) {
  870. _create_property_item(parent, match.doc, match.properties[i]);
  871. }
  872. for (int i = 0; i < match.theme_properties.size(); i++) {
  873. _create_theme_property_item(parent, match.doc, match.theme_properties[i]);
  874. }
  875. iterator_match = iterator_match->next();
  876. return !iterator_match;
  877. }
  878. bool VisualScriptPropertySelector::SearchRunner::_phase_select_match() {
  879. if (matched_item) {
  880. matched_item->select(0);
  881. }
  882. return true;
  883. }
  884. bool VisualScriptPropertySelector::SearchRunner::_match_string(const String &p_term, const String &p_string) const {
  885. if (search_flags & SEARCH_CASE_SENSITIVE) {
  886. return p_string.find(p_term) > -1;
  887. } else {
  888. return p_string.findn(p_term) > -1;
  889. }
  890. }
  891. bool VisualScriptPropertySelector::SearchRunner::_match_visual_script(DocData::ClassDoc &class_doc) {
  892. if (class_doc.category.ends_with("_class")) {
  893. if (class_doc.category.begins_with("VisualScript") && search_flags & SEARCH_CLASSES) {
  894. if (matches.has(class_doc.inherits)) {
  895. return true;
  896. }
  897. }
  898. return false;
  899. }
  900. if (class_doc.category.begins_with("VisualScript") && search_flags & SEARCH_VISUAL_SCRIPT_NODES) {
  901. return true;
  902. }
  903. if (class_doc.name.begins_with("operators") && search_flags & SEARCH_OPERATORS) {
  904. return true;
  905. }
  906. if (class_doc.category.begins_with("VisualScriptNode/deconstruct")) {
  907. if (class_doc.name.find(selector_ui->base_type, 0) > -1) {
  908. return true;
  909. }
  910. }
  911. return false;
  912. }
  913. bool VisualScriptPropertySelector::SearchRunner::_match_is_hidden(DocData::ClassDoc &class_doc) {
  914. if (class_doc.category.begins_with("VisualScript")) {
  915. if (class_doc.name.begins_with("flow_control")) {
  916. return false;
  917. } else if (class_doc.name.begins_with("operators")) {
  918. return !(search_flags & SEARCH_OPERATORS);
  919. } else if (class_doc.name.begins_with("functions/built_in/print")) {
  920. return false;
  921. }
  922. return true;
  923. }
  924. return false;
  925. }
  926. void VisualScriptPropertySelector::SearchRunner::_match_item(TreeItem *p_item, const String &p_text) {
  927. float inverse_length = 1.f / float(p_text.length());
  928. // Favor types where search term is a substring close to the start of the type.
  929. float w = 0.5f;
  930. int pos = p_text.findn(term);
  931. float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.f, .9f - w);
  932. // Favor shorter items: they resemble the search term more.
  933. w = 0.1f;
  934. score *= (1 - w) + w * (term.length() * inverse_length);
  935. if (match_highest_score == 0 || score > match_highest_score) {
  936. matched_item = p_item;
  937. match_highest_score = score;
  938. }
  939. }
  940. void VisualScriptPropertySelector::SearchRunner::_add_class_doc(String class_name, String inherits, String category) {
  941. DocData::ClassDoc class_doc = DocData::ClassDoc();
  942. class_doc.name = class_name;
  943. class_doc.inherits = inherits;
  944. class_doc.category = "VisualScriptNode/" + category;
  945. class_doc.brief_description = category;
  946. combined_docs.insert(class_doc.name, class_doc);
  947. }
  948. DocData::MethodDoc VisualScriptPropertySelector::SearchRunner::_get_method_doc(MethodInfo method_info) {
  949. DocData::MethodDoc method_doc = DocData::MethodDoc();
  950. method_doc.name = method_info.name;
  951. method_doc.return_type = Variant::get_type_name(method_info.return_val.type);
  952. method_doc.description = "No description available";
  953. for (List<PropertyInfo>::Element *P = method_info.arguments.front(); P; P = P->next()) {
  954. DocData::ArgumentDoc argument_doc = DocData::ArgumentDoc();
  955. argument_doc.name = P->get().name;
  956. argument_doc.type = Variant::get_type_name(P->get().type);
  957. method_doc.arguments.push_back(argument_doc);
  958. }
  959. return method_doc;
  960. }
  961. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_class_hierarchy(const ClassMatch &p_match) {
  962. if (class_items.has(p_match.doc->name)) {
  963. return class_items[p_match.doc->name];
  964. }
  965. // Ensure parent nodes are created first.
  966. TreeItem *parent = root_item;
  967. if (p_match.doc->inherits != "") {
  968. if (class_items.has(p_match.doc->inherits)) {
  969. parent = class_items[p_match.doc->inherits];
  970. } else if (matches.has(p_match.doc->inherits)) {
  971. ClassMatch &base_match = matches[p_match.doc->inherits];
  972. parent = _create_class_hierarchy(base_match);
  973. }
  974. }
  975. TreeItem *class_item = _create_class_item(parent, p_match.doc, !p_match.name);
  976. class_items[p_match.doc->name] = class_item;
  977. return class_item;
  978. }
  979. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_class_item(TreeItem *p_parent, const DocData::ClassDoc *p_doc, bool p_gray) {
  980. Ref<Texture2D> icon = empty_icon;
  981. String text_0 = p_doc->name;
  982. String text_1 = "Class";
  983. String what = "Class";
  984. String details = p_doc->name;
  985. if (p_doc->category.begins_with("VisualScriptCustomNode/")) {
  986. Vector<String> path = p_doc->name.split("/");
  987. icon = ui_service->get_theme_icon(SNAME("VisualScript"), SNAME("EditorIcons"));
  988. text_0 = path[path.size() - 1];
  989. text_1 = "VisualScriptCustomNode";
  990. what = "VisualScriptCustomNode";
  991. details = "CustomNode";
  992. } else if (p_doc->category.begins_with("VisualScriptNode/")) {
  993. Vector<String> path = p_doc->name.split("/");
  994. icon = ui_service->get_theme_icon(SNAME("VisualScript"), SNAME("EditorIcons"));
  995. text_0 = path[path.size() - 1];
  996. if (p_doc->category.begins_with("VisualScriptNode/deconstruct")) {
  997. text_0 = "deconstruct " + text_0;
  998. }
  999. text_1 = "VisualScriptNode";
  1000. what = "VisualScriptNode";
  1001. details = p_doc->name;
  1002. if (path.size() == 1) {
  1003. if (path[0] == "functions" || path[0] == "operators") {
  1004. text_1 = "VisualScript";
  1005. p_gray = true;
  1006. what = "no_result";
  1007. details = "";
  1008. }
  1009. }
  1010. } else {
  1011. if (p_doc->name.is_quoted()) {
  1012. text_0 = p_doc->name.unquote().get_file();
  1013. if (ui_service->has_theme_icon(p_doc->inherits, "EditorIcons")) {
  1014. icon = ui_service->get_theme_icon(p_doc->inherits, "EditorIcons");
  1015. }
  1016. } else if (ui_service->has_theme_icon(p_doc->name, "EditorIcons")) {
  1017. icon = ui_service->get_theme_icon(p_doc->name, "EditorIcons");
  1018. } else if (ClassDB::class_exists(p_doc->name) && ClassDB::is_parent_class(p_doc->name, "Object")) {
  1019. icon = ui_service->get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  1020. }
  1021. }
  1022. String tooltip = p_doc->brief_description.strip_edges();
  1023. TreeItem *item = results_tree->create_item(p_parent);
  1024. item->set_icon(0, icon);
  1025. item->set_text(0, text_0);
  1026. item->set_text(1, TTR(text_1));
  1027. item->set_tooltip(0, tooltip);
  1028. item->set_tooltip(1, tooltip);
  1029. item->set_metadata(0, details);
  1030. item->set_metadata(1, what);
  1031. if (p_gray) {
  1032. item->set_custom_color(0, disabled_color);
  1033. item->set_custom_color(1, disabled_color);
  1034. }
  1035. _match_item(item, p_doc->name);
  1036. return item;
  1037. }
  1038. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_method_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const String &p_text, const DocData::MethodDoc *p_doc) {
  1039. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  1040. for (int i = 0; i < p_doc->arguments.size(); i++) {
  1041. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  1042. tooltip += arg.type + " " + arg.name;
  1043. if (arg.default_value != "") {
  1044. tooltip += " = " + arg.default_value;
  1045. }
  1046. if (i < p_doc->arguments.size() - 1) {
  1047. tooltip += ", ";
  1048. }
  1049. }
  1050. tooltip += ")";
  1051. return _create_member_item(p_parent, p_class_doc->name, "MemberMethod", p_doc->name, p_text, TTRC("Method"), "method", tooltip, p_doc->description);
  1052. }
  1053. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_signal_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) {
  1054. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  1055. for (int i = 0; i < p_doc->arguments.size(); i++) {
  1056. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  1057. tooltip += arg.type + " " + arg.name;
  1058. if (arg.default_value != "") {
  1059. tooltip += " = " + arg.default_value;
  1060. }
  1061. if (i < p_doc->arguments.size() - 1) {
  1062. tooltip += ", ";
  1063. }
  1064. }
  1065. tooltip += ")";
  1066. return _create_member_item(p_parent, p_class_doc->name, "MemberSignal", p_doc->name, p_doc->name, TTRC("Signal"), "signal", tooltip, p_doc->description);
  1067. }
  1068. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_constant_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ConstantDoc *p_doc) {
  1069. String tooltip = p_class_doc->name + "." + p_doc->name;
  1070. return _create_member_item(p_parent, p_class_doc->name, "MemberConstant", p_doc->name, p_doc->name, TTRC("Constant"), "constant", tooltip, p_doc->description);
  1071. }
  1072. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::PropertyDoc *p_doc) {
  1073. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  1074. tooltip += "\n " + p_class_doc->name + "." + p_doc->setter + "(value) setter";
  1075. tooltip += "\n " + p_class_doc->name + "." + p_doc->getter + "() getter";
  1076. return _create_member_item(p_parent, p_class_doc->name, "MemberProperty", p_doc->name, p_doc->name, TTRC("Property"), "property", tooltip, p_doc->description);
  1077. }
  1078. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_theme_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ThemeItemDoc *p_doc) {
  1079. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  1080. return _create_member_item(p_parent, p_class_doc->name, "MemberTheme", p_doc->name, p_doc->name, TTRC("Theme Property"), "theme_item", tooltip, p_doc->description);
  1081. }
  1082. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_member_item(TreeItem *p_parent, const String &p_class_name, const String &p_icon, const String &p_name, const String &p_text, const String &p_type, const String &p_metatype, const String &p_tooltip, const String &p_description) {
  1083. Ref<Texture2D> icon;
  1084. String text;
  1085. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  1086. icon = ui_service->get_theme_icon(p_icon, SNAME("EditorIcons"));
  1087. text = p_text;
  1088. } else {
  1089. icon = ui_service->get_theme_icon(p_icon, SNAME("EditorIcons"));
  1090. text = p_class_name + "." + p_text;
  1091. }
  1092. TreeItem *item = results_tree->create_item(p_parent);
  1093. item->set_icon(0, icon);
  1094. item->set_text(0, text);
  1095. item->set_text(1, TTRGET(p_type));
  1096. item->set_tooltip(0, p_tooltip);
  1097. item->set_tooltip(1, p_tooltip);
  1098. item->set_metadata(0, p_class_name + ":" + p_name);
  1099. item->set_metadata(1, "class_" + p_metatype);
  1100. item->set_meta("description", p_description);
  1101. _match_item(item, p_name);
  1102. return item;
  1103. }
  1104. bool VisualScriptPropertySelector::SearchRunner::work(uint64_t slot) {
  1105. // Return true when the search has been completed, otherwise false.
  1106. const uint64_t until = OS::get_singleton()->get_ticks_usec() + slot;
  1107. while (!_slice()) {
  1108. if (OS::get_singleton()->get_ticks_usec() > until) {
  1109. return false;
  1110. }
  1111. }
  1112. return true;
  1113. }
  1114. VisualScriptPropertySelector::SearchRunner::SearchRunner(VisualScriptPropertySelector *p_selector_ui, Tree *p_results_tree) :
  1115. selector_ui(p_selector_ui),
  1116. ui_service(p_selector_ui->vbox),
  1117. results_tree(p_results_tree),
  1118. term(p_selector_ui->search_box->get_text()),
  1119. empty_icon(ui_service->get_theme_icon(SNAME("ArrowRight"), SNAME("EditorIcons"))),
  1120. disabled_color(ui_service->get_theme_color(SNAME("disabled_font_color"), SNAME("Editor"))) {
  1121. }