Доработка. Запретить перемещение карты пользователем за край.

This commit is contained in:
sasha80
2025-05-30 11:37:11 +03:00
parent 2638fad8c1
commit cd5a58433c
2 changed files with 35 additions and 15 deletions

View File

@@ -24,8 +24,8 @@ class_name MercatorTileMap
@export_color_no_alpha() var back_color: = Color.BLACK
const default_size: = 400.0
var is_centered: = false
var is_anim_playng: = false
var is_out_border1: = true
var is_out_border2: = true
@export var animation_time: float = 0.1:
set(val):
@@ -65,7 +65,7 @@ var is_anim_playng: = false
return _xyz.z
var _xyz: Vector3 = Vector3(0, 0, min_zoom_level)
var _xyz: Vector3 = Vector3(0, 0, 0)
var _cache: Dictionary = {}
var _queue: Dictionary = {}
var _dragging: bool = false
@@ -111,6 +111,7 @@ func get_marks_count(): return _marks.size()
func _ready() -> void:
_xyz.z = min_zoom_level
image_type = 'png'
$canvas.connect('mouse_entered', func(): _rollover = true)
$canvas.connect('mouse_exited', func(): _rollover = false)
@@ -199,12 +200,12 @@ func _input(e) -> void:
if e is InputEventMouseButton:
if e.pressed:
if e.button_index == MOUSE_BUTTON_WHEEL_DOWN:
increment_zoom(-zoom_step, lmp)
queue_redraw()
if (not is_out_border1) and (not is_out_border2):
increment_zoom(-zoom_step, lmp)
queue_redraw()
elif e.button_index == MOUSE_BUTTON_WHEEL_UP:
increment_zoom( zoom_step, lmp)
queue_redraw()
if e.button_index == MOUSE_BUTTON_LEFT:
_dragging = e.pressed
_drag_pos = screen_to_world(_xyz, lmp.x, lmp.y)
@@ -214,7 +215,17 @@ func _input(e) -> void:
var wp = screen_to_world(_xyz, lmp.x, lmp.y)
var diff = _drag_pos - wp
_xyz.x += diff.x
_xyz.y += diff.y
if (not is_out_border1) and diff.y >= 0:
_xyz.y += diff.y
if is_out_border1 and diff.y <= 0:
_xyz.y += diff.y
if is_out_border2 and diff.y >= 0:
_xyz.y += diff.y
if (not is_out_border2) and diff.y <= 0:
_xyz.y += diff.y
queue_redraw()
if diff.length() > 0:
signaller.emmit(signaller.map_user_panning)
@@ -253,11 +264,12 @@ func set_zoom(zoom: float, pivot: Vector2) -> void:
func _draw() -> void:
var z = min(max_zoom_level, _xyz.z)
var t1 = screen_to_tile(0, 0, z)
var t2 = screen_to_tile($canvas.size.x, $canvas.size.y, z)
for tx in range(t1.x, t2.x + 1):
for ty in range(t1.y, t2.y + 1):
var t2 = screen_to_tile($canvas.size.x, $canvas.size.y, z) + Vector2(1, 1)
is_out_border1 = true
is_out_border2 = true
for tx in range(t1.x, t2.x):
for ty in range(t1.y, t2.y):
_draw_tile(tx, ty, z)
var n = pow(2, z)
var dx = n * tile_width
var c = $canvas.size / 2.0
@@ -295,7 +307,6 @@ func get_next_in_queue() -> Tile:
return tile
# delete oldest tiles from cache
func _clean_cache() -> void:
var overflow = _cache.size() - max_cached_tiles
if overflow <= 0:
@@ -365,11 +376,9 @@ func _response(result, code, _headers: Array, body: PackedByteArray, req, tile:
push_error('не удалось получить изображение (неверный формат?) из \"%s\"' % tile.url)
return
# create texture from image and add it to the cache
tile.texture = ImageTexture.create_from_image(image)
_cache[tile.i] = tile
# redraw the map
queue_redraw()
@@ -395,11 +404,19 @@ func _draw_subtile(tx: int, ty: int, tz: int, origx: int, origy: int, origz: flo
var rect = Rect2(xrat1 * tile_width, yrat1 * tile_height, xwidth * tile_width, yheight * tile_height)
if subtile.texture:
check_borders(p1, p2, $canvas.size.y)
draw_texture_rect_region(subtile.texture, Rect2(p1, p2 - p1), rect)
return true
return false
func check_borders(p1, p2, h):
if p1.y < 0:
is_out_border1 = false
if p1.y + (p2 - p1).y > h:
is_out_border2 = false
func _draw_tile(tx: int, ty: int, z: float) -> void:
var tz = floor(z)
var p1 = tile_to_screen(tx, ty, z)
@@ -407,6 +424,7 @@ func _draw_tile(tx: int, ty: int, z: float) -> void:
var tile = get_tile(tx, ty, tz)
if tile:
if tile.texture:
check_borders(p1, p2, $canvas.size.y)
draw_texture_rect(tile.texture, Rect2(p1, p2 - p1), false, Color.WHITE, false)
else:
var zzz = tz
@@ -416,7 +434,7 @@ func _draw_tile(tx: int, ty: int, z: float) -> void:
zzz -= 1
txx = floor(txx / 2.0)
tyy = floor(tyy / 2.0)
if _draw_subtile(txx, tyy, zzz, tx, ty,z):
if _draw_subtile(txx, tyy, zzz, tx, ty, z):
break

View File

@@ -84,6 +84,8 @@ metadata/_edit_lock_ = true
[node name="tilemap" parent="." instance=ExtResource("10_gtwyg")]
self_modulate = Color(1, 1, 1, 0.415686)
position = Vector2(360, 360)
min_zoom_level = 3
max_zoom_level_server = 11
[node name="player" type="AnimationPlayer" parent="tilemap"]
libraries = {