Sha256: 1266d5d95c76bebd9dd10f88ceafe03baad91893be8a44fdfd6a5c4abfdf74c0

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

# Provides a mouse pointer usable through mouse or keyboard
module Lotu
  class Cursor < Actor
    attr_reader :clicked_x, :clicked_y
    attr_accessor :speed, :use_mouse

    def initialize(opts={})
      default_opts = {
        :use_mouse => true,
        :speed => 300,
        :x => $lotu.width/2,
        :y => $lotu.height/2
      }
      opts = default_opts.merge!(opts)
      super
      $lotu.mouse_x = opts[:x]
      $lotu.mouse_y = opts[:y]
      @clicked_x = @clicked_y = 0
      @speed = opts[:speed]
      @use_mouse = opts[:use_mouse]
    end

    def update
      super
      if @use_mouse
        @x = $lotu.mouse_x
        @y = $lotu.mouse_y
      end
    end

    # This is the method you want to call when a user press the
    # "click" key of your preference with something like:
    # set_keys Gosu::Button::MsLeft => :click
    # It'll yield the x, y coordinates of the clicked point
    def click
      @clicked_x = @x
      @clicked_y = @y
      fire(:click, @clicked_x, @clicked_y)
    end

    def adjust_mouse
      $lotu.mouse_y = @y
      $lotu.mouse_x = @x
    end

    def up
      @y -= @speed * dt
      adjust_mouse if use_mouse
    end

    def down
      @y += @speed * dt
      adjust_mouse if use_mouse
    end

    def left
      @x -= @speed * dt
      adjust_mouse if use_mouse
    end

    def right
      @x += @speed * dt
      adjust_mouse if use_mouse
    end

    def to_s
      ["@pos(#{format('%.2f, %.2f', @x, @y)})",
       "@clicked(#{format('%.2f, %.2f', @clicked_x, @clicked_y)})"]
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lotu-0.1.16 lib/lotu/cursor.rb
lotu-0.1.15 lib/lotu/cursor.rb
lotu-0.1.14 lib/lotu/cursor.rb
lotu-0.1.13 lib/lotu/cursor.rb
lotu-0.1.12 lib/lotu/cursor.rb