Console::Draw

console 0.2.0 · app/console/draw.rb · 373 lines · view source

Console::Draw -- the rendering helper layer.

Thin, predictable sugar over args.outputs. Every method returns the rect it drew (or the measured text rect), so calls can be chained or fed into hit-testing without recomputing geometry.

Every method accepts either absolute pixel coordinates (x:, y:) or a fractional place: spec relative to the current bounds rect. Bounds are set with within, which restores itself afterwards.

draw.within(panel_rect) do
  draw.text 'READY', place: { x: 0.0, y: 1.0, anchor_y: 1.0 }
  draw.panel place: { x: 0.5, y: 0.5, w: 200, h: 40,
                      anchor_x: 0.5, anchor_y: 0.5 }
end
def initialize(args)
line 21
def outputs
line 27

bounds management

def bounds
line 33
def default_bounds
line 37
def within(r)

Evaluate the block with r as the current bounds. Restores the previous bounds even if the block raises, so a cart bug cannot corrupt later draws.

def resolve(spec)

Resolve a position spec into a concrete rect.

Two coordinate systems, chosen by the presence of place::

draw.rect x: 10, y: 20, w: 30, h: 40
  -> absolute screen pixels (the common case)
draw.rect place: { x: 0.5, y: 0.5, w: 30, h: 40,
                   anchor_x: 0.5, anchor_y: 0.5 }
  -> fractions of the current bounds rect

Keeping these distinct is deliberate: an absolute x of 10 must never be reinterpreted as "10 times the bounds width".

def transform(r)

Map world coordinates to screen when a camera is active. Without a camera this is a no-op, so screen-space and world-space drawing share one code path.

NOTE: camera zoom scales rect geometry but not font size. Labels keep their pixel size so HUD text stays legible at any zoom.

primitives

def rect(spec = {})

A filled rectangle.

DragonRuby 7.21 deprecates outputs.solids in favour of a sprite whose path is :solid, so that is what we emit -- same pixels, one collection, and solids share the sprite pipeline's texture caching.

def border(spec = {}, thickness = 1)

An unfilled rectangle, drawn thickness times inset.

def panel(spec = {})

A panel: filled background plus a 1px border.

def sprite(spec = {})

Draw a sprite. If w/h are omitted the texture's natural size is used, which is what you want for pixel-art.

def sprite_frame(spec = {})

Draw a crop of a sprite sheet by absolute source coordinates.

def sprite_sheet_frame(spec = {})

Draw frame index of a horizontally laid out sheet.

def text(str, spec = {})

Text. Returns the visual rect the text occupies, positioned in screen space, so callers can lay out or hit-test without re-measuring.

def line(x1, y1, x2, y2, color = :white, thickness = 1)

A line of arbitrary thickness (offset copies along the normal).

composites

def bar(r, value, max, spec = {})

A horizontal progress bar occupying r. Returns the fill width.

def grid(spec = {})

Draw a lattice of cells, one per grid slot. Returns the cell rects so callers can reuse them for selection or hit-testing.

draw.grid x: 0, y: 0, w: 200, h: 100, cols: 8, rows: 4
draw.grid place: { x: 0.5, y: 0.5, w: 200, h: 100 }, cols: 8, rows: 4

measurement

def measure(str, size_enum = nil, size_px = nil, font = nil)
line 312
def text_rect(str, spec = {})
line 324
def sprite_size(path)

Natural pixel size of a texture.

def absolute(r)
line 336
def strip_color(spec)
line 340
def natural_size(path, w, h)
line 346
def apply_sprite_extras(prim, spec)
line 366