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.
 
 
 
 
 

145 lines
2.7 KiB

  1. // Example script for using the Place strategy of ui.Frame.
  2. package main
  3. import (
  4. "git.kirsle.net/go/render"
  5. "git.kirsle.net/go/ui"
  6. )
  7. func main() {
  8. mw, err := ui.NewMainWindow("Frame Placement Demo | Click a Button", 800, 600)
  9. if err != nil {
  10. panic(err)
  11. }
  12. mw.SetBackground(render.White)
  13. // Create a sub-frame with its own buttons packed within.
  14. frame := ui.NewFrame("Blue Frame")
  15. frame.Configure(ui.Config{
  16. Width: 300,
  17. Height: 150,
  18. Background: render.DarkBlue,
  19. BorderSize: 1,
  20. BorderStyle: ui.BorderSunken,
  21. })
  22. mw.Place(frame, ui.Place{
  23. Point: render.NewPoint(80, 80),
  24. })
  25. // Create another frame that attaches itself to the bottom right
  26. // of the window.
  27. frame2 := ui.NewFrame("Red Frame")
  28. frame2.Configure(ui.Config{
  29. Width: 300,
  30. Height: 150,
  31. Background: render.DarkRed,
  32. })
  33. mw.Place(frame2, ui.Place{
  34. Right: 80,
  35. Bottom: 80,
  36. })
  37. // Draw rings of buttons around various widgets. The buttons say things
  38. // like "Top Left", "Top Center", "Left Middle", "Center" etc. encompassing
  39. // all 9 side placement options.
  40. CreateButtons(mw, frame)
  41. CreateButtons(mw, frame2)
  42. CreateButtons(mw, mw.Frame())
  43. mw.MainLoop()
  44. }
  45. // CreateButtons creates a set of Placed buttons around all the edges and
  46. // center of the parent frame.
  47. func CreateButtons(window *ui.MainWindow, parent *ui.Frame) {
  48. // Draw buttons around the edges of the window.
  49. buttons := []struct {
  50. Label string
  51. Place ui.Place
  52. }{
  53. {
  54. Label: "Top Left",
  55. Place: ui.Place{
  56. Point: render.NewPoint(12, 12),
  57. },
  58. },
  59. {
  60. Label: "Top Middle",
  61. Place: ui.Place{
  62. Top: 12,
  63. Center: true,
  64. },
  65. },
  66. {
  67. Label: "Top Right",
  68. Place: ui.Place{
  69. Top: 12,
  70. Right: 12,
  71. },
  72. },
  73. {
  74. Label: "Left Middle",
  75. Place: ui.Place{
  76. Left: 12,
  77. Middle: true,
  78. },
  79. },
  80. {
  81. Label: "Center",
  82. Place: ui.Place{
  83. Center: true,
  84. Middle: true,
  85. },
  86. },
  87. {
  88. Label: "Right Middle",
  89. Place: ui.Place{
  90. Right: 12,
  91. Middle: true,
  92. },
  93. },
  94. {
  95. Label: "Bottom Left",
  96. Place: ui.Place{
  97. Left: 12,
  98. Bottom: 12,
  99. },
  100. },
  101. {
  102. Label: "Bottom Center",
  103. Place: ui.Place{
  104. Bottom: 12,
  105. Center: true,
  106. },
  107. },
  108. {
  109. Label: "Bottom Right",
  110. Place: ui.Place{
  111. Bottom: 12,
  112. Right: 12,
  113. },
  114. },
  115. }
  116. for _, setting := range buttons {
  117. setting := setting
  118. button := ui.NewButton(setting.Label, ui.NewLabel(ui.Label{
  119. Text: setting.Label,
  120. Font: render.Text{
  121. FontFilename: "../DejaVuSans.ttf",
  122. Size: 12,
  123. Color: render.Black,
  124. },
  125. }))
  126. // When clicked, change the window title to ID this button.
  127. button.Handle(ui.Click, func(p render.Point) {
  128. window.SetTitle(parent.Name + ": " + setting.Label)
  129. })
  130. parent.Place(button, setting.Place)
  131. window.Add(button)
  132. }
  133. }