Visitar URL original
Text-element render sub-range (#1585) · selectgithub/engine@e5b3344 · GitHub
Skip to content

Commit e5b3344

Browse files
Donovan Hutchencewilleastcott
authored andcommitted
Text-element render sub-range (playcanvas#1585)
* added text-element sub-range * using two separate properties for range start and end * expose range start and end in ElementComponent * small doc update
1 parent 30dd7e9 commit e5b3344

2 files changed

Lines changed: 90 additions & 23 deletions

File tree

‎src/framework/components/element/component.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ Object.assign(pc, function () {
102102
* @property {Number} batchGroupId Assign element to a specific batch group (see {@link pc.BatchGroup}). Default value is -1 (no group).
103103
* @property {Array} layers An array of layer IDs ({@link pc.Layer#id}) to which this element should belong.
104104
* Don't push/pop/splice or modify this array, if you want to change it - set a new one instead.
105-
* @property {Boolean} enableMarkup Flag for enabling markup processing
105+
* @property {Boolean} enableMarkup Flag for enabling markup processing. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
106+
* @property {Number} rangeStart Index of the first character to render. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
107+
* @property {Number} rangeEnd Index of the last character to render. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
106108
*/
107109
var ElementComponent = function ElementComponent(system, entity) {
108110
pc.Component.call(this, system, entity);
@@ -1536,6 +1538,8 @@ Object.assign(pc, function () {
15361538
_define("shadowColor");
15371539
_define("shadowOffset");
15381540
_define("enableMarkup");
1541+
_define("rangeStart");
1542+
_define("rangeEnd");
15391543

15401544
return {
15411545
ElementComponent: ElementComponent

‎src/framework/components/element/text-element.js‎

Lines changed: 85 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ Object.assign(pc, function () {
116116
this._system.app.i18n.on('set:locale', this._onLocaleSet, this);
117117
this._system.app.i18n.on('data:add', this._onLocalizationData, this);
118118
this._system.app.i18n.on('data:remove', this._onLocalizationData, this);
119+
120+
// substring render range
121+
this._rangeStart = 0;
122+
this._rangeEnd = 0;
119123
};
120124

121125
var LINE_BREAK_CHAR = /^[\r\n]$/;
@@ -330,28 +334,7 @@ Object.assign(pc, function () {
330334
this._symbolColors = null;
331335
}
332336

333-
var charactersPerTexture = {};
334-
335-
var char, info, map;
336-
for (i = 0, len = this._symbols.length; i < len; i++) {
337-
char = this._symbols[i];
338-
info = this._font.data.chars[char];
339-
// if char is missing use 'space' or first char in map
340-
if (!info) {
341-
if (this._font.data.chars[' ']) {
342-
info = this._font.data.chars[' '];
343-
} else {
344-
info = this._font.data.chars[Object.keys(this._font.data.chars)[0]];
345-
}
346-
}
347-
348-
map = info.map;
349-
350-
if (!charactersPerTexture[map])
351-
charactersPerTexture[map] = 0;
352-
353-
charactersPerTexture[map]++;
354-
}
337+
var charactersPerTexture = this._calculateCharsPerTexture();
355338

356339
var removedModel = false;
357340

@@ -492,6 +475,11 @@ Object.assign(pc, function () {
492475
}
493476

494477
this._updateMeshes();
478+
479+
// update render range
480+
this._rangeStart = 0;
481+
this._rangeEnd = this._symbols.length;
482+
this._updateRenderRange();
495483
},
496484

497485
_removeMeshInstance: function (meshInstance) {
@@ -1120,6 +1108,53 @@ Object.assign(pc, function () {
11201108
_shouldAutoFit: function () {
11211109
return this._autoFitWidth && !this._autoWidth ||
11221110
this._autoFitHeight && !this._autoHeight;
1111+
},
1112+
1113+
// calculate the number of characters per texture up to, but not including
1114+
// the specified symbolIndex
1115+
_calculateCharsPerTexture: function (symbolIndex) {
1116+
var charactersPerTexture = {};
1117+
1118+
if (symbolIndex === undefined) {
1119+
symbolIndex = this._symbols.length;
1120+
}
1121+
1122+
var i, len, char, info, map;
1123+
for (i = 0, len = symbolIndex; i < len; i++) {
1124+
char = this._symbols[i];
1125+
info = this._font.data.chars[char];
1126+
if (!info) {
1127+
// if char is missing use 'space' or first char in map
1128+
info = this._font.data.chars[' '];
1129+
if (!info) {
1130+
// otherwise if space is also not present use the first character in the
1131+
// set
1132+
info = this._font.data.chars[Object.keys(this._font.data.chars)[0]];
1133+
}
1134+
}
1135+
1136+
map = info.map;
1137+
if (!charactersPerTexture[map]) {
1138+
charactersPerTexture[map] = 1;
1139+
} else {
1140+
charactersPerTexture[map]++;
1141+
}
1142+
}
1143+
return charactersPerTexture;
1144+
},
1145+
1146+
_updateRenderRange: function () {
1147+
var startChars = this._rangeStart === 0 ? 0 : this._calculateCharsPerTexture(this._rangeStart);
1148+
var endChars = this._rangeEnd === 0 ? 0 : this._calculateCharsPerTexture(this._rangeEnd);
1149+
1150+
var i, len;
1151+
for (i = 0, len = this._meshInfo.length; i < len; i++) {
1152+
var start = startChars[i] || 0;
1153+
var end = endChars[i] || 0;
1154+
var mesh = this._meshInfo[i].meshInstance.mesh;
1155+
mesh.primitive[0].base = start * 3 * 2;
1156+
mesh.primitive[0].count = (end - start) * 3 * 2;
1157+
}
11231158
}
11241159
});
11251160

@@ -1752,6 +1787,34 @@ Object.assign(pc, function () {
17521787
}
17531788
});
17541789

1790+
Object.defineProperty(TextElement.prototype, 'rangeStart', {
1791+
get: function () {
1792+
return this._rangeStart;
1793+
},
1794+
set: function (rangeStart) {
1795+
rangeStart = Math.max(0, Math.min(rangeStart, this._symbols.length));
1796+
1797+
if (rangeStart !== this._rangeStart) {
1798+
this._rangeStart = rangeStart;
1799+
this._updateRenderRange();
1800+
}
1801+
}
1802+
});
1803+
1804+
Object.defineProperty(TextElement.prototype, 'rangeEnd', {
1805+
get: function () {
1806+
return this._rangeEnd;
1807+
},
1808+
set: function (rangeEnd) {
1809+
rangeEnd = Math.max(this._rangeStart, Math.min(rangeEnd, this._symbols.length));
1810+
1811+
if (rangeEnd !== this._rangeEnd) {
1812+
this._rangeEnd = rangeEnd;
1813+
this._updateRenderRange();
1814+
}
1815+
}
1816+
});
1817+
17551818
return {
17561819
TextElement: TextElement
17571820
};

0 commit comments

Comments
 (0)