hasen_judy

misc controls

Aug 29th, 2025
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.11 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.  
  6.     app "go.hasen.dev/slay/giobackend"
  7.  
  8.     . "go.hasen.dev/slay"
  9.     . "go.hasen.dev/slay/tw"
  10. )
  11.  
  12. func main() {
  13.     app.SetupWindow("Misc Controls Demo", 800, 600)
  14.     app.Run(frameFn)
  15. }
  16.  
  17. var clsPage = TW(Gap(10), Pad(10), BG(0, 0, 90, 1), MinSize(1200, 800))
  18.  
  19. var active = true
  20.  
  21. var from float32 = 0.4
  22. var to float32 = 1.2
  23. var rmin float32 = 0
  24. var rmax float32 = 2.5
  25.  
  26. func frameFn() {
  27.     ModAttrs(Pad(10), Gap(10))
  28.  
  29.     Label(fmt.Sprintf("Active: %v", active))
  30.     ToggleSwitch(&active)
  31.  
  32.     Nil()
  33.  
  34.     Label(fmt.Sprintf("Range:  from: %f   to: %f", from, to))
  35.     RangePicker(&from, &to, rmin, rmax)
  36.  
  37.     TooltipDemo("Hello", "This is just a greeting")
  38.     TooltipDemo("World", "It means δΈ–η•Œ!!")
  39.  
  40.     TooltipHost()
  41. }
  42.  
  43. func ToggleSwitch(on *bool) {
  44.     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() {
  45.         if IsHovered() && FrameInput.Mouse == MouseClick {
  46.             *on = !*on
  47.         }
  48.  
  49.         var clr = Vec4{240, 100, 50, 1}
  50.  
  51.         if !*on {
  52.             clr[1] = 0
  53.             Nil()
  54.         } else {
  55.             Element(TW(Grow(1)))
  56.         }
  57.         // the inner button (round)
  58.         Element(TW(BR(8), MinSize(16, 16), BGV(clr)))
  59.     })
  60. }
  61.  
  62. func RangePicker(from *float32, to *float32, range_min float32, range_max float32) {
  63.     var width float32 = 300
  64.     if *to < *from {
  65.         *to, *from = *from, *to
  66.     }
  67.  
  68.     var r float32 = 10 // radius of circle
  69.  
  70.     xOffset := func(v float32) float32 {
  71.         return width * (v - range_min) / (range_max - range_min)
  72.     }
  73.  
  74.     Layout(TW(Row, CA(AlignMiddle), MinWidth(width+r*2), MinHeight(r*2)), func() {
  75.         drawCircle := func(v *float32) {
  76.             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() {
  77.                 ActiveDuringPress()
  78.                 if IsActive() {
  79.                     diff := FrameInput.Motion[0] // mouse movement along x-axis
  80.                     // translate the movement to the range given!
  81.                     *v += (diff / width) * (range_max - range_min)
  82.                     *v = max(range_min, min(range_max, *v))
  83.                 }
  84.                 xoffset := xOffset(*v)
  85.                 ModAttrs(Float(xoffset, 0))
  86.             })
  87.         }
  88.  
  89.         // background line
  90.         sz := GetResolvedSize()
  91.         Element(TW(Float(0, sz[1]/2), MinSize(sz[0], 2), BG(0, 0, 0, 1)))
  92.  
  93.         // selected line
  94.         fromOffset := xOffset(*from)
  95.         toOffset := xOffset(*to)
  96.         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)))
  97.  
  98.         drawCircle(to)
  99.         drawCircle(from)
  100.  
  101.     })
  102. }
  103.  
  104. func TooltipDemo(label string, tip string) {
  105.     Layout(TW(), func() {
  106.         Label(label)
  107.         if IsHovered() && FrameInput.Mouse == MouseClick {
  108.             OpenTooltip(tip)
  109.         }
  110.     })
  111. }
  112.  
  113. // tooltip state. Assuming only one tooltip at a time
  114. var tipMsg string
  115. var tipPos Vec2
  116. var tipOn bool
  117. var tipJustOpened bool
  118.  
  119. func OpenTooltip(msg string) {
  120.     tipOn = true
  121.     tipPos = InputState.MousePoint
  122.     tipMsg = msg
  123.     tipJustOpened = true
  124. }
  125.  
  126. func TooltipHost() {
  127.     if tipOn {
  128.         Layout(TW(FloatV(tipPos), Pad(4), BG(0, 0, 10, 1), Bo(0, 0, 100, 0.9), BW(1)), func() {
  129.             Label(tipMsg, Clr(0, 0, 100, 1), Sz(14))
  130.             if FrameInput.Mouse == MouseClick && !IsHovered() && !tipJustOpened {
  131.                 tipOn = false
  132.             }
  133.         })
  134.         tipJustOpened = false
  135.     }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment