# Hurry Curry! - a game about cooking
# Copyright (C) 2025 Hurry Curry! contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
extends Menu

@onready var renderer = $Renderer

func _ready():
	super()

	var input_file: String
	if Cli.opts.has("render-items"):
		renderer.mode = renderer.Mode.ITEMS
		input_file = Cli.opts["render-items"]
	elif Cli.opts.has("render-tiles"):
		renderer.mode = renderer.Mode.TILES
		input_file = Cli.opts["render-tiles"]
	else:
		push_error("cannot open render_tool menu without corresponding cli options")
		return
	
	var file = FileAccess.open(input_file, FileAccess.READ)
	var object_name_list = file.get_as_text().strip_edges().split("\n")
	var resolution = Vector2i.ONE * int(Cli.opts.get("render-resolution", "256"))
	$Renderer/SubViewport.size = resolution

	for object_name in object_name_list:
		renderer.setup_object(object_name)
		await RenderingServer.frame_post_draw
		var path = Cli.opts.get("render-output", ".").path_join(object_name + ".png")
		$Renderer/SubViewport.get_texture().get_image().save_png(path)

	exit()
