I need to draw from 5x5 to 9x9 grids by draw_line method. I need to make sure that my grid will fit perfectly to below red rectangle of viewport middle part with equal square cell sizes.
My question is, is there any easier way to draw grid without calculating cell size by getting viewport rect.width? Is there any node that provides me this feature like scaling children nodes? Or instead of draw_line method, is it better to use textures? If i use textures, then how will i perfectly put “stuff” in cells? Which method should i follow?
Thanks.
Not:My draw grid code:
# Draw vertical lines
for i:int in grid_size + 1:
var from:Vector2 = Vector2(i * Constants.GRID_CELL_SIZE.x, 0)
var to:Vector2 = Vector2(from.x, grid_size * Constants.GRID_CELL_SIZE.y)
draw_line(from, to, Color.GRAY)
# Draw horizontal lines
for i:int in grid_size + 1:
var from:Vector2 = Vector2(0, Constants.GRID_CELL_SIZE.y * i)
var to:Vector2 = Vector2(Constants.GRID_CELL_SIZE.x * grid_size, from.y)
draw_line(from, to, Color.GRAY)
use a StyleBoxTexture with the cells sizes you want and set it to tile, that’s the easiest way to draw a grid.
I gotta be honest I don’t exactly understand what you are trying to do and what the problem is. is this working? if yes, then just leave it. if no, what is the problem? you need to repeat this for other things?
If i use textures, then how do i put “safe, screen size independent circles” in grid cells? Also how do i draw 5x5 or 9x9(cell size must be smaller in this case) grid with same styleboxtexture (Maybe with scale)? Lets assume my grid cell size is 32 px. I wanna put circles or other shapes just in the cells. lets assums i wanna put it in 3rd row and 2nd column, so what i will do is “(32 x(3-1)) + margin” for x, “(32 x(2-1)) + margin” for y to put circle, is this correct method?
For better grasping of the situation, think “flow free” game in ios/play store
(below grid can be up to 9x9)
if this is what you want, StyleBoxes would help for drawing the grid only, this would have a fixed size in pixels, or you can create a finished version of the grid and scale it for different resolutions, at that point just use a texturerect.
as for those lines, you can’t draw them with Styleboxes, styleboxes are boxes, they are square.
I don’t think you should be using Controls for that, in the case of the lines, you have to use scenes or draw it manually.
and yes, you can’t skip the math, it has to happen at some point.
size of controls on screen is managed through project settings. one option keeps 1:1 pixels while the other scales to fit on the screen. you can also scale the 1:1.
if the shapes fit within the grid cells, you CAN use controls.
you have the gridcontainer which can fit nodes and you can set a style for it using a theme and possibly add lines there.
then, every node added will fit and scale with the grid.
you can swap an image with one that is transparent, or just hide it, and they should keep their size.
I will be drawing lines between circles with “touch and drag input” so i cant use controls probably.
As you said, i will use prepared/finished grids for 5x5 and 9x9, and put markers in cell centers (maybe two is enough and calculate the rest) for putting stuff. When they are scaled, markers will be proportionally updated as well so i can put circle center in the marker position and start drag drawing (draw_line) there. Controls dont allow me to draw_line so i think it is out of the question.
You can set the size of the tiles in the layer, and you could actually bake the line drawing animation into the tiles if you wanted; it looks like your lines are all going to be orthogonal, so there aren’t that many tiles in a full animated set.