Sha256: 80acd9e6529a01498d8921cb07eb3ae1ba94d711238af9176001c229238cf38b

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Fusuma
  module Plugin
    module Detectors
      # Detect KeypressEvent from KeypressBuffer
      class KeypressDetector < Detector
        BUFFER_TYPE = 'keypress'

        # @param buffers [Array<Event>]
        # @return [Event] if event is detected
        # @return [NilClass] if event is NOT detected
        def detect(buffers)
          buffer = buffers.find { |b| b.type == BUFFER_TYPE }

          return if buffer.empty?

          records = buffer.events.select do |e|
            e.record.status == 'pressed'
          end.map(&:record)

          index_record = Events::Records::IndexRecord.new(
            index: create_index(records: records),
            position: :surfix
          )

          create_event(record: index_record)
        end

        # @param records [Array<Events::Records::KeypressRecord>]
        # @return [Config::Index]
        def create_index(records:)
          code = records.map(&:code).join('+')
          Config::Index.new(
            [
              Config::Index::Key.new('keypress', skippable: true),
              Config::Index::Key.new(code, skippable: true)
            ]
          )
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fusuma-plugin-keypress-0.4.0.pre lib/fusuma/plugin/detectors/keypress_detector.rb
fusuma-plugin-keypress-0.3.0 lib/fusuma/plugin/detectors/keypress_detector.rb
fusuma-plugin-keypress-0.2.1 lib/fusuma/plugin/detectors/keypress_detector.rb