Skip to content

Commit

Permalink
fixed some issues with UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Aug 26, 2023
1 parent 6a15c8d commit 90e8742
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
38 changes: 27 additions & 11 deletions src/ui/node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,27 @@ proc preLayout*(builder: UINodeBuilder, node: UINode) =
elif FillY in node.flags:
node.h = parent.h - node.y

# Add current invalidation rect for new node, copying parent if it exists
if builder.currentInvalidationRects.len > 0:
if builder.currentInvalidationRects.last.isSome:
node.logi "panel begin, copy invalidation rect from parent ", builder.currentInvalidationRects.last.get, ", ", vec2(node.mX, node.mY), " -> ", builder.currentInvalidationRects.last.get - vec2(node.mX, node.mY)
builder.currentInvalidationRects.add some(builder.currentInvalidationRects.last.get - vec2(node.mX, node.mY))
else:
builder.currentInvalidationRects.add Rect.none
else:
builder.currentInvalidationRects.add Rect.none

if builder.currentInvalidationRects.len > 0 and builder.currentInvalidationRects.last.isSome:
node.logi "preLayout, invalidate", builder.currentInvalidationRects.last.get, ", ", rect(0, 0, node.mW, node.mH), " -> ", builder.currentInvalidationRects.last.get.intersects(rect(0, 0, node.mW, node.mH))
if builder.currentInvalidationRects.last.get.intersects(rect(0, 0, node.mW, node.mH)):
node.mContentDirty = true

if node.mFlags.any &{DrawText, FillBackground}:
if builder.currentInvalidationRects.last.isSome:
builder.currentInvalidationRects.last = some(builder.currentInvalidationRects.last.get or rect(0, 0, node.mW, node.mH))
else:
builder.currentInvalidationRects.last = some rect(0, 0, node.mW, node.mH)

proc postLayoutChild*(builder: UINodeBuilder, node: UINode, child: UINode) =
if SizeToContentX in node.flags:
node.w = max(node.w, child.xw)
Expand Down Expand Up @@ -514,20 +535,15 @@ template panel*(builder: UINodeBuilder, inFlags: UINodeFlags, body: untyped): un
let delta {.inject.} = d
onDragBody


builder.currentParent = currentNode
builder.currentChild = nil
builder.forwardInvalidationRects.add Rect.none

# Add current invalidation rect for new node, copying parent if it exists
if builder.currentInvalidationRects.len > 0:
if builder.currentInvalidationRects.last.isSome:
currentNode.logi "panel begin, copy invalidation rect from parent ", builder.currentInvalidationRects.last.get, ", ", vec2(currentNode.mX, currentNode.mY), " -> ", builder.currentInvalidationRects.last.get - vec2(currentNode.mX, currentNode.mY)
builder.currentInvalidationRects.add some(builder.currentInvalidationRects.last.get - vec2(currentNode.mX, currentNode.mY))
else:
builder.currentInvalidationRects.add Rect.none
else:
builder.currentInvalidationRects.add Rect.none
# if currentNode.mFlags.any &{DrawText, FillBackground}:
# if builder.currentInvalidationRects.last.isSome:
# builder.currentInvalidationRects.last = some(builder.currentInvalidationRects.last.get or rect(0, 0, currentNode.mW, currentNode.mH))
# else:
# builder.currentInvalidationRects.last = some rect(0, 0, currentNode.mW, currentNode.mH)

# if OverlappingChildren in currentNode.mFlags:
# if builder.currentInvalidationRects.len > 0:
Expand Down Expand Up @@ -603,7 +619,7 @@ proc findNodeContaining*(node: UINode, pos: Vec2, predicate: proc(node: UINode):
proc dump*(node: UINode, recurse = false): string =
if node.isNil:
return "nil"
result.add fmt"Node({node.mLastContentChange}, {node.mLastPositionChange}, {node.mLastSizeChange}, {node.id} '{node.text}', {node.flags}, ({node.x}, {node.y}, {node.w}, {node.h}))"
result.add fmt"Node({node.mLastContentChange} ({node.mContentDirty}), {node.mLastPositionChange} ({node.mPositionDirty}), {node.mLastSizeChange} ({node.mSizeDirty}), {node.id} '{node.text}', {node.flags}, ({node.x}, {node.y}, {node.w}, {node.h}))"
if recurse and node.first.isNotNil:
result.add ":"
for c in node.children:
Expand Down
20 changes: 13 additions & 7 deletions test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ proc renderLine(builder: UINodeBuilder, line: string, curs: Option[int]) =
builder.panel(&{FillY, FillBackground}):
currentNode.w = builder.charWidth
currentNode.setBackgroundColor(0.5, 0.5, 1, 1)
onClick:
# echo "clicked cursor ", btn
cursor[1] = rand(0..line.len)
# onClick:
# # echo "clicked cursor ", btn
# cursor[1] = rand(0..line.len)

# Fill rest of line with background
builder.panel(&{FillX, FillY, FillBackground}):
Expand Down Expand Up @@ -363,17 +363,23 @@ iterator drawFrames() {.closure.} =
builder.panel(&{FillX, FillY, FillBackground}):
currentNode.setBackgroundColor(0, 0, 0)

builder.panel(&{FillBackground}): # draggable overlay 2
builder.panel(&{LayoutVertical, DrawBorder}): # draggable overlay 2
currentNode.x = popupPos2.x
currentNode.y = popupPos2.y
currentNode.w = 100
currentNode.h = 100
currentNode.setBackgroundColor(0, 1, 1)
currentNode.w = 150
currentNode.h = 150
currentNode.setBorderColor(0, 1, 1)

onDrag(MouseButton.Left, delta):
echo "drag2 ", delta
popupPos2 += delta

builder.renderText(testText2, 0, (0, 0))

# background filler
builder.panel(&{FillX, FillY, FillBackground}):
currentNode.setBackgroundColor(0, 0, 0)

builder.frameIndex = 0
ctx.fillStyle = rgb(0, 0, 0)
ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32)
Expand Down

0 comments on commit 90e8742

Please sign in to comment.