Browse Source

:truck: Slice slide targeting feature

Split the event handling in different methods to make it more readable.
DricomDragon 2 years ago
parent
commit
43dfaaed89
1 changed files with 22 additions and 15 deletions
  1. 22 15
      godot/projector/show/Show.gd

+ 22 - 15
godot/projector/show/Show.gd

@@ -20,7 +20,7 @@ func _ready() -> void:
 	_enforce()
 
 	if hide_slides:
-		hide_every_slide()
+		_hide_every_slide()
 
 	go_slowly_to_next_slide()
 
@@ -40,18 +40,18 @@ func next_slide(trans: Focus.Transit) -> void:
 func next_slide_at(next_id: int, trans: Focus.Transit) -> void:
 	_disconnect_current_slide()
 	current_id = next_id
-	current_slide = focus_slide(current_id, trans)
+	current_slide = _focus_slide(current_id, trans)
 	_connect_current_slide()
 
 
-func focus_slide(id: int, trans: Focus.Transit) -> Slide:
+func _focus_slide(id: int, trans: Focus.Transit) -> Slide:
 	var slide: Slide = slides[id]
 	slide.focus()
 	new_focus.emit(slide.get_center(), slide.get_scale().x, trans)
 	return slide
 
 
-func hide_every_slide() -> void:
+func _hide_every_slide() -> void:
 	for slide in slides:
 		slide.gently_hide()
 
@@ -115,17 +115,24 @@ func _unhandled_key_input(event: InputEvent) -> void:
 
 func _unhandled_input(event: InputEvent) -> void:
 	if event is InputEventMouseButton and event.is_pressed():
-		var gpos: Vector2 = get_global_mouse_position()
-		var nearest_slide = current_slide
-		var shortest_distance = gpos.distance_squared_to(nearest_slide.get_center())
-		for current_slide in slides:
-			var current_distance = gpos.distance_squared_to(current_slide.get_center())
-			if current_distance < shortest_distance:
-				nearest_slide = current_slide
-				shortest_distance = current_distance
-
-		var next_id = slides.find(nearest_slide)
-		next_slide_at(next_id, Focus.Transit.SMOOTH)
+		_focus_nearest_slide_from(get_global_mouse_position())
+
+
+func _focus_nearest_slide_from(target_pos: Vector2) -> void:
+	var next_id = _search_nearest_slide_from(target_pos)
+	next_slide_at(next_id, Focus.Transit.SMOOTH)
+
+
+func _search_nearest_slide_from(target_pos: Vector2) -> int:
+	var nearest_slide = current_slide
+	var shortest_distance = target_pos.distance_squared_to(nearest_slide.get_center())
+	for current_slide in slides:
+		var current_distance = target_pos.distance_squared_to(current_slide.get_center())
+		if current_distance < shortest_distance:
+			nearest_slide = current_slide
+			shortest_distance = current_distance
+
+	return slides.find(nearest_slide)
 
 
 func _on_current_slide_finished() -> void: