Even better

This commit is contained in:
Ehmry - 2023-09-12 10:03:06 +02:00
parent 0471f7702a
commit 7d910a0ce5
1 changed files with 42 additions and 23 deletions

View File

@ -28,7 +28,14 @@ proc newWell*(width, height: int): Well =
proc dimensions(well): Vec2 = vec2(float well.width, float well.height)
proc margins*(well): tuple[w: int, h: int] = (well.width div 9, well.height div 9)
proc margins(well): tuple[w: int, h: int] = (well.width div 9, well.height div 9)
proc textArea(well): tuple[w: int, h: int] = (
well.width - ((well.width div 9) shl 1),
well.height - ((well.height div 9) shl 1),
)
proc vec2(t: (int, int)): Vec2 = vec2(float t[0], float t[1])
proc rect*(well): Rect =
result.wh = well.dimensions
@ -37,21 +44,33 @@ proc rectAt(well; i: int): Rect =
if i < well.panes.len:
let
(quo, rem) = divmod(i, 3)
shiftOff = quo.succ
scaleDiv = 1 shl quo.succ
shiftOff = succ quo
fullWh = well.dimensions
result.w = float(well.width shr shiftOff)
result.h = float(well.height shr shiftOff)
textArea = well.textArea
paneArea = vec2(float(well.width shr shiftOff), float(well.height shr shiftOff))
result.w = float(textArea.w shr shiftOff)
result.h = float(textArea.h shr shiftOff)
case rem
of 0:
result.xy = fullWh - (result.wh * 2.0)
result.xy = fullWh - (paneArea * 2.0)
of 1:
result.x = fullWh.x - result.w
result.y = fullWh.y - (result.h * 2.0)
result.x = fullWh.x - paneArea.x
result.y = fullWh.y - (paneArea.y * 2.0)
of 2:
result.x = fullWh.x - (result.w * 2.0)
result.y = fullWh.y - result.h
result.x = fullWh.x - (paneArea.x * 2.0)
result.y = fullWh.y - paneArea.y
else: discard
result.xy = result.xy + (result.wh / 9.0)
proc quadAt(well; i: int): Rect =
if i < well.panes.len:
let
(quo, rem) = divmod(i, 3)
shiftOff = succ quo
fullWh = well.dimensions
assert rem == 0
result.wh = vec2(float(well.width shr shiftOff), float(well.height shr shiftOff))
result.xy = fullWh - result.wh
proc place(well; offset, width, height: int): (Pane, Rect) =
## Return the `Pane` at `offset` from the top of `well` or `nil` if
@ -68,9 +87,10 @@ proc append*(well; text: string; font: Font) =
let pane = well.panes.peekLast()
pane.spans.add(span)
var
arrangement = typeset(pane.spans, well.dimensions)
textArea = well.textArea.vec2
arrangement = typeset(pane.spans, textArea)
bounds = layoutBounds arrangement
if bounds.y <= well.dimensions.y:
if bounds.y <= textArea.y:
doAssert pane.spans.len > 0, "text does not find on a single pane - " & $bounds & $well.dimensions
pane.arrangement = arrangement
break
@ -99,9 +119,8 @@ proc texture*(well; index: int; renderer: RendererPtr): TexturePtr =
if pane.texture.isNil and not pane.arrangement.isNil:
assert pane.image.isNil
let
w = (well.width div 9) * 7
h = (well.height div 9) * 7
pane.image = newImage(w, h)
textArea = well.textArea
pane.image = newImage(textArea.w, textArea.h)
pane.image.fill(rgba(255, 255, 255, 255))
pane.image.fillText(pane.arrangement)
var surface = createRGBSurfaceFrom(
@ -124,17 +143,17 @@ iterator intersectingPanes*(well; view: Rect): Intersection =
zoom = 1.0
while intersect.index < well.panes.len:
var paneRect = well.rectAt(intersect.index)
if (intersect.index mod 3) == 0:
let quad = rect(paneRect.xy, paneRect.wh * 2.0)
if not view.overlaps quad:
# all further panes are non-intersecting
break
let stepDown = (intersect.index mod 3) == 0
if stepDown:
zoom = zoom * 2.0
let margin = paneRect.wh / 9.0
paneRect.xy = paneRect.xy + margin
paneRect.wh = paneRect.wh - (margin * 2.0)
if view.overlaps paneRect:
intersect.dst = view and paneRect
intersect.src = rect(intersect.dst.xy - paneRect.xy, intersect.dst.wh) * zoom
yield(intersect)
if stepDown:
let quad = well.quadAt(intersect.index)
if not view.overlaps quad:
echo "view ", view, " does not overlap quadrant ", quad
# all further panes are non-intersecting
break
inc(intersect.index)