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.

68 lines
2.3KB

  1. require_relative '../control'
  2. module DHE
  3. class Toggle < Control
  4. WIDTH = 3.0
  5. attr_reader :size
  6. def initialize(faceplate:, size:, x:, y:, selection:)
  7. super(faceplate: faceplate, **Control::centered(x: x, y: y, width: WIDTH, height: WIDTH * size))
  8. @size = size
  9. @foreground = faceplate.foreground
  10. @background = faceplate.background
  11. @slug = "toggle-#{@size}"
  12. @selection = selection
  13. end
  14. def draw(svg:, x:, y:, selection: @selection)
  15. thumb_position = case selection
  16. when @size
  17. 1.0
  18. when 1
  19. -1.0
  20. else
  21. 0.0
  22. end
  23. box_stroke_width = width / 8.0
  24. interior_inset = box_stroke_width / 2.0
  25. box_width = width - box_stroke_width
  26. box_height = height - box_stroke_width
  27. box_left = -width / 2.0 + interior_inset
  28. box_top = -height / 2.0 + interior_inset
  29. interior_width = box_width - box_stroke_width
  30. interior_height = box_height - box_stroke_width
  31. corner_radius = interior_inset
  32. knurl_stroke_width = 0.25
  33. knurl_inset = knurl_stroke_width * 2.0
  34. knurl_length = interior_width - knurl_inset
  35. knurl_left = knurl_length / -2.0
  36. knurl_right = knurl_left + knurl_length
  37. knurl_spacing = knurl_stroke_width * 2.0
  38. lever_height = knurl_spacing * 4.0 + knurl_stroke_width
  39. lever_inset = knurl_stroke_width
  40. lever_distance = (interior_height - lever_height) / 2.0 - lever_inset
  41. lever_offset = lever_distance * -thumb_position
  42. svg.g(transform: "translate(#{x} #{y})", fill: @background, stroke: @foreground) do |g|
  43. g.rect(x: box_left, y: box_top, width: box_width, height: box_height, rx: corner_radius, ry: corner_radius, 'stroke-width' => box_stroke_width)
  44. (-2..2).map { |index| knurl_spacing * index + lever_offset }.each do |knurl_y|
  45. g.line(x1: knurl_left, x2: knurl_right, y1: knurl_y, y2: knurl_y, 'stroke-width' => knurl_stroke_width, 'stroke-linecap' => 'round')
  46. end
  47. end
  48. end
  49. def svg_files
  50. (1..size).map do |selection|
  51. path = faceplate.slug / "#{@slug}-#{selection}"
  52. svg_file(path: path) do |svg|
  53. draw_control(svg: svg, selection: selection)
  54. end
  55. end
  56. end
  57. end
  58. end