Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- app "go.hasen.dev/slay/giobackend"
- . "go.hasen.dev/slay"
- . "go.hasen.dev/slay/tw"
- )
- func main() {
- app.SetupWindow("Misc Controls Demo", 800, 600)
- app.Run(frameFn)
- }
- var clsPage = TW(Gap(10), Pad(10), BG(0, 0, 90, 1), MinSize(1200, 800))
- var active = true
- var from float32 = 0.4
- var to float32 = 1.2
- var rmin float32 = 0
- var rmax float32 = 2.5
- func frameFn() {
- ModAttrs(Pad(10), Gap(10))
- Label(fmt.Sprintf("Active: %v", active))
- ToggleSwitch(&active)
- Nil()
- Label(fmt.Sprintf("Range: from: %f to: %f", from, to))
- RangePicker(&from, &to, rmin, rmax)
- TooltipDemo("Hello", "This is just a greeting")
- TooltipDemo("World", "It means δΈη!!")
- TooltipHost()
- }
- func ToggleSwitch(on *bool) {
- Layout(TW(Row, BG(0, 0, 80, 1), Pad(4), CA(AlignMiddle), BR(12), MinSize(40, 20), BW(1), Bo(0, 0, 20, 0.7)), func() {
- if IsHovered() && FrameInput.Mouse == MouseClick {
- *on = !*on
- }
- var clr = Vec4{240, 100, 50, 1}
- if !*on {
- clr[1] = 0
- Nil()
- } else {
- Element(TW(Grow(1)))
- }
- // the inner button (round)
- Element(TW(BR(8), MinSize(16, 16), BGV(clr)))
- })
- }
- func RangePicker(from *float32, to *float32, range_min float32, range_max float32) {
- var width float32 = 300
- if *to < *from {
- *to, *from = *from, *to
- }
- var r float32 = 10 // radius of circle
- xOffset := func(v float32) float32 {
- return width * (v - range_min) / (range_max - range_min)
- }
- Layout(TW(Row, CA(AlignMiddle), MinWidth(width+r*2), MinHeight(r*2)), func() {
- drawCircle := func(v *float32) {
- Layout(TW(BR(r), MinSize(r*2, r*2), BG1(0, 0, 99, 1), BG2(0, 0, 95, 1), BW(1), Bo(0, 0, 0, 0.5)), func() {
- ActiveDuringPress()
- if IsActive() {
- diff := FrameInput.Motion[0] // mouse movement along x-axis
- // translate the movement to the range given!
- *v += (diff / width) * (range_max - range_min)
- *v = max(range_min, min(range_max, *v))
- }
- xoffset := xOffset(*v)
- ModAttrs(Float(xoffset, 0))
- })
- }
- // background line
- sz := GetResolvedSize()
- Element(TW(Float(0, sz[1]/2), MinSize(sz[0], 2), BG(0, 0, 0, 1)))
- // selected line
- fromOffset := xOffset(*from)
- toOffset := xOffset(*to)
- Element(TW(Float(fromOffset+r, sz[1]/2-3), MinSize(toOffset-fromOffset, 6), BG(0, 0, 100, 1), BW(1), Bo(0, 0, 0, 0.5)))
- drawCircle(to)
- drawCircle(from)
- })
- }
- func TooltipDemo(label string, tip string) {
- Layout(TW(), func() {
- Label(label)
- if IsHovered() && FrameInput.Mouse == MouseClick {
- OpenTooltip(tip)
- }
- })
- }
- // tooltip state. Assuming only one tooltip at a time
- var tipMsg string
- var tipPos Vec2
- var tipOn bool
- var tipJustOpened bool
- func OpenTooltip(msg string) {
- tipOn = true
- tipPos = InputState.MousePoint
- tipMsg = msg
- tipJustOpened = true
- }
- func TooltipHost() {
- if tipOn {
- Layout(TW(FloatV(tipPos), Pad(4), BG(0, 0, 10, 1), Bo(0, 0, 100, 0.9), BW(1)), func() {
- Label(tipMsg, Clr(0, 0, 100, 1), Sz(14))
- if FrameInput.Mouse == MouseClick && !IsHovered() && !tipJustOpened {
- tipOn = false
- }
- })
- tipJustOpened = false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment