-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.gd
More file actions
193 lines (144 loc) · 5.2 KB
/
control.gd
File metadata and controls
193 lines (144 loc) · 5.2 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
extends Control
var quotes: Array[String] = []
const QUOTES_FILE_PATH: String = "res://quotes.txt"
const QUOTE_SCENE: PackedScene = preload("res://quote.tscn")
const START_Y: float = 40.0 # First quote's Y position (near top)
const SPACING_Y: float = 80.0 # Vertical distance between quotes
const START_X: float = 40.0 # Left margin
func _ready() -> void:
load_quotes_from_file()
show_quotes_on_screen_random()
func _input(event: InputEvent) -> void:
if event is InputEventMouseButton and event.pressed:
print("Mouse click or touch detected at:", event.position)
_respondToInput(event)
if event is InputEventScreenTouch and event.pressed:
print("Screen tap detected at:", event.position)
_respondToInput(event)
func load_quotes_from_file() -> void:
quotes.clear()
if not FileAccess.file_exists(QUOTES_FILE_PATH):
push_warning("Quotes file not found: %s" % QUOTES_FILE_PATH)
return
var file := FileAccess.open(QUOTES_FILE_PATH, FileAccess.READ)
if file == null:
push_warning("Could not open quotes file: %s" % QUOTES_FILE_PATH)
return
while file.get_position() < file.get_length():
var line: String = file.get_line().strip_edges()
# Skip empty lines and comment lines starting with '#'
if line == "" or line.begins_with("#"):
continue
quotes.append(line)
func show_quotes_on_screen_random() -> void:
# Clear any previous labels
for child in get_children():
if child is Label:
child.queue_free()
randomize()
var screen_size: Vector2 = get_viewport_rect().size
var placed_rects: Array[Rect2] = []
$ColorRect.size = screen_size
for quote in quotes:
var label := QUOTE_SCENE.instantiate() as Label
label.text = quote
add_child(label)
label.size = label.get_minimum_size()
var label_rect := Rect2(Vector2.ZERO, label.size)
var max_attempts := 10
var attempt := 0
var pos := Vector2.ZERO
while attempt < max_attempts:
# Leave a small border so text isn't cut off
var x_min := 10.0
var y_min := 10.0
var x_max := screen_size.x - label_rect.size.x - 10.0
var y_max := screen_size.y - label_rect.size.y - 10.0
pos = Vector2(
randf_range(x_min, x_max),
randf_range(y_min, y_max)
)
label_rect.position = pos
var overlaps := false
for existing_rect in placed_rects:
if label_rect.intersects(existing_rect):
overlaps = true
break
if not overlaps:
break
attempt += 1
# After attempts, just use whatever pos we ended with
label.position = pos
placed_rects.append(label_rect)
func _resize_all_labels() -> void:
for child in get_children():
if child is Label:
if child.has_method("_increaseFontSize"):
child._increaseFontSize()
func relayout_existing_quotes_random_non_overlapping() -> void:
var bounds: Vector2 = get_viewport_rect().size
$ColorRect.size = bounds
var padding_between: float = 8.0 # space so they don't touch
var border: float = 10.0 # keep away from edges
# Collect existing quote nodes (adjust filter to match your project)
var quotes_nodes: Array[Control] = []
for c in get_children():
if c is Control and c.is_class("Label"):
quotes_nodes.append(c)
# If sizes depend on font/wrap/theme, let UI settle one frame
await get_tree().process_frame
var placed: Array[Rect2] = []
# Optional: shuffle so layout changes each time
quotes_nodes.shuffle()
for bubble in quotes_nodes:
# Ensure size is up-to-date (works for PanelContainer/Label setups)
bubble.size = bubble.get_minimum_size()
var size: Vector2 = bubble.size
# If a bubble is bigger than the screen, it can never fit
if size.x > bounds.x - border * 2.0 or size.y > bounds.y - border * 2.0:
bubble.visible = false
continue
bubble.visible = true
var rect := Rect2(Vector2.ZERO, size).grow(padding_between)
var max_attempts := 300
var pos := bubble.position
var success := false
for _attempt in range(max_attempts):
var x_max := bounds.x - size.x - border
var y_max := bounds.y - size.y - border
pos = Vector2(
randf_range(border, x_max),
randf_range(border, y_max)
)
rect.position = pos - Vector2(padding_between, padding_between)
var overlaps := false
for r in placed:
if rect.intersects(r):
overlaps = true
break
if not overlaps:
success = true
break
if success:
bubble.position = pos
placed.append(rect)
# else: leave it where it was (or hide it, your call)
func _respondToInput(event) -> void:
var midPointX : float = get_viewport_rect().size.x / 2
var midPointY : float = get_viewport_rect().size.y / 2
if (event.position.x < midPointX and event.position.y < midPointY): # top left
print("top left")
relayout_existing_quotes_random_non_overlapping()
elif (event.position.x < midPointX and event.position.y > midPointY): # bottom left
print("bottom left")
elif (event.position.x > midPointX and event.position.y < midPointY): # top right
print("top right")
_randomizeColorAndFont()
elif (event.position.x > midPointX and event.position.y > midPointY): # bottom right
print("bottom right")
_resize_all_labels()
func _randomizeColorAndFont() -> void:
for child in get_children():
if child is Label:
if child.has_method("_randomizeFontAndSizeAndColor"):
child._randomizeFontAndSizeAndColor()