#draw_multiline(...) inside a control is offset but can't tell why

13 messages · Page 1 of 1 (latest)

sage marsh
#

I have some code that draws a line inside a control node position. When viewed as a node on its own. it draws fine. However inside an example VBoxContainer, it got some spacing, and i've no clue why.

I use global_position to grab the upper corner, and I printed it it says its the position (0,30) that would make it about where I expect it, but its a whole "cell" or so below where I'd expect the first line to be drawn.

I have to give the VBoxContainer theme_override_constants/separation something around -12 to get the offset to look right.

I've looked at all the things, but I'm not sure why it would be offset like this

#

relevant gdscript

@tool
extends Container
class_name InventoryGrid

@export var tileSize: Vector2 = Vector2(12, 12)
  
func _notification(what):
    if what == NOTIFICATION_DRAW:
        _draw_grid()

func _draw_grid():
    var startPos: Vector2 = global_position
    print(startPos)
    var gridSize: Vector2 = (size / tileSize).floor()
    print(gridSize)
    var endPos: Vector2 = startPos + gridSize * tileSize
    print(endPos)

    var lines: Array[Vector2] = []
    
    # Rows / Horizontal lines
    for row in range(gridSize.y + 1):
        var y: int = startPos.y + row * tileSize.y
        lines.append(Vector2(startPos.x, y))
        lines.append(Vector2(endPos.x, y))

    # Cols / Vertical lines
    for col in range(gridSize.x + 1):
        var x: int = startPos.x + col * tileSize.x
        lines.append(Vector2(x, startPos.y))
        lines.append(Vector2(x, endPos.y))

    draw_multiline(lines, Color(1,1,1))
raw pulsar
#

Doesn't the draw_multiline use local coordinates? It would seem that you'd want the startPos to be just 0,0, rather than the global position.

sage marsh
#

I don't see anything in the docs that say that for draw_line or draw_multiline

#

You are right, it appears to sort the problem. Oddly position is also (0,30)

#

startPos = position

#

startPos = Vector2(0,0)

atomic coral
#

All drawing functions use local coordinates, that way you can move the node around without having to redraw it all the time

sage marsh
#

Great, but where does the documentation say this? In editor only shows draw_line() under CanvasItem and it doesn't talk about using relative coordinates

sharp badge
#

You should use func _draw and when changing attributes call queue redraw (name?)

I see a grid so why not use gridcontainer? I did yesterday 10x10 drawing my items.

sharp badge
#

Something like

@export_flags("Top", "Right", "Bottom", "Left") var _borders = 0:
  set(v):
    _borders = v
    queue_redraw()

...

func _draw():
  var delta := Vector2.ONE * 15
  draw_rect(Rect2(Vector2.ZERO+ delta, size - 2 * delta), color, true)
sage marsh