You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
3.5 KiB
145 lines
3.5 KiB
extends Control
|
|
|
|
export var default_width = 500
|
|
export var default_height = 500
|
|
export var default_spacing = 5
|
|
|
|
var data = {
|
|
width = default_width,
|
|
height = default_height,
|
|
spacing = default_spacing
|
|
}
|
|
|
|
var polygon_data = {
|
|
width = 500,
|
|
height = 500,
|
|
spacing = 1.5
|
|
}
|
|
|
|
var poisson_disc_sampling: PoissonDiscSampling = PoissonDiscSampling.new()
|
|
var points = PoolByteArray()
|
|
var poissons = []
|
|
var truncs = []
|
|
|
|
signal new_poisson(points)
|
|
signal new_truncs(truncs)
|
|
|
|
func _ready():
|
|
reset_options(default_width, default_height, default_spacing)
|
|
|
|
func reset_options(width, height, spacing):
|
|
$HBoxContainer/Properties/width/width_edit.text = str(width)
|
|
$HBoxContainer/Properties/height/height_edit.text = str(height)
|
|
$HBoxContainer/Properties/spacing/spacing_edit.text = str(spacing)
|
|
|
|
func set_data(width, height, spacing):
|
|
data.width = width
|
|
data.height = height
|
|
data.spacing = spacing
|
|
|
|
func init_points():
|
|
points.resize(data.width * data.height)
|
|
points.fill(0)
|
|
|
|
func _on_Button_pressed():
|
|
set_data(
|
|
int($HBoxContainer/Properties/width/width_edit.text),
|
|
int($HBoxContainer/Properties/height/height_edit.text),
|
|
int($HBoxContainer/Properties/spacing/spacing_edit.text)
|
|
)
|
|
|
|
init_points()
|
|
|
|
var rect = Rect2(Vector2(0, 0), Vector2(data.width, data.height))
|
|
poissons = poisson_disc_sampling.generate_points(data.spacing, rect, 20)
|
|
|
|
for poisson in poissons:
|
|
points.set(int(poisson.x) + int(poisson.y) * data.width, 1)
|
|
|
|
emit_signal("new_poisson", points)
|
|
|
|
|
|
func _on_save_pressed():
|
|
$SaveFileDialog.popup()
|
|
|
|
func _on_SaveFileDialog_file_selected(path):
|
|
var file = File.new()
|
|
file.open(path, 2)
|
|
file.store_var(data)
|
|
file.store_var(points)
|
|
|
|
|
|
func _on_open_pressed():
|
|
$OpenFileDialog.popup()
|
|
|
|
func _on_OpenFileDialog_file_selected(path):
|
|
var file = File.new()
|
|
file.open(path, 1)
|
|
var new_data = file.get_var()
|
|
set_data(new_data.width, new_data.height, new_data.spacing)
|
|
reset_options(new_data.width, new_data.height, new_data.spacing)
|
|
points = file.get_var()
|
|
emit_signal("new_poisson", points)
|
|
|
|
|
|
func _on_trunc_pressed():
|
|
truncs = []
|
|
# Création d'un polygon
|
|
var polygon = PoolVector2Array([
|
|
Vector2(0, 0),
|
|
Vector2(polygon_data.width, 0),
|
|
Vector2(polygon_data.width,
|
|
polygon_data.height),
|
|
Vector2(0, polygon_data.height)
|
|
])
|
|
|
|
# Exemple d'hexagone
|
|
polygon = PoolVector2Array([
|
|
Vector2(200, 0),
|
|
Vector2(400, 133),
|
|
Vector2(400, 266),
|
|
Vector2(200, 400),
|
|
Vector2(0, 266),
|
|
Vector2(0, 133),
|
|
])
|
|
|
|
|
|
|
|
# Spacing
|
|
# Si sup à celui de base, le polygon doit se rétrécir proportionnellement
|
|
# Les positions des points augmentent à la fin
|
|
# Et inversement
|
|
|
|
var factor = float(polygon_data.spacing) / float(data.spacing)
|
|
|
|
for i in polygon.size():
|
|
var vector = Vector2(polygon[i].x / factor, polygon[i].y / factor)
|
|
polygon.set(i, vector)
|
|
|
|
var max_point = Vector2(0, 0)
|
|
for point in polygon:
|
|
if point.x > max_point.x:
|
|
max_point.x = point.x
|
|
if point.y > max_point.y:
|
|
max_point.y = point.y
|
|
|
|
# Déplacement du polygon de telle sorte qu'il reste dans le carré original.
|
|
var rng = RandomNumberGenerator.new()
|
|
rng.randomize()
|
|
var offset = Vector2(rng.randi_range(0, data.width - max_point.x), rng.randi_range(0, data.height - max_point.y))
|
|
polygon = Transform2D(0, offset).xform(polygon)
|
|
max_point += offset
|
|
|
|
|
|
|
|
# On récupère les points à l'intérieur du polygon
|
|
for i in range(offset.x, max_point.x):
|
|
for j in range(offset.y, max_point.y):
|
|
var trunc = Vector2(i, j)
|
|
if Geometry.is_point_in_polygon(trunc, polygon):
|
|
if points[i + j * data.width]:
|
|
trunc -= offset
|
|
trunc *= factor
|
|
truncs.append(trunc)
|
|
|
|
emit_signal("new_truncs", truncs)
|
|
|