direct-draw #6

Merged
sashakoshka merged 10 commits from direct-draw into main 2023-02-01 04:05:26 +00:00
8 changed files with 25 additions and 36 deletions
Showing only changes of commit b0ff1ca0af - Show all commits

View File

@ -63,6 +63,7 @@ func (canvas BasicCanvas) Buffer () (data []color.RGBA, stride int) {
// Cut returns a sub-canvas of a given canvas. // Cut returns a sub-canvas of a given canvas.
func Cut (canvas Canvas, bounds image.Rectangle) (reduced BasicCanvas) { func Cut (canvas Canvas, bounds image.Rectangle) (reduced BasicCanvas) {
// println(canvas.Bounds().String(), bounds.String())
bounds = bounds.Intersect(canvas.Bounds()) bounds = bounds.Intersect(canvas.Bounds())
if bounds.Empty() { return } if bounds.Empty() { return }
reduced.rect = bounds reduced.rect = bounds

View File

@ -137,7 +137,7 @@ func (element *Button) draw () {
textBounds := element.drawer.LayoutBounds() textBounds := element.drawer.LayoutBounds()
offset := image.Point { offset := image.Point {
X: innerBounds.Min.X + (innerBounds.Dx() - textBounds.Dx()) / 2, X: innerBounds.Min.X + (innerBounds.Dx() - textBounds.Dx()) / 2,
Y: innerBounds.Min.X + (innerBounds.Dy() - textBounds.Dy()) / 2, Y: innerBounds.Min.Y + (innerBounds.Dy() - textBounds.Dy()) / 2,
} }
// account for the fact that the bounding rectangle will be shifted over // account for the fact that the bounding rectangle will be shifted over

View File

@ -207,7 +207,7 @@ func (element *Container) redoAll () {
}) })
artist.FillRectangle(element, pattern, bounds) artist.FillRectangle(element, pattern, bounds)
// resize all elements, having them draw onto us // cut our canvas up and give peices to child elements
for _, entry := range element.children { for _, entry := range element.children {
entry.DrawTo(tomo.Cut(element, entry.Bounds)) entry.DrawTo(tomo.Cut(element, entry.Bounds))
} }
@ -217,31 +217,27 @@ func (element *Container) HandleMouseDown (x, y int, button tomo.Button) {
child, handlesMouse := element.ChildAt(image.Pt(x, y)).(tomo.MouseTarget) child, handlesMouse := element.ChildAt(image.Pt(x, y)).(tomo.MouseTarget)
if !handlesMouse { return } if !handlesMouse { return }
element.drags[button] = child element.drags[button] = child
childPosition := element.childPosition(child) child.HandleMouseDown(x, y, button)
child.HandleMouseDown(x - childPosition.X, y - childPosition.Y, button)
} }
func (element *Container) HandleMouseUp (x, y int, button tomo.Button) { func (element *Container) HandleMouseUp (x, y int, button tomo.Button) {
child := element.drags[button] child := element.drags[button]
if child == nil { return } if child == nil { return }
element.drags[button] = nil element.drags[button] = nil
childPosition := element.childPosition(child) child.HandleMouseUp(x, y, button)
child.HandleMouseUp(x - childPosition.X, y - childPosition.Y, button)
} }
func (element *Container) HandleMouseMove (x, y int) { func (element *Container) HandleMouseMove (x, y int) {
for _, child := range element.drags { for _, child := range element.drags {
if child == nil { continue } if child == nil { continue }
childPosition := element.childPosition(child) child.HandleMouseMove(x, y)
child.HandleMouseMove(x - childPosition.X, y - childPosition.Y)
} }
} }
func (element *Container) HandleMouseScroll (x, y int, deltaX, deltaY float64) { func (element *Container) HandleMouseScroll (x, y int, deltaX, deltaY float64) {
child, handlesMouse := element.ChildAt(image.Pt(x, y)).(tomo.MouseTarget) child, handlesMouse := element.ChildAt(image.Pt(x, y)).(tomo.MouseTarget)
if !handlesMouse { return } if !handlesMouse { return }
childPosition := element.childPosition(child) child.HandleMouseScroll(x, y, deltaX, deltaY)
child.HandleMouseScroll(x - childPosition.X, y - childPosition.Y, deltaX, deltaY)
} }
func (element *Container) HandleKeyDown (key tomo.Key, modifiers tomo.Modifiers) { func (element *Container) HandleKeyDown (key tomo.Key, modifiers tomo.Modifiers) {
@ -475,6 +471,5 @@ func (element *Container) updateMinimumSize () {
} }
func (element *Container) recalculate () { func (element *Container) recalculate () {
bounds := element.Bounds() element.layout.Arrange(element.children, element.Bounds())
element.layout.Arrange(element.children, bounds.Dx(), bounds.Dy())
} }

View File

@ -1,6 +1,5 @@
package basic package basic
import "image"
import "git.tebibyte.media/sashakoshka/tomo/theme" import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/artist" import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/elements/core" import "git.tebibyte.media/sashakoshka/tomo/elements/core"
@ -119,8 +118,5 @@ func (element *Label) draw () {
foreground, _ := theme.ForegroundPattern (theme.PatternState { foreground, _ := theme.ForegroundPattern (theme.PatternState {
Case: labelCase, Case: labelCase,
}) })
element.drawer.Draw (element, foreground, image.Point { element.drawer.Draw (element, foreground, bounds.Min.Sub(textBounds.Min))
X: 0 - textBounds.Min.X,
Y: 0 - textBounds.Min.Y,
})
} }

