Created
November 28, 2021 02:26
-
-
Save boruok/faed5ff51d8d708de920e0cbf429e9cf to your computer and use it in GitHub Desktop.
script for drawing grids (cartesian, isometric)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tool | |
extends Node2D | |
enum modes { CARTESIAN, ISOMETRIC } | |
export(modes) var mode := modes.CARTESIAN setget set_mode | |
export var draw_origin := true setget set_draw_origin | |
export var grid_size := Vector2(160, 160) setget set_grid_size | |
export var cell_size := Vector2(16, 16) setget set_cell_size | |
export var color := Color.black setget set_color | |
export var width := 1.0 setget set_width | |
export var antialiased := false setget set_antialiased | |
func set_mode(value: int) -> void: | |
mode = value | |
update() | |
func set_draw_origin(value: bool) -> void: | |
draw_origin = value | |
update() | |
func set_grid_size(value: Vector2) -> void: | |
grid_size = value | |
update() | |
func set_cell_size(value: Vector2) -> void: | |
cell_size = value | |
update() | |
func set_color(value: Color) -> void: | |
color = value | |
update() | |
func set_width(value: float) -> void: | |
width = value | |
update() | |
func set_antialiased(value: bool) -> void: | |
antialiased = value | |
update() | |
func _draw(): | |
if mode == modes.ISOMETRIC: | |
draw_set_transform_matrix(Transform2D(Vector2(1, .5), Vector2(-1, .5), Vector2(0, 0))) | |
if draw_origin: | |
draw_line(Vector2.ZERO, Vector2(grid_size.x, 0), Color.red, width + 1) # X | |
draw_line(Vector2.ZERO, Vector2(0, grid_size.y), Color.green, width + 1) # Y | |
for x in grid_size.x / cell_size.x: | |
draw_line(Vector2(x * cell_size.x, 0), Vector2(x * cell_size.x, grid_size.y), color, width, antialiased) | |
for y in grid_size.y / cell_size.y: | |
draw_line(Vector2(0, y * cell_size.y), Vector2(grid_size.x, y * cell_size.y), color, width, antialiased) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment