class_name DrawController extends Control

var _draw_queue: Array[Callable] = []

var _dirty := false
var _clear := true
var _show_texture := true

@onready var mask_clear: ColorRect = %MaskClear
@onready var mask_texture: TextureRect = %MaskTexture


func clear(color: Color) -> void:
	mask_clear.color = color
	_clear = true


func queue_draw(op: Callable) -> void:
	_draw_queue.push_back(op)
	_dirty = true


func _draw() -> void:
	while _draw_queue:
		var op: Callable = _draw_queue.pop_front()
		op.call()


func _process(_delta: float) -> void:
	if _dirty:
		queue_redraw()
		_dirty = false

	# show clear rect for one frame
	mask_clear.visible = _clear
	_clear = false
	mask_texture.visible = _show_texture
	_show_texture = false


func _set_dirty() -> void:
	_dirty = true
