WIP: Iterating on warning message. Clamping set_target target in case of target being too close to limits.
This commit is contained in:
@@ -31,18 +31,10 @@ 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 = Rect2(camera.limit_left, camera.limit_top, camera.limit_right - camera.limit_left, camera.limit_bottom - camera.limit_top)
|
||||
var camera_limit: Rect2 = _make_camera_limit_rect(camera)
|
||||
|
||||
if not camera.check_point_is_inside_viewport_limits(new_pos):
|
||||
escoria.logger.warn(
|
||||
self,
|
||||
"[%s]: Invalid camera position. Camera cannot be moved to %s as this is outside the viewport with current camera limit %s."
|
||||
% [
|
||||
get_command_name(),
|
||||
new_pos,
|
||||
camera_limit
|
||||
]
|
||||
)
|
||||
_generate_viewport_warning(new_pos, camera)
|
||||
return false
|
||||
|
||||
return true
|
||||
@@ -64,3 +56,28 @@ 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 = _make_camera_limit_rect(camera)
|
||||
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()
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
@@ -140,6 +140,7 @@ func set_target(p_target, p_time : float = 0.0):
|
||||
set_drag_margin_enabled(false, false)
|
||||
|
||||
_convert_current_global_pos_for_disabled_drag_margin()
|
||||
_target = _convert_pos_for_disabled_drag_margin(_target)
|
||||
|
||||
_tween.interpolate_property(
|
||||
self,
|
||||
@@ -327,6 +328,18 @@ func check_point_is_inside_viewport_limits(point: Vector2) -> bool:
|
||||
return limits_to_test.has_point(point)
|
||||
|
||||
|
||||
func get_current_valid_viewport_values_x() -> Array:
|
||||
var viewport_rect: Rect2 = get_viewport_rect()
|
||||
|
||||
return [limit_left + viewport_rect.size.x * 0.5, limit_right - viewport_rect.size.x * 0.5]
|
||||
|
||||
|
||||
func get_current_valid_viewport_values_y() -> Array:
|
||||
var viewport_rect: Rect2 = get_viewport_rect()
|
||||
|
||||
return [limit_top + viewport_rect.size.y * 0.5, limit_bottom - viewport_rect.size.y * 0.5]
|
||||
|
||||
|
||||
func _target_reached():
|
||||
_tween.stop_all()
|
||||
set_drag_margin_enabled(true, true)
|
||||
@@ -341,11 +354,16 @@ func _target_reached():
|
||||
#
|
||||
# This is something of a hack until we decide on whether we implement an Escoria-specific camera
|
||||
# instead of relying on Camera2D.
|
||||
func _convert_current_global_pos_for_disabled_drag_margin() -> void:
|
||||
var viewport_rect: Rect2 = get_viewport_rect()
|
||||
|
||||
func _convert_current_global_pos_for_disabled_drag_margin() -> void:
|
||||
var cur_camera_pos: Vector2 = self.get_camera_screen_center()
|
||||
var ret_position: Vector2 = cur_camera_pos
|
||||
var ret_position: Vector2 = _convert_pos_for_disabled_drag_margin(cur_camera_pos)
|
||||
|
||||
self.global_position = ret_position
|
||||
|
||||
|
||||
func _convert_pos_for_disabled_drag_margin(pos: Vector2) -> Vector2:
|
||||
var viewport_rect: Rect2 = get_viewport_rect()
|
||||
var ret_position: Vector2 = pos
|
||||
|
||||
# If the current calculated centre of the camera/viewport is close enough to the set camera
|
||||
# limits (i.e. the centre is upto and including half the viewport's size to the limit being
|
||||
@@ -355,32 +373,17 @@ func _convert_current_global_pos_for_disabled_drag_margin() -> void:
|
||||
# Otherwise, we set the global_position to be the value that would allow Camera2D to convert it
|
||||
# to the value of the current calculated centre. This compensates for the switch when disabling
|
||||
# drag margins.
|
||||
if cur_camera_pos.x - viewport_rect.size.x * 0.5 * zoom.x <= limit_left:
|
||||
#ret_position.x = cur_camera_pos.x
|
||||
if ret_position.x - viewport_rect.size.x * 0.5 * zoom.x <= limit_left:
|
||||
ret_position.x = limit_left + viewport_rect.size.x * 0.5 * zoom.x
|
||||
elif cur_camera_pos.x + viewport_rect.size.x * 0.5 * zoom.x >= limit_right:
|
||||
#ret_position.x = cur_camera_pos.x
|
||||
elif ret_position.x + viewport_rect.size.x * 0.5 * zoom.x >= limit_right:
|
||||
ret_position.x = limit_right - viewport_rect.size.x * 0.5 * zoom.x
|
||||
# elif cur_camera_pos.x < ret_position.x:
|
||||
# ret_position.x = ret_position.x - (viewport_rect.size.x * 0.5 * zoom.x * drag_margin_right)
|
||||
# elif cur_camera_pos.x > ret_position.x:
|
||||
# ret_position.x = ret_position.x + (viewport_rect.size.x * 0.5 * zoom.x * drag_margin_left)
|
||||
|
||||
if cur_camera_pos.y - viewport_rect.size.y * 0.5 * zoom.y <= limit_top:
|
||||
#ret_position.y = cur_camera_pos.y
|
||||
if ret_position.y - viewport_rect.size.y * 0.5 * zoom.y <= limit_top:
|
||||
ret_position.y = limit_top + viewport_rect.size.y * 0.5 * zoom.y
|
||||
elif cur_camera_pos.y + viewport_rect.size.y * 0.5 * zoom.y >= limit_bottom:
|
||||
#ret_position.y = cur_camera_pos.y
|
||||
elif ret_position.y + viewport_rect.size.y * 0.5 * zoom.y >= limit_bottom:
|
||||
ret_position.y = limit_bottom - viewport_rect.size.y * 0.5 * zoom.y
|
||||
|
||||
|
||||
#elif cur_camera_pos.y < ret_position.y:
|
||||
# ret_position.y = ret_position.y - (viewport_rect.size.y * 0.5 * zoom.y * drag_margin_bottom)
|
||||
#elif cur_camera_pos.y > ret_position.y:
|
||||
# ret_position.y = cur_camera_pos.y
|
||||
#ret_position.y = ret_position.y + (viewport_rect.size.y * 0.5 * zoom.y * drag_margin_top)
|
||||
|
||||
self.global_position = ret_position
|
||||
return ret_position
|
||||
|
||||
|
||||
func clamp_to_viewport_limits() -> void:
|
||||
|
||||
Reference in New Issue
Block a user