Class: Dedalus::ApplicationViewComposer

Inherits:
Object
  • Object
show all
Includes:
Geometer::DimensionHelpers, Geometer::PointHelpers
Defined in:
lib/dedalus/app_view_composer.rb

Instance Method Summary collapse

Instance Method Details

#click_molecule(structure, window_dims, mouse_position:) ⇒ Object



30
31
32
# File 'lib/dedalus/app_view_composer.rb', line 30

def click_molecule(structure, window_dims, mouse_position:)
  send_molecule(structure, window_dims, mouse_position: mouse_position, message: :click)
end

#hover_molecule(structure, window_dims, mouse_position:) ⇒ Object



26
27
28
# File 'lib/dedalus/app_view_composer.rb', line 26

def hover_molecule(structure, window_dims, mouse_position:)
  send_molecule(structure, window_dims, mouse_position: mouse_position, message: :hover)
end

#render!(structure, dims) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dedalus/app_view_composer.rb', line 34

def render!(structure, dims)
  traverse(structure, origin: [0,0], dimensions: dims, render: true) do
    on_atom do |atom, origin:, dimensions:, freeform:, z_index:|
      if atom.background_color
        atom.draw_bounding_box(
          color: atom.background_color,
          origin: origin,
          dimensions: dimensions
        )
      end

      # TODO may need to do this to get click/hover events for sprites?
      if freeform # then offset by origin 
        x0,y0 = *origin
        x,y = *atom.position
        atom.position = [x+x0,y+y0]
      else
        atom.position = origin
      end

      atom.z_order = z_index

      atom.render
    end

    on_element do |element, origin:, dimensions:, z_index:|
      if element.background_color
        element.draw_bounding_box(
          color: element.background_color,
          origin: origin,
          dimensions: dimensions
        )
      end

      element.position = origin
    end
  end
end

#send_molecule(structure, window_dims, mouse_position:, message:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dedalus/app_view_composer.rb', line 11

def send_molecule(structure, window_dims, mouse_position:, message:)
  mouse_coord = coord(*mouse_position)

  traverse(structure, origin: [0,0], dimensions: window_dims, render: false) do
    on_molecule do |molecule, origin:, dimensions:, z_index:|
      element_bounding_box = Geometer::Rectangle.new(coord(*origin), dim(*dimensions))
      mouse_overlap = element_bounding_box.contains?(mouse_coord) rescue false # could get bad coords...
      if mouse_overlap
        molecule.position = origin
        molecule.send(message)
      end
    end
  end
end

#traverse(structure, origin: [0,0], dimensions:, render: false, &blk) ⇒ Object



6
7
8
9
# File 'lib/dedalus/app_view_composer.rb', line 6

def traverse(structure, origin: [0,0], dimensions:, render: false, &blk)
  traversal = ViewTraversal.new(&blk)
  traversal.walk!(structure, origin: origin, dimensions: dimensions, render: render)
end