Skip to content

Commit

Permalink
Fix some label problems caused by dynamicAtlas change. (cocos#3667)
Browse files Browse the repository at this point in the history
* Fix BMFont simulator error.

* Set default cacheAsMap to false.

* Fix some lable issues because of dynamicatlas change.

* Increase the fault tolerance judgment of original.

* Modify the variable name of cacheAsBitmap to batchAsBitmap.

* Add editor support.

* change batch_as_bitmap content
  • Loading branch information
caryliu1999 authored and pandamicro committed Dec 23, 2018
1 parent 37fe71e commit c6cf6bb
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions cocos2d/core/assets/CCSpriteFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ let SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
},

_resetDynamicAtlasFrame () {
if (!this._original) return;
this._rect.x = this._original._x;
this._rect.y = this._original._y;
this._texture = this._original._texture;
Expand Down
36 changes: 21 additions & 15 deletions cocos2d/core/components/CCLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,22 +432,29 @@ let Label = cc.Class({
}
},

_cacheAsBitmap: true,
_batchAsBitmap: false,
/**
* !#en Whether cache label to static texture and draw in dynamicAtlas.
* !#zh 是否将label缓存成静态图像并加入到动态图集.(对于静态文本建议使用该选项,便于批次合并减少drawcall)
* @property {Boolean} cacheAsBitmap
* @property {Boolean} batchAsBitmap
*/
cacheAsBitmap: {
batchAsBitmap: {
get () {
return this._cacheAsBitmap;
return this._batchAsBitmap;
},
set (value) {
if (this._cacheAsBitmap === value) return;
if (this._batchAsBitmap === value) return;

this._cacheAsBitmap = value;
this._batchAsBitmap = value;

if (!this._batchAsBitmap && !(this.font instanceof cc.BitmapFont)) {
this._frame._resetDynamicAtlasFrame();
}
this._activateMaterial(true);
this._updateRenderData();
},
animatable: false,
tooltip: CC_DEV && 'i18n:COMPONENT.label.batch_as_bitmap',
},

_isBold: {
Expand Down Expand Up @@ -533,6 +540,7 @@ let Label = cc.Class({
if (this._assembler !== assembler) {
this._assembler = assembler;
this._renderData = null;
this._frame = null;
}

if (!this._renderData) {
Expand All @@ -545,10 +553,11 @@ let Label = cc.Class({
let font = this.font;
if (font instanceof cc.BitmapFont) {
let spriteFrame = font.spriteFrame;
this._frame = spriteFrame;
let self = this;
let onBMFontTextureLoaded = function () {
// TODO: old texture in material have been released by loader
self._texture = spriteFrame._texture;
self._frame._texture = spriteFrame._texture;
self._activateMaterial(force);

if (force) {
Expand All @@ -567,9 +576,6 @@ let Label = cc.Class({
spriteFrame.ensureLoadTexture();
}
}

// TODO: old texture in material have been released by loader
this._frame = spriteFrame;
}
else {
if (!this._ttfTexture) {
Expand All @@ -580,14 +586,14 @@ let Label = cc.Class({
}
this._assemblerData = this._assembler._getAssemblerData();
this._ttfTexture.initWithElement(this._assemblerData.canvas);

if (!this._frame) {
this._frame = new LabelFrame();
}
}

this._frame._refreshTexture(this._ttfTexture);
if (!this._frame) {
this._frame = new LabelFrame();
}

this._frame._refreshTexture(this._ttfTexture);

this._activateMaterial(force);

if (force) {
Expand Down
1 change: 1 addition & 0 deletions cocos2d/core/renderer/utils/label/label-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ LabelFrame.prototype = {
this._calculateUV();
},
_resetDynamicAtlasFrame () {
if (!this._original) return;
this._rect.x = this._original._x;
this._rect.y = this._original._y;
this._texture = this._original._texture;
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/core/renderer/utils/label/ttf.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ module.exports = {
},

_calDynamicAtlas (comp) {
if(!comp.cacheAsBitmap) return;
if(!comp.batchAsBitmap) return;

if (!comp._frame._original) {
comp._frame.setRect(cc.rect(0, 0, _canvas.width, _canvas.height));
Expand Down
3 changes: 2 additions & 1 deletion editor/i18n/en/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ module.exports = {
"overflow": "Text layout modes: \n 1. CLAMP: text nodes outside the bounding box will be truncated \n 2. SHRINK: automatically shrink text box according to the constraint node \n 3. RESIZE: Automatically updates the Node based on heightof the text.",
"wrap": "Wrap text?",
"font": "What font to use",
"system_font": "Whether to use the system default font"
"system_font": "Whether to use the system default font",
'batch_as_bitmap': 'Whether to set to static text for batch merging, select this will add text to the dynamic atlas for batch merging, but can not dynamically modify text content frequently'
},
"progress": {
"bar_sprite": "A progress bar is displayed with the Sprite node that can dynamically change the size",
Expand Down
1 change: 1 addition & 0 deletions editor/i18n/zh/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ module.exports = {
'wrap': '是否允许自动换行',
'font': 'Label 使用的字体资源',
'system_font': '是否使用系统默认字体,选中此项会将 file 属性置空',
'batch_as_bitmap': '是否设置为静态文本进行批次合并,选中此项将会把文本加入动态图集进行批次合并,但是不能频繁动态修改文本内容'
},
'progress': {
'bar_sprite': '进度条显示用的 Sprite 节点,可以动态改变尺寸',
Expand Down

0 comments on commit c6cf6bb

Please sign in to comment.