visual_script_property_selector.cpp 45 KB

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