@phaserjs/phaser
Version:
80 lines (79 loc) • 2.27 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
import { BindingQueue } from "../renderer/BindingQueue";
import { Frame } from "./Frame";
import { SetFrameSize } from "./SetFrameSize";
export class Texture {
constructor(image, width, height, glConfig) {
__publicField(this, "key", "");
__publicField(this, "locked", true);
__publicField(this, "width");
__publicField(this, "height");
__publicField(this, "image");
__publicField(this, "binding");
__publicField(this, "firstFrame");
__publicField(this, "frames");
__publicField(this, "data");
if (image) {
width = image.width;
height = image.height;
}
this.image = image;
this.width = width;
this.height = height;
this.frames = new Map();
this.data = {};
this.addFrame("__BASE", 0, 0, width, height);
BindingQueue.add(this, glConfig);
}
addFrame(key, x, y, width, height) {
if (this.frames.has(key)) {
return null;
}
const frame = new Frame(this, key, x, y, width, height);
this.frames.set(key, frame);
if (!this.firstFrame || this.firstFrame.key === "__BASE") {
this.firstFrame = frame;
}
return frame;
}
getFrame(key) {
if (!key) {
return this.firstFrame;
}
if (key instanceof Frame) {
key = key.key;
}
let frame = this.frames.get(key);
if (!frame) {
console.warn(`Frame missing: ${key}`);
frame = this.firstFrame;
}
return frame;
}
setSize(width, height) {
this.width = width;
this.height = height;
const frame = this.frames.get("__BASE");
SetFrameSize(frame, width, height);
}
update(image, glConfig) {
this.image = image;
this.setSize(image.width, image.height);
BindingQueue.add(this, glConfig);
}
destroy() {
if (this.binding) {
this.binding.destroy();
}
this.frames.clear();
this.binding = null;
this.data = null;
this.image = null;
this.firstFrame = null;
}
}