Sha256: 285984f908d69dee34efb576ec5f876876ce4cd6eca07230fb3cabe94310419c

Contents?: true

Size: 1.93 KB

Versions: 5

Compression:

Stored size: 1.93 KB

Contents

module Vedeu

  module Input

    # Rudimentary support for mouse interactions.
    #
    class Mouse

      # Trigger an event depending on which button was pressed.
      #
      # @param (see #initialize)
      # @return [void]
      def self.click(input)
        new(input).click
      end

      # Returns a new instance of Vedeu::Input::Mouse.
      #
      # @param input [String]
      # @return [Vedeu::Input::Mouse]
      def initialize(input)
        @input = input
      end

      # Trigger an event depending on which button was pressed.
      #
      # @return [void]
      def click
        Vedeu.log(type:    :input,
                  message: "Mouse pressed: '#{button}' " \
                           "(x: #{x}, y: #{y})".freeze)

        Vedeu.trigger(:_mouse_event_, input)

        if left_click?
          Vedeu.trigger(:_cursor_reposition_, Vedeu.focus, y, x)

        elsif wheel_up?
          Vedeu.trigger(:_cursor_up_, Vedeu.focus)

        elsif wheel_down?
          Vedeu.trigger(:_cursor_down_, Vedeu.focus)

        else
          Vedeu.log(type:    :input,
                    message: 'Vedeu does not support mouse button ' \
                             "'#{button}' yet.".freeze)

        end
      end

      protected

      # @!attribute [r] input
      # @return [String]
      attr_reader :input

      private

      # @return [Fixnum]
      def button
        mouse[0]
      end

      # @return [Array<Fixnum>]
      def mouse
        @_input ||= input.chars[3..-1].map { |character| character.ord - 32 }
      end

      # @return [Boolean]
      def left_click?
        button == 0
      end

      # @return [Boolean]
      def wheel_up?
        button == 64
      end

      # @return [Boolean]
      def wheel_down?
        button == 65
      end

      # @return [Fixnum]
      def x
        mouse[1]
      end

      # @return [Fixnum]
      def y
        mouse[2]
      end

    end # Mouse

  end # Input

end # Vedeu

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vedeu-0.8.0 lib/vedeu/input/mouse.rb
vedeu-0.7.4 lib/vedeu/input/mouse.rb
vedeu-0.7.3 lib/vedeu/input/mouse.rb
vedeu-0.7.2 lib/vedeu/input/mouse.rb
vedeu-0.7.1 lib/vedeu/input/mouse.rb