2021-12-12 17:04:17 +01:00
tool
extends SummerDayEditable
2022-01-11 21:57:07 +01:00
class_name SummerDayEditableCuboid
2021-12-19 22:26:09 +01:00
2022-01-02 03:09:18 +01:00
export ( PoolVector2Array ) var vertices = PoolVector2Array ( [
Vector2 ( - 0.4 , - 0.4 ) ,
Vector2 ( 0.1 , - 0.4 ) ,
Vector2 ( 0.1 , 0.1 ) ,
Vector2 ( - 0.4 , 0.1 ) ,
Vector2 ( - 0.1 , - 0.1 ) ,
Vector2 ( 0.4 , - 0.1 ) ,
Vector2 ( 0.4 , 0.4 ) ,
Vector2 ( - 0.1 , 0.4 ) ,
] )
2021-12-19 22:26:09 +01:00
2022-01-03 23:53:28 +01:00
var picked_point = - 1
2021-12-19 22:26:09 +01:00
2022-01-11 14:13:56 +01:00
func _init ( ) :
z_range . index = 0
2022-01-02 03:09:18 +01:00
func _check_for_selection_candidate ( event : InputEvent ) - > bool : # Virtual.
if event is InputEventMouseButton :
if event . button_index == BUTTON_LEFT and event . pressed :
return _is_point_included ( event . position )
return false
2021-12-19 22:26:09 +01:00
2022-01-02 03:09:18 +01:00
func _interact ( event : InputEvent ) - > bool : # Virtual.
2022-01-03 23:53:28 +01:00
if event is InputEventMouseButton :
if event . button_index == BUTTON_LEFT :
if event . pressed :
_pick_point ( event . position )
else :
picked_point = - 1
if event is InputEventMouseMotion :
if picked_point != - 1 :
vertices [ picked_point ] = event . position
2022-01-09 17:34:26 +01:00
_update_table ( )
2022-01-03 23:53:28 +01:00
update_interaction_hint ( )
return true
2021-12-31 17:22:58 +01:00
2022-01-09 17:34:26 +01:00
func _update_table ( ) : # Virtual.
var rear_matrix = SummerDayMathHelper . plane_3d_to_xy (
Vector3 ( 1.0 , 1.0 , 1.0 ) ,
Vector3 ( - 1.0 , 1.0 , 1.0 ) ,
Vector3 ( - 1.0 , - 1.0 , 1.0 ) ,
Vector3 ( 1.0 , - 1.0 , 1.0 ) ,
vertices [ 0 ] , vertices [ 1 ] , vertices [ 2 ] , vertices [ 3 ]
)
var front_matrix = SummerDayMathHelper . plane_3d_to_xy (
Vector3 ( 1.0 , 1.0 , - 1.0 ) ,
Vector3 ( - 1.0 , 1.0 , - 1.0 ) ,
Vector3 ( - 1.0 , - 1.0 , - 1.0 ) ,
Vector3 ( 1.0 , - 1.0 , - 1.0 ) ,
vertices [ 4 ] , vertices [ 5 ] , vertices [ 6 ] , vertices [ 7 ]
)
table . apply_matrix ( front_matrix , 0 , 0 )
table . apply_matrix ( rear_matrix , 0 , 1 )
table . flush ( )
2021-12-19 22:26:09 +01:00
2022-01-02 03:09:18 +01:00
func _is_point_included ( point : Vector2 ) - > bool : # Private.
var convex_hull = Geometry . convex_hull_2d ( vertices )
return Geometry . is_point_in_polygon ( point , convex_hull )
2022-01-03 23:53:28 +01:00
func _draw_interaction_hint ( ) :
for i in range ( 4 ) :
var vertex_2d = vertices [ i ]
var vertex = _with_z ( vertex_2d , 0.0 )
draw_dot ( vertex , Color . cornflower )
for i in range ( 4 , 8 ) :
var vertex_2d = vertices [ i ]
var vertex = _with_z ( vertex_2d , - 1.0 )
draw_dot ( vertex , Color . crimson )
for i in range ( 4 ) :
draw_line (
_with_z ( vertices [ i ] , 0.0 ) ,
_with_z ( vertices [ wrapi ( i + 1 , 0 , 4 ) ] , 0.0 ) ,
Color . cornflower
)
for i in range ( 4 ) :
draw_line (
_with_z ( vertices [ i ] , 0.0 ) ,
_with_z ( vertices [ i + 4 ] , - 1.0 ) ,
Color . cornflower ,
Color . crimson
)
for i in range ( 4 ) :
draw_line (
_with_z ( vertices [ i + 4 ] , - 1.0 ) ,
_with_z ( vertices [ wrapi ( i + 5 , 4 , 8 ) ] , - 1.0 ) ,
Color . crimson
)
2021-12-19 22:26:09 +01:00
2022-01-03 23:53:28 +01:00
func _pick_point ( mouse_pos ) :
var picked_point_so_far = - 1
var nearest_distance = 0.02
for i in vertices . size ( ) :
var distance = vertices [ i ] . distance_to ( mouse_pos )
if distance < nearest_distance :
nearest_distance = distance
picked_point_so_far = i
picked_point = picked_point_so_far
func _with_z ( vector : Vector2 , z : float ) - > Vector3 :
return Vector3 ( vector . x , vector . y , z )