<commit_before>package games.opengl

import java.nio.{ Buffer, ByteBuffer, ShortBuffer, IntBuffer, FloatBuffer, DoubleBuffer, ByteOrder }

import org.scalajs.dom

import scala.scalajs.js
import js.Dynamic.{ global => g }

import scala.concurrent.Future

import games.JsUtils

//import scala.scalajs.js.typedarray.TypedArrayBufferOps
import scala.scalajs.js.typedarray.TypedArrayBufferOps._

// See https://github.com/scala-js/scala-js-dom/blob/master/src/main/scala/org/scalajs/dom/WebGL.scala for documentation
// about the WebGL DOM for ScalaJS

// Auxiliary components

object Token {

  type Buffer = org.scalajs.dom.raw.WebGLBuffer

  object Buffer {
    val invalid: Buffer = null
    val none: Buffer = null
  }

  type Program = org.scalajs.dom.raw.WebGLProgram

  object Program {
    val invalid: Program = null
  }

  type Shader = org.scalajs.dom.raw.WebGLShader

  object Shader {
    val invalid: Shader = null
  }

  type UniformLocation = org.scalajs.dom.raw.WebGLUniformLocation

  object UniformLocation {
    val invalid: UniformLocation = null
  }

  type FrameBuffer = org.scalajs.dom.raw.WebGLFramebuffer

  object FrameBuffer {
    val invalid: FrameBuffer = null
    val none: FrameBuffer = null
  }

  type RenderBuffer = org.scalajs.dom.raw.WebGLRenderbuffer

  object RenderBuffer {
    val invalid: RenderBuffer = null
    val none: RenderBuffer = null
  }

  type Texture = org.scalajs.dom.raw.WebGLTexture

  object Texture {
    val invalid: Texture = null
    val none: Texture = null
  }

}

// Main componenents

object GLES2WebGL {
  var orientationLockOnFullscreen: Boolean = false
}

class DisplayGLES2(gl: GLES2WebGL) extends Display {

  private var fullscreenRequested = false

  private val onFullscreenChange: js.Function = (e: js.Dynamic) => {
    if (!this.fullscreen) {
      canvas.width = canvasPrevDim._1
      canvas.height = canvasPrevDim._2
    } else {
      // Necessary for the size of the canvas (Chrome) and its resolution (Firefox)
      canvas.width = screen.width
      canvas.height = screen.height

      if (GLES2WebGL.orientationLockOnFullscreen) {
        games.input.AccelerometerJS.lockOrientation(games.input.AccelerometerJS.currentOrientation())
      }
    }

    if (JsUtils.autoToggling) this.fullscreen = fullscreenRequested // If the fullscreen state has changed against the wish of the user, change back ASAP
  }
  private val onFullscreenError: js.Function = (e: js.Dynamic) => {
    // nothing to do?
    js.Dynamic.global.console.log("onFullscreenError", this.fullscreen, e)
  }

  private val onOrientationChange: js.Function = (e: js.Dynamic) => {
    if (this.fullscreen) { // Update the canvas size in case of fullscreen mode
      /*
       * Wait 1000 ms before reading the screen dimension
       * (Firefox has not yet applied the orientation change, and is a bit
       * slow to do it)
       */
      val delay = 1000
      js.Dynamic.global.setTimeout(() => {
        if (this.fullscreen) { // check again (may have change it the mean time)
          canvas.width = screen.width
          canvas.height = screen.height
        }
      }, delay)
    }
  }

  private val canvas = gl.getWebGLRenderingContext().canvas.asInstanceOf[js.Dynamic]
  private val document = dom.document.asInstanceOf[js.Dynamic]
  private def window = js.Dynamic.global.window
  private def screen = js.Dynamic.global.screen

