forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.ts
215 lines (197 loc) · 6.39 KB
/
docs.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
namespace docs {
function imageToBuffer(img: Image) {
const w = img.width
const h = img.height
const buf = control.createBuffer(1 + w * h)
let j = 0
buf[j++] = w
for (let x = 0; x < w; ++x) {
for (let y = 0; y < h; ++y) {
buf[j++] = img.getPixel(x, y)
}
}
return buf
}
let app: microcode.App
export function setup(theApp: microcode.App) {
app = theApp
_setup()
}
//% shim=TD_NOOP
function _setup() {
control.simmessages.onReceived("docs", (data: Buffer) => {
const msg = JSON.parse(data.toString())
if (msg.type === "art") _renderApp()
else if (msg.type === "screenshot") _renderScreenshot()
})
}
interface RenderedImage {
type: "icon" | "sample" | "icon_sample" | "image" | "program"
name: string
pixels: string
}
//% shim=TD_NOOP
function _renderApp() {
const images: RenderedImage[] = []
appendImage(images, "image", "home", screen)
renderIcons(images)
renderSamples(images)
appendImage(images, "image", "microcode", microcode.wordLogo)
appendImage(images, "image", "microbit", microcode.microbitLogo)
appendImage(
images,
"image",
"editor_background",
microcode.editorBackground
)
control.simmessages.send(
"docs",
Buffer.fromUTF8(
JSON.stringify({
type: "art",
samples: microcode.rawSamples(),
images,
})
)
)
}
function renderSamples(images: RenderedImage[]) {
app.popScene()
for (const sample of microcode.samples(false)) {
console.log(`render sample ${sample.label}`)
const icon = microcode.icons.get(sample.icon, true)
if (icon) appendImage(images, "icon_sample", sample.label, icon)
const src = sample.source
settings.writeString(microcode.SAVESLOT_AUTO, src)
const res = _renderProgram()
Object.keys(res).forEach(iname => {
appendImage(
images,
"sample",
iname == "app" ? sample.label : `${sample.label}_${iname}`,
res[iname]
)
})
app.popScene()
}
microcode.Screen.resetScreenImage()
}
//% shim=TD_NOOP
function _renderScreenshot() {
const res = _renderProgram()
const images: RenderedImage[] = []
Object.keys(res).forEach(iname =>
appendImage(
images,
"program",
iname == "app" ? "current" : `current_${iname}`,
res[iname]
)
)
microcode.Screen.resetScreenImage()
control.simmessages.send(
"docs",
Buffer.fromUTF8(
JSON.stringify({
type: "art",
images,
})
)
)
}
//% shim=TD_NOOP
function _renderProgram(): { [name: string]: Image } {
const r: { [name: string]: Image } = {}
const loader = new microcode.Editor(app)
loader.rendering = true
app.pushScene(loader)
loader.cursor.visible = false
const pages = loader.nonEmptyPages()
let imgs: Image[] = []
let w = 0
let h = 0
const margin = 4
// compute largest rule width
let pw = 160
for (const p of pages) {
loader.switchToPage(p)
const rw = loader.ruleWidth()
pw = Math.max(pw, rw)
}
// when the width is too large (>255?), bad things happen
pw = Math.min(255, pw)
// render all pages
loader.nonEmptyPages().forEach(p => {
loader.switchToPage(p)
loader.pageEditor.layout()
microcode.Screen.setImageSize(pw, loader.pageHeight())
const editor = new microcode.Editor(app)
editor.rendering = true
app.pushScene(editor)
editor.cursor.visible = false
pause(500)
microcode.Screen.image.fill(editor.color)
editor.renderPage(p)
const img = microcode.Screen.image.clone()
imgs.push(img)
w = Math.max(w, img.width)
h += img.height + margin
r[`page_${p + 1}`] = img
// render individual rules
const pageEditor = editor.pageEditor
const rulesEditor = pageEditor.ruleEditors
rulesEditor.forEach((ruleEditor, ri) => {
const bound = ruleEditor.bounds
const imgr = image.create(bound.width, bound.height)
imgr.fill(loader.color)
imgr.blit(
0,
0,
bound.width,
bound.height,
img,
ruleEditor.xfrm.localPos.x - bound.left,
ruleEditor.xfrm.localPos.y - bound.top,
bound.width,
bound.height,
true,
false
)
r[`page_${p + 1}_rule_${ri + 1}`] = imgr
})
app.popScene()
})
const res = image.create(w, h)
r["app"] = res
res.fill(loader.color)
let y = 0
for (let i = 0; i < imgs.length; ++i) {
const img = imgs[i]
res.drawTransparentImage(img, 0, y)
y += img.height + margin
}
app.popScene()
return r
}
function appendImage(
images: RenderedImage[],
type: "icon" | "sample" | "icon_sample" | "image" | "program",
name: string,
img: Image
) {
const msg: RenderedImage = {
type,
name: name.replaceAll(" ", "_").replaceAll(",", ""),
pixels: imageToBuffer(img).toHex(),
}
images.push(msg)
}
function renderIcons(images: RenderedImage[]) {
microcode.icons.init()
for (const name of microcode.icons.names()) {
console.log(`render icon ${name}`)
const icon = microcode.icons.get(name)
appendImage(images, `icon`, name, icon)
}
}
}