View File

@ -17,7 +17,7 @@ type Layout interface {
// and changes the position of the entiries in the slice so that they // and changes the position of the entiries in the slice so that they
// are properly laid out. The given width and height should not be less // are properly laid out. The given width and height should not be less
// than what is returned by MinimumSize. // than what is returned by MinimumSize.
Arrange (entries []LayoutEntry, width, height int) Arrange (entries []LayoutEntry, bounds image.Rectangle)
// MinimumSize returns the minimum width and height that the layout // MinimumSize returns the minimum width and height that the layout
// needs to properly arrange the given slice of layout entries. // needs to properly arrange the given slice of layout entries.

View File

@ -96,7 +96,7 @@ func (layout Dialog) Arrange (entries []tomo.LayoutEntry, width, height int) {
entryBounds := entry.Bounds entryBounds := entry.Bounds
if entryBounds.Dy() != controlRowHeight || if entryBounds.Dy() != controlRowHeight ||
entryBounds.Dx() != entryWidth { entryBounds.Dx() != entryWidth {
entry.Bounds.Max = entryBounds.Min.Add ( entries[index].Bounds.Max = entryBounds.Min.Add (
image.Pt(entryWidth, controlRowHeight)) image.Pt(entryWidth, controlRowHeight))
} }
} }

View File

@ -45,7 +45,7 @@ func (layout Horizontal) Arrange (entries []tomo.LayoutEntry, width, height int)
x += entryWidth x += entryWidth
entryBounds := entry.Bounds entryBounds := entry.Bounds
if entryBounds.Dy() != height || entryBounds.Dx() != entryWidth { if entryBounds.Dy() != height || entryBounds.Dx() != entryWidth {
entry.Bounds.Max = entryBounds.Min.Add ( entries[index].Bounds.Max = entryBounds.Min.Add (
image.Pt(entryWidth, height)) image.Pt(entryWidth, height))
} }
} }

View File

@ -17,22 +17,21 @@ type Vertical struct {
} }
// Arrange arranges a list of entries vertically. // Arrange arranges a list of entries vertically.
func (layout Vertical) Arrange (entries []tomo.LayoutEntry, width, height int) { func (layout Vertical) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle) {
if layout.Pad { if layout.Pad {
width -= theme.Margin() * 2 bounds = bounds.Inset(theme.Margin())
height -= theme.Margin() * 2
} }
freeSpace := height
expandingElements := 0
// count the number of expanding elements and the amount of free space // count the number of expanding elements and the amount of free space
// for them to collectively occupy, while gathering minimum heights. // for them to collectively occupy, while gathering minimum heights.
freeSpace := bounds.Dy()
minimumHeights := make([]int, len(entries)) minimumHeights := make([]int, len(entries))
expandingElements := 0
for index, entry := range entries { for index, entry := range entries {
var entryMinHeight int var entryMinHeight int
if child, flexible := entry.Element.(tomo.Flexible); flexible { if child, flexible := entry.Element.(tomo.Flexible); flexible {
entryMinHeight = child.FlexibleHeightFor(width) entryMinHeight = child.FlexibleHeightFor(bounds.Dx())
} else { } else {
_, entryMinHeight = entry.MinimumSize() _, entryMinHeight = entry.MinimumSize()
} }
@ -47,34 +46,32 @@ func (layout Vertical) Arrange (entries []tomo.LayoutEntry, width, height int) {
freeSpace -= theme.Margin() freeSpace -= theme.Margin()
} }
} }
expandingElementHeight := 0 expandingElementHeight := 0
if expandingElements > 0 { if expandingElements > 0 {
expandingElementHeight = freeSpace / expandingElements expandingElementHeight = freeSpace / expandingElements
} }
x, y := 0, 0 dot := bounds.Min
if layout.Pad {
x += theme.Margin()
y += theme.Margin()
}
// set the size and position of each element // set the size and position of each element
for index, entry := range entries { for index, entry := range entries {
if index > 0 && layout.Gap { y += theme.Margin() } if index > 0 && layout.Gap { dot.Y += theme.Margin() }
entries[index].Bounds.Min = image.Pt(x, y) entry.Bounds.Min = dot
entryHeight := 0 entryHeight := 0
if entry.Expand { if entry.Expand {
entryHeight = expandingElementHeight entryHeight = expandingElementHeight
} else { } else {
entryHeight = minimumHeights[index] entryHeight = minimumHeights[index]
} }
y += entryHeight dot.Y += entryHeight
entryBounds := entry.Bounds entryBounds := entry.Bounds
if entryBounds.Dx() != width || entryBounds.Dy() != entryHeight { if entryBounds.Dx() != bounds.Dx() || entryBounds.Dy() != entryHeight {
entry.Bounds.Max = entryBounds.Min.Add ( entry.Bounds.Max = entryBounds.Min.Add (
image.Pt(width, entryHeight)) image.Pt(bounds.Dx(), entryHeight))
} }
entries[index] = entry
} }
} }