Skip to content

Commit 45a2470

Browse files
committed
bug fixes
1 parent 0393964 commit 45a2470

4 files changed

Lines changed: 64 additions & 5 deletions

File tree

src/components/pg/poco/__examples__/basic/basic.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,44 @@ export default class XPgPocoBasic extends HTMLElement {
4444
this.$code.value = extractBody(demos[0].run);
4545
this.$run.addEventListener('click', this.handleRun.bind(this));
4646
this.$error.style.display = 'none';
47+
// Load testing resources
48+
this.$poco.setResource('colorBitmap.bmp', 40, 40, (ctx, w, h) => {
49+
ctx.fillStyle = "#fff"; ctx.fillRect(0, 0, w, h);
50+
ctx.fillStyle = "#000"; ctx.fillRect(4, 4, 32, 32);
51+
ctx.fillStyle = "#fff"; ctx.fillRect(8, 8, 24, 24);
52+
ctx.fillStyle = "#000"; ctx.fillRect(12, 12, 16, 16);
53+
ctx.fillStyle = "#fff"; ctx.fillRect(16, 16, 8, 8);
54+
});
55+
this.$poco.setResource('monoBitmap.bmp', 32, 32, (ctx, w, h) => {
56+
ctx.fillStyle = "#fff"; ctx.fillRect(0, 0, w, h);
57+
ctx.strokeStyle = "#000"; ctx.lineWidth = 1;
58+
ctx.strokeRect(0.5, 0.5, w - 1, h - 1);
59+
ctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo(w / 2, h / 2); ctx.lineTo(w, 0); ctx.stroke();
60+
ctx.beginPath(); ctx.moveTo(0, h); ctx.lineTo(w / 2, h / 2); ctx.lineTo(w, h); ctx.stroke();
61+
});
62+
this.$poco.setResource('grayBitmap.bmp', 32, 32, (ctx, w, h) => {
63+
const grad = ctx.createRadialGradient(w / 2, h / 2, 2, w / 2, h / 2, w / 2);
64+
grad.addColorStop(0, "#fff");
65+
grad.addColorStop(1, "#000");
66+
ctx.fillStyle = grad;
67+
ctx.fillRect(0, 0, w, h);
68+
});
69+
this.$poco.setResource('circleMask.bmp', 40, 40, (ctx, w, h) => {
70+
ctx.fillStyle = "#000"; ctx.fillRect(0, 0, w, h);
71+
const grad = ctx.createRadialGradient(w / 2, h / 2, 0, w / 2, h / 2, w / 2);
72+
grad.addColorStop(0, "#fff");
73+
grad.addColorStop(1, "#000");
74+
ctx.fillStyle = grad;
75+
ctx.beginPath(); ctx.arc(w / 2, h / 2, w / 2, 0, Math.PI * 2); ctx.fill();
76+
});
77+
this.$poco.setResource('patternBitmap.bmp', 30, 30, (ctx, w, h) => {
78+
const size = 10;
79+
for (let r = 0; r < h; r += size)
80+
for (let c = 0; c < w; c += size) {
81+
ctx.fillStyle = ((r + c) / size % 2 === 0) ? "#000" : "#fff";
82+
ctx.fillRect(c, r, size, size);
83+
}
84+
});
4785
}
4886

4987
handleRun() {

src/components/pg/poco/__examples__/basic/examples.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,11 @@ export const demos = [
177177
run({ poco }) {
178178
const black = poco.makeColor(0, 0, 0);
179179
const white = poco.makeColor(255, 255, 255);
180+
const gray = poco.makeColor(180, 180, 180);
180181
poco.begin();
181182
poco.fillRectangle(white, 0, 0, poco.width, poco.height);
182183
poco.clip(20, 20, poco.width - 40, poco.height - 40);
183-
poco.fillRectangle(black, 0, 0, poco.width, poco.height);
184+
poco.fillRectangle(gray, 0, 0, poco.width, poco.height);
184185
poco.clip(20, 20, 160, 100);
185186
poco.fillRectangle(black, 0, 0, poco.width, poco.height);
186187
poco.clip();

src/components/pg/poco/mockPoco.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,16 +515,28 @@ export class MockPoco {
515515
}
516516
}
517517

518-
const resources = new Map<string, string>();
518+
const resources = new Map<string, MockBitmap>();
519519

520520
export class MockResource {
521-
constructor(file) {
521+
static set(fileName: string, bitmap: MockBitmap) {
522+
resources.set(fileName, bitmap);
523+
}
524+
525+
#file;
526+
constructor(fileName: string) {
527+
if (!resources.has(fileName)) {
528+
throw new Error(`Unknown mock resource "${fileName}". Add with Resource.set(fileName, MockBitmap.create()).`);
529+
}
530+
this.#file = fileName;
531+
}
522532

533+
get mock() {
534+
return resources.get(this.#file);
523535
}
524536
}
525537

526538
export function MockParseBMP(buffer: any) {
527-
return buffer;
539+
return buffer.mock;
528540
}
529541

530542
class MockBMFont {

src/components/pg/poco/poco.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, Prop, Part } from '@pictogrammers/element';
22

3-
import { MockFont, MockPoco, MockResource, MockParseBMP, MockParseBMF } from './mockPoco';
3+
import { MockBitmap, MockPoco, MockResource, MockParseBMP, MockParseBMF } from './mockPoco';
44

55
import template from './poco.html';
66
import style from './poco.css';
@@ -41,4 +41,12 @@ export default class PgPoco extends HTMLElement {
4141
get parseBMF() {
4242
return MockParseBMF;
4343
}
44+
45+
setResource(
46+
fileName: string,
47+
width: number,
48+
height: number,
49+
create: (ctx: CanvasRenderingContext2D, w: number, h: number) => void) {
50+
MockResource.set(fileName, MockBitmap.create(width, height, create));
51+
}
4452
}

0 commit comments

Comments
 (0)