well_of_text/src/frames.nim

78 lines
2.0 KiB
Nim

# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[deques]
import pixie
type
Pane* = ref object
image: pixie.Image
spans: seq[Span]
arrangement: Arrangement
Well* = ref object
panes: Deque[Pane]
proc newFrame(width, height: int): Frame =
let (w, h) = (width div 2, height div 2)
new result
result.panes[0] = Pane(image: newImage(w, h))
result.panes[1] = Pane(image: newImage(w, h))
result.panes[2] = Pane(image: newImage(w, h))
proc paneVec2(frame: Frame): Vec2 =
vec2(frame.panes[0].image.width.float32, frame.panes[0].image.height.float32)
proc allocChild(frame: Frame) =
assert frame.child.isNil
let (w, h) = (frame.panes[0].image.width div 2, frame.panes[0].image.height div 2)
doAssert w > 2 and h > 2, $w & "x" & $h
frame.child = newFrame(w, h)
proc setDepth(frame: Frame; n: Natural) =
var
frame = frame
i = n
while i > 0:
dec(i)
if frame.child.isNil:
allocChild(frame)
frame = frame.child
proc `[]`*(frame: Frame; i: Natural): Pane =
var
frame = frame
i = i
while true:
if i < 4:
return frame.panes[i]
if frame.child.isNil:
allocChild(frame)
frame = frame.child
dec(i, 4)
iterator walkPanes(frame: Frame): tuple[coord: Vec2, pane: Pane] =
var frame = frame
while not frame.isNil and
frame.panes[0].image.width > 2 and
frame.panes[1].image.height > 2:
let coord = frame.paneVec2
yield(coord, frame.panes[0])
yield(coord - vec2(coord.x, 0), frame.panes[1])
yield(coord - vec2(0, coord.y), frame.panes[2])
frame = frame.child
proc append*(frame: Fram; text: string; font: Font) =
let span = newSpan(text, font)
proc append*(frame: Fram; stream: Stream; font: Font) =
var line: string
while readLine(stream, line):
append(frame, font, line)
when isMainModule:
let frame = newFrame(800, 600)
frame.setDepth(3)
for (coord, pane) in walkPanes(frame):
echo coord, " - ", pane.image.width, "x", pane.image.height