1/* 2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15 16import { X2DFast } from "./graphics/X2DFast.js"; 17import { Scr } from "./XDefine.js"; 18 19export var gl; 20 21function touchStart(e) { 22 e.preventDefault(); 23 GLFrame.pinstance_.callbackProctouch(1, e.touches[0].clientX, e.touches[0].clientY); 24} 25function touchMove(e) { 26 e.preventDefault(); 27 GLFrame.pinstance_.callbackProctouch(2, e.touches[0].clientX, e.touches[0].clientY); 28} 29function touchEnd(e) { 30 e.preventDefault(); 31 GLFrame.pinstance_.callbackProctouch(3, e.changedTouches[0].clientX, e.changedTouches[0].clientY); 32} 33 34function mouseDown(e) { 35 e.preventDefault(); 36 GLFrame.pinstance_.callbackProctouch(1, e.offsetX, e.offsetY) 37} 38function mouseMove(e) { 39 e.preventDefault(); 40 GLFrame.pinstance_.callbackProctouch(2, e.offsetX, e.offsetY) 41} 42function mouseUp(e) { 43 e.preventDefault(); 44 GLFrame.pinstance_.callbackProctouch(3, e.offsetX, e.offsetY) 45} 46 47function keyDown(e) { 48 let ret = "" 49 if (e.ctrlKey) { 50 if (ret.length > 0) ret += "+" 51 ret += "ctrl" 52 } 53 if (e.shiftKey) { 54 if (ret.length > 0) ret += "+" 55 ret += "shift" 56 } 57 if (e.altKey) { 58 if (ret.length > 0) ret += "+" 59 ret += "alt" 60 } 61 if (ret.length > 0) ret += "+" 62 ret += e.key 63 GLFrame.pinstance_.callbackKey(1, ret) 64} 65 66function mainLoop() { 67 GLFrame.pinstance_.callbackDraw(); 68 window.requestAnimationFrame(mainLoop) 69} 70 71export class GLFrame { 72 static gi() { 73 return GLFrame.pinstance_; 74 } 75 constructor() { 76 } 77 78 go(cvs, _draw = null, _touch = null, _key = null, _logic = null) { 79 gl = cvs.getContext('webgl', { premultipliedAlpha: false }); 80 81 this.pCallbackDraw = _draw 82 this.pCallbackTouch = _touch 83 this.pCallbackKey = _key 84 this.pCallbackLogic = _logic 85 86 cvs.addEventListener("touchstart", touchStart); 87 cvs.addEventListener("touchmove", touchMove); 88 cvs.addEventListener("touchend", touchEnd); 89 90 cvs.addEventListener("mousedown", mouseDown); 91 cvs.addEventListener("mousemove", mouseMove); 92 cvs.addEventListener("mouseup", mouseUp); 93 94 window.addEventListener('keydown', keyDown); 95 window.requestAnimationFrame(mainLoop) 96 } 97 callbackKey(type, code) { 98 if (this.pCallbackKey != null) { 99 this.pCallbackKey(type, code) 100 } 101 } 102 callbackDraw() { 103 gl.clearColor(0.0, 0.0, 0.0, 1.0); 104 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 105 106 if (this.pCallbackDraw != null) { 107 this.pCallbackDraw(); 108 } 109 } 110 callbackProctouch(msg, x, y) { 111 if (this.pCallbackTouch != null) { 112 x = x * Scr.logicw / Scr.width 113 y = y * Scr.logich / Scr.height 114 this.pCallbackTouch(msg, x, y); 115 } 116 } 117 resize() { 118 gl.viewport(0, 0, Scr.logicw, Scr.logich); 119 X2DFast.gi().resetMat(); 120 } 121} 122 123GLFrame.pinstance_ = new GLFrame()