Sha256: 8989426b24c8e7e90ae36da25822d19c8a0034c53dc1101572761674a83472cf

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 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.authorengine_canvas}.requestFullscreen) {
          #{game.authorengine_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

3 entries across 3 versions & 1 rubygems

Version Path
author_engine-0.9.0 lib/author_engine/game/opal/touch_handler.rb
author_engine-0.8.0 lib/author_engine/game/opal/touch_handler.rb
author_engine-0.7.0 lib/author_engine/game/opal/touch_handler.rb