2020-08-17 22:54:33 +03:00
|
|
|
extends Node2D
|
|
|
|
|
|
|
|
|
|
|
|
func _draw() -> void:
|
2021-01-18 21:59:26 +01:00
|
|
|
if not Global.draw_grid:
|
|
|
|
return
|
2020-08-17 22:54:33 +03:00
|
|
|
|
2021-11-25 14:48:30 +02:00
|
|
|
var target_rect: Rect2
|
2021-01-20 01:17:33 +01:00
|
|
|
if Global.grid_draw_over_tile_mode:
|
2022-06-20 05:07:20 -04:00
|
|
|
target_rect = Global.current_project.tiles.get_bounding_rect()
|
2021-01-20 01:17:33 +01:00
|
|
|
else:
|
|
|
|
target_rect = Rect2(Vector2.ZERO, Global.current_project.size)
|
|
|
|
if target_rect.has_no_area():
|
2021-01-18 21:59:26 +01:00
|
|
|
return
|
2020-08-17 22:54:33 +03:00
|
|
|
|
2021-11-25 14:48:30 +02:00
|
|
|
var grid_type: int = Global.grid_type
|
2021-01-20 15:59:42 +01:00
|
|
|
if grid_type == Global.GridTypes.CARTESIAN || grid_type == Global.GridTypes.ALL:
|
2021-01-20 01:17:33 +01:00
|
|
|
_draw_cartesian_grid(target_rect)
|
2020-08-17 22:54:33 +03:00
|
|
|
|
2021-01-20 15:59:42 +01:00
|
|
|
if grid_type == Global.GridTypes.ISOMETRIC || grid_type == Global.GridTypes.ALL:
|
2021-01-20 01:17:33 +01:00
|
|
|
_draw_isometric_grid(target_rect)
|
|
|
|
|
|
|
|
|
2021-11-25 14:48:30 +02:00
|
|
|
func _draw_cartesian_grid(target_rect: Rect2) -> void:
|
2021-01-20 01:17:33 +01:00
|
|
|
# Using Array instead of PoolVector2Array to avoid kinda
|
|
|
|
# random "resize: Can't resize PoolVector if locked" errors.
|
|
|
|
# See: https://github.com/Orama-Interactive/Pixelorama/issues/331
|
|
|
|
# It will be converted to PoolVector2Array before being sent to be rendered.
|
|
|
|
var grid_multiline_points := []
|
|
|
|
|
2021-11-25 14:48:30 +02:00
|
|
|
var x: float = (
|
|
|
|
target_rect.position.x
|
2023-04-18 16:38:55 +03:00
|
|
|
+ fposmod(Global.grid_offset.x - target_rect.position.x, Global.grid_size.x)
|
2021-11-25 14:48:30 +02:00
|
|
|
)
|
2021-01-20 01:17:33 +01:00
|
|
|
while x <= target_rect.end.x:
|
|
|
|
grid_multiline_points.push_back(Vector2(x, target_rect.position.y))
|
|
|
|
grid_multiline_points.push_back(Vector2(x, target_rect.end.y))
|
2023-04-18 16:38:55 +03:00
|
|
|
x += Global.grid_size.x
|
2021-01-20 01:17:33 +01:00
|
|
|
|
2021-11-25 14:48:30 +02:00
|
|
|
var y: float = (
|
|
|
|
target_rect.position.y
|
2023-04-18 16:38:55 +03:00
|
|
|
+ fposmod(Global.grid_offset.y - target_rect.position.y, Global.grid_size.y)
|
2021-11-25 14:48:30 +02:00
|
|
|
)
|
2021-01-20 01:17:33 +01:00
|
|
|
while y <= target_rect.end.y:
|
|
|
|
grid_multiline_points.push_back(Vector2(target_rect.position.x, y))
|
|
|
|
grid_multiline_points.push_back(Vector2(target_rect.end.x, y))
|
2023-04-18 16:38:55 +03:00
|
|
|
y += Global.grid_size.y
|
2021-01-20 01:17:33 +01:00
|
|
|
|
|
|
|
if not grid_multiline_points.empty():
|
|
|
|
draw_multiline(grid_multiline_points, Global.grid_color)
|
2021-01-18 21:59:26 +01:00
|
|
|
|
|
|
|
|
2021-11-25 14:48:30 +02:00
|
|
|
func _draw_isometric_grid(target_rect: Rect2) -> void:
|
2021-01-18 21:59:26 +01:00
|
|
|
# Using Array instead of PoolVector2Array to avoid kinda
|
|
|
|
# random "resize: Can't resize PoolVector if locked" errors.
|
|
|
|
# See: https://github.com/Orama-Interactive/Pixelorama/issues/331
|
|
|
|
# It will be converted to PoolVector2Array before being sent to be rendered.
|
|
|
|
var grid_multiline_points := []
|
|
|
|
|
2023-04-18 16:38:55 +03:00
|
|
|
var cell_size := Global.isometric_grid_size
|
2021-11-25 14:48:30 +02:00
|
|
|
var max_cell_count: Vector2 = target_rect.size / cell_size
|
2023-04-18 16:38:55 +03:00
|
|
|
var origin_offset: Vector2 = (Global.grid_offset - target_rect.position).posmodv(cell_size)
|
2021-01-18 21:59:26 +01:00
|
|
|
|
|
|
|
# lines ↗↗↗ (from bottom-left to top-right)
|
|
|
|
var per_cell_offset := cell_size * Vector2(1, -1)
|
|
|
|
|
|
|
|
# lines ↗↗↗ starting from the rect's left side (top to bottom):
|
2021-11-25 14:48:30 +02:00
|
|
|
var y: float = fposmod(
|
|
|
|
origin_offset.y + cell_size.y * (0.5 + origin_offset.x / cell_size.x), cell_size.y
|
|
|
|
)
|
2021-01-18 21:59:26 +01:00
|
|
|
while y < target_rect.size.y:
|
2021-11-25 14:48:30 +02:00
|
|
|
var start: Vector2 = target_rect.position + Vector2(0, y)
|
|
|
|
var cells_to_rect_bounds: float = min(max_cell_count.x, y / cell_size.y)
|
|
|
|
var end: Vector2 = start + cells_to_rect_bounds * per_cell_offset
|
2021-01-18 21:59:26 +01:00
|
|
|
grid_multiline_points.push_back(start)
|
|
|
|
grid_multiline_points.push_back(end)
|
|
|
|
y += cell_size.y
|
|
|
|
|
|
|
|
# lines ↗↗↗ starting from the rect's bottom side (left to right):
|
2021-11-25 14:48:30 +02:00
|
|
|
var x: float = (y - target_rect.size.y) / cell_size.y * cell_size.x
|
2021-01-18 21:59:26 +01:00
|
|
|
while x < target_rect.size.x:
|
2021-11-25 14:48:30 +02:00
|
|
|
var start: Vector2 = target_rect.position + Vector2(x, target_rect.size.y)
|
|
|
|
var cells_to_rect_bounds: float = min(max_cell_count.y, max_cell_count.x - x / cell_size.x)
|
|
|
|
var end: Vector2 = start + cells_to_rect_bounds * per_cell_offset
|
2021-01-18 21:59:26 +01:00
|
|
|
grid_multiline_points.push_back(start)
|
|
|
|
grid_multiline_points.push_back(end)
|
|
|
|
x += cell_size.x
|
|
|
|
|
|
|
|
# lines ↘↘↘ (from top-left to bottom-right)
|
|
|
|
per_cell_offset = cell_size
|
|
|
|
|
|
|
|
# lines ↘↘↘ starting from the rect's left side (top to bottom):
|
|
|
|
y = fposmod(origin_offset.y - cell_size.y * (0.5 + origin_offset.x / cell_size.x), cell_size.y)
|
|
|
|
while y < target_rect.size.y:
|
2021-11-25 14:48:30 +02:00
|
|
|
var start: Vector2 = target_rect.position + Vector2(0, y)
|
|
|
|
var cells_to_rect_bounds: float = min(max_cell_count.x, max_cell_count.y - y / cell_size.y)
|
|
|
|
var end: Vector2 = start + cells_to_rect_bounds * per_cell_offset
|
2021-01-18 21:59:26 +01:00
|
|
|
grid_multiline_points.push_back(start)
|
|
|
|
grid_multiline_points.push_back(end)
|
|
|
|
y += cell_size.y
|
|
|
|
|
|
|
|
# lines ↘↘↘ starting from the rect's top side (left to right):
|
|
|
|
x = fposmod(origin_offset.x - cell_size.x * (0.5 + origin_offset.y / cell_size.y), cell_size.x)
|
|
|
|
while x < target_rect.size.x:
|
2021-11-25 14:48:30 +02:00
|
|
|
var start: Vector2 = target_rect.position + Vector2(x, 0)
|
|
|
|
var cells_to_rect_bounds: float = min(max_cell_count.y, max_cell_count.x - x / cell_size.x)
|
|
|
|
var end: Vector2 = start + cells_to_rect_bounds * per_cell_offset
|
2021-01-18 21:59:26 +01:00
|
|
|
grid_multiline_points.push_back(start)
|
|
|
|
grid_multiline_points.push_back(end)
|
|
|
|
x += cell_size.x
|
|
|
|
|
|
|
|
if not grid_multiline_points.empty():
|
|
|
|
draw_multiline(grid_multiline_points, Global.grid_color)
|