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.

70 line
1.5KB

  1. package template
  2. import (
  3. "git.wtrh.nl/patterns/gopatterns/pkg/pattern"
  4. "git.wtrh.nl/patterns/gopatterns/pkg/pattern/path"
  5. "git.wtrh.nl/patterns/gopatterns/pkg/pattern/point"
  6. )
  7. // Lines contains one Line or more in a slice.
  8. type Lines []Line
  9. // Line describes a pattern line.
  10. type Line struct {
  11. Through []point.ID `yaml:"through"`
  12. Curve *Curve `yaml:"curve,omitempty"`
  13. Style *Style `yaml:"style,omitempty"`
  14. }
  15. type Style struct {
  16. Thickness *float64 `yaml:"thickness,omitempty"`
  17. }
  18. // Curve describes if a Line curves and if it has start and end constraints.
  19. type Curve struct {
  20. Start point.ID `yaml:"start,omitempty"`
  21. End point.ID `yaml:"end,omitempty"`
  22. }
  23. // Build adds the line to the provided [pattern.Pattern].
  24. func (l Line) Build(pat *pattern.Pattern) error {
  25. points := pat.GetPoints(l.Through)
  26. for _, p := range points {
  27. p.SetDraw()
  28. }
  29. style := path.NewDefaultStyle()
  30. if l.Style != nil && l.Style.Thickness != nil {
  31. style.Thickness = *l.Style.Thickness
  32. }
  33. switch {
  34. case l.Curve != nil:
  35. pat.AddLine(
  36. path.NewSpline(path.SplineOpts{
  37. Start: pat.GetPoint(l.Curve.Start),
  38. End: pat.GetPoint(l.Curve.End),
  39. Points: points,
  40. Style: style,
  41. }),
  42. )
  43. default:
  44. pat.AddLine(path.NewPath(points, style))
  45. }
  46. return nil
  47. }
  48. // Build adds all the lines to the provided [pattern.Pattern].
  49. func (l Lines) Build(pat *pattern.Pattern) error {
  50. for _, line := range l {
  51. err := line.Build(pat)
  52. if err != nil {
  53. return err
  54. }
  55. }
  56. return nil
  57. }