visual_script_property_selector.cpp 46 KB

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