lib/surface_master/launchpad/interaction.rb in surface_master-0.2.0 vs lib/surface_master/launchpad/interaction.rb in surface_master-0.2.1

- old
+ new

@@ -1,45 +1,15 @@ module SurfaceMaster module Launchpad + # Higher-level interface to Novation Launchpad Mark 2, providing an input + # handling loop and event-hooks for input events. class Interaction < SurfaceMaster::Interaction def initialize(opts = nil) @device_class = Device super(opts) end - # def response_to(types = :all, state = :both, opts = nil, &block) - # logger.debug "setting response to #{types.inspect} for state #{state.inspect} with #{opts.inspect}" - # types = Array(types) - # opts ||= {} - # no_response_to(types, state) if opts[:exclusive] == true - # Array(state == :both ? %i(down up) : state).each do |st| - # types.each do |type| - # combined_types(type, opts).each do |combined_type| - # responses[combined_type][st] << block - # end - # end - # end - # nil - # end - - # def no_response_to(types = nil, state = :both, opts = nil) - # logger.debug "removing response to #{types.inspect} for state #{state.inspect}" - # types = Array(types) - # Array(state == :both ? %i(down up) : state).each do |st| - # types.each do |type| - # combined_types(type, opts).each do |combined_type| - # responses[combined_type][st].clear - # end - # end - # end - # nil - # end - - # def respond_to(type, state, opts = nil) - # respond_to_action((opts || {}).merge(type: type, state: state)) - # end - protected def responses @responses ||= Hash.new { |hash, key| hash[key] = { down: [], up: [] } } end @@ -54,33 +24,45 @@ def combined_types(type, opts = nil) if type.to_sym == :grid && opts x = grid_range(opts[:x]) y = grid_range(opts[:y]) return [:grid] if x.nil? && y.nil? # whole grid - x ||= ['-'] # whole row - y ||= ['-'] # whole column + x ||= ["-"] # whole row + y ||= ["-"] # whole column x.product(y).map { |xx, yy| :"grid#{xx}#{yy}" } else [type.to_sym] end end def respond_to_action(action) + mappings_for_action(action).each do |block| + block.call(self, action) + end + nil + rescue Exception => e # TODO: StandardException, RuntimeError, or Exception? + logger.error "Error when responding to action #{action.inspect}: #{e.inspect}" + raise e + end + + def mappings_for_action(action) type = action[:type].to_sym state = action[:state].to_sym actions = [] if type == :grid - actions += responses[:"grid#{action[:x]}#{action[:y]}"][state] - actions += responses[:"grid#{action[:x]}-"][state] - actions += responses[:"grid-#{action[:y]}"][state] + actions += mappings_for_grid_action(state, action[:x], action[:y]) end actions += responses[type][state] actions += responses[:all][state] - actions.compact.each {|block| block.call(self, action)} - nil - rescue Exception => e # TODO: StandardException, RuntimeError, or Exception? - logger.error "Error when responding to action #{action.inspect}: #{e.inspect}" - raise e + actions.compact + end + + def mappings_for_grid_action(state, x, y) + actions = [] + actions += responses[:"grid#{x}#{y}"][state] + actions += responses[:"grid#{x}-"][state] + actions += responses[:"grid-#{y}"][state] + actions end end end end