  // Init
  {
    val fullscreenRequest = JsUtils.getOptional[js.Dynamic](canvas, "requestFullscreen", "webkitRequestFullscreen", "mozRequestFullscreen", "mozRequestFullScreen")
    val fullscreenExit = JsUtils.getOptional[js.Dynamic](document, "exitFullscreen", "webkitCancelFullScreen", "mozCancelFullScreen")

    canvas.fullscreenRequest = fullscreenRequest.getOrElse(JsUtils.featureUnsupportedFunction("Fullscreen (Request)"))
    document.fullscreenExit = fullscreenExit.getOrElse(JsUtils.featureUnsupportedFunction("Fullscreen (Exit)"))

    document.addEventListener("fullscreenchange", onFullscreenChange, true)
    document.addEventListener("webkitfullscreenchange", onFullscreenChange, true)
    document.addEventListener("mozfullscreenchange", onFullscreenChange, true)
    document.addEventListener("MSFullscreenChange", onFullscreenChange, true)

    document.addEventListener("fullscreenerror", onFullscreenError, true)
    document.addEventListener("webkitfullscreenerror", onFullscreenError, true)
    document.addEventListener("mozfullscreenerror", onFullscreenError, true)
    document.addEventListener("MSFullscreenError", onFullscreenError, true)
<commit_msg>Comment about event for screenorientation change<commit_after>package games.opengl

import java.nio.{ Buffer, ByteBuffer, ShortBuffer, IntBuffer, FloatBuffer, DoubleBuffer, ByteOrder }

import org.scalajs.dom

import scala.scalajs.js
import js.Dynamic.{ global => g }

import scala.concurrent.Future

import games.JsUtils

//import scala.scalajs.js.typedarray.TypedArrayBufferOps
import scala.scalajs.js.typedarray.TypedArrayBufferOps._

// See https://github.com/scala-js/scala-js-dom/blob/master/src/main/scala/org/scalajs/dom/WebGL.scala for documentation
// about the WebGL DOM for ScalaJS

// Auxiliary components

object Token {

  type Buffer = org.scalajs.dom.raw.WebGLBuffer

  object Buffer {
    val invalid: Buffer = null
    val none: Buffer = null
  }

  type Program = org.scalajs.dom.raw.WebGLProgram

  object Program {
    val invalid: Program = null
  }

  type Shader = org.scalajs.dom.raw.WebGLShader

  object Shader {
    val invalid: Shader = null
  }

  type UniformLocation = org.scalajs.dom.raw.WebGLUniformLocation

  object UniformLocation {
    val invalid: UniformLocation = null
  }

  type FrameBuffer = org.scalajs.dom.raw.WebGLFramebuffer

  object FrameBuffer {
    val invalid: FrameBuffer = null
    val none: FrameBuffer = null
  }

  type RenderBuffer = org.scalajs.dom.raw.WebGLRenderbuffer

  object RenderBuffer {
    val invalid: RenderBuffer = null
    val none: RenderBuffer = null
  }

  type Texture = org.scalajs.dom.raw.WebGLTexture

  object Texture {
    val invalid: Texture = null
    val none: Texture = null
  }

}

// Main componenents

object GLES2WebGL {
  var orientationLockOnFullscreen: Boolean = false
}

class DisplayGLES2(gl: GLES2WebGL) extends Display {

  private var fullscreenRequested = false

  private val onFullscreenChange: js.Function = (e: js.Dynamic) => {
    if (!this.fullscreen) {
      canvas.width = canvasPrevDim._1
      canvas.height = canvasPrevDim._2
    } else {
      // Necessary for the size of the canvas (Chrome) and its resolution (Firefox)
      canvas.width = screen.width
      canvas.height = screen.height

      if (GLES2WebGL.orientationLockOnFullscreen) {
        games.input.AccelerometerJS.lockOrientation(games.input.AccelerometerJS.currentOrientation())
      }
    }

    if (JsUtils.autoToggling) this.fullscreen = fullscreenRequested // If the fullscreen state has changed against the wish of the user, change back ASAP
  }
  private val onFullscreenError: js.Function = (e: js.Dynamic) => {
    // nothing to do?
    js.Dynamic.global.console.log("onFullscreenError", this.fullscreen, e)
  }

  private val onOrientationChange: js.Function = (e: js.Dynamic) => {
    if (this.fullscreen) { // Update the canvas size in case of fullscreen mode
      /*
       * Wait 1000 ms before reading the screen dimension
       * (Firefox has not yet applied the orientation change, and is a bit
       * slow to do it)
       */
      val delay = 1000
      js.Dynamic.global.setTimeout(() => {
        if (this.fullscreen) { // check again (may have change it the mean time)
          canvas.width = screen.width
          canvas.height = screen.height
        }
      }, delay)
    }
  }

  private val canvas = gl.getWebGLRenderingContext().canvas.asInstanceOf[js.Dynamic]
  private val document = dom.document.asInstanceOf[js.Dynamic]
  private def window = js.Dynamic.global.window
  private def screen = js.Dynamic.global.screen

  // Init
  {
    val fullscreenRequest = JsUtils.getOptional[js.Dynamic](canvas, "requestFullscreen", "webkitRequestFullscreen", "mozRequestFullscreen", "mozRequestFullScreen")
    val fullscreenExit = JsUtils.getOptional[js.Dynamic](document, "exitFullscreen", "webkitCancelFullScreen", "mozCancelFullScreen")

    canvas.fullscreenRequest = fullscreenRequest.getOrElse(JsUtils.featureUnsupportedFunction("Fullscreen (Request)"))
    document.fullscreenExit = fullscreenExit.getOrElse(JsUtils.featureUnsupportedFunction("Fullscreen (Exit)"))

    document.addEventListener("fullscreenchange", onFullscreenChange, true)
    document.addEventListener("webkitfullscreenchange", onFullscreenChange, true)
    document.addEventListener("mozfullscreenchange", onFullscreenChange, true)
    document.addEventListener("MSFullscreenChange", onFullscreenChange, true)

    document.addEventListener("fullscreenerror", onFullscreenError, true)
    document.addEventListener("webkitfullscreenerror", onFullscreenError, true)
    document.addEventListener("mozfullscreenerror", onFullscreenError, true)
    document.addEventListener("MSFullscreenError", onFullscreenError, true)

    // What about this one? https://w3c.github.io/screen-orientation/#widl-ScreenOrientation-onchange
{'fraction_non_alphanumeric': 0.0939959038482268, 'fraction_numerical': 0.0031260105637598364, 'mean_word_length': 3.81974025974026, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5099660', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2683, 'n_words': 652}