Skip to content

Instantly share code, notes, and snippets.

@cbilski
Created July 28, 2021 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cbilski/e3bc5fdf404eeb85f90530e014fef767 to your computer and use it in GitHub Desktop.
Save cbilski/e3bc5fdf404eeb85f90530e014fef767 to your computer and use it in GitHub Desktop.
A lazy way to wrap strings in dragonruby.
# This breaks when the characters no longer fit on the line. A smarter version would iterate using word/line boundaries
# Sample Usage:
# rect = args.layout.rect(row: 3, col: 12, w: 6, h: 7, dx: 0, dy: 0) # OBJECTIVE
# text = "OBJECTIVE:\n\nMECHs are threatening\nour CITIZENS:\n+ Bring the CITIZENS home.\n+ Destroy the MECHs."
# render_text(args, args.outputs.primitives, rect, text, 1, "/fonts/PixeloidSans.ttf")
#
def render_text(args, pipeline, rect, text, font_size, font)
work = ""
len = text.length
font_h = args.gtk.calcstringbox(text, font_size, font).y
y_offset = 0
x_offset = 8
i = 0
while i < len
x_len = args.gtk.calcstringbox(work + text[i], font_size, font).x
if (x_len >= rect.w || text[i] == "\n")
pipeline << {
x: rect.x + x_offset, y: (rect.y + rect.h) - y_offset,
text: work, size_enum: font_size,
alignment_enum: 0, vertical_alignment_enum: 2,
font: font,
}.label
work = text[i]
y_offset += font_h
else
work += text[i]
end
i += 1
end
if (work.length > 0)
pipeline << {
x: rect.x + x_offset, y: (rect.y + rect.h) - y_offset,
text: work, size_enum: font_size,
alignment_enum: 0, vertical_alignment_enum: 2,
font: font,
}.label
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment