shader_create_dialog.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /**************************************************************************/
  2. /* shader_create_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "shader_create_dialog.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor/gui/editor_file_dialog.h"
  34. #include "scene/resources/shader_include.h"
  35. #include "scene/resources/visual_shader.h"
  36. #include "servers/rendering/shader_types.h"
  37. enum ShaderType {
  38. SHADER_TYPE_TEXT,
  39. SHADER_TYPE_VISUAL,
  40. SHADER_TYPE_INC,
  41. SHADER_TYPE_MAX,
  42. };
  43. void ShaderCreateDialog::_notification(int p_what) {
  44. switch (p_what) {
  45. case NOTIFICATION_ENTER_TREE: {
  46. _update_theme();
  47. String last_lang = EditorSettings::get_singleton()->get_project_metadata("shader_setup", "last_selected_language", "");
  48. if (!last_lang.is_empty()) {
  49. for (int i = 0; i < type_menu->get_item_count(); i++) {
  50. if (type_menu->get_item_text(i) == last_lang) {
  51. type_menu->select(i);
  52. current_type = i;
  53. break;
  54. }
  55. }
  56. } else {
  57. type_menu->select(default_type);
  58. }
  59. current_mode = EditorSettings::get_singleton()->get_project_metadata("shader_setup", "last_selected_mode", 0);
  60. mode_menu->select(current_mode);
  61. } break;
  62. case NOTIFICATION_THEME_CHANGED: {
  63. _update_theme();
  64. } break;
  65. }
  66. }
  67. void ShaderCreateDialog::_update_theme() {
  68. Ref<Texture2D> shader_icon = gc->get_theme_icon(SNAME("Shader"), SNAME("EditorIcons"));
  69. if (shader_icon.is_valid()) {
  70. type_menu->set_item_icon(0, shader_icon);
  71. }
  72. Ref<Texture2D> visual_shader_icon = gc->get_theme_icon(SNAME("VisualShader"), SNAME("EditorIcons"));
  73. if (visual_shader_icon.is_valid()) {
  74. type_menu->set_item_icon(1, visual_shader_icon);
  75. }
  76. Ref<Texture2D> include_icon = gc->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  77. if (include_icon.is_valid()) {
  78. type_menu->set_item_icon(2, include_icon);
  79. }
  80. path_button->set_icon(get_theme_icon(SNAME("Folder"), SNAME("EditorIcons")));
  81. status_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  82. }
  83. void ShaderCreateDialog::_update_language_info() {
  84. type_data.clear();
  85. for (int i = 0; i < SHADER_TYPE_MAX; i++) {
  86. ShaderTypeData shader_type_data;
  87. if (i == int(SHADER_TYPE_TEXT)) {
  88. shader_type_data.use_templates = true;
  89. shader_type_data.extensions.push_back("gdshader");
  90. shader_type_data.default_extension = "gdshader";
  91. } else if (i == int(SHADER_TYPE_INC)) {
  92. shader_type_data.extensions.push_back("gdshaderinc");
  93. shader_type_data.default_extension = "gdshaderinc";
  94. } else {
  95. shader_type_data.default_extension = "tres";
  96. }
  97. shader_type_data.extensions.push_back("res");
  98. shader_type_data.extensions.push_back("tres");
  99. type_data.push_back(shader_type_data);
  100. }
  101. }
  102. void ShaderCreateDialog::_path_hbox_sorted() {
  103. if (is_visible()) {
  104. int filename_start_pos = initial_base_path.rfind("/") + 1;
  105. int filename_end_pos = initial_base_path.length();
  106. if (!is_built_in) {
  107. file_path->select(filename_start_pos, filename_end_pos);
  108. }
  109. file_path->set_caret_column(file_path->get_text().length());
  110. file_path->set_caret_column(filename_start_pos);
  111. file_path->grab_focus();
  112. }
  113. }
  114. void ShaderCreateDialog::_mode_changed(int p_mode) {
  115. current_mode = p_mode;
  116. EditorSettings::get_singleton()->set_project_metadata("shader_setup", "last_selected_mode", p_mode);
  117. }
  118. void ShaderCreateDialog::_template_changed(int p_template) {
  119. current_template = p_template;
  120. EditorSettings::get_singleton()->set_project_metadata("shader_setup", "last_selected_template", p_template);
  121. }
  122. void ShaderCreateDialog::ok_pressed() {
  123. if (is_new_shader_created) {
  124. _create_new();
  125. } else {
  126. _load_exist();
  127. }
  128. is_new_shader_created = true;
  129. _update_dialog();
  130. }
  131. void ShaderCreateDialog::_create_new() {
  132. Ref<Resource> shader;
  133. Ref<Resource> shader_inc;
  134. switch (type_menu->get_selected()) {
  135. case SHADER_TYPE_TEXT: {
  136. Ref<Shader> text_shader;
  137. text_shader.instantiate();
  138. shader = text_shader;
  139. StringBuilder code;
  140. code += vformat("shader_type %s;\n", mode_menu->get_text().to_snake_case());
  141. if (current_template == 0) { // Default template.
  142. code += "\n";
  143. switch (current_mode) {
  144. case Shader::MODE_SPATIAL:
  145. code += "void fragment() {\n";
  146. code += "\t// Place fragment code here.\n";
  147. code += "}\n";
  148. break;
  149. case Shader::MODE_CANVAS_ITEM:
  150. code += "void fragment() {\n";
  151. code += "\t// Place fragment code here.\n";
  152. code += "}\n";
  153. break;
  154. case Shader::MODE_PARTICLES:
  155. code += "void start() {\n";
  156. code += "\t// Place start code here.\n";
  157. code += "}\n";
  158. code += "\n";
  159. code += "void process() {\n";
  160. code += "\t// Place process code here.\n";
  161. code += "}\n";
  162. break;
  163. case Shader::MODE_SKY:
  164. code += "void sky() {\n";
  165. code += "\t// Place sky code here.\n";
  166. code += "}\n";
  167. break;
  168. case Shader::MODE_FOG:
  169. code += "void fog() {\n";
  170. code += "\t// Place fog code here.\n";
  171. code += "}\n";
  172. break;
  173. }
  174. }
  175. text_shader->set_code(code.as_string());
  176. } break;
  177. case SHADER_TYPE_VISUAL: {
  178. Ref<VisualShader> visual_shader;
  179. visual_shader.instantiate();
  180. shader = visual_shader;
  181. visual_shader->set_mode(Shader::Mode(current_mode));
  182. } break;
  183. case SHADER_TYPE_INC: {
  184. Ref<ShaderInclude> include;
  185. include.instantiate();
  186. shader_inc = include;
  187. } break;
  188. default: {
  189. } break;
  190. }
  191. if (shader.is_null()) {
  192. String lpath = ProjectSettings::get_singleton()->localize_path(file_path->get_text());
  193. shader_inc->set_path(lpath);
  194. Error error = ResourceSaver::save(shader_inc, lpath, ResourceSaver::FLAG_CHANGE_PATH);
  195. if (error != OK) {
  196. alert->set_text(TTR("Error - Could not create shader include in filesystem."));
  197. alert->popup_centered();
  198. return;
  199. }
  200. emit_signal(SNAME("shader_include_created"), shader_inc);
  201. } else {
  202. if (!is_built_in) {
  203. String lpath = ProjectSettings::get_singleton()->localize_path(file_path->get_text());
  204. shader->set_path(lpath);
  205. Error error = ResourceSaver::save(shader, lpath, ResourceSaver::FLAG_CHANGE_PATH);
  206. if (error != OK) {
  207. alert->set_text(TTR("Error - Could not create shader in filesystem."));
  208. alert->popup_centered();
  209. return;
  210. }
  211. }
  212. emit_signal(SNAME("shader_created"), shader);
  213. }
  214. file_path->set_text(file_path->get_text().get_base_dir());
  215. hide();
  216. }
  217. void ShaderCreateDialog::_load_exist() {
  218. String path = file_path->get_text();
  219. Ref<Resource> p_shader = ResourceLoader::load(path, "Shader");
  220. if (p_shader.is_null()) {
  221. alert->set_text(vformat(TTR("Error loading shader from %s"), path));
  222. alert->popup_centered();
  223. return;
  224. }
  225. emit_signal(SNAME("shader_created"), p_shader);
  226. hide();
  227. }
  228. void ShaderCreateDialog::_type_changed(int p_language) {
  229. current_type = p_language;
  230. ShaderTypeData shader_type_data = type_data[p_language];
  231. String selected_ext = "." + shader_type_data.default_extension;
  232. String path = file_path->get_text();
  233. String extension = "";
  234. if (!path.is_empty()) {
  235. if (path.contains(".")) {
  236. extension = path.get_extension();
  237. }
  238. if (extension.length() == 0) {
  239. path += selected_ext;
  240. } else {
  241. path = path.get_basename() + selected_ext;
  242. }
  243. } else {
  244. path = "shader" + selected_ext;
  245. }
  246. _path_changed(path);
  247. file_path->set_text(path);
  248. type_menu->set_item_disabled(int(SHADER_TYPE_INC), load_enabled);
  249. mode_menu->set_disabled(p_language == SHADER_TYPE_INC);
  250. template_menu->set_disabled(!shader_type_data.use_templates);
  251. template_menu->clear();
  252. if (shader_type_data.use_templates) {
  253. int last_template = EditorSettings::get_singleton()->get_project_metadata("shader_setup", "last_selected_template", 0);
  254. template_menu->add_item(TTR("Default"));
  255. template_menu->add_item(TTR("Empty"));
  256. template_menu->select(last_template);
  257. current_template = last_template;
  258. } else {
  259. template_menu->add_item(TTR("N/A"));
  260. }
  261. EditorSettings::get_singleton()->set_project_metadata("shader_setup", "last_selected_language", type_menu->get_item_text(type_menu->get_selected()));
  262. _update_dialog();
  263. }
  264. void ShaderCreateDialog::_built_in_toggled(bool p_enabled) {
  265. is_built_in = p_enabled;
  266. if (p_enabled) {
  267. is_new_shader_created = true;
  268. } else {
  269. _path_changed(file_path->get_text());
  270. }
  271. _update_dialog();
  272. }
  273. void ShaderCreateDialog::_browse_path() {
  274. file_browse->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  275. file_browse->set_title(TTR("Open Shader / Choose Location"));
  276. file_browse->set_ok_button_text(TTR("Open"));
  277. file_browse->set_disable_overwrite_warning(true);
  278. file_browse->clear_filters();
  279. List<String> extensions = type_data[type_menu->get_selected()].extensions;
  280. for (const String &E : extensions) {
  281. file_browse->add_filter("*." + E);
  282. }
  283. file_browse->set_current_path(file_path->get_text());
  284. file_browse->popup_file_dialog();
  285. }
  286. void ShaderCreateDialog::_file_selected(const String &p_file) {
  287. String p = ProjectSettings::get_singleton()->localize_path(p_file);
  288. file_path->set_text(p);
  289. _path_changed(p);
  290. String filename = p.get_file().get_basename();
  291. int select_start = p.rfind(filename);
  292. file_path->select(select_start, select_start + filename.length());
  293. file_path->set_caret_column(select_start + filename.length());
  294. file_path->grab_focus();
  295. }
  296. void ShaderCreateDialog::_path_changed(const String &p_path) {
  297. if (is_built_in) {
  298. return;
  299. }
  300. is_path_valid = false;
  301. is_new_shader_created = true;
  302. String path_error = _validate_path(p_path);
  303. if (!path_error.is_empty()) {
  304. _msg_path_valid(false, path_error);
  305. _update_dialog();
  306. return;
  307. }
  308. Ref<DirAccess> f = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  309. String p = ProjectSettings::get_singleton()->localize_path(p_path.strip_edges());
  310. if (f->file_exists(p)) {
  311. is_new_shader_created = false;
  312. _msg_path_valid(true, TTR("File exists, it will be reused."));
  313. }
  314. is_path_valid = true;
  315. _update_dialog();
  316. }
  317. void ShaderCreateDialog::_path_submitted(const String &p_path) {
  318. ok_pressed();
  319. }
  320. void ShaderCreateDialog::config(const String &p_base_path, bool p_built_in_enabled, bool p_load_enabled, int p_preferred_type, int p_preferred_mode) {
  321. if (!p_base_path.is_empty()) {
  322. initial_base_path = p_base_path.get_basename();
  323. file_path->set_text(initial_base_path + "." + type_data[type_menu->get_selected()].default_extension);
  324. current_type = type_menu->get_selected();
  325. } else {
  326. initial_base_path = "";
  327. file_path->set_text("");
  328. }
  329. file_path->deselect();
  330. built_in_enabled = p_built_in_enabled;
  331. load_enabled = p_load_enabled;
  332. if (p_preferred_type > -1) {
  333. type_menu->select(p_preferred_type);
  334. _type_changed(p_preferred_type);
  335. }
  336. if (p_preferred_mode > -1) {
  337. mode_menu->select(p_preferred_mode);
  338. _mode_changed(p_preferred_mode);
  339. }
  340. _type_changed(current_type);
  341. _path_changed(file_path->get_text());
  342. }
  343. String ShaderCreateDialog::_validate_path(const String &p_path) {
  344. String p = p_path.strip_edges();
  345. if (p.is_empty()) {
  346. return TTR("Path is empty.");
  347. }
  348. if (p.get_file().get_basename().is_empty()) {
  349. return TTR("Filename is empty.");
  350. }
  351. p = ProjectSettings::get_singleton()->localize_path(p);
  352. if (!p.begins_with("res://")) {
  353. return TTR("Path is not local.");
  354. }
  355. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  356. if (d->change_dir(p.get_base_dir()) != OK) {
  357. return TTR("Invalid base path.");
  358. }
  359. Ref<DirAccess> f = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  360. if (f->dir_exists(p)) {
  361. return TTR("A directory with the same name exists.");
  362. }
  363. String extension = p.get_extension();
  364. HashSet<String> extensions;
  365. for (int i = 0; i < SHADER_TYPE_MAX; i++) {
  366. for (const String &ext : type_data[i].extensions) {
  367. if (!extensions.has(ext)) {
  368. extensions.insert(ext);
  369. }
  370. }
  371. }
  372. bool found = false;
  373. bool match = false;
  374. for (const String &ext : extensions) {
  375. if (ext.nocasecmp_to(extension) == 0) {
  376. found = true;
  377. for (const String &type_ext : type_data[current_type].extensions) {
  378. if (type_ext.nocasecmp_to(extension) == 0) {
  379. match = true;
  380. break;
  381. }
  382. }
  383. break;
  384. }
  385. }
  386. if (!found) {
  387. return TTR("Invalid extension.");
  388. }
  389. if (!match) {
  390. return TTR("Wrong extension chosen.");
  391. }
  392. return "";
  393. }
  394. void ShaderCreateDialog::_msg_script_valid(bool valid, const String &p_msg) {
  395. error_label->set_text(String::utf8("• ") + p_msg);
  396. if (valid) {
  397. error_label->add_theme_color_override("font_color", gc->get_theme_color(SNAME("success_color"), SNAME("Editor")));
  398. } else {
  399. error_label->add_theme_color_override("font_color", gc->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  400. }
  401. }
  402. void ShaderCreateDialog::_msg_path_valid(bool valid, const String &p_msg) {
  403. path_error_label->set_text(String::utf8("• ") + p_msg);
  404. if (valid) {
  405. path_error_label->add_theme_color_override("font_color", gc->get_theme_color(SNAME("success_color"), SNAME("Editor")));
  406. } else {
  407. path_error_label->add_theme_color_override("font_color", gc->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  408. }
  409. }
  410. void ShaderCreateDialog::_update_dialog() {
  411. bool shader_ok = true;
  412. if (!is_built_in && !is_path_valid) {
  413. _msg_script_valid(false, TTR("Invalid path."));
  414. shader_ok = false;
  415. }
  416. if (shader_ok) {
  417. _msg_script_valid(true, TTR("Shader path/name is valid."));
  418. }
  419. if (!built_in_enabled) {
  420. internal->set_pressed(false);
  421. }
  422. if (is_built_in) {
  423. file_path->set_editable(false);
  424. path_button->set_disabled(true);
  425. re_check_path = true;
  426. } else {
  427. file_path->set_editable(true);
  428. path_button->set_disabled(false);
  429. if (re_check_path) {
  430. re_check_path = false;
  431. _path_changed(file_path->get_text());
  432. }
  433. }
  434. internal->set_disabled(!built_in_enabled);
  435. builtin_warning_label->set_visible(is_built_in);
  436. if (is_built_in) {
  437. set_ok_button_text(TTR("Create"));
  438. _msg_path_valid(true, TTR("Built-in shader (into scene file)."));
  439. } else if (is_new_shader_created) {
  440. set_ok_button_text(TTR("Create"));
  441. if (is_path_valid) {
  442. _msg_path_valid(true, TTR("Will create a new shader file."));
  443. }
  444. } else if (load_enabled) {
  445. set_ok_button_text(TTR("Load"));
  446. if (is_path_valid) {
  447. _msg_path_valid(true, TTR("Will load an existing shader file."));
  448. }
  449. } else {
  450. set_ok_button_text(TTR("Create"));
  451. _msg_path_valid(false, TTR("Shader file already exists."));
  452. shader_ok = false;
  453. }
  454. get_ok_button()->set_disabled(!shader_ok);
  455. Callable entered_call = callable_mp(this, &ShaderCreateDialog::_path_submitted);
  456. if (shader_ok) {
  457. if (!file_path->is_connected("text_submitted", entered_call)) {
  458. file_path->connect("text_submitted", entered_call);
  459. }
  460. } else if (file_path->is_connected("text_submitted", entered_call)) {
  461. file_path->disconnect("text_submitted", entered_call);
  462. }
  463. }
  464. void ShaderCreateDialog::_bind_methods() {
  465. ClassDB::bind_method(D_METHOD("config", "path", "built_in_enabled", "load_enabled"), &ShaderCreateDialog::config, DEFVAL(true), DEFVAL(true));
  466. ADD_SIGNAL(MethodInfo("shader_created", PropertyInfo(Variant::OBJECT, "shader", PROPERTY_HINT_RESOURCE_TYPE, "Shader")));
  467. ADD_SIGNAL(MethodInfo("shader_include_created", PropertyInfo(Variant::OBJECT, "shader_include", PROPERTY_HINT_RESOURCE_TYPE, "ShaderInclude")));
  468. }
  469. ShaderCreateDialog::ShaderCreateDialog() {
  470. _update_language_info();
  471. // Main Controls.
  472. gc = memnew(GridContainer);
  473. gc->set_columns(2);
  474. // Error Fields.
  475. VBoxContainer *vb = memnew(VBoxContainer);
  476. error_label = memnew(Label);
  477. vb->add_child(error_label);
  478. path_error_label = memnew(Label);
  479. vb->add_child(path_error_label);
  480. builtin_warning_label = memnew(Label);
  481. builtin_warning_label->set_text(
  482. TTR("Note: Built-in shaders can't be edited using an external editor."));
  483. vb->add_child(builtin_warning_label);
  484. builtin_warning_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  485. builtin_warning_label->hide();
  486. status_panel = memnew(PanelContainer);
  487. status_panel->set_h_size_flags(Control::SIZE_FILL);
  488. status_panel->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  489. status_panel->add_child(vb);
  490. // Spacing.
  491. Control *spacing = memnew(Control);
  492. spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
  493. vb = memnew(VBoxContainer);
  494. vb->add_child(gc);
  495. vb->add_child(spacing);
  496. vb->add_child(status_panel);
  497. add_child(vb);
  498. // Type.
  499. type_menu = memnew(OptionButton);
  500. type_menu->set_custom_minimum_size(Size2(250, 0) * EDSCALE);
  501. type_menu->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  502. gc->add_child(memnew(Label(TTR("Type:"))));
  503. gc->add_child(type_menu);
  504. for (int i = 0; i < SHADER_TYPE_MAX; i++) {
  505. String type;
  506. bool invalid = false;
  507. switch (i) {
  508. case SHADER_TYPE_TEXT:
  509. type = "Shader";
  510. default_type = i;
  511. break;
  512. case SHADER_TYPE_VISUAL:
  513. type = "VisualShader";
  514. break;
  515. case SHADER_TYPE_INC:
  516. type = "ShaderInclude";
  517. break;
  518. case SHADER_TYPE_MAX:
  519. invalid = true;
  520. break;
  521. default:
  522. invalid = true;
  523. break;
  524. }
  525. if (invalid) {
  526. continue;
  527. }
  528. type_menu->add_item(type);
  529. }
  530. if (default_type >= 0) {
  531. type_menu->select(default_type);
  532. }
  533. current_type = default_type;
  534. type_menu->connect("item_selected", callable_mp(this, &ShaderCreateDialog::_type_changed));
  535. // Modes.
  536. mode_menu = memnew(OptionButton);
  537. for (const String &type_name : ShaderTypes::get_singleton()->get_types_list()) {
  538. mode_menu->add_item(type_name.capitalize());
  539. }
  540. gc->add_child(memnew(Label(TTR("Mode:"))));
  541. gc->add_child(mode_menu);
  542. mode_menu->connect("item_selected", callable_mp(this, &ShaderCreateDialog::_mode_changed));
  543. // Templates.
  544. template_menu = memnew(OptionButton);
  545. gc->add_child(memnew(Label(TTR("Template:"))));
  546. gc->add_child(template_menu);
  547. template_menu->connect("item_selected", callable_mp(this, &ShaderCreateDialog::_template_changed));
  548. // Built-in Shader.
  549. internal = memnew(CheckBox);
  550. internal->set_text(TTR("On"));
  551. internal->connect("toggled", callable_mp(this, &ShaderCreateDialog::_built_in_toggled));
  552. gc->add_child(memnew(Label(TTR("Built-in Shader:"))));
  553. gc->add_child(internal);
  554. // Path.
  555. HBoxContainer *hb = memnew(HBoxContainer);
  556. hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  557. hb->connect("sort_children", callable_mp(this, &ShaderCreateDialog::_path_hbox_sorted));
  558. file_path = memnew(LineEdit);
  559. file_path->connect("text_changed", callable_mp(this, &ShaderCreateDialog::_path_changed));
  560. file_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  561. hb->add_child(file_path);
  562. path_button = memnew(Button);
  563. path_button->connect("pressed", callable_mp(this, &ShaderCreateDialog::_browse_path));
  564. hb->add_child(path_button);
  565. gc->add_child(memnew(Label(TTR("Path:"))));
  566. gc->add_child(hb);
  567. // Dialog Setup.
  568. file_browse = memnew(EditorFileDialog);
  569. file_browse->connect("file_selected", callable_mp(this, &ShaderCreateDialog::_file_selected));
  570. file_browse->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  571. add_child(file_browse);
  572. alert = memnew(AcceptDialog);
  573. alert->get_label()->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  574. alert->get_label()->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  575. alert->get_label()->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  576. alert->get_label()->set_custom_minimum_size(Size2(325, 60) * EDSCALE);
  577. add_child(alert);
  578. set_ok_button_text(TTR("Create"));
  579. set_hide_on_ok(false);
  580. set_title(TTR("Create Shader"));
  581. }