2024-12-21 18:25:11 +01:00
|
|
|
extends Node
|
|
|
|
|
2024-12-25 00:07:51 +01:00
|
|
|
@onready var template_shader: Shader = load("res://src/shader/template.gdshader")
|
2025-01-06 22:11:53 +01:00
|
|
|
@onready var shader_code: String = template_shader.code
|
2024-12-26 22:18:18 +01:00
|
|
|
|
2024-12-21 18:25:11 +01:00
|
|
|
var original_image: ImageTexture
|
2024-12-26 22:18:18 +01:00
|
|
|
var additional_images: Dictionary
|
2024-12-21 18:25:11 +01:00
|
|
|
var result: Image
|
|
|
|
|
|
|
|
var cwd = "."
|
|
|
|
var last_image_savepath = ""
|
|
|
|
var last_shader_savepath = ""
|
|
|
|
var last_original_image_path = ""
|
|
|
|
|
2024-12-25 00:07:51 +01:00
|
|
|
func reset():
|
2025-01-06 22:11:53 +01:00
|
|
|
self.shader_code = self.template_shader.code
|
2024-12-25 00:07:51 +01:00
|
|
|
self.last_image_savepath = ""
|
|
|
|
self.last_shader_savepath = ""
|
|
|
|
self.last_original_image_path = ""
|
|
|
|
self.original_image = null
|
|
|
|
self.result = null
|
|
|
|
|
2024-12-21 18:25:11 +01:00
|
|
|
func get_absolute_path(p: String) -> String:
|
|
|
|
# this only works on Linux!
|
|
|
|
if !p.begins_with("/"):
|
|
|
|
return self.cwd + "/" + p.lstrip("./")
|
|
|
|
return p
|
|
|
|
|
2024-12-26 22:18:18 +01:00
|
|
|
func load_original_image(path: String) -> String: # returns an error message
|
2024-12-21 18:25:11 +01:00
|
|
|
var img = Image.new()
|
|
|
|
var err = img.load(path)
|
|
|
|
if err == OK:
|
|
|
|
original_image = ImageTexture.create_from_image(img)
|
|
|
|
if path != self.last_original_image_path:
|
|
|
|
self.last_original_image_path = path
|
|
|
|
if self.last_image_savepath == "":
|
|
|
|
self.last_image_savepath = path
|
2024-12-26 22:18:18 +01:00
|
|
|
return ""
|
|
|
|
return error_string(err) + " " + path
|
2024-12-21 18:25:11 +01:00
|
|
|
|
2024-12-26 22:18:18 +01:00
|
|
|
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
|
2024-12-21 18:25:11 +01:00
|
|
|
|
|
|
|
func save_result(path: String):
|
|
|
|
print("Export ", path)
|
|
|
|
var err = self.result.save_png(path)
|
|
|
|
if err != OK:
|
|
|
|
print("An error occured!")
|
|
|
|
else:
|
|
|
|
self.last_image_savepath = path
|
|
|
|
|
|
|
|
func load_shader(path: String):
|
|
|
|
print("Load ", path)
|
|
|
|
var file = FileAccess.open(path, FileAccess.READ)
|
2025-01-06 22:11:53 +01:00
|
|
|
self.shader_code = file.get_as_text()
|
2024-12-21 18:25:11 +01:00
|
|
|
if "/" in path: # update current working directory
|
|
|
|
self.cwd = path.substr(0, path.rfind("/"))
|
|
|
|
self.last_shader_savepath = path
|
2025-01-07 22:46:13 +01:00
|
|
|
store_last_opened_file()
|
2024-12-21 18:25:11 +01:00
|
|
|
|
2024-12-25 00:07:51 +01:00
|
|
|
func save_shader(path: String):
|
2024-12-21 18:25:11 +01:00
|
|
|
print("Save ", path)
|
|
|
|
var file = FileAccess.open(path, FileAccess.WRITE)
|
2025-01-06 22:11:53 +01:00
|
|
|
file.store_string(self.shader_code)
|
2025-01-07 22:46:13 +01:00
|
|
|
file.flush()
|
2024-12-21 18:25:11 +01:00
|
|
|
if "/" in path: # update current working directory
|
|
|
|
self.cwd = path.substr(0, path.rfind("/"))
|
|
|
|
self.last_shader_savepath = path
|
2025-01-07 22:46:13 +01:00
|
|
|
store_last_opened_file()
|
|
|
|
|
|
|
|
func store_last_opened_file():
|
|
|
|
var f = FileAccess.open("user://last_opened", FileAccess.WRITE)
|
|
|
|
if f != null:
|
|
|
|
f.store_pascal_string(last_shader_savepath)
|
|
|
|
f.flush()
|
|
|
|
|
|
|
|
func remember_last_opened_file():
|
|
|
|
var f = FileAccess.open("user://last_opened", FileAccess.READ)
|
|
|
|
if f != null:
|
|
|
|
last_shader_savepath = f.get_pascal_string()
|