-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext.ts
178 lines (166 loc) · 6.44 KB
/
text.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
namespace SpriteKind {
//% isKind
export const Text = SpriteKind.create();
}
//% blockNamespace="textsprite"
//% blockGap=8
class TextSprite extends Sprite {
constructor(
public text: string,
public bg: number,
public fg: number,
public maxFontHeight: number,
public borderWidth: number,
public borderColor: number,
public padding: number,
public outlineWidth: number,
public outlineColor: number,
public icon: Image = null,
) {
super(image.create(0,0));
this.setKind(SpriteKind.Text);
this.setFlag(SpriteFlag.Ghost, true);
this.update()
}
public update() {
const borderAndPadding = this.borderWidth + this.padding + this.outlineWidth;
const iconWidth = this.icon ? this.icon.width + this.padding + this.outlineWidth : 0;
const iconHeight = this.icon ? this.icon.height : 0;
const font = textsprite.getFontForTextAndHeight(this.text, this.maxFontHeight);
const width = iconWidth + font.charWidth * this.text.length + 2 * borderAndPadding;
const height = Math.max(iconHeight, font.charHeight) + 2 * borderAndPadding;
const img = image.create(width, height);
img.fill(this.borderColor);
img.fillRect(this.borderWidth, this.borderWidth, width - this.borderWidth * 2, height - this.borderWidth * 2, this.bg)
if (this.icon) {
const iconHeightOffset = (height - iconHeight) / 2
textsprite.renderScaledImage(this.icon, img, borderAndPadding, iconHeightOffset)
}
const textHeightOffset = (height - font.charHeight) / 2
img.print(this.text, iconWidth + borderAndPadding, textHeightOffset, this.fg, font);
if (this.outlineWidth > 0)
textsprite.outlineOtherColor(img, this.fg, this.outlineWidth, this.outlineColor)
this.setImage(img)
}
//% block="set $this(textSprite) max font height $height"
//% group="Modify"
//% weight=50
public setMaxFontHeight(height: number) {
this.maxFontHeight = height
this.update();
}
//% block="set $this(textSprite) icon $icon=screen_image_picker"
//% group="Modify"
//% weight=46
public setIcon(icon: Image) {
this.icon = icon
this.update()
}
//% block="set $this(textSprite) text $text"
//% group="Modify"
//% weight=47
public setText(text: string) {
this.text = text || ""
this.update()
}
//% block="set $this(textSprite) border $width $color || and padding $padding"
//% width.defl=1
//% color.defl=6
//% color.shadow="colorindexpicker"
//% group="Modify"
//% weight=48
public setBorder(width: number, color: number, padding: number = 0) {
this.borderWidth = Math.max(width, 0);
this.borderColor = color;
this.padding = Math.max(padding, 0);
this.update()
}
//% block="set $this(textSprite) outline $width $color"
//% width.defl=1
//% color.defl=6
//% color.shadow="colorindexpicker"
//% group="Modify"
//% weight=49
public setOutline(width: number, color: number) {
this.outlineWidth = Math.max(width, 0);
this.outlineColor = color;
this.update();
}
}
//% color=#3e99de
//% icon="\uf031"
//% blockGap=8 block="Text Sprite"
//% groups='["Create", "Modify"]'
namespace textsprite {
// TODO: downscale and upscale icons?
export function renderScaledImage(source: Image, destination: Image, x: number, y: number, downScalePowerOfTwo: number = 0) {
const scale = downScalePowerOfTwo;
const tile = source
for (let i = 0; i < source.width; i += 1 << scale) {
for (let j = 0; j < source.height; j += 1 << scale) {
if (source.getPixel(i, j) != 0) {
destination.setPixel(x + (i >> scale), y + (j >> scale), source.getPixel(i, j))
}
}
}
}
export function getFontForTextAndHeight(text: string, maxHeight: number): image.Font {
const baseFont = image.getFontForText(text)
const hasUnicode = baseFont.charHeight === 12 // this is a hack
const availableFonts: image.Font[] = hasUnicode
? [baseFont]
: [image.font8, image.font5] // 8 and 5 are generally better fonts than 12
const remainders = availableFonts.map(s => maxHeight % s.charHeight)
const fontIdx = remainders.reduce((p, n, i) => remainders[p] <= n ? p : i, 99)
const font = availableFonts[fontIdx]
return image.scaledFont(font, maxHeight / font.charHeight)
}
//% block="text sprite $text || as $fg on $bg"
//% blockId="textsprite_create"
//% blockSetVariable="textSprite"
//% expandableArgumentMode="toggle"
//% bg.defl=0
//% bg.shadow="colorindexpicker"
//% fg.defl=1
//% fg.shadow="colorindexpicker"
//% group="Create"
//% weight=100
export function create(
text: string,
bg: number = 0,
fg: number = 1,
): TextSprite {
const sprite = new TextSprite(text, bg, fg, 8, 0, 0, 0, 0, 0);
game.currentScene().physicsEngine.addSprite(sprite);
return sprite;
}
export function outlineOtherColor(img: Image, targetColor: number, outlineWidth: number, outlineColor: number) {
let toOutlineX: number[] = [];
let toOutlineY: number[] = [];
for (let x = 0; x < img.width; x++) {
for (let y = 0; y < img.height; y++) {
for (let sx = 0; sx <= outlineWidth; sx++) {
for (let sy = 0; sy <= outlineWidth; sy++) {
if (sx + sy === 0)
continue;
if (img.getPixel(x, y) === targetColor)
continue
if (img.getPixel(x + sx, y + sy) === targetColor
|| img.getPixel(x - sx, y + sy) === targetColor
|| img.getPixel(x + sx, y - sy) === targetColor
|| img.getPixel(x - sx, y - sy) === targetColor
) {
toOutlineX.push(x)
toOutlineY.push(y)
}
}
}
}
}
for (let i = 0; i < toOutlineX.length; i++) {
const x = toOutlineX[i]
const y = toOutlineY[i]
img.setPixel(x, y, outlineColor)
}
}
}