Added a list example
This commit is contained in:
parent
68f1d052d2
commit
5edfbf8110
@ -123,6 +123,8 @@ func abs (in int) (out int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: this method of doing things sucks and can cause a segfault. we should
|
||||||
|
// not be doing it this way
|
||||||
func squareAround (
|
func squareAround (
|
||||||
data []color.RGBA,
|
data []color.RGBA,
|
||||||
stride int,
|
stride int,
|
||||||
|
@ -359,6 +359,9 @@ func (element *List) selectUnderMouse (x, y int) (updated bool) {
|
|||||||
if element.onSelectedEntryChange != nil {
|
if element.onSelectedEntryChange != nil {
|
||||||
element.onSelectedEntryChange(element.selectedEntry)
|
element.onSelectedEntryChange(element.selectedEntry)
|
||||||
}
|
}
|
||||||
|
if element.selectedEntry >= 0 {
|
||||||
|
element.entries[element.selectedEntry].RunSelect()
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,13 +12,13 @@ type ListEntry struct {
|
|||||||
textPoint image.Point
|
textPoint image.Point
|
||||||
text string
|
text string
|
||||||
forcedMinimumWidth int
|
forcedMinimumWidth int
|
||||||
onClick func ()
|
onSelect func ()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewListEntry (text string, onClick func ()) (entry ListEntry) {
|
func NewListEntry (text string, onSelect func ()) (entry ListEntry) {
|
||||||
entry = ListEntry {
|
entry = ListEntry {
|
||||||
text: text,
|
text: text,
|
||||||
onClick: onClick,
|
onSelect: onSelect,
|
||||||
}
|
}
|
||||||
entry.drawer.SetText([]rune(text))
|
entry.drawer.SetText([]rune(text))
|
||||||
entry.drawer.SetFace(theme.FontFaceRegular())
|
entry.drawer.SetFace(theme.FontFaceRegular())
|
||||||
@ -62,6 +62,12 @@ func (entry *ListEntry) Draw (
|
|||||||
offset.Add(entry.textPoint))
|
offset.Add(entry.textPoint))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (entry *ListEntry) RunSelect () {
|
||||||
|
if entry.onSelect != nil {
|
||||||
|
entry.onSelect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (entry *ListEntry) Bounds () (bounds image.Rectangle) {
|
func (entry *ListEntry) Bounds () (bounds image.Rectangle) {
|
||||||
return entry.bounds
|
return entry.bounds
|
||||||
}
|
}
|
||||||
|
@ -28,22 +28,23 @@ func NewMouse () (element *Mouse) {
|
|||||||
|
|
||||||
func (element *Mouse) Resize (width, height int) {
|
func (element *Mouse) Resize (width, height int) {
|
||||||
element.core.AllocateCanvas(width, height)
|
element.core.AllocateCanvas(width, height)
|
||||||
|
bounds := element.Bounds()
|
||||||
artist.FillRectangle (
|
artist.FillRectangle (
|
||||||
element.core,
|
element.core,
|
||||||
theme.AccentPattern(),
|
theme.AccentPattern(),
|
||||||
element.Bounds())
|
bounds)
|
||||||
artist.StrokeRectangle (
|
artist.StrokeRectangle (
|
||||||
element.core,
|
element.core,
|
||||||
artist.NewUniform(color.Black), 1,
|
artist.NewUniform(color.Black), 1,
|
||||||
element.Bounds())
|
bounds)
|
||||||
artist.Line (
|
|
||||||
element.core, artist.NewUniform(color.White), 3,
|
|
||||||
image.Pt(1, 1),
|
|
||||||
image.Pt(width - 2, height - 2))
|
|
||||||
artist.Line (
|
artist.Line (
|
||||||
element.core, artist.NewUniform(color.White), 1,
|
element.core, artist.NewUniform(color.White), 1,
|
||||||
image.Pt(1, height - 2),
|
image.Pt(1, 1),
|
||||||
image.Pt(width - 2, 1))
|
image.Pt(bounds.Dx() - 2, bounds.Dy() - 2))
|
||||||
|
artist.Line (
|
||||||
|
element.core, artist.NewUniform(color.White), 1,
|
||||||
|
image.Pt(1, bounds.Dy() - 2),
|
||||||
|
image.Pt(bounds.Dx() - 2, 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (element *Mouse) HandleMouseDown (x, y int, button tomo.Button) {
|
func (element *Mouse) HandleMouseDown (x, y int, button tomo.Button) {
|
||||||
|
59
examples/list/main.go
Normal file
59
examples/list/main.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "git.tebibyte.media/sashakoshka/tomo"
|
||||||
|
import "git.tebibyte.media/sashakoshka/tomo/popups"
|
||||||
|
import "git.tebibyte.media/sashakoshka/tomo/layouts"
|
||||||
|
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
|
||||||
|
import "git.tebibyte.media/sashakoshka/tomo/elements/testing"
|
||||||
|
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
|
||||||
|
|
||||||
|
func main () {
|
||||||
|
tomo.Run(run)
|
||||||
|
}
|
||||||
|
|
||||||
|
func run () {
|
||||||
|
window, _ := tomo.NewWindow(300, 2)
|
||||||
|
window.SetTitle("List Sidebar")
|
||||||
|
|
||||||
|
container := basic.NewContainer(layouts.Horizontal { true, true })
|
||||||
|
window.Adopt(container)
|
||||||
|
|
||||||
|
var currentPage tomo.Element
|
||||||
|
turnPage := func (newPage tomo.Element) {
|
||||||
|
container.Warp (func () {
|
||||||
|
if currentPage != nil {
|
||||||
|
container.Disown(currentPage)
|
||||||
|
}
|
||||||
|
container.Adopt(newPage, true)
|
||||||
|
currentPage = newPage
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
button := basic.NewButton("I do nothing!")
|
||||||
|
button.OnClick (func () {
|
||||||
|
popups.NewDialog(popups.DialogKindInfo, "", "Sike!")
|
||||||
|
})
|
||||||
|
mouse := testing.NewMouse()
|
||||||
|
input := basic.NewTextBox("Write some text", "")
|
||||||
|
form := basic.NewContainer(layouts.Vertical { true, false})
|
||||||
|
form.Adopt(basic.NewLabel("I have:", false), false)
|
||||||
|
form.Adopt(basic.NewSpacer(true), false)
|
||||||
|
form.Adopt(basic.NewCheckbox("Skin", true), false)
|
||||||
|
form.Adopt(basic.NewCheckbox("Blood", false), false)
|
||||||
|
form.Adopt(basic.NewCheckbox("Bone", false), false)
|
||||||
|
|
||||||
|
list := basic.NewList (
|
||||||
|
basic.NewListEntry("button", func () { turnPage(button) }),
|
||||||
|
basic.NewListEntry("mouse", func () { turnPage(mouse) }),
|
||||||
|
basic.NewListEntry("input", func () { turnPage(input) }),
|
||||||
|
basic.NewListEntry("form", func () { turnPage(form) }))
|
||||||
|
list.Collapse(96, 0)
|
||||||
|
|
||||||
|
container.Adopt(list, false)
|
||||||
|
turnPage (basic.NewLabel (
|
||||||
|
"The List element can be easily used as a sidebar. " +
|
||||||
|
"Click on entries to flip pages!", true))
|
||||||
|
|
||||||
|
window.OnClose(tomo.Stop)
|
||||||
|
window.Show()
|
||||||
|
}
|
Reference in New Issue
Block a user