Added an example of how to use the flow

This commit is contained in:
Sasha Koshka 2023-01-12 15:33:50 -05:00
parent 0f9153e496
commit 9710e57b2b
2 changed files with 112 additions and 0 deletions

104
examples/flow/main.go Normal file
View File

@ -0,0 +1,104 @@
package main
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/flow"
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("adventure")
container := basic.NewContainer(layouts.Vertical { true, true })
window.Adopt(container)
var world flow.Flow
world.Transition = container.DisownAll
world.Stages = map [string] func () {
"start": func () {
label := basic.NewLabel (
"you are standing next to a river.", false)
container.Adopt(label, true)
button0 := basic.NewButton("go in the river")
button0.OnClick(world.SwitchFunc("wet"))
container.Adopt(button0, false)
button0.Select()
button1 := basic.NewButton("walk along the river")
button1.OnClick(world.SwitchFunc("house"))
container.Adopt(button1, false)
button2 := basic.NewButton("turn around")
button2.OnClick(world.SwitchFunc("bear"))
container.Adopt(button2, false)
},
"wet": func () {
label := basic.NewLabel (
"you get completely soaked.\n" +
"you die of hypothermia.", false)
container.Adopt(label, true)
button0 := basic.NewButton("try again")
button0.OnClick(world.SwitchFunc("start"))
container.Adopt(button0, false)
button0.Select()
button1 := basic.NewButton("exit")
button1.OnClick(tomo.Stop)
container.Adopt(button1, false)
},
"house": func () {
label := basic.NewLabel (
"you are standing in front of a delapidated " +
"house.", false)
container.Adopt(label, true)
button1 := basic.NewButton("go inside")
button1.OnClick(world.SwitchFunc("inside"))
container.Adopt(button1, false)
button1.Select()
button0 := basic.NewButton("turn back")
button0.OnClick(world.SwitchFunc("start"))
container.Adopt(button0, false)
},
"inside": func () {
label := basic.NewLabel (
"you are standing inside of the house.\n" +
"it is dark, but rays of light stream " +
"through the window.\n" +
"there is nothing particularly interesting " +
"here.", false)
container.Adopt(label, true)
button0 := basic.NewButton("go back outside")
button0.OnClick(world.SwitchFunc("house"))
container.Adopt(button0, false)
button0.Select()
},
"bear": func () {
label := basic.NewLabel (
"you come face to face with a bear.\n" +
"it eats you (it was hungry).", false)
container.Adopt(label, true)
button0 := basic.NewButton("try again")
button0.OnClick(world.SwitchFunc("start"))
container.Adopt(button0, false)
button0.Select()
button1 := basic.NewButton("exit")
button1.OnClick(tomo.Stop)
container.Adopt(button1, false)
},
}
world.Switch("start")
window.OnClose(tomo.Stop)
window.Show()
}

View File

@ -25,6 +25,14 @@ func (flow Flow) Switch (stage string) {
stageCallback()
}
// SwitchFunc returns a function that calles Switch with the specfied stage
// name. This is useful for creating callbacks.
func (flow Flow) SwitchFunc (stage string) (callback func ()) {
return func () {
flow.Switch(stage)
}
}
// Stage returns the name of the current stage the flow is on.
func (flow Flow) Stage () (name string) {
return flow.stage