UNPKG

@koreez/phaser3-ninepatch

Version:
141 lines 6.05 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var IPatchesConfig_1 = require("./IPatchesConfig"); var NinePatch = (function (_super) { __extends(NinePatch, _super); function NinePatch(scene, x, y, width, height, key, frame, config) { var _this = _super.call(this, scene, x, y) || this; _this.config = config || _this.scene.cache.custom.ninePatch.get(frame ? "" + frame : key); IPatchesConfig_1.normalizePatchesConfig(_this.config); _this.setSize(width, height); _this.setTexture(key, frame); return _this; } NinePatch.prototype.resize = function (width, height) { width = Math.round(width); height = Math.round(height); if (!this.config) { return this; } if (this.width === width && this.height === height) { return this; } width = Math.max(width, this.config.left + this.config.right); height = Math.max(height, this.config.top + this.config.bottom); this.setSize(width, height); this.drawPatches(); return this; }; NinePatch.prototype.setTexture = function (key, frame) { this.originTexture = this.scene.textures.get(key); this.setFrame(frame); return this; }; NinePatch.prototype.setFrame = function (frame) { this.originFrame = this.originTexture.frames[frame] || this.originTexture.frames[NinePatch.__BASE]; this.createPatches(); this.drawPatches(); return this; }; NinePatch.prototype.setSize = function (width, height) { _super.prototype.setSize.call(this, width, height); this.finalXs = [0, this.config.left, this.width - this.config.right, this.width]; this.finalYs = [0, this.config.top, this.height - this.config.bottom, this.height]; return this; }; NinePatch.prototype.setTint = function (tint) { this.tint = tint; return this; }; NinePatch.prototype.setTintFill = function (tint) { this.tint = tint; this.tintFill = true; return this; }; NinePatch.prototype.clearTint = function () { this.each(function (patch) { return patch.clearTint(); }); this.internalTint = undefined; return this; }; Object.defineProperty(NinePatch.prototype, "tintFill", { get: function () { return this.first && this.first.tintFill; }, set: function (value) { this.each(function (patch) { return (patch.tintFill = value); }); }, enumerable: true, configurable: true }); Object.defineProperty(NinePatch.prototype, "tint", { set: function (value) { this.each(function (patch) { return patch.setTint(value); }); this.internalTint = value; }, enumerable: true, configurable: true }); Object.defineProperty(NinePatch.prototype, "isTinted", { get: function () { return this.first && this.first.isTinted; }, enumerable: true, configurable: true }); NinePatch.prototype.createPatches = function () { var textureXs = [0, this.config.left, this.originFrame.width - this.config.right, this.originFrame.width]; var textureYs = [0, this.config.top, this.originFrame.height - this.config.bottom, this.originFrame.height]; var patchIndex = 0; for (var yi = 0; yi < 3; yi++) { for (var xi = 0; xi < 3; xi++) { this.createPatchFrame(this.getPatchNameByIndex(patchIndex), textureXs[xi], textureYs[yi], textureXs[xi + 1] - textureXs[xi], textureYs[yi + 1] - textureYs[yi]); ++patchIndex; } } }; NinePatch.prototype.drawPatches = function () { var tintFill = this.tintFill; this.removeAll(true); var patchIndex = 0; for (var yi = 0; yi < 3; yi++) { for (var xi = 0; xi < 3; xi++) { var patch = this.originTexture.frames[this.getPatchNameByIndex(patchIndex)]; var patchImg = new Phaser.GameObjects.Image(this.scene, 0, 0, patch.texture.key, patch.name); patchImg.setOrigin(0); patchImg.setPosition(this.finalXs[xi] - this.width * this.originX, this.finalYs[yi] - this.height * this.originY); patchImg.setScale((this.finalXs[xi + 1] - this.finalXs[xi]) / patch.width, (this.finalYs[yi + 1] - this.finalYs[yi]) / patch.height); this.add(patchImg); patchImg.setTint(this.internalTint); patchImg.tintFill = tintFill; ++patchIndex; } } }; NinePatch.prototype.createPatchFrame = function (patch, x, y, width, height) { if (this.originTexture.frames.hasOwnProperty(patch)) { return; } this.originTexture.add(patch, this.originFrame.sourceIndex, this.originFrame.cutX + x, this.originFrame.cutY + y, width, height); }; NinePatch.prototype.getPatchNameByIndex = function (index) { return this.originFrame.name + "|" + NinePatch.patches[index]; }; NinePatch.__BASE = "__BASE"; NinePatch.patches = ["[0][0]", "[1][0]", "[2][0]", "[0][1]", "[1][1]", "[2][1]", "[0][2]", "[1][2]", "[2][2]"]; return NinePatch; }(Phaser.GameObjects.Container)); exports.NinePatch = NinePatch; //# sourceMappingURL=NinePatch.js.map