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