mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
fix fireball/issues/7405 (#2754)
* fix fireball/issues/7405 * refine code
This commit is contained in:
@@ -164,6 +164,7 @@ let Label = cc.Class({
|
||||
}
|
||||
|
||||
this._actualFontSize = 0;
|
||||
this._assemblerData = null;
|
||||
},
|
||||
|
||||
editor: CC_EDITOR && {
|
||||
@@ -475,6 +476,11 @@ let Label = cc.Class({
|
||||
this._activateMaterial();
|
||||
},
|
||||
|
||||
onDestroy () {
|
||||
this._assembler._resetAssemblerData && this._assembler._resetAssemblerData(this._assemblerData);
|
||||
this._assemblerData = null;
|
||||
},
|
||||
|
||||
_checkStringEmpty () {
|
||||
this.markForRender(!!this.string);
|
||||
},
|
||||
@@ -514,8 +520,8 @@ let Label = cc.Class({
|
||||
}
|
||||
else {
|
||||
this._texture = new cc.Texture2D();
|
||||
let _assemblerData = this._assembler.getAssemblerData();
|
||||
this._texture.initWithElement(_assemblerData.canvas);
|
||||
this._assemblerData = this._assembler._getAssemblerData();
|
||||
this._texture.initWithElement(this._assemblerData.canvas);
|
||||
}
|
||||
|
||||
material.texture = this._texture;
|
||||
|
||||
@@ -33,8 +33,6 @@ const Overflow = Label.Overflow;
|
||||
const TextUtils = utils.TextUtils;
|
||||
const CustomFontLoader = utils.CustomFontLoader;
|
||||
|
||||
let _comp = null;
|
||||
|
||||
let _context = null;
|
||||
let _canvas = null;
|
||||
let _texture = null;
|
||||
@@ -71,56 +69,89 @@ let _isUnderline = false;
|
||||
|
||||
let _sharedLabelData;
|
||||
|
||||
//
|
||||
let _canvasPool = {
|
||||
pool: [],
|
||||
get () {
|
||||
let data = this.pool.pop();
|
||||
|
||||
if (!data) {
|
||||
let canvas = document.createElement("canvas");
|
||||
let context = canvas.getContext("2d");
|
||||
data = {
|
||||
canvas: canvas,
|
||||
context: context
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
},
|
||||
put (canvas) {
|
||||
if (this.pool.length >= 32) {
|
||||
return;
|
||||
}
|
||||
this.pool.push(canvas);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
|
||||
getAssemblerData () {
|
||||
if (!_sharedLabelData) {
|
||||
let labelCanvas = document.createElement("canvas");
|
||||
_sharedLabelData = {
|
||||
canvas: labelCanvas,
|
||||
context: labelCanvas.getContext("2d")
|
||||
};
|
||||
_getAssemblerData () {
|
||||
if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) {
|
||||
_sharedLabelData = _canvasPool.get();
|
||||
}
|
||||
else {
|
||||
if (!_sharedLabelData) {
|
||||
let labelCanvas = document.createElement("canvas");
|
||||
_sharedLabelData = {
|
||||
canvas: labelCanvas,
|
||||
context: labelCanvas.getContext("2d")
|
||||
};
|
||||
}
|
||||
}
|
||||
_sharedLabelData.canvas.width = _sharedLabelData.canvas.height = 1;
|
||||
return _sharedLabelData;
|
||||
},
|
||||
|
||||
_resetAssemblerData (assemblerData) {
|
||||
if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS && assemblerData) {
|
||||
_canvasPool.put(assemblerData);
|
||||
}
|
||||
},
|
||||
|
||||
updateRenderData (comp) {
|
||||
if (!comp._renderData.vertDirty) return;
|
||||
|
||||
_comp = comp;
|
||||
|
||||
if (this._updateFontFamly()) {
|
||||
this._updateProperties();
|
||||
if (this._updateFontFamly(comp)) {
|
||||
this._updateProperties(comp);
|
||||
this._calculateLabelFont();
|
||||
this._calculateSplitedStrings();
|
||||
this._updateLabelDimensions();
|
||||
this._calculateTextBaseline();
|
||||
this._updateTexture(_comp);
|
||||
|
||||
_comp._actualFontSize = _fontSize;
|
||||
_comp.node.setContentSize(_canvasSize);
|
||||
this._updateTexture(comp);
|
||||
|
||||
this._updateVerts(_comp);
|
||||
comp._actualFontSize = _fontSize;
|
||||
comp.node.setContentSize(_canvasSize);
|
||||
|
||||
_comp._renderData.vertDirty = _comp._renderData.uvDirty = false;
|
||||
this._updateVerts(comp);
|
||||
|
||||
comp._renderData.vertDirty = comp._renderData.uvDirty = false;
|
||||
|
||||
_context = null;
|
||||
_canvas = null;
|
||||
_texture = null;
|
||||
}
|
||||
|
||||
_comp = null;
|
||||
},
|
||||
|
||||
_updateVerts () {
|
||||
},
|
||||
|
||||
_updateFontFamly () {
|
||||
if (!_comp.useSystemFont) {
|
||||
if (!_comp.font) return false;
|
||||
_updateFontFamly (comp) {
|
||||
if (!comp.useSystemFont) {
|
||||
if (!comp.font) return false;
|
||||
|
||||
let url = _comp.font.nativeUrl;
|
||||
let url = comp.font.nativeUrl;
|
||||
if (CC_WECHATGAME) {
|
||||
_fontFamily = wx.loadFont(url);
|
||||
//avoid the error in wechat devtool platform
|
||||
@@ -139,31 +170,31 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
else {
|
||||
_fontFamily = _comp.fontFamily;
|
||||
_fontFamily = comp.fontFamily;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
|
||||
_updateProperties () {
|
||||
let assemblerData = this.getAssemblerData();
|
||||
_updateProperties (comp) {
|
||||
let assemblerData = comp._assemblerData;
|
||||
_context = assemblerData.context;
|
||||
_canvas = assemblerData.canvas;
|
||||
_texture = _comp._texture;
|
||||
_texture = comp._texture;
|
||||
|
||||
_string = _comp.string.toString();
|
||||
_fontSize = _comp._fontSize;
|
||||
_string = comp.string.toString();
|
||||
_fontSize = comp._fontSize;
|
||||
_drawFontsize = _fontSize;
|
||||
_overflow = _comp.overflow;
|
||||
_canvasSize.width = _comp.node.width;
|
||||
_canvasSize.height = _comp.node.height;
|
||||
_lineHeight = _comp._lineHeight;
|
||||
_hAlign = _comp.horizontalAlign;
|
||||
_vAlign = _comp.verticalAlign;
|
||||
_color = _comp.node.color;
|
||||
_isBold = _comp._isBold;
|
||||
_isItalic = _comp._isItalic;
|
||||
_isUnderline = _comp._isUnderline;
|
||||
_overflow = comp.overflow;
|
||||
_canvasSize.width = comp.node.width;
|
||||
_canvasSize.height = comp.node.height;
|
||||
_lineHeight = comp._lineHeight;
|
||||
_hAlign = comp.horizontalAlign;
|
||||
_vAlign = comp.verticalAlign;
|
||||
_color = comp.node.color;
|
||||
_isBold = comp._isBold;
|
||||
_isItalic = comp._isItalic;
|
||||
_isUnderline = comp._isUnderline;
|
||||
|
||||
if (_overflow === Overflow.NONE) {
|
||||
_isWrapText = false;
|
||||
@@ -172,11 +203,11 @@ module.exports = {
|
||||
_isWrapText = true;
|
||||
}
|
||||
else {
|
||||
_isWrapText = _comp.enableWrapText;
|
||||
_isWrapText = comp.enableWrapText;
|
||||
}
|
||||
|
||||
// outline
|
||||
let outline = LabelOutline && _comp.getComponent(LabelOutline);
|
||||
let outline = LabelOutline && comp.getComponent(LabelOutline);
|
||||
if (outline && outline.enabled) {
|
||||
_isOutlined = true;
|
||||
_margin = _outlineWidth = outline.width;
|
||||
@@ -217,7 +248,7 @@ module.exports = {
|
||||
return cc.v2(labelX, firstLinelabelY);
|
||||
},
|
||||
|
||||
_updateTexture (comp) {
|
||||
_updateTexture () {
|
||||
_context.clearRect(0, 0, _canvas.width, _canvas.height);
|
||||
_context.font = _fontDesc;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user