User interface toolkit for Go with support for SDL2 and HTML Canvas render targets.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

130 lines
2.4 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "git.kirsle.net/go/render"
  6. "git.kirsle.net/go/render/event"
  7. "git.kirsle.net/go/render/sdl"
  8. "git.kirsle.net/go/ui"
  9. )
  10. // Program globals.
  11. var (
  12. // Size of the MainWindow.
  13. Width = 1024
  14. Height = 768
  15. // Cascade offset for creating multiple windows.
  16. Cascade = render.NewPoint(10, 32)
  17. CascadeStep = render.NewPoint(24, 24)
  18. CascadeLoops = 1
  19. // Colors for each window created.
  20. WindowColors = []render.Color{
  21. render.Blue,
  22. render.Red,
  23. render.DarkYellow,
  24. render.DarkGreen,
  25. render.DarkCyan,
  26. render.DarkBlue,
  27. render.DarkRed,
  28. }
  29. WindowID int
  30. OpenWindows int
  31. )
  32. func init() {
  33. sdl.DefaultFontFilename = "../DejaVuSans.ttf"
  34. }
  35. func main() {
  36. mw, err := ui.NewMainWindow("Hello World", Width, Height)
  37. if err != nil {
  38. panic(err)
  39. }
  40. // Menu bar.
  41. menu := ui.NewMenuBar("Main Menu")
  42. file := menu.AddMenu("Options")
  43. file.AddItem("New window", func() {
  44. addWindow(mw)
  45. })
  46. file.AddItem("Close all windows", func() {
  47. OpenWindows -= mw.Supervisor().CloseAllWindows()
  48. })
  49. menu.Supervise(mw.Supervisor())
  50. menu.Compute(mw.Engine)
  51. mw.Pack(menu, menu.PackTop())
  52. // Add some windows to play with.
  53. addWindow(mw)
  54. addWindow(mw)
  55. mw.SetBackground(render.White)
  56. mw.OnLoop(func(e *event.State) {
  57. if e.Escape {
  58. os.Exit(0)
  59. }
  60. })
  61. mw.MainLoop()
  62. }
  63. // Add a new child window.
  64. func addWindow(mw *ui.MainWindow) {
  65. var (
  66. color = WindowColors[WindowID%len(WindowColors)]
  67. title = fmt.Sprintf("Window %d", WindowID+1)
  68. )
  69. WindowID++
  70. win1 := ui.NewWindow(title)
  71. win1.SetButtons(ui.CloseButton)
  72. win1.ActiveTitleBackground = color
  73. win1.InactiveTitleBackground = color.Darken(60)
  74. win1.InactiveTitleForeground = render.Grey
  75. win1.Configure(ui.Config{
  76. Width: 320,
  77. Height: 240,
  78. })
  79. win1.Compute(mw.Engine)
  80. win1.Supervise(mw.Supervisor())
  81. // Re-open a window when the last one is closed.
  82. OpenWindows++
  83. win1.Handle(ui.CloseWindow, func(ed ui.EventData) error {
  84. OpenWindows--
  85. if OpenWindows <= 0 {
  86. addWindow(mw)
  87. }
  88. return nil
  89. })
  90. // Default placement via cascade.
  91. win1.MoveTo(Cascade)
  92. Cascade.Add(CascadeStep)
  93. if Cascade.Y > Height-240-64 {
  94. CascadeLoops++
  95. Cascade.Y = 32
  96. Cascade.X = 24 * CascadeLoops
  97. }
  98. // Add a window duplicator button.
  99. btn2 := ui.NewButton(title+":Button2", ui.NewLabel(ui.Label{
  100. Text: "New Window",
  101. }))
  102. btn2.Handle(ui.Click, func(ed ui.EventData) error {
  103. addWindow(mw)
  104. return nil
  105. })
  106. mw.Add(btn2)
  107. win1.Place(btn2, ui.Place{
  108. Top: 10,
  109. Right: 10,
  110. })
  111. }