visual_script_property_selector.cpp 45 KB

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