visual_script_property_selector.cpp 45 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_scale.h"
  40. #include "scene/main/node.h"
  41. #include "scene/main/window.h"
  42. void VisualScriptPropertySelector::_update_icons() {
  43. search_box->set_right_icon(results_tree->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  44. search_box->set_clear_button_enabled(true);
  45. search_box->add_theme_icon_override("right_icon", results_tree->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  46. search_visual_script_nodes->set_icon(results_tree->get_theme_icon(SNAME("VisualScript"), SNAME("EditorIcons")));
  47. search_classes->set_icon(results_tree->get_theme_icon(SNAME("Object"), SNAME("EditorIcons")));
  48. search_methods->set_icon(results_tree->get_theme_icon(SNAME("MemberMethod"), SNAME("EditorIcons")));
  49. search_operators->set_icon(results_tree->get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  50. search_signals->set_icon(results_tree->get_theme_icon(SNAME("MemberSignal"), SNAME("EditorIcons")));
  51. search_constants->set_icon(results_tree->get_theme_icon(SNAME("MemberConstant"), SNAME("EditorIcons")));
  52. search_properties->set_icon(results_tree->get_theme_icon(SNAME("MemberProperty"), SNAME("EditorIcons")));
  53. search_theme_items->set_icon(results_tree->get_theme_icon(SNAME("MemberTheme"), SNAME("EditorIcons")));
  54. case_sensitive_button->set_icon(results_tree->get_theme_icon(SNAME("MatchCase"), SNAME("EditorIcons")));
  55. hierarchy_button->set_icon(results_tree->get_theme_icon(SNAME("ClassList"), SNAME("EditorIcons")));
  56. }
  57. void VisualScriptPropertySelector::_sbox_input(const Ref<InputEvent> &p_ie) {
  58. Ref<InputEventKey> k = p_ie;
  59. if (k.is_valid()) {
  60. switch (k->get_keycode()) {
  61. case Key::UP:
  62. case Key::DOWN:
  63. case Key::PAGEUP:
  64. case Key::PAGEDOWN: {
  65. results_tree->gui_input(k);
  66. search_box->accept_event();
  67. } break;
  68. default:
  69. break;
  70. }
  71. }
  72. }
  73. void VisualScriptPropertySelector::_update_results_i(int p_int) {
  74. _update_results();
  75. }
  76. void VisualScriptPropertySelector::_update_results_s(String p_string) {
  77. _update_results();
  78. }
  79. void VisualScriptPropertySelector::_update_results_search_all() {
  80. if (search_classes->is_pressed()) {
  81. scope_combo->select(COMBO_ALL);
  82. }
  83. _update_results();
  84. }
  85. void VisualScriptPropertySelector::_update_results() {
  86. _update_icons();
  87. search_runner = Ref<SearchRunner>(memnew(SearchRunner(this, results_tree)));
  88. set_process(true);
  89. }
  90. void VisualScriptPropertySelector::_confirmed() {
  91. TreeItem *ti = results_tree->get_selected();
  92. if (!ti) {
  93. return;
  94. }
  95. emit_signal(SNAME("selected"), ti->get_metadata(0), ti->get_metadata(1), connecting);
  96. set_visible(false);
  97. }
  98. void VisualScriptPropertySelector::_item_selected() {
  99. help_bit->set_text(results_tree->get_selected()->get_meta("description", "No description available"));
  100. }
  101. void VisualScriptPropertySelector::_hide_requested() {
  102. _cancel_pressed(); // From AcceptDialog.
  103. }
  104. void VisualScriptPropertySelector::_notification(int p_what) {
  105. switch (p_what) {
  106. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  107. _update_icons();
  108. } break;
  109. case NOTIFICATION_ENTER_TREE: {
  110. connect("confirmed", callable_mp(this, &VisualScriptPropertySelector::_confirmed));
  111. } break;
  112. case NOTIFICATION_PROCESS: {
  113. // Update background search.
  114. if (search_runner.is_valid()) {
  115. if (search_runner->work()) {
  116. // Search done.
  117. get_ok_button()->set_disabled(!results_tree->get_selected());
  118. search_runner = Ref<SearchRunner>();
  119. set_process(false);
  120. }
  121. } else {
  122. // if one is valid
  123. set_process(false);
  124. }
  125. } break;
  126. }
  127. }
  128. void VisualScriptPropertySelector::select_method_from_base_type(const String &p_base, const bool p_virtuals_only, const bool p_connecting, bool clear_text) {
  129. set_title(TTR("Select method from base type"));
  130. base_type = p_base;
  131. base_script = "";
  132. type = Variant::NIL;
  133. connecting = p_connecting;
  134. if (clear_text) {
  135. if (p_virtuals_only) {
  136. search_box->set_text("._"); // show all _methods
  137. search_box->set_caret_column(2);
  138. } else {
  139. search_box->set_text("."); // show all methods
  140. search_box->set_caret_column(1);
  141. }
  142. }
  143. search_visual_script_nodes->set_pressed(false);
  144. search_classes->set_pressed(false);
  145. search_methods->set_pressed(true);
  146. search_operators->set_pressed(false);
  147. search_signals->set_pressed(false);
  148. search_constants->set_pressed(false);
  149. search_properties->set_pressed(false);
  150. search_theme_items->set_pressed(false);
  151. scope_combo->select(COMBO_BASE);
  152. results_tree->clear();
  153. show_window(.5f);
  154. search_box->grab_focus();
  155. _update_results();
  156. }
  157. 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) {
  158. set_title(TTR("Select from base type"));
  159. base_type = p_base;
  160. base_script = p_base_script.lstrip("res://").quote(); // filepath to EditorHelp::get_doc_data().name
  161. type = Variant::NIL;
  162. connecting = p_connecting;
  163. if (clear_text) {
  164. if (p_virtuals_only) {
  165. search_box->set_text("_");
  166. } else {
  167. search_box->set_text(" ");
  168. }
  169. }
  170. search_box->select_all();
  171. search_visual_script_nodes->set_pressed(false);
  172. search_classes->set_pressed(false);
  173. search_methods->set_pressed(true);
  174. search_operators->set_pressed(false);
  175. search_signals->set_pressed(true);
  176. search_constants->set_pressed(false);
  177. search_properties->set_pressed(true);
  178. search_theme_items->set_pressed(false);
  179. scope_combo->select(COMBO_RELATED);
  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(COMBO_BASE);
  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(COMBO_BASE);
  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(COMBO_RELATED);
  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(COMBO_BASE);
  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(COMBO_BASE);
  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_search_all));
  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 filtered options\nEnter \".\" to show all filtered 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 supported 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> properties;
  622. Object::cast_to<Script>(obj)->get_script_property_list(&properties);
  623. for (List<PropertyInfo>::Element *P = properties.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 registered_node_name = vs_nodes[0];
  647. vs_nodes.pop_front();
  648. Vector<String> path = registered_node_name.split("/");
  649. if (path[0] == "constants") {
  650. _add_class_doc(registered_node_name, "", "constants");
  651. } else if (path[0] == "custom") {
  652. _add_class_doc(registered_node_name, "", "custom");
  653. } else if (path[0] == "data") {
  654. _add_class_doc(registered_node_name, "", "data");
  655. } else if (path[0] == "flow_control") {
  656. _add_class_doc(registered_node_name, "", "flow_control");
  657. } else if (path[0] == "functions") {
  658. if (path[1] == "built_in") {
  659. _add_class_doc(registered_node_name, "functions", "built_in");
  660. } else if (path[1] == "by_type") {
  661. // No action is required.
  662. // Using function references from ClassDB to remove confusion for users.
  663. } else if (path[1] == "constructors") {
  664. _add_class_doc(registered_node_name, "", "constructors");
  665. } else if (path[1] == "deconstruct") {
  666. _add_class_doc(registered_node_name, "", "deconstruct");
  667. } else if (path[1] == "wait") {
  668. _add_class_doc(registered_node_name, "functions", "yield");
  669. } else {
  670. _add_class_doc(registered_node_name, "functions", "");
  671. }
  672. } else if (path[0] == "index") {
  673. _add_class_doc(registered_node_name, "", "index");
  674. } else if (path[0] == "operators") {
  675. if (path[1] == "bitwise") {
  676. _add_class_doc(registered_node_name, "operators", "bitwise");
  677. } else if (path[1] == "compare") {
  678. _add_class_doc(registered_node_name, "operators", "compare");
  679. } else if (path[1] == "logic") {
  680. _add_class_doc(registered_node_name, "operators", "logic");
  681. } else if (path[1] == "math") {
  682. _add_class_doc(registered_node_name, "operators", "math");
  683. } else {
  684. _add_class_doc(registered_node_name, "operators", "");
  685. }
  686. }
  687. return false;
  688. }
  689. bool VisualScriptPropertySelector::SearchRunner::_phase_match_classes() {
  690. DocData::ClassDoc &class_doc = iterator_doc->value();
  691. if (
  692. (!_is_class_disabled_by_feature_profile(class_doc.name) && !_is_class_disabled_by_scope(class_doc.name)) ||
  693. _match_visual_script(class_doc)) {
  694. if (class_doc.inherits == "VisualScriptCustomNode") {
  695. class_doc.script_path = "res://" + class_doc.name.unquote();
  696. Ref<Script> script = ResourceLoader::load(class_doc.script_path);
  697. Ref<VisualScriptCustomNode> vsn;
  698. vsn.instantiate();
  699. vsn->set_script(script);
  700. class_doc.name = vsn->get_caption();
  701. if (combined_docs.has(vsn->get_category())) {
  702. class_doc.inherits = vsn->get_category();
  703. } else if (combined_docs.has("VisualScriptNode/" + vsn->get_category())) {
  704. class_doc.inherits = "VisualScriptNode/" + vsn->get_category();
  705. } else if (combined_docs.has("VisualScriptCustomNode/" + vsn->get_category())) {
  706. class_doc.inherits = "VisualScriptCustomNode/" + vsn->get_category();
  707. } else {
  708. class_doc.inherits = "";
  709. }
  710. class_doc.category = "VisualScriptCustomNode/" + vsn->get_category();
  711. class_doc.brief_description = "";
  712. class_doc.constructors.clear();
  713. class_doc.methods.clear();
  714. class_doc.operators.clear();
  715. class_doc.signals.clear();
  716. class_doc.constants.clear();
  717. class_doc.enums.clear();
  718. class_doc.properties.clear();
  719. class_doc.theme_properties.clear();
  720. }
  721. matches[class_doc.name] = ClassMatch();
  722. ClassMatch &match = matches[class_doc.name];
  723. match.category = class_doc.category;
  724. match.doc = &class_doc;
  725. // Match class name.
  726. if (search_flags & SEARCH_CLASSES || _match_visual_script(class_doc)) {
  727. if (term == "") {
  728. match.name = !_match_is_hidden(class_doc);
  729. } else {
  730. match.name = _match_string(term, class_doc.name);
  731. }
  732. // match.name = term == "" || _match_string(term, class_doc.name);
  733. }
  734. // Match members if the term is long enough.
  735. if (term.length() >= 0) {
  736. if (search_flags & SEARCH_CONSTRUCTORS) {
  737. for (int i = 0; i < class_doc.constructors.size(); i++) {
  738. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.constructors[i].name : class_doc.constructors[i].name.to_lower();
  739. if (method_name.find(term) > -1 ||
  740. term == " " ||
  741. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  742. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  743. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  744. match.constructors.push_back(const_cast<DocData::MethodDoc *>(&class_doc.constructors[i]));
  745. }
  746. }
  747. }
  748. if (search_flags & SEARCH_METHODS) {
  749. for (int i = 0; i < class_doc.methods.size(); i++) {
  750. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.methods[i].name : class_doc.methods[i].name.to_lower();
  751. if (method_name.find(term) > -1 ||
  752. term == " " ||
  753. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  754. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  755. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  756. match.methods.push_back(const_cast<DocData::MethodDoc *>(&class_doc.methods[i]));
  757. }
  758. }
  759. }
  760. if (search_flags & SEARCH_OPERATORS) {
  761. for (int i = 0; i < class_doc.operators.size(); i++) {
  762. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.operators[i].name : class_doc.operators[i].name.to_lower();
  763. if (method_name.find(term) > -1 ||
  764. term == " " ||
  765. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  766. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  767. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  768. match.operators.push_back(const_cast<DocData::MethodDoc *>(&class_doc.operators[i]));
  769. }
  770. }
  771. }
  772. if (search_flags & SEARCH_SIGNALS) {
  773. for (int i = 0; i < class_doc.signals.size(); i++) {
  774. if (_match_string(term, class_doc.signals[i].name) ||
  775. term == " ") {
  776. match.signals.push_back(const_cast<DocData::MethodDoc *>(&class_doc.signals[i]));
  777. }
  778. }
  779. }
  780. if (search_flags & SEARCH_CONSTANTS) {
  781. for (int i = 0; i < class_doc.constants.size(); i++) {
  782. if (_match_string(term, class_doc.constants[i].name) ||
  783. term == " ") {
  784. match.constants.push_back(const_cast<DocData::ConstantDoc *>(&class_doc.constants[i]));
  785. }
  786. }
  787. }
  788. if (search_flags & SEARCH_PROPERTIES) {
  789. for (int i = 0; i < class_doc.properties.size(); i++) {
  790. if (_match_string(term, class_doc.properties[i].name) ||
  791. term == " " ||
  792. _match_string(term, class_doc.properties[i].getter) ||
  793. _match_string(term, class_doc.properties[i].setter)) {
  794. match.properties.push_back(const_cast<DocData::PropertyDoc *>(&class_doc.properties[i]));
  795. }
  796. }
  797. }
  798. if (search_flags & SEARCH_THEME_ITEMS) {
  799. for (int i = 0; i < class_doc.theme_properties.size(); i++) {
  800. if (_match_string(term, class_doc.theme_properties[i].name) ||
  801. term == " ") {
  802. match.theme_properties.push_back(const_cast<DocData::ThemeItemDoc *>(&class_doc.theme_properties[i]));
  803. }
  804. }
  805. }
  806. }
  807. }
  808. iterator_doc = iterator_doc->next();
  809. return !iterator_doc;
  810. }
  811. bool VisualScriptPropertySelector::SearchRunner::_phase_class_items_init() {
  812. results_tree->clear();
  813. iterator_match = matches.front();
  814. root_item = results_tree->create_item();
  815. class_items.clear();
  816. return true;
  817. }
  818. bool VisualScriptPropertySelector::SearchRunner::_phase_class_items() {
  819. if (!iterator_match) {
  820. return true;
  821. }
  822. ClassMatch &match = iterator_match->value();
  823. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  824. if (match.required()) {
  825. _create_class_hierarchy(match);
  826. }
  827. } else {
  828. if (match.name) {
  829. _create_class_item(root_item, match.doc, true);
  830. }
  831. }
  832. iterator_match = iterator_match->next();
  833. return !iterator_match;
  834. }
  835. bool VisualScriptPropertySelector::SearchRunner::_phase_member_items_init() {
  836. iterator_match = matches.front();
  837. return true;
  838. }
  839. bool VisualScriptPropertySelector::SearchRunner::_phase_member_items() {
  840. if (!iterator_match) {
  841. return true;
  842. }
  843. ClassMatch &match = iterator_match->value();
  844. TreeItem *parent = (search_flags & SEARCH_SHOW_HIERARCHY) ? class_items[match.doc->name] : root_item;
  845. bool constructor_created = false;
  846. for (int i = 0; i < match.methods.size(); i++) {
  847. String text = match.methods[i]->name;
  848. if (!constructor_created) {
  849. if (match.doc->name == match.methods[i]->name) {
  850. text += " " + TTR("(constructors)");
  851. constructor_created = true;
  852. }
  853. } else {
  854. if (match.doc->name == match.methods[i]->name) {
  855. continue;
  856. }
  857. }
  858. _create_method_item(parent, match.doc, text, match.methods[i]);
  859. }
  860. for (int i = 0; i < match.signals.size(); i++) {
  861. _create_signal_item(parent, match.doc, match.signals[i]);
  862. }
  863. for (int i = 0; i < match.constants.size(); i++) {
  864. _create_constant_item(parent, match.doc, match.constants[i]);
  865. }
  866. for (int i = 0; i < match.properties.size(); i++) {
  867. _create_property_item(parent, match.doc, match.properties[i]);
  868. }
  869. for (int i = 0; i < match.theme_properties.size(); i++) {
  870. _create_theme_property_item(parent, match.doc, match.theme_properties[i]);
  871. }
  872. iterator_match = iterator_match->next();
  873. return !iterator_match;
  874. }
  875. bool VisualScriptPropertySelector::SearchRunner::_phase_select_match() {
  876. if (matched_item) {
  877. matched_item->select(0);
  878. }
  879. return true;
  880. }
  881. bool VisualScriptPropertySelector::SearchRunner::_match_string(const String &p_term, const String &p_string) const {
  882. if (search_flags & SEARCH_CASE_SENSITIVE) {
  883. return p_string.find(p_term) > -1;
  884. } else {
  885. return p_string.findn(p_term) > -1;
  886. }
  887. }
  888. bool VisualScriptPropertySelector::SearchRunner::_match_visual_script(DocData::ClassDoc &class_doc) {
  889. if (class_doc.category.ends_with("_class")) {
  890. if (class_doc.category.begins_with("VisualScript") && search_flags & SEARCH_CLASSES) {
  891. if (matches.has(class_doc.inherits)) {
  892. return true;
  893. }
  894. }
  895. return false;
  896. }
  897. if (class_doc.category.begins_with("VisualScript") && search_flags & SEARCH_VISUAL_SCRIPT_NODES) {
  898. return true;
  899. }
  900. if (class_doc.name.begins_with("operators") && search_flags & SEARCH_OPERATORS) {
  901. return true;
  902. }
  903. if (class_doc.category.begins_with("VisualScriptNode/deconstruct")) {
  904. if (class_doc.name.find(selector_ui->base_type, 0) > -1) {
  905. return true;
  906. }
  907. }
  908. return false;
  909. }
  910. bool VisualScriptPropertySelector::SearchRunner::_match_is_hidden(DocData::ClassDoc &class_doc) {
  911. if (class_doc.category.begins_with("VisualScript")) {
  912. if (class_doc.name.begins_with("flow_control")) {
  913. return false;
  914. } else if (class_doc.name.begins_with("operators")) {
  915. return !(search_flags & SEARCH_OPERATORS);
  916. } else if (class_doc.name.begins_with("functions/built_in/print")) {
  917. return false;
  918. }
  919. return true;
  920. }
  921. return false;
  922. }
  923. void VisualScriptPropertySelector::SearchRunner::_match_item(TreeItem *p_item, const String &p_text) {
  924. float inverse_length = 1.f / float(p_text.length());
  925. // Favor types where search term is a substring close to the start of the type.
  926. float w = 0.5f;
  927. int pos = p_text.findn(term);
  928. float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.f, .9f - w);
  929. // Favor shorter items: they resemble the search term more.
  930. w = 0.1f;
  931. score *= (1 - w) + w * (term.length() * inverse_length);
  932. if (match_highest_score == 0 || score > match_highest_score) {
  933. matched_item = p_item;
  934. match_highest_score = score;
  935. }
  936. }
  937. void VisualScriptPropertySelector::SearchRunner::_add_class_doc(String class_name, String inherits, String category) {
  938. DocData::ClassDoc class_doc = DocData::ClassDoc();
  939. class_doc.name = class_name;
  940. class_doc.inherits = inherits;
  941. class_doc.category = "VisualScriptNode/" + category;
  942. class_doc.brief_description = category;
  943. combined_docs.insert(class_doc.name, class_doc);
  944. }
  945. DocData::MethodDoc VisualScriptPropertySelector::SearchRunner::_get_method_doc(MethodInfo method_info) {
  946. DocData::MethodDoc method_doc = DocData::MethodDoc();
  947. method_doc.name = method_info.name;
  948. method_doc.return_type = Variant::get_type_name(method_info.return_val.type);
  949. method_doc.description = "No description available";
  950. for (List<PropertyInfo>::Element *P = method_info.arguments.front(); P; P = P->next()) {
  951. DocData::ArgumentDoc argument_doc = DocData::ArgumentDoc();
  952. argument_doc.name = P->get().name;
  953. argument_doc.type = Variant::get_type_name(P->get().type);
  954. method_doc.arguments.push_back(argument_doc);
  955. }
  956. return method_doc;
  957. }
  958. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_class_hierarchy(const ClassMatch &p_match) {
  959. if (class_items.has(p_match.doc->name)) {
  960. return class_items[p_match.doc->name];
  961. }
  962. // Ensure parent nodes are created first.
  963. TreeItem *parent = root_item;
  964. if (p_match.doc->inherits != "") {
  965. if (class_items.has(p_match.doc->inherits)) {
  966. parent = class_items[p_match.doc->inherits];
  967. } else if (matches.has(p_match.doc->inherits)) {
  968. ClassMatch &base_match = matches[p_match.doc->inherits];
  969. parent = _create_class_hierarchy(base_match);
  970. }
  971. }
  972. TreeItem *class_item = _create_class_item(parent, p_match.doc, !p_match.name);
  973. class_items[p_match.doc->name] = class_item;
  974. return class_item;
  975. }
  976. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_class_item(TreeItem *p_parent, const DocData::ClassDoc *p_doc, bool p_gray) {
  977. Ref<Texture2D> icon = empty_icon;
  978. String text_0 = p_doc->name;
  979. String text_1 = "Class";
  980. String what = "Class";
  981. String details = p_doc->name;
  982. if (p_doc->category.begins_with("VisualScriptCustomNode/")) {
  983. Vector<String> path = p_doc->name.split("/");
  984. icon = ui_service->get_theme_icon(SNAME("VisualScript"), SNAME("EditorIcons"));
  985. text_0 = path[path.size() - 1];
  986. text_1 = "VisualScriptCustomNode";
  987. what = "VisualScriptCustomNode";
  988. details = "CustomNode";
  989. } else if (p_doc->category.begins_with("VisualScriptNode/")) {
  990. Vector<String> path = p_doc->name.split("/");
  991. icon = ui_service->get_theme_icon(SNAME("VisualScript"), SNAME("EditorIcons"));
  992. text_0 = path[path.size() - 1];
  993. if (p_doc->category.begins_with("VisualScriptNode/deconstruct")) {
  994. text_0 = "deconstruct " + text_0;
  995. }
  996. text_1 = "VisualScriptNode";
  997. what = "VisualScriptNode";
  998. details = p_doc->name;
  999. if (path.size() == 1) {
  1000. if (path[0] == "functions" || path[0] == "operators") {
  1001. text_1 = "VisualScript";
  1002. p_gray = true;
  1003. what = "no_result";
  1004. details = "";
  1005. }
  1006. }
  1007. } else {
  1008. if (p_doc->name.is_quoted()) {
  1009. text_0 = p_doc->name.unquote().get_file();
  1010. if (ui_service->has_theme_icon(p_doc->inherits, "EditorIcons")) {
  1011. icon = ui_service->get_theme_icon(p_doc->inherits, "EditorIcons");
  1012. }
  1013. } else if (ui_service->has_theme_icon(p_doc->name, "EditorIcons")) {
  1014. icon = ui_service->get_theme_icon(p_doc->name, "EditorIcons");
  1015. } else if (ClassDB::class_exists(p_doc->name) && ClassDB::is_parent_class(p_doc->name, "Object")) {
  1016. icon = ui_service->get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  1017. }
  1018. }
  1019. String tooltip = p_doc->brief_description.strip_edges();
  1020. TreeItem *item = results_tree->create_item(p_parent);
  1021. item->set_icon(0, icon);
  1022. item->set_text(0, text_0);
  1023. item->set_text(1, TTR(text_1));
  1024. item->set_tooltip(0, tooltip);
  1025. item->set_tooltip(1, tooltip);
  1026. item->set_metadata(0, details);
  1027. item->set_metadata(1, what);
  1028. if (p_gray) {
  1029. item->set_custom_color(0, disabled_color);
  1030. item->set_custom_color(1, disabled_color);
  1031. }
  1032. _match_item(item, p_doc->name);
  1033. return item;
  1034. }
  1035. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_method_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const String &p_text, const DocData::MethodDoc *p_doc) {
  1036. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  1037. for (int i = 0; i < p_doc->arguments.size(); i++) {
  1038. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  1039. tooltip += arg.type + " " + arg.name;
  1040. if (arg.default_value != "") {
  1041. tooltip += " = " + arg.default_value;
  1042. }
  1043. if (i < p_doc->arguments.size() - 1) {
  1044. tooltip += ", ";
  1045. }
  1046. }
  1047. tooltip += ")";
  1048. return _create_member_item(p_parent, p_class_doc->name, "MemberMethod", p_doc->name, p_text, TTRC("Method"), "method", tooltip, p_doc->description);
  1049. }
  1050. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_signal_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) {
  1051. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  1052. for (int i = 0; i < p_doc->arguments.size(); i++) {
  1053. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  1054. tooltip += arg.type + " " + arg.name;
  1055. if (arg.default_value != "") {
  1056. tooltip += " = " + arg.default_value;
  1057. }
  1058. if (i < p_doc->arguments.size() - 1) {
  1059. tooltip += ", ";
  1060. }
  1061. }
  1062. tooltip += ")";
  1063. return _create_member_item(p_parent, p_class_doc->name, "MemberSignal", p_doc->name, p_doc->name, TTRC("Signal"), "signal", tooltip, p_doc->description);
  1064. }
  1065. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_constant_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ConstantDoc *p_doc) {
  1066. String tooltip = p_class_doc->name + "." + p_doc->name;
  1067. return _create_member_item(p_parent, p_class_doc->name, "MemberConstant", p_doc->name, p_doc->name, TTRC("Constant"), "constant", tooltip, p_doc->description);
  1068. }
  1069. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::PropertyDoc *p_doc) {
  1070. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  1071. tooltip += "\n " + p_class_doc->name + "." + p_doc->setter + "(value) setter";
  1072. tooltip += "\n " + p_class_doc->name + "." + p_doc->getter + "() getter";
  1073. return _create_member_item(p_parent, p_class_doc->name, "MemberProperty", p_doc->name, p_doc->name, TTRC("Property"), "property", tooltip, p_doc->description);
  1074. }
  1075. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_theme_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ThemeItemDoc *p_doc) {
  1076. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  1077. 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);
  1078. }
  1079. 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) {
  1080. Ref<Texture2D> icon;
  1081. String text;
  1082. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  1083. icon = ui_service->get_theme_icon(p_icon, SNAME("EditorIcons"));
  1084. text = p_text;
  1085. } else {
  1086. icon = ui_service->get_theme_icon(p_icon, SNAME("EditorIcons"));
  1087. text = p_class_name + "." + p_text;
  1088. }
  1089. TreeItem *item = results_tree->create_item(p_parent);
  1090. item->set_icon(0, icon);
  1091. item->set_text(0, text);
  1092. item->set_text(1, TTRGET(p_type));
  1093. item->set_tooltip(0, p_tooltip);
  1094. item->set_tooltip(1, p_tooltip);
  1095. item->set_metadata(0, p_class_name + ":" + p_name);
  1096. item->set_metadata(1, "class_" + p_metatype);
  1097. item->set_meta("description", p_description);
  1098. _match_item(item, p_name);
  1099. return item;
  1100. }
  1101. bool VisualScriptPropertySelector::SearchRunner::work(uint64_t slot) {
  1102. // Return true when the search has been completed, otherwise false.
  1103. const uint64_t until = OS::get_singleton()->get_ticks_usec() + slot;
  1104. while (!_slice()) {
  1105. if (OS::get_singleton()->get_ticks_usec() > until) {
  1106. return false;
  1107. }
  1108. }
  1109. return true;
  1110. }
  1111. VisualScriptPropertySelector::SearchRunner::SearchRunner(VisualScriptPropertySelector *p_selector_ui, Tree *p_results_tree) :
  1112. selector_ui(p_selector_ui),
  1113. ui_service(p_selector_ui->vbox),
  1114. results_tree(p_results_tree),
  1115. term(p_selector_ui->search_box->get_text()),
  1116. empty_icon(ui_service->get_theme_icon(SNAME("ArrowRight"), SNAME("EditorIcons"))),
  1117. disabled_color(ui_service->get_theme_color(SNAME("disabled_font_color"), SNAME("Editor"))) {
  1118. }