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.

53 lines
1.3KB

  1. require_relative 'svg_file'
  2. module DHE
  3. class Control
  4. attr_reader :faceplate, :width, :height, :top, :right, :bottom, :left, :x, :y
  5. def initialize(faceplate:, top:, right:, bottom:, left:, x: (right + left) / 2.0, y: (bottom + top) / 2.0)
  6. @faceplate = faceplate
  7. @top = top
  8. @right = right
  9. @bottom = bottom
  10. @left = left
  11. @width = right - left
  12. @height = bottom - top
  13. @x = x
  14. @y = y
  15. end
  16. def self.centered(x:, y:, width:, height: width)
  17. {left: x - width / 2, right: x + width / 2, top: y - height / 2, bottom: y + height / 2, }
  18. end
  19. def draw_control(svg:, **options)
  20. draw(svg: svg, x: width / 2.0, y: height / 2.0, **options)
  21. end
  22. def draw_faceplate(svg:)
  23. draw(svg: svg, x: @x, y: @y)
  24. end
  25. def svg_file(path:, has_text: false)
  26. SvgFile.new(path: path,
  27. width: "#{width}mm", height: "#{height}mm", viewBox: "0 0 #{width} #{height}",
  28. has_text: has_text) do |svg|
  29. yield svg
  30. end
  31. end
  32. end
  33. class RoundControl < Control
  34. attr_reader :diameter
  35. def initialize(faceplate:, x:, y:, diameter:)
  36. super(faceplate: faceplate, **Control::centered(x: x, y: y, width: diameter, height: diameter))
  37. @diameter = diameter
  38. end
  39. def radius
  40. diameter / 2
  41. end
  42. end
  43. end