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.
 
 
 
 
 

97 lines
2.1 KiB

  1. package ui
  2. import (
  3. "fmt"
  4. "git.kirsle.net/go/render"
  5. )
  6. // Frame is a widget that contains other widgets.
  7. type Frame struct {
  8. Name string
  9. BaseWidget
  10. // Widget placement settings.
  11. packs map[Side][]packedWidget // Packed widgets
  12. placed []placedWidget // Placed widgets
  13. widgets []Widget
  14. }
  15. // NewFrame creates a new Frame.
  16. func NewFrame(name string) *Frame {
  17. w := &Frame{
  18. Name: name,
  19. packs: map[Side][]packedWidget{},
  20. widgets: []Widget{},
  21. }
  22. w.SetBackground(render.RGBA(1, 0, 0, 0)) // invisible default BG
  23. w.IDFunc(func() string {
  24. return fmt.Sprintf("Frame<%s>",
  25. name,
  26. )
  27. })
  28. return w
  29. }
  30. // Setup ensures all the Frame's data is initialized and not null.
  31. func (w *Frame) Setup() {
  32. if w.packs == nil {
  33. w.packs = map[Side][]packedWidget{}
  34. }
  35. if w.widgets == nil {
  36. w.widgets = []Widget{}
  37. }
  38. }
  39. // Children returns all of the child widgets.
  40. func (w *Frame) Children() []Widget {
  41. return w.widgets
  42. }
  43. // Compute the size of the Frame.
  44. func (w *Frame) Compute(e render.Engine) {
  45. w.computePacked(e)
  46. w.computePlaced(e)
  47. }
  48. // Present the Frame.
  49. func (w *Frame) Present(e render.Engine, P render.Point) {
  50. if w.Hidden() {
  51. return
  52. }
  53. var (
  54. S = w.Size()
  55. )
  56. // Draw the widget's border and everything.
  57. w.DrawBox(e, P)
  58. // Draw the background color.
  59. e.DrawBox(w.Background(), render.Rect{
  60. X: P.X + w.BoxThickness(1),
  61. Y: P.Y + w.BoxThickness(1),
  62. W: S.W - w.BoxThickness(2),
  63. H: S.H - w.BoxThickness(2),
  64. })
  65. // Draw the widgets.
  66. for _, child := range w.widgets {
  67. // child.Compute(e)
  68. p := child.Point()
  69. moveTo := render.NewPoint(
  70. P.X+p.X+w.BoxThickness(1),
  71. P.Y+p.Y+w.BoxThickness(1),
  72. )
  73. // if child.ID() == "Canvas" {
  74. // log.Debug("Frame X=%d Child X=%d Box=%d Point=%s", P.X, p.X, w.BoxThickness(1), p)
  75. // log.Debug("Frame Y=%d Child Y=%d Box=%d MoveTo=%s", P.Y, p.Y, w.BoxThickness(1), moveTo)
  76. // }
  77. // child.MoveTo(moveTo) // TODO: if uncommented the child will creep down the parent each tick
  78. // if child.ID() == "Canvas" {
  79. // log.Debug("New Point: %s", child.Point())
  80. // }
  81. child.Present(e, moveTo)
  82. }
  83. }