extends Node

var cwd = "."

var shader_path = "":
	get():
		return shader_path
	set(v):
		shader_path = v
		if "/" in v: # update current working directory
			cwd = v.substr(0, v.rfind("/"))

var shader: Shader:
	get():
		if shader_path == "":
			return null
		print("Load ", shader_path)
		return load(shader_path)

var image_path = ""

var original_image: ImageTexture
var result: Image

var last_image_savepath = ""
var last_original_image_path = ""

func get_absolute_path(p: String) -> String:
	# this only works on Linux!
	if !p.begins_with("/"):
		return self.cwd + "/" + p.lstrip("./")
	return p

func load_image() -> String: # returns an error message
	print("Load ", image_path)
	var img = Image.new()
	var err = img.load(image_path)
	if err == OK:
		original_image = ImageTexture.create_from_image(img)
		if self.last_image_savepath == "" or image_path != self.last_original_image_path:
			self.last_image_savepath = image_path
		self.last_original_image_path = image_path
		return ""
	return error_string(err) + " " + image_path

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
