visual_script_property_selector.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. virtuals_only = false;
  325. vbox = memnew(VBoxContainer);
  326. add_child(vbox);
  327. HBoxContainer *hbox = memnew(HBoxContainer);
  328. hbox->set_alignment(hbox->ALIGNMENT_CENTER);
  329. vbox->add_child(hbox);
  330. case_sensitive_button = memnew(Button);
  331. case_sensitive_button->set_flat(true);
  332. case_sensitive_button->set_tooltip(TTR("Case Sensitive"));
  333. case_sensitive_button->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  334. case_sensitive_button->set_toggle_mode(true);
  335. case_sensitive_button->set_focus_mode(Control::FOCUS_NONE);
  336. hbox->add_child(case_sensitive_button);
  337. hierarchy_button = memnew(Button);
  338. hierarchy_button->set_flat(true);
  339. hierarchy_button->set_tooltip(TTR("Show Hierarchy"));
  340. hierarchy_button->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  341. hierarchy_button->set_toggle_mode(true);
  342. hierarchy_button->set_pressed(true);
  343. hierarchy_button->set_focus_mode(Control::FOCUS_NONE);
  344. hbox->add_child(hierarchy_button);
  345. hbox->add_child(memnew(VSeparator));
  346. search_visual_script_nodes = memnew(Button);
  347. search_visual_script_nodes->set_flat(true);
  348. search_visual_script_nodes->set_tooltip(TTR("Search Visual Script Nodes"));
  349. search_visual_script_nodes->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  350. search_visual_script_nodes->set_toggle_mode(true);
  351. search_visual_script_nodes->set_pressed(true);
  352. search_visual_script_nodes->set_focus_mode(Control::FOCUS_NONE);
  353. hbox->add_child(search_visual_script_nodes);
  354. search_classes = memnew(Button);
  355. search_classes->set_flat(true);
  356. search_classes->set_tooltip(TTR("Search Classes"));
  357. search_classes->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  358. search_classes->set_toggle_mode(true);
  359. search_classes->set_pressed(true);
  360. search_classes->set_focus_mode(Control::FOCUS_NONE);
  361. hbox->add_child(search_classes);
  362. search_operators = memnew(Button);
  363. search_operators->set_flat(true);
  364. search_operators->set_tooltip(TTR("Search Operators"));
  365. search_operators->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  366. search_operators->set_toggle_mode(true);
  367. search_operators->set_pressed(true);
  368. search_operators->set_focus_mode(Control::FOCUS_NONE);
  369. hbox->add_child(search_operators);
  370. hbox->add_child(memnew(VSeparator));
  371. search_methods = memnew(Button);
  372. search_methods->set_flat(true);
  373. search_methods->set_tooltip(TTR("Search Methods"));
  374. search_methods->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  375. search_methods->set_toggle_mode(true);
  376. search_methods->set_pressed(true);
  377. search_methods->set_focus_mode(Control::FOCUS_NONE);
  378. hbox->add_child(search_methods);
  379. search_signals = memnew(Button);
  380. search_signals->set_flat(true);
  381. search_signals->set_tooltip(TTR("Search Signals"));
  382. search_signals->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  383. search_signals->set_toggle_mode(true);
  384. search_signals->set_pressed(true);
  385. search_signals->set_focus_mode(Control::FOCUS_NONE);
  386. hbox->add_child(search_signals);
  387. search_constants = memnew(Button);
  388. search_constants->set_flat(true);
  389. search_constants->set_tooltip(TTR("Search Constants"));
  390. search_constants->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  391. search_constants->set_toggle_mode(true);
  392. search_constants->set_pressed(true);
  393. search_constants->set_focus_mode(Control::FOCUS_NONE);
  394. hbox->add_child(search_constants);
  395. search_properties = memnew(Button);
  396. search_properties->set_flat(true);
  397. search_properties->set_tooltip(TTR("Search Properties"));
  398. search_properties->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  399. search_properties->set_toggle_mode(true);
  400. search_properties->set_pressed(true);
  401. search_properties->set_focus_mode(Control::FOCUS_NONE);
  402. hbox->add_child(search_properties);
  403. search_theme_items = memnew(Button);
  404. search_theme_items->set_flat(true);
  405. search_theme_items->set_tooltip(TTR("Search Theme Items"));
  406. search_theme_items->connect("pressed", callable_mp(this, &VisualScriptPropertySelector::_update_results));
  407. search_theme_items->set_toggle_mode(true);
  408. search_theme_items->set_pressed(true);
  409. search_theme_items->set_focus_mode(Control::FOCUS_NONE);
  410. hbox->add_child(search_theme_items);
  411. scope_combo = memnew(OptionButton);
  412. scope_combo->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  413. scope_combo->set_tooltip(TTR("Select the search limits"));
  414. scope_combo->set_stretch_ratio(0); // Fixed width.
  415. scope_combo->add_item(TTR("Search Related"), SCOPE_RELATED);
  416. scope_combo->add_separator();
  417. scope_combo->add_item(TTR("Search Base"), SCOPE_BASE);
  418. scope_combo->add_item(TTR("Search Inheriters"), SCOPE_INHERITERS);
  419. scope_combo->add_item(TTR("Search Unrelated"), SCOPE_UNRELATED);
  420. scope_combo->add_item(TTR("Search All"), SCOPE_ALL);
  421. scope_combo->connect("item_selected", callable_mp(this, &VisualScriptPropertySelector::_update_results_i));
  422. hbox->add_child(scope_combo);
  423. search_box = memnew(LineEdit);
  424. 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"));
  425. search_box->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  426. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  427. search_box->connect("text_changed", callable_mp(this, &VisualScriptPropertySelector::_update_results_s));
  428. search_box->connect("gui_input", callable_mp(this, &VisualScriptPropertySelector::_sbox_input));
  429. register_text_enter(search_box);
  430. vbox->add_child(search_box);
  431. results_tree = memnew(Tree);
  432. results_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  433. results_tree->set_hide_root(true);
  434. results_tree->set_hide_folding(false);
  435. results_tree->set_columns(2);
  436. results_tree->set_column_title(0, TTR("Name"));
  437. results_tree->set_column_clip_content(0, true);
  438. results_tree->set_column_title(1, TTR("Member Type"));
  439. results_tree->set_column_expand(1, false);
  440. results_tree->set_column_custom_minimum_width(1, 150 * EDSCALE);
  441. results_tree->set_column_clip_content(1, true);
  442. results_tree->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
  443. results_tree->set_select_mode(Tree::SELECT_ROW);
  444. results_tree->connect("item_activated", callable_mp(this, &VisualScriptPropertySelector::_confirmed));
  445. results_tree->connect("item_selected", callable_mp(this, &VisualScriptPropertySelector::_item_selected));
  446. vbox->add_child(results_tree);
  447. help_bit = memnew(EditorHelpBit);
  448. vbox->add_child(help_bit);
  449. help_bit->connect("request_hide", callable_mp(this, &VisualScriptPropertySelector::_hide_requested));
  450. get_ok_button()->set_text(TTR("Open"));
  451. get_ok_button()->set_disabled(true);
  452. set_hide_on_ok(false);
  453. }
  454. bool VisualScriptPropertySelector::SearchRunner::_is_class_disabled_by_feature_profile(const StringName &p_class) {
  455. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  456. if (profile.is_null()) {
  457. return false;
  458. }
  459. StringName class_name = p_class;
  460. while (class_name != StringName()) {
  461. if (!ClassDB::class_exists(class_name)) {
  462. return false;
  463. }
  464. if (profile->is_class_disabled(class_name)) {
  465. return true;
  466. }
  467. class_name = ClassDB::get_parent_class(class_name);
  468. }
  469. return false;
  470. }
  471. bool VisualScriptPropertySelector::SearchRunner::_is_class_disabled_by_scope(const StringName &p_class) {
  472. bool is_base_script = false;
  473. if (p_class == selector_ui->base_script) {
  474. is_base_script = true;
  475. }
  476. bool is_base = false;
  477. if (selector_ui->base_type == p_class) {
  478. is_base = true;
  479. }
  480. bool is_parent = false;
  481. if ((ClassDB::is_parent_class(selector_ui->base_type, p_class)) && !is_base) {
  482. is_parent = true;
  483. }
  484. bool is_inheriter = false;
  485. List<StringName> inheriters;
  486. ClassDB::get_inheriters_from_class(selector_ui->base_type, &inheriters);
  487. if (inheriters.find(p_class)) {
  488. is_inheriter = true;
  489. }
  490. if (scope_flags & SCOPE_BASE) {
  491. if (is_base_script || is_base || is_parent) {
  492. return false;
  493. }
  494. }
  495. if (scope_flags & SCOPE_INHERITERS) {
  496. if (is_base_script || is_base || is_inheriter) {
  497. return false;
  498. }
  499. }
  500. // if (scope_flags & SCOPE_RELATED) {
  501. // /* code */
  502. // }
  503. if (scope_flags & SCOPE_UNRELATED) {
  504. if (!is_base_script && !is_base && !is_inheriter) {
  505. return false;
  506. }
  507. }
  508. return true;
  509. }
  510. bool VisualScriptPropertySelector::SearchRunner::_slice() {
  511. bool phase_done = false;
  512. switch (phase) {
  513. case PHASE_INIT:
  514. phase_done = _phase_init();
  515. break;
  516. case PHASE_MATCH_CLASSES_INIT:
  517. phase_done = _phase_match_classes_init();
  518. break;
  519. case PHASE_NODE_CLASSES_INIT:
  520. phase_done = _phase_node_classes_init();
  521. break;
  522. case PHASE_NODE_CLASSES_BUILD:
  523. phase_done = _phase_node_classes_build();
  524. break;
  525. case PHASE_MATCH_CLASSES:
  526. phase_done = _phase_match_classes();
  527. break;
  528. case PHASE_CLASS_ITEMS_INIT:
  529. phase_done = _phase_class_items_init();
  530. break;
  531. case PHASE_CLASS_ITEMS:
  532. phase_done = _phase_class_items();
  533. break;
  534. case PHASE_MEMBER_ITEMS_INIT:
  535. phase_done = _phase_member_items_init();
  536. break;
  537. case PHASE_MEMBER_ITEMS:
  538. phase_done = _phase_member_items();
  539. break;
  540. case PHASE_SELECT_MATCH:
  541. phase_done = _phase_select_match();
  542. break;
  543. case PHASE_MAX:
  544. return true;
  545. default:
  546. WARN_PRINT("Invalid or unhandled phase in EditorHelpSearch::Runner, aborting search.");
  547. return true;
  548. };
  549. if (phase_done) {
  550. phase++;
  551. }
  552. return false;
  553. }
  554. bool VisualScriptPropertySelector::SearchRunner::_phase_init() {
  555. search_flags = 0; // selector_ui->filter_combo->get_selected_id();
  556. if (selector_ui->search_visual_script_nodes->is_pressed()) {
  557. search_flags |= SEARCH_VISUAL_SCRIPT_NODES;
  558. }
  559. if (selector_ui->search_classes->is_pressed()) {
  560. search_flags |= SEARCH_CLASSES;
  561. }
  562. // if (selector_ui->search_constructors->is_pressed()) {
  563. search_flags |= SEARCH_CONSTRUCTORS;
  564. // }
  565. if (selector_ui->search_methods->is_pressed()) {
  566. search_flags |= SEARCH_METHODS;
  567. }
  568. if (selector_ui->search_operators->is_pressed()) {
  569. search_flags |= SEARCH_OPERATORS;
  570. }
  571. if (selector_ui->search_signals->is_pressed()) {
  572. search_flags |= SEARCH_SIGNALS;
  573. }
  574. if (selector_ui->search_constants->is_pressed()) {
  575. search_flags |= SEARCH_CONSTANTS;
  576. }
  577. if (selector_ui->search_properties->is_pressed()) {
  578. search_flags |= SEARCH_PROPERTIES;
  579. }
  580. if (selector_ui->search_theme_items->is_pressed()) {
  581. search_flags |= SEARCH_THEME_ITEMS;
  582. }
  583. if (selector_ui->case_sensitive_button->is_pressed()) {
  584. search_flags |= SEARCH_CASE_SENSITIVE;
  585. }
  586. if (selector_ui->hierarchy_button->is_pressed()) {
  587. search_flags |= SEARCH_SHOW_HIERARCHY;
  588. }
  589. scope_flags = selector_ui->scope_combo->get_selected_id();
  590. return true;
  591. }
  592. bool VisualScriptPropertySelector::SearchRunner::_phase_match_classes_init() {
  593. combined_docs = EditorHelp::get_doc_data()->class_list;
  594. matches.clear();
  595. matched_item = nullptr;
  596. match_highest_score = 0;
  597. if (
  598. (selector_ui->base_script.unquote() != "") &&
  599. (selector_ui->base_script.unquote() != ".") &&
  600. !combined_docs.has(selector_ui->base_script)) {
  601. String file_path = "res://" + selector_ui->base_script.unquote(); // EditorHelp::get_doc_data().name to filepath
  602. Ref<Script> script;
  603. script = ResourceLoader::load(file_path);
  604. if (!script.is_null()) {
  605. DocData::ClassDoc class_doc = DocData::ClassDoc();
  606. class_doc.name = selector_ui->base_script;
  607. class_doc.inherits = script->get_instance_base_type();
  608. class_doc.brief_description = ".vs files not suported by EditorHelp::get_doc_data()";
  609. class_doc.description = "";
  610. Object *obj = ObjectDB::get_instance(script->get_instance_id());
  611. if (Object::cast_to<Script>(obj)) {
  612. List<MethodInfo> methods;
  613. Object::cast_to<Script>(obj)->get_script_method_list(&methods);
  614. for (List<MethodInfo>::Element *M = methods.front(); M; M = M->next()) {
  615. class_doc.methods.push_back(_get_method_doc(M->get()));
  616. }
  617. List<MethodInfo> signals;
  618. Object::cast_to<Script>(obj)->get_script_signal_list(&signals);
  619. for (List<MethodInfo>::Element *S = signals.front(); S; S = S->next()) {
  620. class_doc.signals.push_back(_get_method_doc(S->get()));
  621. }
  622. List<PropertyInfo> propertys;
  623. Object::cast_to<Script>(obj)->get_script_property_list(&propertys);
  624. for (List<PropertyInfo>::Element *P = propertys.front(); P; P = P->next()) {
  625. DocData::PropertyDoc pd = DocData::PropertyDoc();
  626. pd.name = P->get().name;
  627. pd.type = Variant::get_type_name(P->get().type);
  628. class_doc.properties.push_back(pd);
  629. }
  630. }
  631. combined_docs.insert(class_doc.name, class_doc);
  632. }
  633. }
  634. iterator_doc = combined_docs.front();
  635. return true;
  636. }
  637. bool VisualScriptPropertySelector::SearchRunner::_phase_node_classes_init() {
  638. VisualScriptLanguage::singleton->get_registered_node_names(&vs_nodes);
  639. _add_class_doc("functions", "", "");
  640. _add_class_doc("operators", "", "");
  641. return true;
  642. }
  643. bool VisualScriptPropertySelector::SearchRunner::_phase_node_classes_build() {
  644. if (vs_nodes.is_empty()) {
  645. return true;
  646. }
  647. String registerd_node_name = vs_nodes[0];
  648. vs_nodes.pop_front();
  649. Vector<String> path = registerd_node_name.split("/");
  650. if (path[0] == "constants") {
  651. _add_class_doc(registerd_node_name, "", "constants");
  652. } else if (path[0] == "custom") {
  653. _add_class_doc(registerd_node_name, "", "custom");
  654. } else if (path[0] == "data") {
  655. _add_class_doc(registerd_node_name, "", "data");
  656. } else if (path[0] == "flow_control") {
  657. _add_class_doc(registerd_node_name, "", "flow_control");
  658. } else if (path[0] == "functions") {
  659. if (path[1] == "built_in") {
  660. _add_class_doc(registerd_node_name, "functions", "built_in");
  661. } else if (path[1] == "by_type") {
  662. if (search_flags & SEARCH_CLASSES) {
  663. _add_class_doc(registerd_node_name, path[2], "by_type_class");
  664. }
  665. } else if (path[1] == "constructors") {
  666. if (search_flags & SEARCH_CLASSES) {
  667. _add_class_doc(registerd_node_name, path[2].substr(0, path[2].find_char('(')), "constructors_class");
  668. }
  669. } else if (path[1] == "deconstruct") {
  670. _add_class_doc(registerd_node_name, "", "deconstruct");
  671. } else if (path[1] == "wait") {
  672. _add_class_doc(registerd_node_name, "functions", "yield");
  673. } else {
  674. _add_class_doc(registerd_node_name, "functions", "");
  675. }
  676. } else if (path[0] == "index") {
  677. _add_class_doc(registerd_node_name, "", "index");
  678. } else if (path[0] == "operators") {
  679. if (path[1] == "bitwise") {
  680. _add_class_doc(registerd_node_name, "operators", "bitwise");
  681. } else if (path[1] == "compare") {
  682. _add_class_doc(registerd_node_name, "operators", "compare");
  683. } else if (path[1] == "logic") {
  684. _add_class_doc(registerd_node_name, "operators", "logic");
  685. } else if (path[1] == "math") {
  686. _add_class_doc(registerd_node_name, "operators", "math");
  687. } else {
  688. _add_class_doc(registerd_node_name, "operators", "");
  689. }
  690. }
  691. return false;
  692. }
  693. bool VisualScriptPropertySelector::SearchRunner::_phase_match_classes() {
  694. DocData::ClassDoc &class_doc = iterator_doc->value();
  695. if (
  696. (!_is_class_disabled_by_feature_profile(class_doc.name) && !_is_class_disabled_by_scope(class_doc.name)) ||
  697. _match_visual_script(class_doc)) {
  698. if (class_doc.inherits == "VisualScriptCustomNode") {
  699. class_doc.script_path = "res://" + class_doc.name.unquote();
  700. Ref<Script> script = ResourceLoader::load(class_doc.script_path);
  701. Ref<VisualScriptCustomNode> vsn;
  702. vsn.instantiate();
  703. vsn->set_script(script);
  704. class_doc.name = vsn->get_caption();
  705. if (combined_docs.has(vsn->get_category())) {
  706. class_doc.inherits = vsn->get_category();
  707. } else if (combined_docs.has("VisualScriptNode/" + vsn->get_category())) {
  708. class_doc.inherits = "VisualScriptNode/" + vsn->get_category();
  709. } else if (combined_docs.has("VisualScriptCustomNode/" + vsn->get_category())) {
  710. class_doc.inherits = "VisualScriptCustomNode/" + vsn->get_category();
  711. } else {
  712. class_doc.inherits = "";
  713. }
  714. class_doc.category = "VisualScriptCustomNode/" + vsn->get_category();
  715. class_doc.brief_description = "";
  716. class_doc.constructors.clear();
  717. class_doc.methods.clear();
  718. class_doc.operators.clear();
  719. class_doc.signals.clear();
  720. class_doc.constants.clear();
  721. class_doc.enums.clear();
  722. class_doc.properties.clear();
  723. class_doc.theme_properties.clear();
  724. }
  725. matches[class_doc.name] = ClassMatch();
  726. ClassMatch &match = matches[class_doc.name];
  727. match.category = class_doc.category;
  728. match.doc = &class_doc;
  729. // Match class name.
  730. if (search_flags & SEARCH_CLASSES || _match_visual_script(class_doc)) {
  731. if (term == "") {
  732. match.name = !_match_is_hidden(class_doc);
  733. } else {
  734. match.name = _match_string(term, class_doc.name);
  735. }
  736. // match.name = term == "" || _match_string(term, class_doc.name);
  737. }
  738. // Match members if the term is long enough.
  739. if (term.length() >= 0) {
  740. if (search_flags & SEARCH_CONSTRUCTORS) {
  741. for (int i = 0; i < class_doc.constructors.size(); i++) {
  742. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.constructors[i].name : class_doc.constructors[i].name.to_lower();
  743. if (method_name.find(term) > -1 ||
  744. term == " " ||
  745. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  746. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  747. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  748. match.constructors.push_back(const_cast<DocData::MethodDoc *>(&class_doc.constructors[i]));
  749. }
  750. }
  751. }
  752. if (search_flags & SEARCH_METHODS) {
  753. for (int i = 0; i < class_doc.methods.size(); i++) {
  754. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.methods[i].name : class_doc.methods[i].name.to_lower();
  755. if (method_name.find(term) > -1 ||
  756. term == " " ||
  757. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  758. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  759. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  760. match.methods.push_back(const_cast<DocData::MethodDoc *>(&class_doc.methods[i]));
  761. }
  762. }
  763. }
  764. if (search_flags & SEARCH_OPERATORS) {
  765. for (int i = 0; i < class_doc.operators.size(); i++) {
  766. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.operators[i].name : class_doc.operators[i].name.to_lower();
  767. if (method_name.find(term) > -1 ||
  768. term == " " ||
  769. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  770. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  771. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  772. match.operators.push_back(const_cast<DocData::MethodDoc *>(&class_doc.operators[i]));
  773. }
  774. }
  775. }
  776. if (search_flags & SEARCH_SIGNALS) {
  777. for (int i = 0; i < class_doc.signals.size(); i++) {
  778. if (_match_string(term, class_doc.signals[i].name) ||
  779. term == " ") {
  780. match.signals.push_back(const_cast<DocData::MethodDoc *>(&class_doc.signals[i]));
  781. }
  782. }
  783. }
  784. if (search_flags & SEARCH_CONSTANTS) {
  785. for (int i = 0; i < class_doc.constants.size(); i++) {
  786. if (_match_string(term, class_doc.constants[i].name) ||
  787. term == " ") {
  788. match.constants.push_back(const_cast<DocData::ConstantDoc *>(&class_doc.constants[i]));
  789. }
  790. }
  791. }
  792. if (search_flags & SEARCH_PROPERTIES) {
  793. for (int i = 0; i < class_doc.properties.size(); i++) {
  794. if (_match_string(term, class_doc.properties[i].name) ||
  795. term == " " ||
  796. _match_string(term, class_doc.properties[i].getter) ||
  797. _match_string(term, class_doc.properties[i].setter)) {
  798. match.properties.push_back(const_cast<DocData::PropertyDoc *>(&class_doc.properties[i]));
  799. }
  800. }
  801. }
  802. if (search_flags & SEARCH_THEME_ITEMS) {
  803. for (int i = 0; i < class_doc.theme_properties.size(); i++) {
  804. if (_match_string(term, class_doc.theme_properties[i].name) ||
  805. term == " ") {
  806. match.theme_properties.push_back(const_cast<DocData::ThemeItemDoc *>(&class_doc.theme_properties[i]));
  807. }
  808. }
  809. }
  810. }
  811. }
  812. iterator_doc = iterator_doc->next();
  813. return !iterator_doc;
  814. }
  815. bool VisualScriptPropertySelector::SearchRunner::_phase_class_items_init() {
  816. results_tree->clear();
  817. iterator_match = matches.front();
  818. root_item = results_tree->create_item();
  819. class_items.clear();
  820. return true;
  821. }
  822. bool VisualScriptPropertySelector::SearchRunner::_phase_class_items() {
  823. if (!iterator_match) {
  824. return true;
  825. }
  826. ClassMatch &match = iterator_match->value();
  827. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  828. if (match.required()) {
  829. _create_class_hierarchy(match);
  830. }
  831. } else {
  832. if (match.name) {
  833. _create_class_item(root_item, match.doc, true);
  834. }
  835. }
  836. iterator_match = iterator_match->next();
  837. return !iterator_match;
  838. }
  839. bool VisualScriptPropertySelector::SearchRunner::_phase_member_items_init() {
  840. iterator_match = matches.front();
  841. return true;
  842. }
  843. bool VisualScriptPropertySelector::SearchRunner::_phase_member_items() {
  844. if (!iterator_match) {
  845. return true;
  846. }
  847. ClassMatch &match = iterator_match->value();
  848. TreeItem *parent = (search_flags & SEARCH_SHOW_HIERARCHY) ? class_items[match.doc->name] : root_item;
  849. bool constructor_created = false;
  850. for (int i = 0; i < match.methods.size(); i++) {
  851. String text = match.methods[i]->name;
  852. if (!constructor_created) {
  853. if (match.doc->name == match.methods[i]->name) {
  854. text += " " + TTR("(constructors)");
  855. constructor_created = true;
  856. }
  857. } else {
  858. if (match.doc->name == match.methods[i]->name) {
  859. continue;
  860. }
  861. }
  862. _create_method_item(parent, match.doc, text, match.methods[i]);
  863. }
  864. for (int i = 0; i < match.signals.size(); i++) {
  865. _create_signal_item(parent, match.doc, match.signals[i]);
  866. }
  867. for (int i = 0; i < match.constants.size(); i++) {
  868. _create_constant_item(parent, match.doc, match.constants[i]);
  869. }
  870. for (int i = 0; i < match.properties.size(); i++) {
  871. _create_property_item(parent, match.doc, match.properties[i]);
  872. }
  873. for (int i = 0; i < match.theme_properties.size(); i++) {
  874. _create_theme_property_item(parent, match.doc, match.theme_properties[i]);
  875. }
  876. iterator_match = iterator_match->next();
  877. return !iterator_match;
  878. }
  879. bool VisualScriptPropertySelector::SearchRunner::_phase_select_match() {
  880. if (matched_item) {
  881. matched_item->select(0);
  882. }
  883. return true;
  884. }
  885. bool VisualScriptPropertySelector::SearchRunner::_match_string(const String &p_term, const String &p_string) const {
  886. if (search_flags & SEARCH_CASE_SENSITIVE) {
  887. return p_string.find(p_term) > -1;
  888. } else {
  889. return p_string.findn(p_term) > -1;
  890. }
  891. }
  892. bool VisualScriptPropertySelector::SearchRunner::_match_visual_script(DocData::ClassDoc &class_doc) {
  893. if (class_doc.category.ends_with("_class")) {
  894. if (class_doc.category.begins_with("VisualScript") && search_flags & SEARCH_CLASSES) {
  895. if (matches.has(class_doc.inherits)) {
  896. return true;
  897. }
  898. }
  899. return false;
  900. }
  901. if (class_doc.category.begins_with("VisualScript") && search_flags & SEARCH_VISUAL_SCRIPT_NODES) {
  902. return true;
  903. }
  904. if (class_doc.name.begins_with("operators") && search_flags & SEARCH_OPERATORS) {
  905. return true;
  906. }
  907. if (class_doc.category.begins_with("VisualScriptNode/deconstruct")) {
  908. if (class_doc.name.find(selector_ui->base_type, 0) > -1) {
  909. return true;
  910. }
  911. }
  912. return false;
  913. }
  914. bool VisualScriptPropertySelector::SearchRunner::_match_is_hidden(DocData::ClassDoc &class_doc) {
  915. if (class_doc.category.begins_with("VisualScript")) {
  916. if (class_doc.name.begins_with("flow_control")) {
  917. return false;
  918. } else if (class_doc.name.begins_with("operators")) {
  919. return !(search_flags & SEARCH_OPERATORS);
  920. } else if (class_doc.name.begins_with("functions/built_in/print")) {
  921. return false;
  922. }
  923. return true;
  924. }
  925. return false;
  926. }
  927. void VisualScriptPropertySelector::SearchRunner::_match_item(TreeItem *p_item, const String &p_text) {
  928. float inverse_length = 1.f / float(p_text.length());
  929. // Favor types where search term is a substring close to the start of the type.
  930. float w = 0.5f;
  931. int pos = p_text.findn(term);
  932. float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.f, .9f - w);
  933. // Favor shorter items: they resemble the search term more.
  934. w = 0.1f;
  935. score *= (1 - w) + w * (term.length() * inverse_length);
  936. if (match_highest_score == 0 || score > match_highest_score) {
  937. matched_item = p_item;
  938. match_highest_score = score;
  939. }
  940. }
  941. void VisualScriptPropertySelector::SearchRunner::_add_class_doc(String class_name, String inherits, String category) {
  942. DocData::ClassDoc class_doc = DocData::ClassDoc();
  943. class_doc.name = class_name;
  944. class_doc.inherits = inherits;
  945. class_doc.category = "VisualScriptNode/" + category;
  946. class_doc.brief_description = category;
  947. combined_docs.insert(class_doc.name, class_doc);
  948. }
  949. DocData::MethodDoc VisualScriptPropertySelector::SearchRunner::_get_method_doc(MethodInfo method_info) {
  950. DocData::MethodDoc method_doc = DocData::MethodDoc();
  951. method_doc.name = method_info.name;
  952. method_doc.return_type = Variant::get_type_name(method_info.return_val.type);
  953. method_doc.description = "No description available";
  954. for (List<PropertyInfo>::Element *P = method_info.arguments.front(); P; P = P->next()) {
  955. DocData::ArgumentDoc argument_doc = DocData::ArgumentDoc();
  956. argument_doc.name = P->get().name;
  957. argument_doc.type = Variant::get_type_name(P->get().type);
  958. method_doc.arguments.push_back(argument_doc);
  959. }
  960. return method_doc;
  961. }
  962. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_class_hierarchy(const ClassMatch &p_match) {
  963. if (class_items.has(p_match.doc->name)) {
  964. return class_items[p_match.doc->name];
  965. }
  966. // Ensure parent nodes are created first.
  967. TreeItem *parent = root_item;
  968. if (p_match.doc->inherits != "") {
  969. if (class_items.has(p_match.doc->inherits)) {
  970. parent = class_items[p_match.doc->inherits];
  971. } else if (matches.has(p_match.doc->inherits)) {
  972. ClassMatch &base_match = matches[p_match.doc->inherits];
  973. parent = _create_class_hierarchy(base_match);
  974. }
  975. }
  976. TreeItem *class_item = _create_class_item(parent, p_match.doc, !p_match.name);
  977. class_items[p_match.doc->name] = class_item;
  978. return class_item;
  979. }
  980. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_class_item(TreeItem *p_parent, const DocData::ClassDoc *p_doc, bool p_gray) {
  981. Ref<Texture2D> icon = empty_icon;
  982. String text_0 = p_doc->name;
  983. String text_1 = "Class";
  984. String what = "Class";
  985. String details = p_doc->name;
  986. if (p_doc->category.begins_with("VisualScriptCustomNode/")) {
  987. Vector<String> path = p_doc->name.split("/");
  988. icon = ui_service->get_theme_icon("VisualScript", "EditorIcons");
  989. text_0 = path[path.size() - 1];
  990. text_1 = "VisualScriptCustomNode";
  991. what = "VisualScriptCustomNode";
  992. details = "CustomNode";
  993. } else if (p_doc->category.begins_with("VisualScriptNode/")) {
  994. Vector<String> path = p_doc->name.split("/");
  995. icon = ui_service->get_theme_icon("VisualScript", "EditorIcons");
  996. text_0 = path[path.size() - 1];
  997. if (p_doc->category.begins_with("VisualScriptNode/deconstruct")) {
  998. text_0 = "deconstruct " + text_0;
  999. }
  1000. text_1 = "VisualScriptNode";
  1001. what = "VisualScriptNode";
  1002. details = p_doc->name;
  1003. if (path.size() == 1) {
  1004. if (path[0] == "functions" || path[0] == "operators") {
  1005. text_1 = "VisualScript";
  1006. p_gray = true;
  1007. what = "no_result";
  1008. details = "";
  1009. }
  1010. }
  1011. } else {
  1012. if (p_doc->name.is_quoted()) {
  1013. text_0 = p_doc->name.unquote().get_file();
  1014. if (ui_service->has_theme_icon(p_doc->inherits, "EditorIcons")) {
  1015. icon = ui_service->get_theme_icon(p_doc->inherits, "EditorIcons");
  1016. }
  1017. } else if (ui_service->has_theme_icon(p_doc->name, "EditorIcons")) {
  1018. icon = ui_service->get_theme_icon(p_doc->name, "EditorIcons");
  1019. } else if (ClassDB::class_exists(p_doc->name) && ClassDB::is_parent_class(p_doc->name, "Object")) {
  1020. icon = ui_service->get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  1021. }
  1022. }
  1023. String tooltip = p_doc->brief_description.strip_edges();
  1024. TreeItem *item = results_tree->create_item(p_parent);
  1025. item->set_icon(0, icon);
  1026. item->set_text(0, text_0);
  1027. item->set_text(1, TTR(text_1));
  1028. item->set_tooltip(0, tooltip);
  1029. item->set_tooltip(1, tooltip);
  1030. item->set_metadata(0, details);
  1031. item->set_metadata(1, what);
  1032. if (p_gray) {
  1033. item->set_custom_color(0, disabled_color);
  1034. item->set_custom_color(1, disabled_color);
  1035. }
  1036. _match_item(item, p_doc->name);
  1037. return item;
  1038. }
  1039. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_method_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const String &p_text, const DocData::MethodDoc *p_doc) {
  1040. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  1041. for (int i = 0; i < p_doc->arguments.size(); i++) {
  1042. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  1043. tooltip += arg.type + " " + arg.name;
  1044. if (arg.default_value != "") {
  1045. tooltip += " = " + arg.default_value;
  1046. }
  1047. if (i < p_doc->arguments.size() - 1) {
  1048. tooltip += ", ";
  1049. }
  1050. }
  1051. tooltip += ")";
  1052. return _create_member_item(p_parent, p_class_doc->name, "MemberMethod", p_doc->name, p_text, TTRC("Method"), "method", tooltip, p_doc->description);
  1053. }
  1054. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_signal_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) {
  1055. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  1056. for (int i = 0; i < p_doc->arguments.size(); i++) {
  1057. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  1058. tooltip += arg.type + " " + arg.name;
  1059. if (arg.default_value != "") {
  1060. tooltip += " = " + arg.default_value;
  1061. }
  1062. if (i < p_doc->arguments.size() - 1) {
  1063. tooltip += ", ";
  1064. }
  1065. }
  1066. tooltip += ")";
  1067. return _create_member_item(p_parent, p_class_doc->name, "MemberSignal", p_doc->name, p_doc->name, TTRC("Signal"), "signal", tooltip, p_doc->description);
  1068. }
  1069. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_constant_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ConstantDoc *p_doc) {
  1070. String tooltip = p_class_doc->name + "." + p_doc->name;
  1071. return _create_member_item(p_parent, p_class_doc->name, "MemberConstant", p_doc->name, p_doc->name, TTRC("Constant"), "constant", tooltip, p_doc->description);
  1072. }
  1073. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::PropertyDoc *p_doc) {
  1074. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  1075. tooltip += "\n " + p_class_doc->name + "." + p_doc->setter + "(value) setter";
  1076. tooltip += "\n " + p_class_doc->name + "." + p_doc->getter + "() getter";
  1077. return _create_member_item(p_parent, p_class_doc->name, "MemberProperty", p_doc->name, p_doc->name, TTRC("Property"), "property", tooltip, p_doc->description);
  1078. }
  1079. TreeItem *VisualScriptPropertySelector::SearchRunner::_create_theme_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ThemeItemDoc *p_doc) {
  1080. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  1081. 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);
  1082. }
  1083. 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) {
  1084. Ref<Texture2D> icon;
  1085. String text;
  1086. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  1087. icon = ui_service->get_theme_icon(p_icon, SNAME("EditorIcons"));
  1088. text = p_text;
  1089. } else {
  1090. icon = ui_service->get_theme_icon(p_icon, SNAME("EditorIcons"));
  1091. text = p_class_name + "." + p_text;
  1092. }
  1093. TreeItem *item = results_tree->create_item(p_parent);
  1094. item->set_icon(0, icon);
  1095. item->set_text(0, text);
  1096. item->set_text(1, TTRGET(p_type));
  1097. item->set_tooltip(0, p_tooltip);
  1098. item->set_tooltip(1, p_tooltip);
  1099. item->set_metadata(0, p_class_name + ":" + p_name);
  1100. item->set_metadata(1, "class_" + p_metatype);
  1101. item->set_meta("description", p_description);
  1102. _match_item(item, p_name);
  1103. return item;
  1104. }
  1105. bool VisualScriptPropertySelector::SearchRunner::work(uint64_t slot) {
  1106. // Return true when the search has been completed, otherwise false.
  1107. const uint64_t until = OS::get_singleton()->get_ticks_usec() + slot;
  1108. while (!_slice()) {
  1109. if (OS::get_singleton()->get_ticks_usec() > until) {
  1110. return false;
  1111. }
  1112. }
  1113. return true;
  1114. }
  1115. VisualScriptPropertySelector::SearchRunner::SearchRunner(VisualScriptPropertySelector *p_selector_ui, Tree *p_results_tree) :
  1116. selector_ui(p_selector_ui),
  1117. ui_service(p_selector_ui->vbox),
  1118. results_tree(p_results_tree),
  1119. term(p_selector_ui->search_box->get_text()),
  1120. empty_icon(ui_service->get_theme_icon(SNAME("ArrowRight"), SNAME("EditorIcons"))),
  1121. disabled_color(ui_service->get_theme_color(SNAME("disabled_font_color"), SNAME("Editor"))) {
  1122. }