| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- 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) {
- if(value == 'undefined'){
- Log.error(`字符出错${value}`);
- return;
- }
- 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;
- }
- }
|