summaryrefslogtreecommitdiff
path: root/ui/window.gd
blob: 8e6525a91acbaab6d5676d9f68a374ddd03ac959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class_name GameWindow
extends Control


var is_dragging := false
var drag_anchor := Vector2.ZERO

var is_resizeing := false

@export var closeable := false:
	set = _set_closeable

@export var resizeable := false:
	set = _set_resizeable

@export var minimum_size: Vector2


func _ready() -> void:
	# trigger initially
	closeable = closeable
	resizeable = resizeable
	
	if not minimum_size:
		minimum_size = size
	
	# move elements above resize handle
	for node: Control in %ButtonBarElements.get_children():
		node.z_index += 1


func _process(_delta: float) -> void:
	if is_dragging:
		global_position += get_global_mouse_position() - drag_anchor
		#%TitleBar.size # TODO: use title bar size as min/max. dont let the bar vanish
		drag_anchor = get_global_mouse_position()


func _input(event: InputEvent) -> void:
	if is_resizeing and event is InputEventMouseMotion:
		size = Vector2(
			max(minimum_size.x, size.x + event.relative.x),
			max(minimum_size.y, size.y + event.relative.y)
		)


func _set_closeable(value: bool) -> void:
	closeable = value
	%CloseButton.visible = closeable


func _set_resizeable(value: bool) -> void:
	resizeable = value
	%ResizeHandle.visible = resizeable


func _on_title_bar_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("primary_click"):
		is_dragging = true
		drag_anchor = get_global_mouse_position()
	elif event.is_action_released("primary_click"):
		is_dragging = false


func _on_resize_handle_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("primary_click"):
		is_resizeing = true
	elif event.is_action_released("primary_click"):
		is_resizeing = false


func _on_close_button_pressed() -> void:
	queue_free()