circle.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package canvas
  2. import (
  3. "image/color"
  4. "math"
  5. "fyne.io/fyne/v2"
  6. )
  7. // Declare conformity with CanvasObject interface
  8. var _ fyne.CanvasObject = (*Circle)(nil)
  9. // Circle describes a colored circle primitive in a Fyne canvas
  10. type Circle struct {
  11. Position1 fyne.Position // The current top-left position of the Circle
  12. Position2 fyne.Position // The current bottomright position of the Circle
  13. Hidden bool // Is this circle currently hidden
  14. FillColor color.Color // The circle fill color
  15. StrokeColor color.Color // The circle stroke color
  16. StrokeWidth float32 // The stroke width of the circle
  17. }
  18. // NewCircle returns a new Circle instance
  19. func NewCircle(color color.Color) *Circle {
  20. return &Circle{
  21. FillColor: color,
  22. }
  23. }
  24. // Hide will set this circle to not be visible
  25. func (c *Circle) Hide() {
  26. c.Hidden = true
  27. repaint(c)
  28. }
  29. // MinSize for a Circle simply returns Size{1, 1} as there is no
  30. // explicit content
  31. func (c *Circle) MinSize() fyne.Size {
  32. return fyne.NewSize(1, 1)
  33. }
  34. // Move the circle object to a new position, relative to its parent / canvas
  35. func (c *Circle) Move(pos fyne.Position) {
  36. size := c.Size()
  37. c.Position1 = pos
  38. c.Position2 = fyne.NewPos(c.Position1.X+size.Width, c.Position1.Y+size.Height)
  39. repaint(c)
  40. }
  41. // Position gets the current top-left position of this circle object, relative to its parent / canvas
  42. func (c *Circle) Position() fyne.Position {
  43. return c.Position1
  44. }
  45. // Refresh causes this object to be redrawn with its configured state.
  46. func (c *Circle) Refresh() {
  47. Refresh(c)
  48. }
  49. // Resize sets a new bottom-right position for the circle object
  50. // If it has a stroke width this will cause it to Refresh.
  51. func (c *Circle) Resize(size fyne.Size) {
  52. if size == c.Size() {
  53. return
  54. }
  55. c.Position2 = fyne.NewPos(c.Position1.X+size.Width, c.Position1.Y+size.Height)
  56. Refresh(c)
  57. }
  58. // Show will set this circle to be visible
  59. func (c *Circle) Show() {
  60. c.Hidden = false
  61. c.Refresh()
  62. }
  63. // Size returns the current size of bounding box for this circle object
  64. func (c *Circle) Size() fyne.Size {
  65. return fyne.NewSize(float32(math.Abs(float64(c.Position2.X)-float64(c.Position1.X))),
  66. float32(math.Abs(float64(c.Position2.Y)-float64(c.Position1.Y))))
  67. }
  68. // Visible returns true if this circle is visible, false otherwise
  69. func (c *Circle) Visible() bool {
  70. return !c.Hidden
  71. }