| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import { Component, Node, Sprite, SpriteFrame, _decorator } from "cc";
- import { Log } from "../utils/Log";
- const { ccclass, property } = _decorator;
- /**
- * 字符集数据
- */
- @ccclass("BitmapFontData")
- export class BitmapFontData {
- @property({ tooltip: "字符" })
- public chair: string = '';
- @property({ tooltip: "图片", type: SpriteFrame })
- public spriteFrame: SpriteFrame;
- }
- /**
- * 位图文本
- * @author 袁浩
- */
- @ccclass("BitmapFont")
- export class BitmapFont extends Component {
- @property({ tooltip: "字符集配置", type: [BitmapFontData] })
- public dataList: BitmapFontData[] = [];
- @property({ visible: false })
- private _string: string = '';
- @property({ tooltip: "文本内容字符串", serializable: true })
- public get string(): string {
- return this._string;
- }
- public set string(value: string) {
- this._string = value + "";
- for (let i = 0; i < this.string.length; i++) {
- let child = this.node.children[i];
- const chair = this.string[i];
- let data = this.getData(chair);
- if (data) {
- if (child) {
- child.active = true;
- child.getComponent(Sprite).spriteFrame = data.spriteFrame;
- }
- else {
- let node = new Node(chair);
- node.layer = this.node.layer;
- let sprite = node.addComponent(Sprite);
- sprite.spriteFrame = data.spriteFrame;
- node.parent = this.node;
- }
- }
- else {
- Log.error(`BitmapFont中缺少字符 ${chair} 的配置。`)
- }
- }
- for (let i = this.string.length; i < this.node.children.length; i++) {
- const child = this.node.children[i];
- child.active = false;
- }
- }
- private getData(chair: string) {
- for (let j = 0; j < this.dataList.length; j++) {
- const data = this.dataList[j];
- if (data.chair == chair) {
- return data;
- }
- }
- return null;
- }
- }
|