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.
 
 
 
 
 

209 lines
4.6 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 = 640
  14. Height = 480
  15. BGColor = render.White
  16. )
  17. func init() {
  18. sdl.DefaultFontFilename = "../DejaVuSans.ttf"
  19. }
  20. func main() {
  21. mw, err := ui.NewMainWindow("Menu Demo", Width, Height)
  22. if err != nil {
  23. panic(err)
  24. }
  25. setupMainMenu(mw)
  26. // Menu button in middle of window.
  27. {
  28. btn := ui.NewMenuButton("MenuBtn", ui.NewLabel(ui.Label{
  29. Text: "Click me!",
  30. }))
  31. btn.Supervise(mw.Supervisor())
  32. mw.Place(btn, ui.Place{
  33. Center: true,
  34. Middle: true,
  35. })
  36. for _, label := range []string{
  37. "MenuButtons open menus",
  38. "when clicked. MenuBar is ",
  39. "a Frame of MenuButtons",
  40. "attached to the top of the",
  41. "window.",
  42. "",
  43. "They all provide a nice API",
  44. "to insert menus and items.",
  45. } {
  46. label := label
  47. if label == "" {
  48. btn.AddSeparator()
  49. continue
  50. }
  51. btn.AddItem(label, func() {
  52. fmt.Printf("Button '%s' clicked!\n", label)
  53. })
  54. }
  55. }
  56. // Menu button on the bottom right of screen.
  57. {
  58. btn := ui.NewMenuButton("BrBtn", ui.NewLabel(ui.Label{
  59. Text: "Fruits",
  60. }))
  61. btn.Supervise(mw.Supervisor())
  62. mw.Place(btn, ui.Place{
  63. Right: 20,
  64. Bottom: 20,
  65. })
  66. btn.AddItem("Apples", func() {})
  67. btn.AddItem("Oranges", func() {})
  68. btn.AddItem("Bananas", func() {})
  69. btn.AddItem("Pears", func() {})
  70. }
  71. // Menu button on the bottom left of screen.
  72. {
  73. btn := ui.NewMenuButton("BlBtn", ui.NewLabel(ui.Label{
  74. Text: "Set Window Color",
  75. }))
  76. btn.Supervise(mw.Supervisor())
  77. mw.Place(btn, ui.Place{
  78. Left: 20,
  79. Bottom: 20,
  80. })
  81. setBg := func(color render.Color) func() {
  82. return func() {
  83. BGColor = color
  84. }
  85. }
  86. // Really fancy buttons.
  87. var colors = []struct {
  88. label string
  89. hex string
  90. color render.Color
  91. }{
  92. {"Black", "#000", render.Black},
  93. {"Red", "#F00", render.Red},
  94. {"Yellow", "#FF0", render.Yellow},
  95. {"Green", "#0F0", render.Green},
  96. {"Cyan", "#0FF", render.Cyan},
  97. {"Blue", "#00F", render.Blue},
  98. {"Magenta", "#F0F", render.Magenta},
  99. {"White", "#FFF", render.White},
  100. }
  101. for _, opt := range colors {
  102. item := btn.AddItemAccel(opt.label, opt.hex, setBg(opt.color))
  103. item.SetBackground(opt.color.Lighten(128))
  104. }
  105. // btn.AddItemAccel("Black", "#000", setBg(render.White))
  106. // btn.AddItemAccel("Red", "#F00", setBg(render.Red))
  107. // btn.AddItemAccel("Yellow", "#FF0", setBg(render.Yellow))
  108. // btn.AddItemAccel("Green", "#0F0", setBg(render.Green))
  109. // btn.AddItemAccel("Cyan", "#0FF", setBg(render.Cyan))
  110. // btn.AddItemAccel("Blue", "#00F", setBg(render.Blue))
  111. // btn.AddItemAccel("Magenta", "#F0F", setBg(render.Magenta))
  112. // btn.AddItemAccel("White", "#FFF", setBg(render.White))
  113. }
  114. // The "Long Menu" on the middle left side
  115. {
  116. btn := ui.NewMenuButton("BlBtn", ui.NewLabel(ui.Label{
  117. Text: "Tall Growing Menu",
  118. }))
  119. btn.Supervise(mw.Supervisor())
  120. mw.Place(btn, ui.Place{
  121. Left: 20,
  122. Middle: true,
  123. })
  124. var id int
  125. btn.AddItem("Add New Option", func() {
  126. id++
  127. id := id
  128. btn.AddItem(fmt.Sprintf("Menu Item #%d", id), func() {
  129. fmt.Printf("Chosen menu item %d\n", id)
  130. })
  131. })
  132. btn.AddSeparator()
  133. }
  134. mw.OnLoop(func(e *event.State) {
  135. mw.SetBackground(BGColor)
  136. if e.Up {
  137. fmt.Println("Supervised widgets:")
  138. for widg := range mw.Supervisor().Widgets() {
  139. fmt.Printf("%+v\n", widg)
  140. }
  141. }
  142. if e.Escape {
  143. os.Exit(0)
  144. }
  145. })
  146. mw.MainLoop()
  147. }
  148. func setupMainMenu(mw *ui.MainWindow) {
  149. bar := ui.NewMenuBar("Main Menu")
  150. fileMenu := bar.AddMenu("File")
  151. fileMenu.AddItemAccel("New", "Ctrl-N", func() {
  152. fmt.Println("Chose File->New")
  153. })
  154. fileMenu.AddItemAccel("Open", "Ctrl-O", func() {
  155. fmt.Println("Chose File->Open")
  156. })
  157. fileMenu.AddSeparator()
  158. fileMenu.AddItemAccel("Exit", "Alt-F4", func() {
  159. fmt.Println("Chose File->Exit")
  160. os.Exit(0)
  161. })
  162. editMenu := bar.AddMenu("Edit")
  163. editMenu.AddItemAccel("Undo", "Ctrl-Z", func() {})
  164. editMenu.AddItemAccel("Redo", "Shift-Ctrl-Z", func() {})
  165. editMenu.AddSeparator()
  166. editMenu.AddItemAccel("Cut", "Ctrl-X", func() {})
  167. editMenu.AddItemAccel("Copy", "Ctrl-C", func() {})
  168. editMenu.AddItemAccel("Paste", "Ctrl-V", func() {})
  169. editMenu.AddSeparator()
  170. editMenu.AddItem("Settings...", func() {})
  171. viewMenu := bar.AddMenu("View")
  172. viewMenu.AddItemAccel("Toggle Full Screen", "F11", func() {})
  173. helpMenu := bar.AddMenu("Help")
  174. helpMenu.AddItemAccel("Contents", "F1", func() {})
  175. helpMenu.AddItem("About", func() {})
  176. bar.Supervise(mw.Supervisor())
  177. bar.Compute(mw.Engine)
  178. mw.Pack(bar, bar.PackTop())
  179. fmt.Printf("Setup MenuBar: %s\n", bar.Size())
  180. }