Layouts work!!!!
This commit is contained in:
parent
6eed70e79e
commit
cb2136f7cb
@ -1,5 +1,6 @@
|
|||||||
package basic
|
package basic
|
||||||
|
|
||||||
|
import "image"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo"
|
import "git.tebibyte.media/sashakoshka/tomo"
|
||||||
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"
|
||||||
@ -12,6 +13,8 @@ type Container struct {
|
|||||||
layout tomo.Layout
|
layout tomo.Layout
|
||||||
children []tomo.LayoutEntry
|
children []tomo.LayoutEntry
|
||||||
selectable bool
|
selectable bool
|
||||||
|
|
||||||
|
drags [10]tomo.Element
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContainer (layout tomo.Layout) (element *Container) {
|
func NewContainer (layout tomo.Layout) (element *Container) {
|
||||||
@ -24,23 +27,36 @@ func NewContainer (layout tomo.Layout) (element *Container) {
|
|||||||
func (element *Container) SetLayout (layout tomo.Layout) {
|
func (element *Container) SetLayout (layout tomo.Layout) {
|
||||||
element.layout = layout
|
element.layout = layout
|
||||||
element.recalculate()
|
element.recalculate()
|
||||||
|
if element.core.HasImage() {
|
||||||
|
element.draw()
|
||||||
|
element.core.PushAll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (element *Container) Adopt (child tomo.Element, expand bool) {
|
func (element *Container) Adopt (child tomo.Element, expand bool) {
|
||||||
child.SetParentHooks (tomo.ParentHooks {
|
child.SetParentHooks (tomo.ParentHooks {
|
||||||
MinimumSizeChange:
|
MinimumSizeChange: func (int, int) {
|
||||||
func (int, int) { element.updateMinimumSize() },
|
element.updateMinimumSize()
|
||||||
SelectabilityChange:
|
},
|
||||||
func (bool) { element.updateSelectable() },
|
SelectabilityChange: func (bool) {
|
||||||
|
element.updateSelectable()
|
||||||
|
},
|
||||||
|
Draw: func (region tomo.Image) {
|
||||||
|
element.drawChildRegion(child, region)
|
||||||
|
},
|
||||||
})
|
})
|
||||||
element.children = append (element.children, tomo.LayoutEntry {
|
element.children = append (element.children, tomo.LayoutEntry {
|
||||||
Element: child,
|
Element: child,
|
||||||
|
Expand: expand,
|
||||||
})
|
})
|
||||||
|
|
||||||
element.updateMinimumSize()
|
element.updateMinimumSize()
|
||||||
element.updateSelectable()
|
element.updateSelectable()
|
||||||
element.recalculate()
|
element.recalculate()
|
||||||
if element.core.HasImage() { element.draw() }
|
if element.core.HasImage() {
|
||||||
|
element.draw()
|
||||||
|
element.core.PushAll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disown removes the given child from the container if it is contained within
|
// Disown removes the given child from the container if it is contained within
|
||||||
@ -59,7 +75,10 @@ func (element *Container) Disown (child tomo.Element) {
|
|||||||
element.updateMinimumSize()
|
element.updateMinimumSize()
|
||||||
element.updateSelectable()
|
element.updateSelectable()
|
||||||
element.recalculate()
|
element.recalculate()
|
||||||
if element.core.HasImage() { element.draw() }
|
if element.core.HasImage() {
|
||||||
|
element.draw()
|
||||||
|
element.core.PushAll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Children returns a slice containing this element's children.
|
// Children returns a slice containing this element's children.
|
||||||
@ -83,6 +102,28 @@ func (element *Container) Child (index int) (child tomo.Element) {
|
|||||||
return element.children[index].Element
|
return element.children[index].Element
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ChildAt returns the child that contains the specified x and y coordinates. If
|
||||||
|
// there are no children at the coordinates, this method will return nil.
|
||||||
|
func (element *Container) ChildAt (point image.Point) (child tomo.Element) {
|
||||||
|
for _, entry := range element.children {
|
||||||
|
if point.In(entry.Bounds().Add(entry.Position)) {
|
||||||
|
child = entry.Element
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (element *Container) childPosition (child tomo.Element) (position image.Point) {
|
||||||
|
for _, entry := range element.children {
|
||||||
|
if entry.Element == child {
|
||||||
|
position = entry.Position
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (element *Container) Handle (event tomo.Event) {
|
func (element *Container) Handle (event tomo.Event) {
|
||||||
switch event.(type) {
|
switch event.(type) {
|
||||||
case tomo.EventResize:
|
case tomo.EventResize:
|
||||||
@ -93,7 +134,42 @@ func (element *Container) Handle (event tomo.Event) {
|
|||||||
element.recalculate()
|
element.recalculate()
|
||||||
element.draw()
|
element.draw()
|
||||||
|
|
||||||
// TODO:
|
case tomo.EventMouseDown:
|
||||||
|
mouseDownEvent := event.(tomo.EventMouseDown)
|
||||||
|
child := element.ChildAt (image.Pt (
|
||||||
|
mouseDownEvent.X,
|
||||||
|
mouseDownEvent.Y))
|
||||||
|
if child == nil { break }
|
||||||
|
element.drags[mouseDownEvent.Button] = child
|
||||||
|
childPosition := element.childPosition(child)
|
||||||
|
child.Handle (tomo.EventMouseDown {
|
||||||
|
Button: mouseDownEvent.Button,
|
||||||
|
X: mouseDownEvent.X - childPosition.X,
|
||||||
|
Y: mouseDownEvent.Y - childPosition.Y,
|
||||||
|
})
|
||||||
|
|
||||||
|
case tomo.EventMouseUp:
|
||||||
|
mouseUpEvent := event.(tomo.EventMouseUp)
|
||||||
|
child := element.drags[mouseUpEvent.Button]
|
||||||
|
if child == nil { break }
|
||||||
|
element.drags[mouseUpEvent.Button] = nil
|
||||||
|
childPosition := element.childPosition(child)
|
||||||
|
child.Handle (tomo.EventMouseUp {
|
||||||
|
Button: mouseUpEvent.Button,
|
||||||
|
X: mouseUpEvent.X - childPosition.X,
|
||||||
|
Y: mouseUpEvent.Y - childPosition.Y,
|
||||||
|
})
|
||||||
|
|
||||||
|
case tomo.EventMouseMove:
|
||||||
|
mouseMoveEvent := event.(tomo.EventMouseMove)
|
||||||
|
for _, child := range element.drags {
|
||||||
|
if child == nil { continue }
|
||||||
|
childPosition := element.childPosition(child)
|
||||||
|
child.Handle (tomo.EventMouseMove {
|
||||||
|
X: mouseMoveEvent.X - childPosition.X,
|
||||||
|
Y: mouseMoveEvent.Y - childPosition.Y,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -129,8 +205,18 @@ func (element *Container) draw () {
|
|||||||
nil, 0,
|
nil, 0,
|
||||||
bounds)
|
bounds)
|
||||||
|
|
||||||
// TODO
|
|
||||||
for _, entry := range element.children {
|
for _, entry := range element.children {
|
||||||
artist.Paste(element.core, entry, entry.Position)
|
artist.Paste(element.core, entry, entry.Position)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (element *Container) drawChildRegion (child tomo.Element, region tomo.Image) {
|
||||||
|
for _, entry := range element.children {
|
||||||
|
if entry.Element == child {
|
||||||
|
artist.Paste(element.core, region, entry.Position)
|
||||||
|
element.core.PushRegion (
|
||||||
|
region.Bounds().Add(entry.Position))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -10,6 +10,9 @@ import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
|||||||
type Test struct {
|
type Test struct {
|
||||||
*core.Core
|
*core.Core
|
||||||
core core.CoreControl
|
core core.CoreControl
|
||||||
|
drawing bool
|
||||||
|
color tomo.Image
|
||||||
|
lastMousePos image.Point
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTest creates a new test element.
|
// NewTest creates a new test element.
|
||||||
@ -17,6 +20,7 @@ func NewTest () (element *Test) {
|
|||||||
element = &Test { }
|
element = &Test { }
|
||||||
element.Core, element.core = core.NewCore(element)
|
element.Core, element.core = core.NewCore(element)
|
||||||
element.core.SetMinimumSize(32, 32)
|
element.core.SetMinimumSize(32, 32)
|
||||||
|
element.color = artist.NewUniform(color.Black)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +47,33 @@ func (element *Test) Handle (event tomo.Event) {
|
|||||||
image.Pt(0, resizeEvent.Height),
|
image.Pt(0, resizeEvent.Height),
|
||||||
image.Pt(resizeEvent.Width, 0))
|
image.Pt(resizeEvent.Width, 0))
|
||||||
|
|
||||||
default:
|
case tomo.EventMouseDown:
|
||||||
|
element.drawing = true
|
||||||
|
mouseDownEvent := event.(tomo.EventMouseDown)
|
||||||
|
element.lastMousePos = image.Pt (
|
||||||
|
mouseDownEvent.X,
|
||||||
|
mouseDownEvent.Y)
|
||||||
|
|
||||||
|
case tomo.EventMouseUp:
|
||||||
|
element.drawing = false
|
||||||
|
mouseUpEvent := event.(tomo.EventMouseUp)
|
||||||
|
mousePos := image.Pt (
|
||||||
|
mouseUpEvent.X,
|
||||||
|
mouseUpEvent.Y)
|
||||||
|
element.core.PushRegion (artist.Line (
|
||||||
|
element.core, element.color, 1,
|
||||||
|
element.lastMousePos, mousePos))
|
||||||
|
element.lastMousePos = mousePos
|
||||||
|
|
||||||
|
case tomo.EventMouseMove:
|
||||||
|
mouseMoveEvent := event.(tomo.EventMouseMove)
|
||||||
|
mousePos := image.Pt (
|
||||||
|
mouseMoveEvent.X,
|
||||||
|
mouseMoveEvent.Y)
|
||||||
|
element.core.PushRegion (artist.Line (
|
||||||
|
element.core, element.color, 1,
|
||||||
|
element.lastMousePos, mousePos))
|
||||||
|
element.lastMousePos = mousePos
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,7 @@ func (layout Vertical) Arrange (entries []tomo.LayoutEntry, width, height int) {
|
|||||||
} else {
|
} else {
|
||||||
_, entryMinHeight := entry.MinimumSize()
|
_, entryMinHeight := entry.MinimumSize()
|
||||||
freeSpace -= entryMinHeight
|
freeSpace -= entryMinHeight
|
||||||
|
freeSpace -= theme.Padding()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expandingElementHeight := 0
|
expandingElementHeight := 0
|
||||||
|
@ -17,17 +17,21 @@ func run () {
|
|||||||
window.Adopt(container)
|
window.Adopt(container)
|
||||||
|
|
||||||
label := basic.NewLabel("it is a label hehe")
|
label := basic.NewLabel("it is a label hehe")
|
||||||
button := basic.NewButton("press me")
|
button := basic.NewButton("drawing pad")
|
||||||
|
okButton := basic.NewButton("OK")
|
||||||
button.OnClick (func () {
|
button.OnClick (func () {
|
||||||
label.SetText (
|
container.Disown(label)
|
||||||
"woah, this button changes the label text! since the " +
|
container.Disown(button)
|
||||||
"size of this text box has changed, the window " +
|
container.Disown(okButton)
|
||||||
"should expand (unless you resized it already).")
|
container.Adopt(basic.NewLabel("Draw here:"), false)
|
||||||
|
container.Adopt(basic.NewTest(), true)
|
||||||
|
container.Adopt(okButton, false)
|
||||||
})
|
})
|
||||||
|
okButton.OnClick(tomo.Stop)
|
||||||
|
|
||||||
container.Adopt(label, true)
|
container.Adopt(label, true)
|
||||||
container.Adopt(basic.NewButton("yeah"), false)
|
|
||||||
container.Adopt(button, false)
|
container.Adopt(button, false)
|
||||||
|
container.Adopt(okButton, false)
|
||||||
|
|
||||||
window.OnClose(tomo.Stop)
|
window.OnClose(tomo.Stop)
|
||||||
window.Show()
|
window.Show()
|
||||||
|
Reference in New Issue
Block a user