This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Paragraph.ts
94 lines (77 loc) · 2.71 KB
/
Paragraph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Node } from "./Node";
import { ensureColor, ensureFloat } from "../utils/TypeUtils";
import { Element, TextTexture } from "tree2d";
import { TextTextureSettings } from "./textures";
import { Delegator } from "../utils/Delegator";
import { VugelStage } from "../../wrapper";
class Paragraph extends Node {
private settings = new TextTextureSettings(() => this.update());
private _text: string = "";
private _lineHeight: number = 0;
private _fontColor: number = 0xffffffff;
constructor(stage: VugelStage) {
super(stage);
this.el.flex = true;
this.el.flexWrap = true;
}
get "line-height"() {
return this._lineHeight;
}
set "line-height"(v: number) {
this._lineHeight = ensureFloat(v);
this.update();
}
get "font-color"() {
return this._fontColor;
}
set "font-color"(v: number | string) {
this._fontColor = ensureColor(v);
this.update();
}
get text() {
return this._text;
}
set text(text: string) {
this._text = text;
this.update();
}
setElementText(text: string) {
this._text = text.trim();
this.update();
}
static newlinePattern = "@+~^";
private update() {
const s = this.settings.textSettings;
const fontSize = s.fontSize || 24;
const lineHeight = Math.round(this._lineHeight || fontSize * 1.3);
const letterSpacing = Math.round(fontSize * 0.2);
const margin = Math.round(lineHeight - fontSize);
const fontColor = this._fontColor || 0xffffffff;
const text = this._text.replace(/(\r?\n)/, ` ${Paragraph.newlinePattern} `);
const words = text.split(/\s+/);
if (words.length > 1 || words[0] != "") {
const els = words.map((word: string) => {
const el = new Element(this.stage);
if (word === Paragraph.newlinePattern) {
// Force line break.
el.funcW = (w: number) => w;
el.h = 0;
} else {
const texture = new TextTexture(this.stage);
texture.text = word;
texture.setSettings(s);
el.texture = texture;
el.marginRight = letterSpacing;
el.marginTop = Math.round(margin * 0.8);
el.marginBottom = Math.round(margin * 0.2);
el.color = fontColor;
}
return el;
});
this.el.childList.setItems(els);
}
}
}
Delegator.delegate(Paragraph, TextTextureSettings, "settings");
interface Paragraph extends TextTextureSettings {}
export { Paragraph };