/*
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { Label, HorizontalTextAlignment, VerticalTextAlignment } from '../2d/components/label';
import codec from '../../external/compression/ZipUtils.js';
import zlib from '../../external/compression/zlib.min.js';
import { SAXParser } from '../asset/asset-manager/plist-parser';
import {
GID, MixedGID, Orientation, PropertiesInfo, RenderOrder, StaggerAxis, StaggerIndex, TiledAnimation, TiledAnimationType,
TileFlag, TMXImageLayerInfo, TMXLayerInfo, TMXObject, TMXObjectGroupInfo, TMXObjectType, TMXTilesetInfo,
} from './tiled-types';
import { Color, errorID, logID, Size, Vec2 } from '../core';
import { SpriteFrame } from '../2d/assets';
function uint8ArrayToUint32Array (uint8Arr: Uint8Array): null | Uint32Array | number[] {
if (uint8Arr.length % 4 !== 0) return null;
const arrLen = uint8Arr.length / 4;
const retArr = window.Uint32Array ? new Uint32Array(arrLen) : [];
for (let i = 0; i < arrLen; i++) {
const offset = i * 4;
retArr[i] = uint8Arr[offset] + uint8Arr[offset + 1] * (1 << 8) + uint8Arr[offset + 2] * (1 << 16) + uint8Arr[offset + 3] * (1 << 24);
}
return retArr;
}
function strToHAlign (value): HorizontalTextAlignment {
const hAlign = Label.HorizontalAlign;
switch (value) {
case 'center':
return hAlign.CENTER;
case 'right':
return hAlign.RIGHT;
default:
return hAlign.LEFT;
}
}
function strToVAlign (value): VerticalTextAlignment {
const vAlign = Label.VerticalAlign;
switch (value) {
case 'center':
return vAlign.CENTER;
case 'bottom':
return vAlign.BOTTOM;
default:
return vAlign.TOP;
}
}
function strToColor (value: string): Color {
if (!value) {
return new Color(0, 0, 0, 255);
}
value = (value.indexOf('#') !== -1) ? value.substring(1) : value;
if (value.length === 8) {
const a = parseInt(value.substr(0, 2), 16) || 255;
const r = parseInt(value.substr(2, 2), 16) || 0;
const g = parseInt(value.substr(4, 2), 16) || 0;
const b = parseInt(value.substr(6, 2), 16) || 0;
return new Color(r, g, b, a);
} else {
const r = parseInt(value.substr(0, 2), 16) || 0;
const g = parseInt(value.substr(2, 2), 16) || 0;
const b = parseInt(value.substr(4, 2), 16) || 0;
return new Color(r, g, b, 255);
}
}
function getPropertyList (node: Element, map?: PropertiesInfo): PropertiesInfo {
const res: any[] = [];
const properties = node.getElementsByTagName('properties');
for (let i = 0; i < properties.length; ++i) {
const property = properties[i].getElementsByTagName('property');
for (let j = 0; j < property.length; ++j) {
res.push(property[j]);
}
}
map = map || ({} as any);
for (let i = 0; i < res.length; i++) {
const element = res[i];
const name = element.getAttribute('name');
const type = element.getAttribute('type') || 'string';
let value = element.getAttribute('value');
if (type === 'int') {
value = parseInt(value);
} else if (type === 'float') {
value = parseFloat(value);
} else if (type === 'bool') {
value = value === 'true';
} else if (type === 'color') {
value = strToColor(value);
}
map![name] = value;
}
return map!;
}
/**
*
cc.TMXMapInfo contains the information about the map like:
* - Map orientation (hexagonal, isometric or orthogonal)
* - Tile size
* - Map size
*
* And it also contains:
* - Layers (an array of TMXLayerInfo objects)
* - Tilesets (an array of TMXTilesetInfo objects)
* - ObjectGroups (an array of TMXObjectGroupInfo objects)
*
* This information is obtained from the TMX file.
* @class TMXMapInfo
*/
export class TMXMapInfo {
/**
* Properties of the map info.
* @property {Array} properties
*/
properties: PropertiesInfo = {} as any;
/**
* Map orientation.
* @property {Number} orientation
*/
orientation: Orientation | null = null;
/**
* Parent element.
* @property {Object} parentElement
*/
parentElement: Record | null = null;
/**
* Parent GID.
* @property {Number} parentGID
*/
parentGID: MixedGID = 0 as unknown as any;
/**
* Layer attributes.
* @property {Object} layerAttrs
*/
layerAttrs = 0;
/**
* Is reading storing characters stream.
* @property {Boolean} storingCharacters
*/
storingCharacters = false;
/**
* Current string stored from characters stream.
* @property {String} currentString
*/
currentString: string | null = null;
renderOrder: RenderOrder = RenderOrder.RightDown;
protected _supportVersion = [1, 4, 0];
protected _objectGroups: TMXObjectGroupInfo[] = [];
protected _allChildren: (TMXLayerInfo | TMXImageLayerInfo | TMXObjectGroupInfo)[] = [];
protected _mapSize = new Size(0, 0);
get mapSize () { return this._mapSize; }
protected _tileSize = new Size(0, 0);
get tileSize () { return this._tileSize; }
protected _layers: TMXLayerInfo[] = [];
protected _tilesets: TMXTilesetInfo[] = [];
protected _imageLayers: TMXImageLayerInfo[] = [];
protected _tileProperties: Map = new Map();
protected _tileAnimations: TiledAnimationType = {} as any;
protected _tsxContentMap: { [key: string]: string } | null = null;
// map of textures indexed by name
protected _spriteFrameMap: { [key: string]: SpriteFrame } | null = null;
protected _spfSizeMap: { [key: string]: Size } = {};
// hex map values
protected _staggerAxis: StaggerAxis | null = null;
protected _staggerIndex: StaggerIndex | null = null;
protected _hexSideLength = 0;
protected _imageLayerSPF: { [key: string]: SpriteFrame } | null = null;
constructor (tmxFile: string, tsxContentMap: { [key: string]: string }, spfTexturesMap: { [key: string]: SpriteFrame },
textureSizes: { [key: string]: Size }, imageLayerTextures: { [key: string]: SpriteFrame }) {
this.initWithXML(tmxFile, tsxContentMap, spfTexturesMap, textureSizes, imageLayerTextures);
}
/* Gets Map orientation.
* @return {Number}
*/
getOrientation () {
return this.orientation;
}
/**
* Set the Map orientation.
* @param {Number} value
*/
setOrientation (value: Orientation) {
this.orientation = value;
}
/**
* Gets the staggerAxis of map.
* @return {TiledMap.StaggerAxis}
*/
getStaggerAxis () {
return this._staggerAxis;
}
/**
* Set the staggerAxis of map.
* @param {TiledMap.StaggerAxis} value
*/
setStaggerAxis (value: StaggerAxis) {
this._staggerAxis = value;
}
/**
* Gets stagger index
* @return {TiledMap.StaggerIndex}
*/
getStaggerIndex () {
return this._staggerIndex;
}
/**
* Set the stagger index.
* @param {TiledMap.StaggerIndex} value
*/
setStaggerIndex (value) {
this._staggerIndex = value;
}
/**
* Gets Hex side length.
* @return {Number}
*/
getHexSideLength () {
return this._hexSideLength;
}
/**
* Set the Hex side length.
* @param {Number} value
*/
setHexSideLength (value: number) {
this._hexSideLength = value;
}
/**
* Map width & height
* @return {Size}
*/
getMapSize () {
return new Size(this._mapSize.width, this._mapSize.height);
}
/**
* Map width & height
* @param {Size} value
*/
setMapSize (value: Size) {
this._mapSize.width = value.width;
this._mapSize.height = value.height;
}
get mapWidth () {
return this._mapSize.width;
}
set mapWidth (width: number) {
this._mapSize.width = width;
}
get mapHeight () {
return this._mapSize.height;
}
set mapHeight (height: number) {
this._mapSize.height = height;
}
/**
* Tiles width & height
* @return {Size}
*/
getTileSize () {
return new Size(this._tileSize.width, this._tileSize.height);
}
/**
* Tiles width & height
* @param {Size} value
*/
setTileSize (value: Size) {
this._tileSize.width = value.width;
this._tileSize.height = value.height;
}
get tileWidth () {
return this._tileSize.width;
}
set tileWidth (width) {
this._tileSize.width = width;
}
/**
* Height of a tile
*/
get tileHeight () {
return this._tileSize.height;
}
set tileHeight (height: number) {
this._tileSize.height = height;
}
/**
* Layers
* @return {Array}
*/
getLayers () {
return this._layers;
}
/**
* Layers
* @param {cc.TMXLayerInfo} value
*/
setLayers (value: TMXLayerInfo) {
this._allChildren.push(value);
this._layers.push(value);
}
/**
* ImageLayers
* @return {Array}
*/
getImageLayers () {
return this._imageLayers;
}
/**
* ImageLayers
* @param {cc.TMXImageLayerInfo} value
*/
setImageLayers (value: TMXImageLayerInfo) {
this._allChildren.push(value);
this._imageLayers.push(value);
}
/**
* tilesets
* @return {Array}
*/
getTilesets () {
return this._tilesets;
}
/**
* tilesets
* @param {cc.TMXTilesetInfo} value
*/
setTilesets (value: TMXTilesetInfo) {
this._tilesets.push(value);
}
/**
* ObjectGroups
* @return {Array}
*/
getObjectGroups () {
return this._objectGroups;
}
/**
* ObjectGroups
* @param {cc.TMXObjectGroup} value
*/
setObjectGroups (value: TMXObjectGroupInfo) {
this._allChildren.push(value);
this._objectGroups.push(value);
}
getAllChildren () {
return this._allChildren;
}
/**
* parent element
* @return {Object}
*/
getParentElement () {
return this.parentElement;
}
/**
* parent element
* @param {Object} value
*/
setParentElement (value) {
this.parentElement = value;
}
/**
* parent GID
* @return {Number}
*/
getParentGID () {
return this.parentGID;
}
/**
* parent GID
* @param {Number} value
*/
setParentGID (value) {
this.parentGID = value;
}
/**
* Layer attribute
* @return {Object}
*/
getLayerAttribs () {
return this.layerAttrs;
}
/**
* Layer attribute
* @param {Object} value
*/
setLayerAttribs (value) {
this.layerAttrs = value;
}
/**
* Is reading storing characters stream
* @return {Boolean}
*/
getStoringCharacters () {
return this.storingCharacters;
}
/**
* Is reading storing characters stream
* @param {Boolean} value
*/
setStoringCharacters (value) {
this.storingCharacters = value;
}
/**
* Properties
* @return {Array}
*/
getProperties () {
return this.properties;
}
/**
* Properties
* @param {object} value
*/
setProperties (value) {
this.properties = value;
}
/**
* initializes a TMX format with an XML string and a TMX resource path
* @param {String} tmxString
* @param {Object} tsxMap
* @param {Object} spfTextureMap
* @return {Boolean}
*/
initWithXML (tmxString: string, tsxMap: { [key: string]: string }, spfTextureMap: { [key: string]: SpriteFrame },
textureSizes: { [key: string]: Size }, imageLayerTextures: { [key: string]: SpriteFrame }) {
this._tilesets.length = 0;
this._layers.length = 0;
this._imageLayers.length = 0;
this._tsxContentMap = tsxMap;
this._spriteFrameMap = spfTextureMap;
this._imageLayerSPF = imageLayerTextures;
this._spfSizeMap = textureSizes;
this._objectGroups.length = 0;
this._allChildren.length = 0;
this.properties = {} as any;
this._tileProperties = new Map();
this._tileAnimations = new Map();
// tmp vars
this.currentString = '';
this.storingCharacters = false;
this.layerAttrs = TMXLayerInfo.ATTRIB_NONE;
this.parentElement = null;
return this.parseXMLString(tmxString);
}
/**
* Initializes parsing of an XML string, either a tmx (Map) string or tsx (Tileset) string
* @param {String} xmlString
* @param {Number} tilesetFirstGid
* @return {Element}
*/
parseXMLString (xmlStr: string, tilesetFirstGid?: number) {
const parser = new SAXParser();
const mapXML: Document = parser.parse(xmlStr);
let i: number;
// PARSE