@tool
extends ColorRect


@export_tool_button('Randomize', 'RandomNumberGenerator') var randomize_action = randomize


@export var smoothing := true:
	set(x): material.set_shader_parameter('smoothing', x)
	get(): return material.get_shader_parameter('smoothing')

@export var use_color_gradient := false:
	set(x): material.set_shader_parameter('use_color_gradient', x)
	get(): return material.get_shader_parameter('use_color_gradient')

@export var gradient_start := Color(.18, 0, .45):
	set(x): material.set_shader_parameter('gradient_start', x)
	get(): return material.get_shader_parameter('gradient_start')

@export var gradient_end := Color.YELLOW:
	set(x): material.set_shader_parameter('gradient_end', x)
	get(): return material.get_shader_parameter('gradient_end')

@export var gradient_accent := Color.YELLOW:
	set(x): material.set_shader_parameter('gradient_accent', x)
	get(): return material.get_shader_parameter('gradient_accent')

@export var fractal_scale := 0.5:
	set(x): material.set_shader_parameter('scale', x)
	get(): return material.get_shader_parameter('scale')

@export var fractal_seed := Vector2(-0.529, 0.136):
	set(x): material.set_shader_parameter('seed', x)
	get(): return material.get_shader_parameter('seed')

@export var fractal_power := 3:
	set(x): material.set_shader_parameter('power', x)
	get(): return material.get_shader_parameter('power')

@export var fractal_escape := 1.0:
	set(x): material.set_shader_parameter('escape', x)
	get(): return material.get_shader_parameter('escape')


func _on_resized() -> void:
	material.set_shader_parameter('aspect_ratio', size.aspect())


func _ready() -> void:
	_on_resized()


func randomize(rng_seed := randi()) -> void:
	var rng := RandomNumberGenerator.new()
	rng.seed = rng_seed
	fractal_power = rng.randi_range(2, 5)
	fractal_escape = rng.randi_range(1, 4)
	#fractal_scale = randf_range(0.5, 0.5 * size.aspect()) - 0.1
	match fractal_power:
		5: fractal_seed = Vector2(rng.randf_range(-0.7, -0.6), rng.randf_range(-0.1, 0.1))
		4: fractal_seed = Vector2(rng.randf_range(-1.2, -1), rng.randf_range(-0.2, 0.2))
		3: fractal_seed = Vector2(rng.randf_range(0.45, 0.7), rng.randf_range(-0.7, 0.7))
		2: match fractal_escape:
			1.0: fractal_seed = Vector2(rng.randf_range(-0.9, -0.8), rng.randf_range(0, 0))
			2.0: fractal_seed = Vector2(rng.randf_range(-1.8, -1.), rng.randf_range(0, 0))
			_: fractal_seed = Vector2.RIGHT.rotated(randf_range(0, TAU)) * Vector2(0.6, 1)
		_: fractal_seed = Vector2(rng.randf_range(-1, 1), rng.randf_range(-1, 1))
