Skip to content

Commit

Permalink
Merge pull request #29 from yohamta/layout-on-draw
Browse files Browse the repository at this point in the history
Update view logic to update the layout on draw
  • Loading branch information
yohamta authored Nov 26, 2022
2 parents 04dc837 + 08038b2 commit 6e559a8
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package furex

import (
"image"
"sync"

"github.com/hajimehoshi/ebiten/v2"
)
Expand Down Expand Up @@ -31,17 +32,13 @@ type View struct {
containerEmbed
flexEmbed
hasParent bool
lock sync.Mutex
}

// Update updates the view
func (v *View) Update() {
if v.isDirty {
if !v.hasParent {
v.frame = image.Rect(v.Left, v.Top, v.Left+v.Width, v.Top+v.Height)
}
v.flexEmbed.View = v
v.layout(v.frame.Dx(), v.frame.Dy(), &v.containerEmbed)
v.isDirty = false
v.startLayout()
}
for _, v := range v.children {
v.item.Update()
Expand All @@ -55,6 +52,17 @@ func (v *View) Update() {
}
}

func (v *View) startLayout() {
v.lock.Lock()
defer v.lock.Unlock()
if !v.hasParent {
v.frame = image.Rect(v.Left, v.Top, v.Left+v.Width, v.Top+v.Height)
}
v.flexEmbed.View = v
v.layout(v.frame.Dx(), v.frame.Dy(), &v.containerEmbed)
v.isDirty = false
}

// UpdateWithSize the view with modified height and width
func (v *View) UpdateWithSize(width, height int) {
if !v.hasParent && (v.Width != width || v.Height != height) {
Expand All @@ -67,8 +75,10 @@ func (v *View) UpdateWithSize(width, height int) {

// Draw draws the view
func (v *View) Draw(screen *ebiten.Image) {
if v.isDirty {
v.startLayout()
}
v.containerEmbed.Draw(screen)

if Debug && !v.hasParent {
debugBorders(screen, v.containerEmbed)
}
Expand Down

0 comments on commit 6e559a8

Please sign in to comment.