Browse Source

Made minor changes to GUI in 3D demo script

TwistedTwigleg 6 years ago
parent
commit
61ea89e836
1 changed files with 17 additions and 15 deletions
  1. 17 15
      viewport/gui_in_3d/gui_3d.gd

+ 17 - 15
viewport/gui_in_3d/gui_3d.gd

@@ -2,9 +2,9 @@ extends Spatial
 # Member variables
 # The size of the quad mesh itself.
 var quad_mesh_size
-# Indentify if the mouse is inside the Area
+# Used for checking if the mouse is inside the Area
 var is_mouse_inside = false
-# Identify if the mouse was pressed inside the Area
+# Used for checking if the mouse was pressed inside the Area
 var is_mouse_held = false
 # The last non-empty mouse position. Used when dragging outside of the box.
 var last_mouse_pos3D = null
@@ -25,7 +25,7 @@ func _ready():
 
 
 func _process(_delta):
-	#NOTE: Remove this function if you don't plan on using billboard settings.
+	# NOTE: Remove this function if you don't plan on using billboard settings.
 	rotate_area_to_billboard()
 
 
@@ -52,18 +52,18 @@ func _input(event):
 
 # Handle mouse events inside Area. (Area.input_event had many issues with dragging)
 func handle_mouse(event):
-	#Get mesh size to detect edges and make conversions. This code only support PlaneMesh and QuadMesh.
+	# Get mesh size to detect edges and make conversions. This code only support PlaneMesh and QuadMesh.
 	quad_mesh_size = node_quad.mesh.size
 	
-	#Detect mouse being held to mantain event while outside of bounds. Avoid orphan clicks
+	# Detect mouse being held to mantain event while outside of bounds. Avoid orphan clicks
 	if event is InputEventMouseButton or event is InputEventScreenTouch:
 		is_mouse_held = event.pressed
 	
-	#Find mouse position in Area
+	# Find mouse position in Area
 	var mouse_pos3D = find_mouse(event.global_position)
 	
 	# Check if the mouse is outside of bounds, use last position to avoid errors
-	#NOTE: mouse_exited signal was unrealiable in this situation
+	# NOTE: mouse_exited signal was unrealiable in this situation
 	is_mouse_inside = mouse_pos3D != null
 	if is_mouse_inside:
 		# Convert click_pos from world coordinate space to a coordinate space relative to the Area node.
@@ -72,8 +72,10 @@ func handle_mouse(event):
 		last_mouse_pos3D = mouse_pos3D
 	else:
 		mouse_pos3D = last_mouse_pos3D
+		if mouse_pos3D == null:
+			mouse_pos3D = Vector3.ZERO
 	
-	#TODO: adapt to bilboard mode or avoid completelly
+	# TODO: adapt to bilboard mode or avoid completely
 	
 	# convert the relative event position from 3D to 2D
 	var mouse_pos2D = Vector2(mouse_pos3D.x, -mouse_pos3D.y)
@@ -114,13 +116,13 @@ func handle_mouse(event):
 func find_mouse(global_position):
 	var camera = get_viewport().get_camera()
 	
-	#from camera center to the mouse position in the Area
+	# From camera center to the mouse position in the Area
 	var from = camera.project_ray_origin(global_position)
 	var dist = find_further_distance_to(camera.transform.origin)
 	var to = from + camera.project_ray_normal(global_position) * dist
 	
 	
-	#Manually raycasts the are to find the mouse position
+	# Manually raycasts the are to find the mouse position
 	var result = get_world().direct_space_state.intersect_ray(from, to, [], node_area.collision_layer,false,true) #for 3.1 changes
 	
 	if result.size() > 0:
@@ -130,14 +132,14 @@ func find_mouse(global_position):
 
 
 func find_further_distance_to(origin):
-	#Find edges of collision and change to global positions
+	# Find edges of collision and change to global positions
 	var edges = []
 	edges.append(node_area.to_global(Vector3(quad_mesh_size.x / 2, quad_mesh_size.y / 2, 0)))
 	edges.append(node_area.to_global(Vector3(quad_mesh_size.x / 2, -quad_mesh_size.y / 2, 0)))
 	edges.append(node_area.to_global(Vector3(-quad_mesh_size.x / 2, quad_mesh_size.y / 2, 0)))
 	edges.append(node_area.to_global(Vector3(-quad_mesh_size.x / 2, -quad_mesh_size.y / 2, 0)))
 	
-	#Get the furthest distance between the camera and collision to avoid raycasting too far or too short
+	# Get the furthest distance between the camera and collision to avoid raycasting too far or too short
 	var far_dist = 0
 	var temp_dist
 	for edge in edges:
@@ -151,11 +153,11 @@ func find_further_distance_to(origin):
 func rotate_area_to_billboard():
 	var billboard_mode = node_quad.get_surface_material(0).params_billboard_mode
 	
-	#try to match the area with the material's billboard setting, if enabled
+	# Try to match the area with the material's billboard setting, if enabled
 	if billboard_mode > 0:
 		# Get the camera
 		var camera = get_viewport().get_camera()
-		#Look in the same direction as the camera
+		# Look in the same direction as the camera
 		var look = camera.to_global(Vector3(0, 0, -100)) - camera.global_transform.origin
 		look = node_area.translation + look
 		
@@ -165,6 +167,6 @@ func rotate_area_to_billboard():
 		
 		node_area.look_at(look, Vector3.UP)
 		
-		#Ratate in the Z axis to compensate camera tilt
+		# Rotate in the Z axis to compensate camera tilt
 		node_area.rotate_object_local(Vector3.BACK, camera.rotation.z)