-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
label processing string & remove shared label info #16292
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,104 +24,130 @@ | |
|
||
import { JSB } from 'internal:constants'; | ||
import { error } from '@base/debug'; | ||
import { IConfig, FontAtlas } from '../../assets/bitmap-font'; | ||
import { FontAtlas, BitmapFont } from '../../assets/bitmap-font'; | ||
import { SpriteFrame } from '../../assets/sprite-frame'; | ||
import { Rect } from '../../../core'; | ||
import { Rect, Vec2 } from '../../../core'; | ||
import { Label, Overflow, CacheMode } from '../../components/label'; | ||
import { UITransform } from '../../framework/ui-transform'; | ||
import { LetterAtlas, shareLabelInfo } from './font-utils'; | ||
import { dynamicAtlasManager } from '../../utils/dynamic-atlas/atlas-manager'; | ||
import { TextProcessing } from './text-processing'; | ||
import { TextOutputLayoutData, TextOutputRenderData } from './text-output-data'; | ||
import { TextStyle } from './text-style'; | ||
import { TextLayout } from './text-layout'; | ||
import { view } from '../../../ui/view'; | ||
|
||
const _defaultLetterAtlas = new LetterAtlas(64, 64); | ||
const _defaultFontAtlas = new FontAtlas(null); | ||
|
||
let _comp: Label | null = null; | ||
let _uiTrans: UITransform | null = null; | ||
|
||
let _fntConfig: IConfig | null = null; | ||
let _spriteFrame: SpriteFrame|null = null; | ||
let QUAD_INDICES; | ||
let QUAD_INDICES: Uint16Array | null = null; | ||
|
||
export const bmfontUtils = { | ||
|
||
updateProcessingData (style: TextStyle, layout: TextLayout, | ||
outputLayoutData: TextOutputLayoutData, outputRenderData: TextOutputRenderData, | ||
comp: Label, trans: UITransform): void { | ||
style.fontSize = comp.fontSize; | ||
style.actualFontSize = comp.fontSize; | ||
style.originFontSize = _fntConfig ? _fntConfig.fontSize : comp.fontSize; | ||
layout.horizontalAlign = comp.horizontalAlign; | ||
layout.verticalAlign = comp.verticalAlign; | ||
layout.spacingX = comp.spacingX; | ||
updateLayoutProcessingData ( | ||
style: TextStyle, | ||
layout: TextLayout, | ||
outputLayoutData: TextOutputLayoutData, | ||
comp: Label, | ||
trans: UITransform, | ||
): void { | ||
style.fontSize = comp.fontSize; //both | ||
style.actualFontSize = comp.fontSize; //both | ||
layout.horizontalAlign = comp.horizontalAlign; //both | ||
layout.verticalAlign = comp.verticalAlign; //both | ||
layout.spacingX = comp.spacingX; // layout only | ||
const overflow = comp.overflow; | ||
layout.overFlow = overflow; | ||
layout.lineHeight = comp.lineHeight; | ||
|
||
outputLayoutData.nodeContentSize.width = trans.width; | ||
outputLayoutData.nodeContentSize.height = trans.height; | ||
layout.overFlow = overflow; // both | ||
layout.lineHeight = comp.lineHeight; // both | ||
|
||
// should wrap text | ||
if (overflow === Overflow.NONE) { | ||
layout.wrapping = false; | ||
outputLayoutData.nodeContentSize.width += shareLabelInfo.margin * 2; | ||
outputLayoutData.nodeContentSize.height += shareLabelInfo.margin * 2; | ||
layout.wrapping = false; // both | ||
} else if (overflow === Overflow.RESIZE_HEIGHT) { | ||
layout.wrapping = true; | ||
outputLayoutData.nodeContentSize.height += shareLabelInfo.margin * 2; | ||
} else { | ||
layout.wrapping = comp.enableWrapText; | ||
} | ||
outputRenderData.uiTransAnchorX = trans.anchorX; | ||
outputRenderData.uiTransAnchorY = trans.anchorY; | ||
const fontAsset = comp.font as BitmapFont; | ||
style.fntConfig = fontAsset.fntConfig; // layout only | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 应该直接把 font 传下来,不应该拆成多个传递 |
||
style.originFontSize = fontAsset.fntConfig?.fontSize; //both | ||
style.fontAtlas = fontAsset.fontDefDictionary; | ||
if (!style.fontAtlas) { | ||
style.fontAtlas = _defaultFontAtlas; | ||
} | ||
|
||
style.isOutlined = false; | ||
style.outlineWidth = 0; | ||
|
||
style.hash = ''; | ||
}, | ||
|
||
// render Only | ||
updateRenderProcessingData ( | ||
style: TextStyle, | ||
outputRenderData: TextOutputRenderData, | ||
comp: Label, | ||
anchor: Readonly<Vec2>, | ||
): void { | ||
// render info | ||
outputRenderData.uiTransAnchorX = anchor.x; | ||
outputRenderData.uiTransAnchorY = anchor.y; | ||
|
||
if (comp.font instanceof BitmapFont) { | ||
const fontAsset = comp.font; | ||
style.spriteFrame = fontAsset.spriteFrame; | ||
dynamicAtlasManager.packToDynamicAtlas(comp, style.spriteFrame); | ||
} | ||
style.color.set(comp.color); // render only | ||
}, | ||
|
||
updateLayoutData (comp: Label): void { | ||
// Todo: dirtyFlag | ||
const trans = comp.node._uiProps.uiTransformComp!; | ||
const processing = TextProcessing.instance; | ||
const style = comp.textStyle; | ||
const layout = comp.textLayout; | ||
const outputLayoutData = comp.textLayoutData; | ||
style.fontScale = view.getScaleX(); | ||
|
||
shareLabelInfo.lineHeight = comp.lineHeight; | ||
shareLabelInfo.fontSize = comp.fontSize; | ||
this.updateLayoutProcessingData(style, layout, outputLayoutData, comp, trans); | ||
|
||
style.spriteFrame = _spriteFrame; | ||
style.fntConfig = _fntConfig; | ||
style.fontFamily = shareLabelInfo.fontFamily; | ||
// TextProcessing | ||
processing.processingString(true, style, layout, outputLayoutData, comp.string); | ||
|
||
style.color.set(comp.color); | ||
comp.actualFontSize = style.actualFontSize; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 组件上为什么要缓存这个变量 |
||
trans.setContentSize(outputLayoutData.nodeContentSize); | ||
}, | ||
|
||
updateRenderData (comp: Label): void { | ||
if (!comp.renderData) { | ||
return; | ||
} | ||
|
||
if (_comp === comp) { return; } | ||
|
||
if (comp.renderData.vertDirty) { | ||
_comp = comp; | ||
_uiTrans = _comp.node._uiProps.uiTransformComp!; | ||
this.updateLayoutData(comp);// Todo: move to layout manager | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 更新渲染时,需要先更新布局? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不需要,只是在此PR中未进行流程分离,在流程分离 PR 中移除了此步 |
||
const renderData = comp.renderData; | ||
|
||
const processing = TextProcessing.instance; | ||
const style = comp.textStyle; | ||
const layout = comp.textLayout; | ||
const outputLayoutData = comp.textLayoutData; | ||
const outputRenderData = comp.textRenderData; | ||
style.fontScale = view.getScaleX(); | ||
this._updateFontFamily(comp); | ||
|
||
this.updateProcessingData(style, layout, outputLayoutData, outputRenderData, comp, _uiTrans); | ||
const anchor = comp.node._uiProps.uiTransformComp!.anchorPoint; | ||
this.updateRenderProcessingData(style, outputRenderData, comp, anchor); | ||
|
||
this._updateLabelInfo(comp); | ||
|
||
style.fontDesc = shareLabelInfo.fontDesc; | ||
|
||
// TextProcessing | ||
processing.processingString(true, style, layout, outputLayoutData, comp.string); | ||
// generateVertex | ||
this.resetRenderData(comp); | ||
outputRenderData.quadCount = 0; | ||
processing.generateRenderInfo(true, style, layout, outputLayoutData, outputRenderData, | ||
comp.string, this.generateVertexData); | ||
processing.generateRenderInfo( | ||
true, | ||
style, | ||
layout, | ||
outputLayoutData, | ||
outputRenderData, | ||
comp.string, | ||
this.generateVertexData, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为什么会需要回调? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 由于在两种类型中,顶点处理方式无法统一,所以使用回调函数 |
||
); | ||
|
||
renderData.dataLength = outputRenderData.quadCount; | ||
renderData.resize(renderData.dataLength, renderData.dataLength / 2 * 3); | ||
|
@@ -132,17 +158,12 @@ export const bmfontUtils = { | |
|
||
const indexCount = renderData.indexCount; | ||
this.createQuadIndices(indexCount); | ||
renderData.chunk.setIndexBuffer(QUAD_INDICES); | ||
renderData.chunk.setIndexBuffer(QUAD_INDICES!); | ||
|
||
_comp.actualFontSize = style.actualFontSize; | ||
_uiTrans.setContentSize(outputLayoutData.nodeContentSize); | ||
this.updateUVs(comp);// dirty need | ||
this.updateColor(comp); // dirty need | ||
|
||
renderData.vertDirty = false; | ||
_comp = null; | ||
|
||
this._resetProperties(); | ||
} | ||
|
||
if (comp.spriteFrame) { | ||
|
@@ -195,8 +216,17 @@ export const bmfontUtils = { | |
}, | ||
|
||
// callBack function | ||
generateVertexData (style: TextStyle, outputLayoutData: TextOutputLayoutData, outputRenderData: TextOutputRenderData, offset: number, | ||
spriteFrame: SpriteFrame, rect: Rect, rotated: boolean, x: number, y: number): void { | ||
generateVertexData ( | ||
style: TextStyle, | ||
outputLayoutData: TextOutputLayoutData, | ||
outputRenderData: TextOutputRenderData, | ||
offset: number, | ||
spriteFrame: SpriteFrame, | ||
rect: Rect, | ||
rotated: boolean, | ||
x: number, | ||
y: number, | ||
): void { | ||
const dataOffset = offset; | ||
const scale = style.bmfontScale; | ||
|
||
|
@@ -251,37 +281,7 @@ export const bmfontUtils = { | |
dataList[dataOffset + 3].y = y; | ||
}, | ||
|
||
_updateFontFamily (comp): void { | ||
const fontAsset = comp.font; | ||
_spriteFrame = fontAsset.spriteFrame; | ||
_fntConfig = fontAsset.fntConfig; | ||
shareLabelInfo.fontAtlas = fontAsset.fontDefDictionary; | ||
if (!shareLabelInfo.fontAtlas) { | ||
if (comp.cacheMode === CacheMode.CHAR) { | ||
shareLabelInfo.fontAtlas = _defaultLetterAtlas; | ||
} else { | ||
shareLabelInfo.fontAtlas = _defaultFontAtlas; | ||
} | ||
} | ||
|
||
dynamicAtlasManager.packToDynamicAtlas(comp, _spriteFrame); | ||
// TODO update material and uv | ||
}, | ||
|
||
_updateLabelInfo (comp): void { | ||
// clear | ||
shareLabelInfo.hash = ''; | ||
shareLabelInfo.margin = 0; | ||
}, | ||
|
||
_resetProperties (): void { | ||
_fntConfig = null; | ||
_spriteFrame = null; | ||
shareLabelInfo.hash = ''; | ||
shareLabelInfo.margin = 0; | ||
}, | ||
|
||
createQuadIndices (indexCount): void { | ||
createQuadIndices (indexCount: number): void { | ||
if (indexCount % 6 !== 0) { | ||
error('illegal index count!'); | ||
return; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里没用了么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
重复函数