pos.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
  2. // Use of this source code is governed by a MIT license that can
  3. // be found in the LICENSE file.
  4. package termui
  5. import "image"
  6. // Align is the position of the gauge's label.
  7. type Align uint
  8. // All supported positions.
  9. const (
  10. AlignNone Align = 0
  11. AlignLeft Align = 1 << iota
  12. AlignRight
  13. AlignBottom
  14. AlignTop
  15. AlignCenterVertical
  16. AlignCenterHorizontal
  17. AlignCenter = AlignCenterVertical | AlignCenterHorizontal
  18. )
  19. func AlignArea(parent, child image.Rectangle, a Align) image.Rectangle {
  20. w, h := child.Dx(), child.Dy()
  21. // parent center
  22. pcx, pcy := parent.Min.X+parent.Dx()/2, parent.Min.Y+parent.Dy()/2
  23. // child center
  24. ccx, ccy := child.Min.X+child.Dx()/2, child.Min.Y+child.Dy()/2
  25. if a&AlignLeft == AlignLeft {
  26. child.Min.X = parent.Min.X
  27. child.Max.X = child.Min.X + w
  28. }
  29. if a&AlignRight == AlignRight {
  30. child.Max.X = parent.Max.X
  31. child.Min.X = child.Max.X - w
  32. }
  33. if a&AlignBottom == AlignBottom {
  34. child.Max.Y = parent.Max.Y
  35. child.Min.Y = child.Max.Y - h
  36. }
  37. if a&AlignTop == AlignRight {
  38. child.Min.Y = parent.Min.Y
  39. child.Max.Y = child.Min.Y + h
  40. }
  41. if a&AlignCenterHorizontal == AlignCenterHorizontal {
  42. child.Min.X += pcx - ccx
  43. child.Max.X = child.Min.X + w
  44. }
  45. if a&AlignCenterVertical == AlignCenterVertical {
  46. child.Min.Y += pcy - ccy
  47. child.Max.Y = child.Min.Y + h
  48. }
  49. return child
  50. }
  51. func MoveArea(a image.Rectangle, dx, dy int) image.Rectangle {
  52. a.Min.X += dx
  53. a.Max.X += dx
  54. a.Min.Y += dy
  55. a.Max.Y += dy
  56. return a
  57. }
  58. var termWidth int
  59. var termHeight int
  60. func TermRect() image.Rectangle {
  61. return image.Rect(0, 0, termWidth, termHeight)
  62. }