Added an error indicator and error message box and refactored some code in the process - implements #21, prepares #16

This commit is contained in:
ChaoticByte 2024-12-26 22:18:18 +01:00
parent ca40971e53
commit 8394ad9d3c
No known key found for this signature in database
12 changed files with 345 additions and 31 deletions

View file

@ -6,9 +6,17 @@ extends Control
@onready var save_shader_dialog = %SaveShaderDialog
@onready var ui_control_filesave = %SaveImageDialog
@onready var status_indicator = %StatusIndicator
@onready var error_msg_dialog = %ErrorMessageDialog
@onready var image_viewport = get_tree().root.get_node("Main/%ImageViewport")
@onready var camera = get_tree().root.get_node("Main/%Camera")
#
var status_okay_texture: CompressedTexture2D = preload("uid://m1omb6g45vst")
var status_error_texture: CompressedTexture2D = preload("uid://04iv1gogpuhu")
# # # # # # # # # # #
# GDShader keywords #
# https://github.com/godotengine/godot/blob/e96ad5af98547df71b50c4c4695ac348638113e0/servers/rendering/shader_language.cpp
@ -173,7 +181,7 @@ func _on_code_edit_code_completion_requested():
func _ready():
code_editor.code_completion_enabled = true
code_editor.syntax_highlighter = ShaderSyntaxHighlighter.new()
self.update()
self.update_code_edit()
func _input(event):
if event.is_action_pressed("apply_shader"):
@ -182,15 +190,32 @@ func _input(event):
accept_event() # Event is now handled.
_on_save_shader_button_pressed()
func update():
func update_code_edit():
code_editor.text = Filesystem.shader.code
enum Status {OKAY, ERROR, UNKNOWN = -1}
func update_status(status: Status, msg: String = ""):
error_msg_dialog.dialog_text = msg
error_msg_dialog.reset_size()
if status == Status.OKAY:
status_indicator.texture_normal = status_okay_texture
elif status == Status.ERROR:
status_indicator.texture_normal = status_error_texture
else:
status_indicator.texture_normal = null
if msg == "":
status_indicator.disabled = true
else:
status_indicator.disabled = false
#
func _on_new_shader_button_pressed():
Filesystem.reset()
self.update()
self.update_code_edit()
image_viewport.update()
update_status(Status.UNKNOWN)
func _on_open_shader_button_pressed():
open_shader_dialog.show()
@ -215,7 +240,11 @@ func _on_fit_image_button_pressed():
func _on_apply_shader_button_pressed():
Filesystem.shader.code = code_editor.text
image_viewport.update()
var errors = await image_viewport.update()
if len(errors) > 0:
update_status(Status.ERROR, "\n".join(errors))
else:
update_status(Status.OKAY)
func _on_save_image_button_pressed():
if Filesystem.result != null:
@ -226,11 +255,16 @@ func _on_save_image_button_pressed():
func _on_open_shader_dialog_file_selected(path: String):
Filesystem.load_shader(path)
image_viewport.update()
self.update()
self.update_code_edit()
self._on_apply_shader_button_pressed()
func _on_save_shader_dialog_file_selected(path):
Filesystem.save_shader(path)
func _on_save_image_dialog_file_selected(path):
Filesystem.save_result(path)
#
func _on_status_indicator_pressed() -> void:
error_msg_dialog.show()

View file

@ -2,7 +2,9 @@ extends Node
@onready var template_shader: Shader = load("res://src/shader/template.gdshader")
@onready var shader: Shader = template_shader.duplicate()
var original_image: ImageTexture
var additional_images: Dictionary
var result: Image
var cwd = "."
@ -24,8 +26,7 @@ func get_absolute_path(p: String) -> String:
return self.cwd + "/" + p.lstrip("./")
return p
func load_original_image(path: String):
print("Load ", path)
func load_original_image(path: String) -> String: # returns an error message
var img = Image.new()
var err = img.load(path)
if err == OK:
@ -34,12 +35,20 @@ func load_original_image(path: String):
self.last_original_image_path = path
if self.last_image_savepath == "":
self.last_image_savepath = path
else:
print("An error occured!")
return ""
return error_string(err) + " " + path
func load_image(path: String) -> ImageTexture:
print("Load ", path)
return ImageTexture.create_from_image(Image.load_from_file(path))
func clear_additional_images():
additional_images.clear()
func load_additional_image(key: String, path: String) -> String: # returns Error Message String
var img = Image.new()
var err = img.load(path)
if err == OK:
additional_images[key] = ImageTexture.create_from_image(img)
return ""
else:
return error_string(err) + " " + path
func save_result(path: String):
print("Export ", path)

View file

@ -4,31 +4,42 @@ extends SubViewport
@onready var image_sprite = %ImageSprite
@onready var image_viewport_display = %ImageViewportDisplay
func update():
func update() -> Array: # returns error messages (strings)
var errors = []
var fit_image = false
# load image from //!load directive -> TEXTURE
# load texture(s)
Filesystem.clear_additional_images()
# ... from //!load directive -> TEXTURE
var m = ShaderDirectiveParser.parse_load_directive(Filesystem.shader)
if len(m) < 1:
return # AAAAAAAa
errors.append("Didn't find a load directive!")
return errors
var original_image_path = Filesystem.get_absolute_path(m[1])
if original_image_path != Filesystem.last_original_image_path:
fit_image = true
Filesystem.load_original_image(original_image_path)
if Filesystem.original_image == null:
var err = Filesystem.load_original_image(original_image_path)
if err != "":
errors.append(err)
image_viewport_display.hide()
return
return errors
# ... from //!load+ directives
for n in ShaderDirectiveParser.parse_load_additional_directive(Filesystem.shader):
err = Filesystem.load_additional_image(n[1], Filesystem.get_absolute_path(n[2]))
if err != "":
errors.append(err)
if len(errors) > 0:
return errors
# apply textures
image_sprite.texture = Filesystem.original_image
image_sprite.offset = Filesystem.original_image.get_size() / 2
self.size = Filesystem.original_image.get_size()
var mat = ShaderMaterial.new()
mat.shader = Filesystem.shader
# load images from //!load+ directives and apply them to
# the material as shader parameters
for n in ShaderDirectiveParser.parse_load_additional_directive(Filesystem.shader):
# ... as shader parameters
for key in Filesystem.additional_images:
mat.set_shader_parameter(
n[1], # uniform param name
Filesystem.load_image(Filesystem.get_absolute_path(n[2]))
)
key, # uniform param name
Filesystem.additional_images[key])
# assign material
image_sprite.material = mat
# Get viewport texture
@ -39,3 +50,5 @@ func update():
if fit_image:
camera.fit_image()
image_viewport_display.show()
# done
return errors

View file

@ -11,8 +11,7 @@ func _ready():
func parse_load_directive(shader: Shader) -> PackedStringArray:
var regex_match = self._load_regex.search(Filesystem.shader.code)
if regex_match == null: # Error!
printerr("Didn't find any load directives!")
if regex_match == null:
return []
return regex_match.strings