shader_create_dialog.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. } else {
  118. _load_exist();
  119. }
  120. is_new_shader_created = true;
  121. validation_panel->update();
  122. }
  123. void ShaderCreateDialog::_create_new() {
  124. Ref<Resource> shader;
  125. Ref<Resource> shader_inc;
  126. switch (type_menu->get_selected()) {
  127. case SHADER_TYPE_TEXT: {
  128. Ref<Shader> text_shader;
  129. text_shader.instantiate();
  130. shader = text_shader;
  131. StringBuilder code;
  132. code += vformat("shader_type %s;\n", mode_menu->get_text().to_snake_case());
  133. if (current_template == 0) { // Default template.
  134. switch (current_mode) {
  135. case Shader::MODE_SPATIAL:
  136. code += R"(
  137. void vertex() {
  138. // Called for every vertex the material is visible on.
  139. }
  140. void fragment() {
  141. // Called for every pixel the material is visible on.
  142. }
  143. //void light() {
  144. // Called for every pixel for every light affecting the material.
  145. // Uncomment to replace the default light processing function with this one.
  146. //}
  147. )";
  148. break;
  149. case Shader::MODE_CANVAS_ITEM:
  150. code += R"(
  151. void vertex() {
  152. // Called for every vertex the material is visible on.
  153. }
  154. void fragment() {
  155. // Called for every pixel the material is visible on.
  156. }
  157. //void light() {
  158. // Called for every pixel for every light affecting the CanvasItem.
  159. // Uncomment to replace the default light processing function with this one.
  160. //}
  161. )";
  162. break;
  163. case Shader::MODE_PARTICLES:
  164. code += R"(
  165. void start() {
  166. // Called when a particle is spawned.
  167. }
  168. void process() {
  169. // Called every frame on existing particles (according to the Fixed FPS property).
  170. }
  171. )";
  172. break;
  173. case Shader::MODE_SKY:
  174. code += R"(
  175. void sky() {
  176. // Called for every visible pixel in the sky background, as well as all pixels
  177. // in the radiance cubemap.
  178. }
  179. )";
  180. break;
  181. case Shader::MODE_FOG:
  182. code += R"(
  183. void fog() {
  184. // Called once for every froxel that is touched by an axis-aligned bounding box
  185. // of the associated FogVolume. This means that froxels that just barely touch
  186. // a given FogVolume will still be used.
  187. }
  188. )";
  189. }
  190. }
  191. text_shader->set_code(code.as_string());
  192. } break;
  193. case SHADER_TYPE_VISUAL: {
  194. Ref<VisualShader> visual_shader;
  195. visual_shader.instantiate();
  196. shader = visual_shader;
  197. visual_shader->set_mode(Shader::Mode(current_mode));
  198. } break;
  199. case SHADER_TYPE_INC: {
  200. Ref<ShaderInclude> include;
  201. include.instantiate();
  202. shader_inc = include;
  203. } break;
  204. default: {
  205. } break;
  206. }
  207. if (shader.is_null()) {
  208. String lpath = ProjectSettings::get_singleton()->localize_path(file_path->get_text());
  209. shader_inc->set_path(lpath);
  210. Error error = ResourceSaver::save(shader_inc, lpath, ResourceSaver::FLAG_CHANGE_PATH);
  211. if (error != OK) {
  212. alert->set_text(TTR("Error - Could not create shader include in filesystem."));
  213. alert->popup_centered();
  214. return;
  215. }
  216. EditorNode::get_singleton()->ensure_uid_file(lpath);
  217. emit_signal(SNAME("shader_include_created"), shader_inc);
  218. } else {
  219. if (is_built_in) {
  220. Node *edited_scene = get_tree()->get_edited_scene_root();
  221. if (likely(edited_scene)) {
  222. shader->set_path(edited_scene->get_scene_file_path() + "::");
  223. }
  224. } else {
  225. String lpath = ProjectSettings::get_singleton()->localize_path(file_path->get_text());
  226. shader->set_path(lpath);
  227. Error error = ResourceSaver::save(shader, lpath, ResourceSaver::FLAG_CHANGE_PATH);
  228. if (error != OK) {
  229. alert->set_text(TTR("Error - Could not create shader in filesystem."));
  230. alert->popup_centered();
  231. return;
  232. }
  233. EditorNode::get_singleton()->ensure_uid_file(lpath);
  234. }
  235. emit_signal(SNAME("shader_created"), shader);
  236. }
  237. file_path->set_text(file_path->get_text().get_base_dir());
  238. hide();
  239. }
  240. void ShaderCreateDialog::_load_exist() {
  241. String path = file_path->get_text();
  242. Ref<Resource> p_shader = ResourceLoader::load(path, "Shader");
  243. if (p_shader.is_null()) {
  244. alert->set_text(vformat(TTR("Error loading shader from %s"), path));
  245. alert->popup_centered();
  246. return;
  247. }
  248. emit_signal(SNAME("shader_created"), p_shader);
  249. hide();
  250. }
  251. void ShaderCreateDialog::_type_changed(int p_language) {
  252. current_type = p_language;
  253. ShaderTypeData shader_type_data = type_data.get(p_language);
  254. String selected_ext = "." + shader_type_data.default_extension;
  255. String path = file_path->get_text();
  256. String extension = "";
  257. if (!path.is_empty()) {
  258. if (path.contains(".")) {
  259. extension = path.get_extension();
  260. }
  261. if (extension.length() == 0) {
  262. path += selected_ext;
  263. } else {
  264. path = path.get_basename() + selected_ext;
  265. }
  266. } else {
  267. path = "shader" + selected_ext;
  268. }
  269. _path_changed(path);
  270. file_path->set_text(path);
  271. type_menu->set_item_disabled(int(SHADER_TYPE_INC), load_enabled);
  272. mode_menu->set_disabled(p_language == SHADER_TYPE_INC);
  273. template_menu->set_disabled(!shader_type_data.use_templates);
  274. template_menu->clear();
  275. if (shader_type_data.use_templates) {
  276. int last_template = EditorSettings::get_singleton()->get_project_metadata("shader_setup", "last_selected_template", 0);
  277. template_menu->add_item(TTR("Default"));
  278. template_menu->add_item(TTR("Empty"));
  279. template_menu->select(last_template);
  280. current_template = last_template;
  281. } else {
  282. template_menu->add_item(TTR("N/A"));
  283. }
  284. EditorSettings::get_singleton()->set_project_metadata("shader_setup", "last_selected_language", type_menu->get_item_text(type_menu->get_selected()));
  285. validation_panel->update();
  286. }
  287. void ShaderCreateDialog::_built_in_toggled(bool p_enabled) {
  288. is_built_in = p_enabled;
  289. if (p_enabled) {
  290. is_new_shader_created = true;
  291. } else {
  292. _path_changed(file_path->get_text());
  293. }
  294. validation_panel->update();
  295. }
  296. void ShaderCreateDialog::_browse_path() {
  297. file_browse->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  298. file_browse->set_title(TTR("Open Shader / Choose Location"));
  299. file_browse->set_ok_button_text(TTR("Open"));
  300. file_browse->set_disable_overwrite_warning(true);
  301. file_browse->clear_filters();
  302. List<String> extensions = type_data.get(type_menu->get_selected()).extensions;
  303. for (const String &E : extensions) {
  304. file_browse->add_filter("*." + E);
  305. }
  306. file_browse->set_current_path(file_path->get_text());
  307. file_browse->popup_file_dialog();
  308. }
  309. void ShaderCreateDialog::_file_selected(const String &p_file) {
  310. String p = ProjectSettings::get_singleton()->localize_path(p_file);
  311. file_path->set_text(p);
  312. _path_changed(p);
  313. String filename = p.get_file().get_basename();
  314. int select_start = p.rfind(filename);
  315. file_path->select(select_start, select_start + filename.length());
  316. file_path->set_caret_column(select_start + filename.length());
  317. file_path->grab_focus();
  318. }
  319. void ShaderCreateDialog::_path_changed(const String &p_path) {
  320. if (is_built_in) {
  321. return;
  322. }
  323. is_path_valid = false;
  324. is_new_shader_created = true;
  325. path_error = _validate_path(p_path);
  326. if (!path_error.is_empty()) {
  327. validation_panel->update();
  328. return;
  329. }
  330. Ref<DirAccess> f = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  331. String p = ProjectSettings::get_singleton()->localize_path(p_path.strip_edges());
  332. if (f->file_exists(p)) {
  333. is_new_shader_created = false;
  334. }
  335. is_path_valid = true;
  336. validation_panel->update();
  337. }
  338. void ShaderCreateDialog::_path_submitted(const String &p_path) {
  339. if (!get_ok_button()->is_disabled()) {
  340. ok_pressed();
  341. }
  342. }
  343. 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) {
  344. if (!p_base_path.is_empty()) {
  345. initial_base_path = p_base_path.get_basename();
  346. file_path->set_text(initial_base_path + "." + type_data.get(type_menu->get_selected()).default_extension);
  347. current_type = type_menu->get_selected();
  348. } else {
  349. initial_base_path = "";
  350. file_path->set_text("");
  351. }
  352. file_path->deselect();
  353. built_in_enabled = p_built_in_enabled;
  354. load_enabled = p_load_enabled;
  355. if (p_preferred_type > -1) {
  356. type_menu->select(p_preferred_type);
  357. _type_changed(p_preferred_type);
  358. }
  359. if (p_preferred_mode > -1) {
  360. mode_menu->select(p_preferred_mode);
  361. _mode_changed(p_preferred_mode);
  362. }
  363. _type_changed(current_type);
  364. _path_changed(file_path->get_text());
  365. }
  366. String ShaderCreateDialog::_validate_path(const String &p_path) {
  367. String p = p_path.strip_edges();
  368. if (p.is_empty()) {
  369. return TTR("Path is empty.");
  370. }
  371. if (p.get_file().get_basename().is_empty()) {
  372. return TTR("Filename is empty.");
  373. }
  374. p = ProjectSettings::get_singleton()->localize_path(p);
  375. if (!p.begins_with("res://")) {
  376. return TTR("Path is not local.");
  377. }
  378. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  379. if (d->change_dir(p.get_base_dir()) != OK) {
  380. return TTR("Invalid base path.");
  381. }
  382. Ref<DirAccess> f = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  383. if (f->dir_exists(p)) {
  384. return TTR("A directory with the same name exists.");
  385. }
  386. String extension = p.get_extension();
  387. HashSet<String> extensions;
  388. List<ShaderCreateDialog::ShaderTypeData>::ConstIterator itr = type_data.begin();
  389. for (int i = 0; i < SHADER_TYPE_MAX; ++itr, ++i) {
  390. for (const String &ext : itr->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.get(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(SceneStringName(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(SceneStringName(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(SceneStringName(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(SceneStringName(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(SceneStringName(sort_children), callable_mp(this, &ShaderCreateDialog::_path_hbox_sorted));
  546. file_path = memnew(LineEdit);
  547. file_path->connect(SceneStringName(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(SceneStringName(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. }