Sha256: c04c7efee6e54b82221d1589a19e9a2dc7336d7bb52f6415a31932221c8b326d

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

class AuthorEngine
  class TouchButton
    attr_reader :x, :y, :width, :height
    def initialize(label:, color:, x:, y: nil, width:, height:, side:, contact_proc:, no_contact_proc:)
      @label, @color, @x, @y, @width, @height = label, color, x, y, width, height
      @side, @contact_proc, @no_contact_proc = side, contact_proc, no_contact_proc

      @game       = AuthorEngine::GameRunner.instance.game
      @game_width = 128 * @game.scale
      @game_x     = `window.innerWidth/2 - #{@game_width/2}`

      if @side == :left
        @x = @game_x-@x
      elsif @side == :right
        @x = @game_x+@game_width+@x
      else
        raise "side must be :left or :right"
      end

      @y = `window.innerHeight/2 - #{height}` unless @y.is_a?(Numeric)
    end

    def draw
      `#{@game.canvas_context}.fillStyle = #{@color}`
      `#{@game.canvas_context}.fillRect(#{@x}, #{@y}, #{width}, #{width})`
    end

    def active
      @contact_proc.call
    end

    def inactive
      @no_contact_proc.call
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
author_engine-0.5.0 lib/author_engine/game/opal/touch_button.rb