Sha256: 645b32464f9d9f04a8d6cf44a4953545e3a02e3681f7b3893cde30dffd82205e

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

class AuthorEngine
  module TouchHandler
    Touch = Struct.new(:origin_x, :origin_y, :x, :y)
    def touch_handler_setup
      @current_touches = {}
    end

    def copy_touch(touch)
      Touch.new(`touch.pageX`, `touch.pageY`, `touch.pageX`, `touch.pageY`)
    end

    def set_touch(touch)
      struct = @current_touches[`#{touch}.identifier`]

      struct.x = `#{touch}.pageX`
      struct.y = `#{touch}.pageY`

      return nil
    end

    def handle_touch_start(event)
      `#{event}.preventDefault()`

      touches = `#{event}.changedTouches`
      `for (var i = 0; i < #{touches}.length; i++) {
        #{@current_touches[`touches[i].identifier`] = copy_touch(`touches[i]`)}
      }`

      if @fullscreen_button && @fullscreen_button.trigger?(@current_touches)
        `if (document.fullscreenElement == null && #{@game.canvas}.requestFullscreen) {
          #{game.canvas}.requestFullscreen()
        } else if(document.fullscreenElement != null && document.exitFullscreen) {
          document.exitFullscreen()
        } `
      end

      return nil
    end

    def handle_touch_move(event)
      `#{event}.preventDefault()`

      touches = `#{event}.changedTouches`
      `for (var i = 0; i < #{touches}.length; i++) {
        #{set_touch(`touches[i]`)}
      }`

      return nil
    end

    def handle_touch_cancel(event)
      handle_touch_end(event)
    end

    def handle_touch_end(event)
      `#{event}.preventDefault()`

      touches = `#{event}.changedTouches`
      `for (var i = 0; i < #{touches}.length; i++) {
        #{@current_touches.delete(`touches[i].identifier`)}
      }`

      return nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
author_engine-0.6.1 lib/author_engine/game/opal/touch_handler.rb
author_engine-0.6.0 lib/author_engine/game/opal/touch_handler.rb