shader_create_dialog.cpp 20 KB

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