Add zooming in map

This commit is contained in:
Hamcha 2020-07-24 13:49:22 +02:00
parent 4997130d1f
commit 922ab0ea7f
Signed by: hamcha
GPG key ID: 41467804B19A3315
2 changed files with 44 additions and 8 deletions

View file

@ -1,6 +1,6 @@
extends Control extends Control
export var cell_size = 100 export var cell_size = 150
export var bgzoom = 50 export var bgzoom = 50
const BORDER_WIDTH = 4 const BORDER_WIDTH = 4
@ -8,6 +8,10 @@ const BORDER_LENGTH = 30
const RADAR_EFFECT_DELAY = 1 const RADAR_EFFECT_DELAY = 1
const HALF_BORDER = BORDER_LENGTH/2 const HALF_BORDER = BORDER_LENGTH/2
const MIN_ZOOM = 30
const ZOOM_STEP = 10
const MAX_ZOOM = 400
export(Texture) var background export(Texture) var background
export(Font) var font export(Font) var font
@ -17,6 +21,7 @@ var last_origin = origin
var last_mouse_pos = Vector2.ZERO var last_mouse_pos = Vector2.ZERO
var set_position = true var set_position = true
var radar_next_remaining = RADAR_EFFECT_DELAY var radar_next_remaining = RADAR_EFFECT_DELAY
var offset = Vector2.ZERO
onready var scene = $"/root/scene" onready var scene = $"/root/scene"
onready var speed_text = $"../HBoxContainer/HBoxContainer/CurrentSpeed" onready var speed_text = $"../HBoxContainer/HBoxContainer/CurrentSpeed"
@ -27,7 +32,7 @@ var last_pos = []
func _physics_process(delta): func _physics_process(delta):
radar_next_remaining -= delta radar_next_remaining -= delta
if radar_next_remaining < 0: if radar_next_remaining < 0:
var current_position = (scene.world.map.current_ship_position + scene.world.map.current_ship_subpos) * 100 var current_position = (scene.world.map.current_ship_position + scene.world.map.current_ship_subpos)
last_pos.append(current_position) last_pos.append(current_position)
if last_pos.size() > 20: if last_pos.size() > 20:
last_pos.pop_front() last_pos.pop_front()
@ -40,9 +45,9 @@ func _physics_process(delta):
func _draw(): func _draw():
var win_size = get_global_rect().size var win_size = get_global_rect().size
var current_position = (scene.world.map.current_ship_position + scene.world.map.current_ship_subpos) * 100 var current_position = (scene.world.map.current_ship_position + scene.world.map.current_ship_subpos) * cell_size
if set_position: if set_position:
origin = current_position - win_size/2.0 origin = current_position - win_size/2
last_origin = origin last_origin = origin
if win_size.x > 0: if win_size.x > 0:
set_position = false set_position = false
@ -53,7 +58,7 @@ func _draw():
draw_texture_rect_region(background, Rect2(Vector2.ZERO, win_size), Rect2(origin*bgzoom, win_size*bgzoom), Color(0.5,0.5,0.5,1)) draw_texture_rect_region(background, Rect2(Vector2.ZERO, win_size), Rect2(origin*bgzoom, win_size*bgzoom), Color(0.5,0.5,0.5,1))
for i in range(0, cols+1): for i in range(0, cols+1):
draw_line(Vector2(i * cell_size-xoffset, 0), Vector2(i * cell_size-xoffset, win_size.y), Color.white * 0.5) draw_line(Vector2(i * cell_size-xoffset, 0), Vector2(i * cell_size-xoffset, win_size.y), Color.white * 0.5)
for i in range(0, rows): for i in range(0, rows+1):
draw_line(Vector2(0, i * cell_size-yoffset), Vector2(win_size.x, i * cell_size-yoffset), Color.white * 0.5) draw_line(Vector2(0, i * cell_size-yoffset), Vector2(win_size.x, i * cell_size-yoffset), Color.white * 0.5)
for x in range(-1, cols+1): for x in range(-1, cols+1):
for y in range(-1, rows+1): for y in range(-1, rows+1):
@ -66,7 +71,7 @@ func _draw():
var point_count = last_pos.size() var point_count = last_pos.size()
for pos_index in range(0, point_count): for pos_index in range(0, point_count):
draw_circle(last_pos[pos_index] - origin, 2, Color(1, 0, 0, pos_index*1.0/point_count)) draw_circle(last_pos[pos_index] * cell_size - origin, 2, Color(1, 0, 0, pos_index*1.0/point_count))
draw_target(viewport, current_position, Color.red) draw_target(viewport, current_position, Color.red)
# Debug directions: # Debug directions:
@ -75,7 +80,7 @@ func _draw():
var current_target = scene.world.map.current_ship_target var current_target = scene.world.map.current_ship_target
if current_target != null: if current_target != null:
var current_target_adj = current_target * 100 var current_target_adj = current_target * cell_size
draw_target(viewport, current_target_adj, Color.green) draw_target(viewport, current_target_adj, Color.green)
draw_line(current_position - origin, current_target_adj - origin, Color.darkcyan) draw_line(current_position - origin, current_target_adj - origin, Color.darkcyan)
@ -97,15 +102,36 @@ func draw_target(viewport: Rect2, position: Vector2, color: Color):
draw_line(Vector2(clamped.x-HALF_BORDER, viewport.size.y), Vector2(clamped.x+HALF_BORDER, viewport.size.y), color, BORDER_WIDTH) draw_line(Vector2(clamped.x-HALF_BORDER, viewport.size.y), Vector2(clamped.x+HALF_BORDER, viewport.size.y), color, BORDER_WIDTH)
func _input(event): func _input(event):
if not visible:
return
if event.is_action_pressed("ui_zoomin"):
print(cell_size)
if cell_size < MAX_ZOOM:
_recalc_offset(ZOOM_STEP)
cell_size += ZOOM_STEP
update()
return
if event.is_action_pressed("ui_zoomout"):
if cell_size > MIN_ZOOM:
_recalc_offset(-ZOOM_STEP)
cell_size -= ZOOM_STEP
update()
return
if event is InputEventMouseButton: if event is InputEventMouseButton:
dragging = event.pressed dragging = event.pressed
last_origin = origin last_origin = origin
last_mouse_pos = event.position last_mouse_pos = event.position
update() update()
elif event is InputEventMouseMotion: return
if event is InputEventMouseMotion:
if dragging: if dragging:
origin = last_origin - (event.position - last_mouse_pos) origin = last_origin - (event.position - last_mouse_pos)
update() update()
return
func _recalc_offset(mult: float):
var mouse = get_local_mouse_position()
origin = ((origin + mouse) / cell_size * (cell_size + mult)) - mouse
func _reset_position(): func _reset_position():
set_position = true set_position = true

View file

@ -231,6 +231,16 @@ inspect={
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777237,"unicode":0,"echo":false,"script":null) "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777237,"unicode":0,"echo":false,"script":null)
] ]
} }
ui_zoomin={
"deadzone": 0.5,
"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":4,"pressed":false,"doubleclick":false,"script":null)
]
}
ui_zoomout={
"deadzone": 0.5,
"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":5,"pressed":false,"doubleclick":false,"script":null)
]
}
[layer_names] [layer_names]