diff --git a/elements/basic/test.go b/elements/testing/mouse.go similarity index 85% rename from elements/basic/test.go rename to elements/testing/mouse.go index ea38453..30a7c3b 100644 --- a/elements/basic/test.go +++ b/elements/testing/mouse.go @@ -1,4 +1,4 @@ -package basic +package testing import "image" import "image/color" @@ -7,8 +7,9 @@ import "git.tebibyte.media/sashakoshka/tomo/theme" import "git.tebibyte.media/sashakoshka/tomo/artist" import "git.tebibyte.media/sashakoshka/tomo/elements/core" -// Test is a simple element that can be used as a placeholder. -type Test struct { +// Mouse is an element capable of testing mouse input. When the mouse is clicked +// and dragged on it, it draws a trail. +type Mouse struct { *core.Core core core.CoreControl drawing bool @@ -16,16 +17,16 @@ type Test struct { lastMousePos image.Point } -// NewTest creates a new test element. -func NewTest () (element *Test) { - element = &Test { } +// NewMouse creates a new mouse test element. +func NewMouse () (element *Mouse) { + element = &Mouse { } element.Core, element.core = core.NewCore(element) element.core.SetMinimumSize(32, 32) element.color = artist.NewUniform(color.Black) return } -func (element *Test) Handle (event tomo.Event) { +func (element *Mouse) Handle (event tomo.Event) { switch event.(type) { case tomo.EventResize: resizeEvent := event.(tomo.EventResize) diff --git a/examples/horizontalLayout/main.go b/examples/horizontalLayout/main.go index 092ed5e..c1fed69 100644 --- a/examples/horizontalLayout/main.go +++ b/examples/horizontalLayout/main.go @@ -2,6 +2,7 @@ package main import "git.tebibyte.media/sashakoshka/tomo" import "git.tebibyte.media/sashakoshka/tomo/elements/basic" +import "git.tebibyte.media/sashakoshka/tomo/elements/testing" import "git.tebibyte.media/sashakoshka/tomo/elements/layouts" import _ "git.tebibyte.media/sashakoshka/tomo/backends/x" @@ -16,9 +17,9 @@ func run () { container := basic.NewContainer(layouts.Horizontal { true, true }) window.Adopt(container) - container.Adopt(basic.NewTest(), true) + container.Adopt(testing.NewMouse(), true) container.Adopt(basic.NewLabel("<- left\nright ->", false), false) - container.Adopt(basic.NewTest(), true) + container.Adopt(testing.NewMouse(), true) window.OnClose(tomo.Stop) window.Show() diff --git a/examples/popups/main.go b/examples/popups/main.go new file mode 100644 index 0000000..fdf726e --- /dev/null +++ b/examples/popups/main.go @@ -0,0 +1,68 @@ +package main + +import "git.tebibyte.media/sashakoshka/tomo" +import "git.tebibyte.media/sashakoshka/tomo/popups" +import "git.tebibyte.media/sashakoshka/tomo/elements/basic" +import "git.tebibyte.media/sashakoshka/tomo/elements/layouts" +import _ "git.tebibyte.media/sashakoshka/tomo/backends/x" + +func main () { + tomo.Run(run) +} + +func run () { + window, _ := tomo.NewWindow(2, 2) + window.SetTitle("Dialog Boxes") + + container := basic.NewContainer(layouts.Vertical { true, true }) + window.Adopt(container) + + container.Adopt(basic.NewLabel("Try out different dialogs:", false), true) + + infoButton := basic.NewButton("popups.DialogKindInfo") + infoButton.OnClick (func () { + popups.NewDialog ( + popups.DialogKindInfo, + "Information", + "You are wacky") + }) + container.Adopt(infoButton, false) + infoButton.Select() + + questionButton := basic.NewButton("popups.DialogKindQuestion") + questionButton.OnClick (func () { + popups.NewDialog ( + popups.DialogKindQuestion, + "The Big Question", + "Are you real?", + popups.Button { "Yes", func () { } }, + popups.Button { "No", func () { } }, + popups.Button { "Not sure", func () { } }) + }) + container.Adopt(questionButton, false) + + warningButton := basic.NewButton("popups.DialogKindWarning") + warningButton.OnClick (func () { + popups.NewDialog ( + popups.DialogKindQuestion, + "Warning", + "They are fast approaching.") + }) + container.Adopt(warningButton, false) + + errorButton := basic.NewButton("popups.DialogKindError") + errorButton.OnClick (func () { + popups.NewDialog ( + popups.DialogKindQuestion, + "Error", + "There is nowhere left to go.") + }) + container.Adopt(errorButton, false) + + cancelButton := basic.NewButton("No thank you.") + cancelButton.OnClick(tomo.Stop) + container.Adopt(cancelButton, false) + + window.OnClose(tomo.Stop) + window.Show() +} diff --git a/examples/test/main.go b/examples/test/main.go index 59efab1..1cd9692 100644 --- a/examples/test/main.go +++ b/examples/test/main.go @@ -1,7 +1,7 @@ package main import "git.tebibyte.media/sashakoshka/tomo" -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 () { @@ -11,7 +11,7 @@ func main () { func run () { window, _ := tomo.NewWindow(128, 128) window.SetTitle("hellorld!") - window.Adopt(basic.NewTest()) + window.Adopt(testing.NewMouse()) window.OnClose(tomo.Stop) window.Show() } diff --git a/examples/verticalLayout/main.go b/examples/verticalLayout/main.go index 5e1b2a3..bf01e80 100644 --- a/examples/verticalLayout/main.go +++ b/examples/verticalLayout/main.go @@ -2,6 +2,7 @@ package main import "git.tebibyte.media/sashakoshka/tomo" import "git.tebibyte.media/sashakoshka/tomo/elements/basic" +import "git.tebibyte.media/sashakoshka/tomo/elements/testing" import "git.tebibyte.media/sashakoshka/tomo/elements/layouts" import _ "git.tebibyte.media/sashakoshka/tomo/backends/x" @@ -22,7 +23,7 @@ func run () { button.OnClick (func () { container.DisownAll() container.Adopt(basic.NewLabel("Draw here:", false), false) - container.Adopt(basic.NewTest(), true) + container.Adopt(testing.NewMouse(), true) container.Adopt(okButton, false) okButton.Select() }) diff --git a/popups/dialog.go b/popups/dialog.go new file mode 100644 index 0000000..b40dc39 --- /dev/null +++ b/popups/dialog.go @@ -0,0 +1,49 @@ +package popups + +import "git.tebibyte.media/sashakoshka/tomo" +import "git.tebibyte.media/sashakoshka/tomo/elements/basic" +import "git.tebibyte.media/sashakoshka/tomo/elements/layouts" + +type DialogKind int + +const ( + DialogKindInfo DialogKind = iota + DialogKindQuestion + DialogKindWarning + DialogKindError +) + +type Button struct { + Name string + OnPress func () +} + +func NewDialog (kind DialogKind, title, message string, buttons ...Button) { + window, _ := tomo.NewWindow(2, 2) + window.SetTitle(title) + + container := basic.NewContainer(layouts.Dialog { true, true }) + window.Adopt(container) + + container.Adopt(basic.NewLabel(message, false), true) + if len(buttons) == 0 { + button := basic.NewButton("OK") + button.OnClick(window.Close) + container.Adopt(button, false) + button.Select() + } else { + var button *basic.Button + for _, buttonDescriptor := range buttons { + button = basic.NewButton(buttonDescriptor.Name) + button.SetEnabled(buttonDescriptor.OnPress != nil) + button.OnClick (func () { + buttonDescriptor.OnPress() + window.Close() + }) + container.Adopt(button, false) + } + button.Select() + } + + window.Show() +}