140 lines
5.6 KiB
GDScript
140 lines
5.6 KiB
GDScript
|
|
extends Container
|
||
|
|
## A rotating word sphere, with enough separation to keep every answer clickable.
|
||
|
|
var hovered: Control
|
||
|
|
var yaw := 0.35
|
||
|
|
var pitch := -0.12
|
||
|
|
var velocity := Vector2(0.065,0.018)
|
||
|
|
var pointer := Vector2.ZERO
|
||
|
|
var pointer_inside := false
|
||
|
|
var pressed_tag: Control
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
mouse_filter=Control.MOUSE_FILTER_STOP
|
||
|
|
mouse_exited.connect(func():
|
||
|
|
pointer_inside=false
|
||
|
|
hovered=null)
|
||
|
|
|
||
|
|
func pick_tag(point: Vector2) -> Control:
|
||
|
|
var selected: Control = null
|
||
|
|
for child in get_children():
|
||
|
|
if not child is Button or child.disabled: continue
|
||
|
|
var rect := Rect2(child.position+child.pivot_offset*(Vector2.ONE-child.scale),child.size*child.scale)
|
||
|
|
if rect.has_point(point) and (selected==null or child.z_index>selected.z_index): selected=child
|
||
|
|
return selected
|
||
|
|
|
||
|
|
func _gui_input(event: InputEvent) -> void:
|
||
|
|
if event is InputEventMouseMotion:
|
||
|
|
pointer=event.position
|
||
|
|
pointer_inside=true
|
||
|
|
hovered=pick_tag(pointer)
|
||
|
|
elif event is InputEventMouseButton and event.button_index==MOUSE_BUTTON_LEFT:
|
||
|
|
pointer=event.position
|
||
|
|
hovered=pick_tag(pointer)
|
||
|
|
if event.pressed:
|
||
|
|
pressed_tag=hovered
|
||
|
|
else:
|
||
|
|
var selected := pressed_tag
|
||
|
|
pressed_tag=null
|
||
|
|
if is_instance_valid(selected) and selected==hovered and not selected.disabled:
|
||
|
|
selected.pressed.emit()
|
||
|
|
accept_event()
|
||
|
|
|
||
|
|
func _get_minimum_size() -> Vector2:
|
||
|
|
return Vector2(0,300)
|
||
|
|
|
||
|
|
func _notification(what: int) -> void:
|
||
|
|
if what == NOTIFICATION_SORT_CHILDREN: arrange()
|
||
|
|
|
||
|
|
func arrange() -> void:
|
||
|
|
var count := get_child_count()
|
||
|
|
for i in range(count):
|
||
|
|
var child := get_child(i) as Control
|
||
|
|
if child == null: continue
|
||
|
|
# Canvas z-order does not define native Control mouse order. Pick the visibly
|
||
|
|
# frontmost tag in this container, rather than an invisible rear rectangle.
|
||
|
|
child.mouse_filter=Control.MOUSE_FILTER_IGNORE
|
||
|
|
var dimensions := child.get_combined_minimum_size()
|
||
|
|
fit_child_in_rect(child,Rect2(child.position,dimensions))
|
||
|
|
child.pivot_offset = dimensions/2
|
||
|
|
var latitude := 1.0-2.0*(i+0.5)/maxf(1,count)
|
||
|
|
var ring := sqrt(maxf(0,1-latitude*latitude))
|
||
|
|
child.set_meta("cloud_seed",Vector3(cos(i*2.39996)*ring,latitude,sin(i*2.39996)*ring))
|
||
|
|
if not child is Button: child.position=(size-dimensions)/2
|
||
|
|
|
||
|
|
func _process(delta: float) -> void:
|
||
|
|
if size.x<1 or size.y<1: return
|
||
|
|
if not is_instance_valid(hovered): hovered=null
|
||
|
|
if pointer_inside: hovered=pick_tag(pointer)
|
||
|
|
mouse_default_cursor_shape=Control.CURSOR_POINTING_HAND if hovered!=null else Control.CURSOR_ARROW
|
||
|
|
var tags: Array[Control] = []
|
||
|
|
var active: Control = null
|
||
|
|
var largest := Vector2.ZERO
|
||
|
|
for child in get_children():
|
||
|
|
if not child is Button: continue
|
||
|
|
tags.append(child)
|
||
|
|
largest.x=maxf(largest.x,child.size.x)
|
||
|
|
largest.y=maxf(largest.y,child.size.y)
|
||
|
|
if not child.disabled and (child==hovered or child.has_focus()): active=child
|
||
|
|
if tags.is_empty(): return
|
||
|
|
var mouse := pointer
|
||
|
|
var direction := Vector2(0.065,0.018)
|
||
|
|
if pointer_inside and Rect2(Vector2.ZERO,size).has_point(mouse):
|
||
|
|
var relative := (mouse-size/2)/(size/2)
|
||
|
|
direction=Vector2(0.04+relative.x*0.15,-relative.y*0.10)
|
||
|
|
velocity=velocity.lerp(direction,1.0-exp(-delta/0.65))
|
||
|
|
if active==null:
|
||
|
|
yaw+=delta*velocity.x
|
||
|
|
pitch+=delta*velocity.y
|
||
|
|
var rotation_basis := Basis(Vector3.UP,yaw)*Basis(Vector3.RIGHT,pitch)
|
||
|
|
var cloud_origin := Vector2(size.x/2,size.y*0.60)
|
||
|
|
var radius := Vector2(maxf(90,(size.x-largest.x*1.18)/2-20),maxf(55,size.y*0.40-largest.y*0.59-14))
|
||
|
|
var targets: Array[Vector2] = []
|
||
|
|
var scales: Array[float] = []
|
||
|
|
var depths: Array[float] = []
|
||
|
|
for child in tags:
|
||
|
|
var point: Vector3 = rotation_basis*child.get_meta("cloud_seed",Vector3.ZERO)
|
||
|
|
var depth := (point.z+1)/2
|
||
|
|
var scale_factor := 0.68+depth*0.37
|
||
|
|
var target := cloud_origin+Vector2(point.x*radius.x,point.y*radius.y)*scale_factor
|
||
|
|
if child==active:
|
||
|
|
if not child.get_meta("cloud_active",false): child.set_meta("cloud_hold",child.position+child.size/2)
|
||
|
|
target=child.get_meta("cloud_hold")
|
||
|
|
scale_factor=1.18
|
||
|
|
child.set_meta("cloud_active",child==active)
|
||
|
|
targets.append(target)
|
||
|
|
scales.append(scale_factor)
|
||
|
|
depths.append(depth)
|
||
|
|
# Separate intersecting words without losing their depth or curved movement.
|
||
|
|
for iteration in range(7):
|
||
|
|
for i in range(tags.size()):
|
||
|
|
for j in range(i+1,tags.size()):
|
||
|
|
var a := tags[i].size*scales[i]+Vector2(12,8)
|
||
|
|
var b := tags[j].size*scales[j]+Vector2(12,8)
|
||
|
|
var difference := targets[i]-targets[j]
|
||
|
|
var overlap := (a+b)/2-difference.abs()
|
||
|
|
if overlap.x<=0 or overlap.y<=0: continue
|
||
|
|
var push := Vector2.ZERO
|
||
|
|
if overlap.x<overlap.y*1.4:
|
||
|
|
push.x=(overlap.x+2)*0.52*(1.0 if difference.x>=0 else -1.0)
|
||
|
|
else:
|
||
|
|
push.y=(overlap.y+2)*0.52*(1.0 if difference.y>=0 else -1.0)
|
||
|
|
if tags[i]!=active: targets[i]+=push*(2 if tags[j]==active else 1)
|
||
|
|
if tags[j]!=active: targets[j]-=push*(2 if tags[i]==active else 1)
|
||
|
|
for i in range(tags.size()):
|
||
|
|
if tags[i]==active: continue
|
||
|
|
var edge := tags[i].size*scales[i]/2+Vector2(9,9)
|
||
|
|
targets[i]=targets[i].clamp(edge,size-edge)
|
||
|
|
for i in range(tags.size()):
|
||
|
|
var child := tags[i]
|
||
|
|
var center: Vector2 = child.get_meta("cloud_center",targets[i])
|
||
|
|
center=targets[i] if child==active else center.lerp(targets[i],1.0-exp(-delta/0.35))
|
||
|
|
var growth: float = lerpf(child.get_meta("cloud_scale",scales[i]),scales[i],1.0-exp(-delta/0.13))
|
||
|
|
child.set_meta("cloud_center",center)
|
||
|
|
child.set_meta("cloud_scale",growth)
|
||
|
|
child.position=center-child.size/2
|
||
|
|
child.scale=Vector2.ONE*growth
|
||
|
|
child.modulate.a=1.0 if child==active else 0.48+depths[i]*0.52
|
||
|
|
var ink := Color("e4a3c0") if child==active else Color("ddd1e5")
|
||
|
|
if child.get_theme_color("font_color")!=ink: child.add_theme_color_override("font_color",ink)
|
||
|
|
child.z_index=50 if child==active else int(depths[i]*20)
|