public bool onpointevent(spriteCanvas c, canvaspointevent e, float x, float y) { bool skipevent = false; this.showtxt = "point= " + x + " |,| " + y; if (x > this.trectBtn.x && y > this.trectBtn.y && x < (this.trectBtn.x + this.trectBtn.w) && y < (this.trectBtn.y + this.trectBtn.h)) { this.btndown = true; } else { this.btndown = false; } return(skipevent); }
public void onresize(spriteCanvas c) { //throw new NotImplementedException(); }
public void ondraw(spriteCanvas c) { this.timer += 0.015f; if (this.timer > 2.0f) { this.timer -= 2.0f; } //get all sprite in atlas who named "1" if (this.spritenames.Count == 0) { var atlas = lighttool.atlasMgr.Instance().load(c.webgl, "1"); if (atlas != null) { foreach (var iname in atlas.sprites.Keys) { this.spritenames.Add(iname); } //init for drawer //for (var cc = 0; cc < 10; cc++) //{ // this.cdDrawer.push(new coolDownDrawer(atlas, this.spritenames[cc])); // this.cdDrawer[cc].setDestRect(new lighttool.spriteRect(50 * cc, 50, 50, 50)); //} } } var t = lighttool.textureMgr.Instance().load(c.webgl, "tex/1.jpg"); if (t != null) { this.trect.x = 0; this.trect.y = 0; this.trect.w = c.width; this.trect.h = c.height; c.drawTexture(t, this.trect, lighttool.spriteRect.one, new lighttool.spriteColor(1, 1, 1, 1.0f)); } //draw atlas if (this.spritenames.Count > 0) { //this.spriteBatcher.begindraw(this.atlas.mat); for (var i = 0; i < 30; i++) { var x = (float)Math.Random() * 500; var y = (float)Math.Random() * 500; var si = (int)(Math.Random() * this.spritenames.Count); this.trect.x = x; this.trect.y = y; this.trect.w = 100; this.trect.h = 100; //canvas 做法 c.drawSprite("1", this.spritenames[si], this.trect); //等同于下面的两行 //var atlas = lighttool.atlasMgr.Instance().load(c.webgl, "1"); //atlas.draw(c.spriteBatcher, this.spritenames[si], this.trect, this.white); } } //draw font(底层方法) var font = lighttool.fontMgr.Instance().load(c.webgl, "f1"); if (font != null && font.cmap != null) { this.trect.x = 50; this.trect.y = 50; this.trect.w = 50; this.trect.h = 50; font.drawChar(c.spriteBatcher, "古", this.trect, lighttool.spriteColor.white, lighttool.spriteColor.gray); this.trect.x = 100; this.trect.y = 50; font.drawChar(c.spriteBatcher, "老", this.trect, new lighttool.spriteColor(0.1f, 0.8f, 0.2f, 0.8f), new lighttool.spriteColor(1, 1, 1, 0)); } //drawfont canvas 方法 this.trect.x = 50; this.trect.y = 150; this.trect.w = 200; this.trect.h = 50; if (t != null) { c.drawTexture(t, this.trectBtn, lighttool.spriteRect.one, this.btndown ? lighttool.spriteColor.white : new lighttool.spriteColor(1, 1, 1, 0.5f)); } c.drawText("f1", "this is Btn", this.trectBtn, this.btndown ? new lighttool.spriteColor(1, 0, 0, 1) : lighttool.spriteColor.white); this.trect.x = 0; this.trect.y = 0; this.trect.w = 500; this.trect.h = 25; c.drawText("f1", this.showtxt, this.trect); }
public static spriteCanvas CreateScreenCanvas(WebGLRenderingContext webgl, canvasAction useraction) { var el = webgl.Canvas; el.Width = el.ClientWidth; el.Height = el.ClientHeight; var c = new spriteCanvas(webgl, webgl.DrawingBufferWidth, webgl.DrawingBufferHeight); //var asp = range.width / range.height; c.spriteBatcher.matrix = new Float32Array(new float[] { 1.0f * 2 / c.width, 0, 0, 0, //去掉asp的影响 0, 1 * -1 * 2 / c.height, 0, 0, 0, 0, 1, 0, -1, 1, 0, 1 }); c.spriteBatcher.ztest = false;//最前不需要ztest var ua = useraction; Bridge.Html5.Window.SetInterval(() => { webgl.Viewport(0, 0, webgl.DrawingBufferWidth, webgl.DrawingBufferHeight); webgl.Clear(webgl.COLOR_BUFFER_BIT | webgl.DEPTH_BUFFER_BIT); webgl.ClearColor(1.0, 0.0, 1.0, 1.0); c.spriteBatcher.begindraw(); ua.ondraw(c); c.spriteBatcher.enddraw(); //dynamic _webgl = webgl; //_webgl.flush(); //webgl.Flush(); }, 20); Window.AddEventListener("resize", () => { var sel = webgl.Canvas; sel.Width = sel.ClientWidth; sel.Height = sel.ClientHeight; sel.Width = sel.ClientWidth; sel.Height = sel.ClientHeight; c.width = sel.Width; c.height = sel.Height; c.spriteBatcher.matrix = new Float32Array(new float[] { 1.0f * 2 / c.width, 0, 0, 0,//去掉asp的影响 0, 1.0f * -1 * 2 / c.height, 0, 0, 0, 0, 1, 0, -1, 1, 0, 1 }); ////do resize func ua.onresize(c); }); el.OnMouseMove = (ev) => { ua.onpointevent(c, canvaspointevent.POINT_MOVE, (float)ev["offsetX"], (float)ev["offsetY"]); }; el.OnMouseUp = (MouseEvent <HTMLCanvasElement> ev) => { ua.onpointevent(c, canvaspointevent.POINT_UP, (float)ev["offsetX"], (float)ev["offsetY"]); }; el.OnMouseDown = (MouseEvent <HTMLCanvasElement> ev) => { ua.onpointevent(c, canvaspointevent.POINT_DOWN, (float)ev["offsetX"], (float)ev["offsetY"]); }; //scene.onPointerObservable.add((pinfo: BABYLON.PointerInfo, state: BABYLON.EventState) => //{ // var range = scene.getEngine().getRenderingCanvasClientRect(); // //输入 // var e: lighttool.canvaspointevent = lighttool.canvaspointevent.NONE; // if (pinfo.type == BABYLON.PointerEventTypes.POINTERDOWN) // e = lighttool.canvaspointevent.POINT_DOWN; // if (pinfo.type == BABYLON.PointerEventTypes.POINTERMOVE) // e = lighttool.canvaspointevent.POINT_MOVE; // if (pinfo.type == BABYLON.PointerEventTypes.POINTERUP) // e = lighttool.canvaspointevent.POINT_UP; // //缩放到canvas size // var x = pinfo.event.offsetX / range.width * c.width; // var y = pinfo.event.offsetY / range.height * c.height; // var skip: boolean = ua.onpointevent(c, e, x, y); // //对 babylon,来说 2d在这里输入,3d 要 pick 以后咯 // state.skipNextObservers = skip;//是否中断事件 //} //); return(c); }