Last active
September 26, 2021 20:30
-
-
Save bitsycore/c2edc0bb0315f0bd05a44a749fa86f6f to your computer and use it in GitHub Desktop.
Simple Control grid for godot
This file contains hidden or 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 Control | |
| class_name SimpleGrid | |
| export (bool) var fixed_cell_size := true setget set_fixed_cell_size | |
| export (Vector2) var cell_size = Vector2(16,16) setget set_cell_size | |
| export (Vector2) var grid_size = Vector2(16,24) setget set_grid_size | |
| enum Mode { EDITOR, DEBUG, ALWAYS } | |
| export(Mode) var mode = Mode.EDITOR | |
| func set_fixed_cell_size(value): | |
| fixed_cell_size = value | |
| update() | |
| func set_grid_size(value): | |
| value.x = max(value.x, 1.0) | |
| value.y = max(value.y, 1.0) | |
| grid_size = value | |
| update() | |
| func set_cell_size(value): | |
| value.x = max(value.x, 0.001) | |
| value.y = max(value.y, 0.001) | |
| cell_size = value | |
| update() | |
| func _draw(): | |
| if (Engine.is_editor_hint() and mode == Mode.EDITOR) or \ | |
| (OS.is_debug_build() and mode == Mode.DEBUG) or \ | |
| (mode == Mode.ALWAYS): | |
| var gs_x := int(grid_size.x) | |
| var gs_y := int(grid_size.y) | |
| if gs_x == 0 or gs_y ==0: | |
| return | |
| var cell_x : float = cell_size.x if fixed_cell_size else rect_size.x / gs_x | |
| var cell_y : float = cell_size.y if fixed_cell_size else rect_size.y / gs_y | |
| for y in gs_y+1: | |
| draw_line (Vector2(0, y*cell_y), Vector2(gs_x*cell_x, y*cell_y), Color.red, 1.0, true) | |
| for x in gs_x+1: | |
| draw_line (Vector2(x*cell_x,0), Vector2(x*cell_x, cell_y*gs_y), Color.red, 1.0, true) | |
| if fixed_cell_size: | |
| rect_size = Vector2(cell_x*gs_x, cell_y*gs_y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment