fix: cleaning up and updating validation code.

This commit is contained in:
Duncan Brown
2022-10-28 11:48:31 -04:00
parent 3673977d5c
commit e807044e45
3 changed files with 49 additions and 37 deletions

View File

@@ -60,16 +60,7 @@ func validate(arguments: Array):
var camera_limit: Rect2 = Rect2(camera.limit_left, camera.limit_top, camera.limit_right - camera.limit_left, camera.limit_bottom - camera.limit_top)
if not camera.check_point_is_inside_viewport_limits(target_pos):
escoria.logger.warn(
self,
"[%s]: Invalid camera position. Camera cannot be moved to %s at position %s as this is outside the current viewport with camera limit %s."
% [
get_command_name(),
arguments[0],
target_pos,
camera_limit
]
)
_generate_viewport_warning(target_pos - Vector2.ONE, camera)
return false
if not arguments[2] in SUPPORTED_TRANSITIONS:
@@ -108,3 +99,24 @@ func interrupt():
self,
"[%s] interrupt() function not implemented." % get_command_name()
)
func _generate_viewport_warning(new_pos: Vector2, camera: ESCCamera) -> void:
var camera_limit: Rect2 = camera.get_camera_limit_rect()
var message: String = \
"""
[%s]: Invalid camera position. Camera cannot be moved to %s as this is outside the viewport with current camera limit %s.
Current valid ranges for positions are: x = %s inclusive; y = %s inclusive.
"""
escoria.logger.warn(
self,
message
% [
get_command_name(),
new_pos.floor(),
camera_limit,
camera.get_current_valid_viewport_values_x(),
camera.get_current_valid_viewport_values_y()
]
)

View File

@@ -31,7 +31,7 @@ func validate(arguments: Array):
var new_pos: Vector2 = Vector2(arguments[1], arguments[2])
var camera: ESCCamera = escoria.object_manager.get_object(escoria.object_manager.CAMERA).node as ESCCamera
var camera_limit: Rect2 = _make_camera_limit_rect(camera)
var camera_limit: Rect2 = camera.get_camera_limit_rect()
if not camera.check_point_is_inside_viewport_limits(new_pos):
_generate_viewport_warning(new_pos, camera)
@@ -59,7 +59,7 @@ func interrupt():
func _generate_viewport_warning(new_pos: Vector2, camera: ESCCamera) -> void:
var camera_limit: Rect2 = _make_camera_limit_rect(camera)
var camera_limit: Rect2 = camera.get_camera_limit_rect()
var message: String = \
"""
[%s]: Invalid camera position. Camera cannot be moved to %s as this is outside the viewport with current camera limit %s.
@@ -77,7 +77,3 @@ func _generate_viewport_warning(new_pos: Vector2, camera: ESCCamera) -> void:
camera.get_current_valid_viewport_values_y()
]
)
func _make_camera_limit_rect(camera: ESCCamera) -> Rect2:
return Rect2(camera.limit_left, camera.limit_top, camera.limit_right - camera.limit_left, camera.limit_bottom - camera.limit_top)

View File

@@ -340,6 +340,31 @@ func get_current_valid_viewport_values_y() -> Array:
return [limit_top + viewport_rect.size.y * 0.5, limit_bottom - viewport_rect.size.y * 0.5]
func get_camera_limit_rect() -> Rect2:
return Rect2(limit_left, limit_top, limit_right - limit_left, limit_bottom - limit_top)
# Used when drag margins are enabled. Clamps the camera so it respects the viewport limits inside
# the camera limits.
func clamp_to_viewport_limits() -> void:
var viewport_rect: Rect2 = get_viewport_rect()
var cur_camera_pos: Vector2 = self.get_camera_screen_center()
var ret_position: Vector2 = cur_camera_pos
if cur_camera_pos.x - viewport_rect.size.x * 0.5 * zoom.x <= limit_left:
ret_position.x = limit_left + viewport_rect.size.x * 0.5 * zoom.x * (1 + drag_margin_left)
elif cur_camera_pos.x + viewport_rect.size.x * 0.5 * zoom.x >= limit_right:
ret_position.x = limit_right - viewport_rect.size.x * 0.5 * zoom.x * (1 + drag_margin_right)
if cur_camera_pos.y - viewport_rect.size.y * 0.5 * zoom.y <= limit_top:
ret_position.y = limit_top + viewport_rect.size.y * 0.5 * zoom.y * (1 + drag_margin_top)
elif cur_camera_pos.y + viewport_rect.size.y * 0.5 * zoom.y >= limit_bottom:
ret_position.y = limit_bottom - viewport_rect.size.y * 0.5 * zoom.y * (1 + drag_margin_bottom)
self.global_position = ret_position
func _target_reached():
_tween.stop_all()
set_drag_margin_enabled(true, true)
@@ -384,24 +409,3 @@ func _convert_pos_for_disabled_drag_margin(pos: Vector2) -> Vector2:
ret_position.y = limit_bottom - viewport_rect.size.y * 0.5 * zoom.y
return ret_position
# Used when drag margins are enabled. Clamps the camera so it respects the viewport limits inside
# the camera limits.
func clamp_to_viewport_limits() -> void:
var viewport_rect: Rect2 = get_viewport_rect()
var cur_camera_pos: Vector2 = self.get_camera_screen_center()
var ret_position: Vector2 = cur_camera_pos
if cur_camera_pos.x - viewport_rect.size.x * 0.5 * zoom.x <= limit_left:
ret_position.x = limit_left + viewport_rect.size.x * 0.5 * zoom.x * (1 + drag_margin_left)
elif cur_camera_pos.x + viewport_rect.size.x * 0.5 * zoom.x >= limit_right:
ret_position.x = limit_right - viewport_rect.size.x * 0.5 * zoom.x * (1 + drag_margin_right)
if cur_camera_pos.y - viewport_rect.size.y * 0.5 * zoom.y <= limit_top:
ret_position.y = limit_top + viewport_rect.size.y * 0.5 * zoom.y * (1 + drag_margin_top)
elif cur_camera_pos.y + viewport_rect.size.y * 0.5 * zoom.y >= limit_bottom:
ret_position.y = limit_bottom - viewport_rect.size.y * 0.5 * zoom.y * (1 + drag_margin_bottom)
self.global_position = ret_position