Spaces:
Sleeping
Sleeping
File size: 3,215 Bytes
b593f0b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | <%
const {
arrayClass,
members,
size,
usedTypes,
hasAnchorPoint,
layoutClass,
includeStructAccessors
} = locals
const StructTypeClass = arrayClass.replace('Array', 'Struct');
const StructArrayClass = arrayClass;
const StructArrayLayoutClass = layoutClass;
-%>
<%
// collect components
const components = [];
for (const member of members) {
for (let c = 0; c < member.components; c++) {
let name = member.name;
if (member.components > 1) {
name += c;
}
components.push({name, member, component: c});
}
}
// exceptions for which we generate accessors on the array rather than a separate struct for performance
const useComponentGetters = StructArrayClass === 'GlyphOffsetArray' || StructArrayClass === 'SymbolLineVertexArray';
if (includeStructAccessors && !useComponentGetters) {
-%>
class <%=StructTypeClass%> extends Struct {
_structArray: <%=StructArrayClass%>;
<%
// property declarations
for (const {name} of components) {-%>
<%=name%>: number;
<% }
if (hasAnchorPoint) { -%>
anchorPoint: Point;
<% } -%>
<%
for (const {name, member, component} of components) {
const elementOffset = `this._pos${member.size.toFixed(0)}`;
const componentOffset = (member.offset / member.size + component).toFixed(0);
const index = `${elementOffset} + ${componentOffset}`;
const componentAccess = `this._structArray.${member.view}[${index}]`;
-%>
get <%=name%>() { return <%=componentAccess%>; }
<%
// generate setters for properties that are updated during runtime symbol placement; others are read-only
if (name === 'crossTileID' || name === 'placedOrientation' || name === 'hidden') {
-%>
set <%=name%>(x: number) { <%=componentAccess%> = x; }
<%
}
}
// Special case used for the CollisionBoxArray type
if (hasAnchorPoint) {
-%>
get anchorPoint() { return new Point(this.anchorPointX, this.anchorPointY); }
<%
}
-%>
}
<%=StructTypeClass%>.prototype.size = <%=size%>;
export type <%=StructTypeClass.replace('Struct', '')%> = <%=StructTypeClass%>;
<%
} // end 'if (includeStructAccessors)'
-%>
/**
* @private
*/
export class <%=StructArrayClass%> extends <%=StructArrayLayoutClass%> {
<%
if (useComponentGetters) {
for (const member of members) {
for (let c = 0; c < member.components; c++) {
if (!includeStructAccessors) continue;
let name = `get${member.name}`;
if (member.components > 1) {
name += c;
}
const componentOffset = (member.offset / member.size + c).toFixed(0);
const componentStride = size / member.size;
-%>
<%=name%>(index: number) { return this.<%=member.view%>[index * <%=componentStride%> + <%=componentOffset%>]; }
<%
}
}
} else if (includeStructAccessors) { // get(i)
-%>
/**
* Return the <%=StructTypeClass%> at the given location in the array.
* @param {number} index The index of the element.
* @private
*/
get(index: number): <%=StructTypeClass%> {
assert(!this.isTransferred);
return new <%=StructTypeClass%>(this, index);
}
<%
}
-%>
}
register('<%=StructArrayClass%>', <%=StructArrayClass%>);
|