shader_create_dialog.cpp 21 KB

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