Skip to content

Commit

Permalink
Fix focus coordinates going into negative
Browse files Browse the repository at this point in the history
  • Loading branch information
malashin committed Nov 27, 2021
1 parent be28e57 commit c08188e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ func start() {
fillFocusChildAndParentData()
pBar.SetValue(pBar.Value + 0.1/i)

// Move coordinates of focuses so that negative values are no longer present.
moveAbsoluteFocusPositionsToPositiveValues()

// Create image.
x, y := maxFocusPos(focusMap)
w := (x+2)*gui.FocusSpacing.X + spacingX + 17
Expand Down
30 changes: 30 additions & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,32 @@ func fillAbsoluteFocusPositions(finished bool) bool {
return finished
}

func moveAbsoluteFocusPositionsToPositiveValues() {
lowestX := 0
lowestY := 0

for _, f := range focusMap {
if !f.AllowBranch {
continue
}
if f.X < lowestX {
lowestX = f.X
}
if f.Y < lowestY {
lowestY = f.Y
}
}

if lowestX < 0 || lowestY < 0 {
for _, f := range focusMap {
f.X -= lowestX
f.Y -= lowestY

focusMap[f.ID] = f
}
}
}

// buildFocusTree adds children data to each focus.
// Sorts children by X coordinate from left to right.
func fillFocusChildAndParentData() {
Expand Down Expand Up @@ -279,6 +305,10 @@ func containsPoint(s []image.Point, a image.Point) bool {
// maxFocusPos returns maximum x and y values in focus tree.
func maxFocusPos(m map[string]Focus) (x, y int) {
for _, f := range m {
if !f.AllowBranch {
continue
}

if f.X > x {
x = f.X
}
Expand Down

0 comments on commit c08188e

Please sign in to comment.