forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield_tilemap.ts
More file actions
162 lines (132 loc) · 4.89 KB
/
field_tilemap.ts
File metadata and controls
162 lines (132 loc) · 4.89 KB
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
/// <reference path="../../built/pxtlib.d.ts" />
namespace pxtblockly {
export interface FieldTilemapOptions {
initWidth: string;
initHeight: string;
disableResize: string;
tileWidth: string | number;
filter?: string;
lightMode: boolean;
}
interface ParsedFieldTilemapOptions {
initWidth: number;
initHeight: number;
disableResize: boolean;
tileWidth: 8 | 16 | 32;
filter?: string;
lightMode: boolean;
}
export class FieldTilemap extends FieldAssetEditor<FieldTilemapOptions, ParsedFieldTilemapOptions> {
protected initText: string;
protected asset: pxt.ProjectTilemap;
getInitText() {
return this.initText;
}
getTileset() {
return (this.asset as pxt.ProjectTilemap)?.data.tileset;
}
protected getAssetType(): pxt.AssetType {
return pxt.AssetType.Tilemap;
}
protected createNewAsset(newText = ""): pxt.Asset {
if (newText) {
// backticks are escaped inside markdown content
newText = newText.replace(/`/g, "`");
}
const project = pxt.react.getTilemapProject();
const existing = pxt.lookupProjectAssetByTSReference(newText, project);
if (existing) return existing;
const tilemap = pxt.sprite.decodeTilemap(newText, "typescript", project) || project.blankTilemap(this.params.tileWidth, this.params.initWidth, this.params.initHeight);
let newAsset: pxt.ProjectTilemap;
// Ignore invalid bitmaps
if (checkTilemap(tilemap)) {
this.initText = newText;
this.isGreyBlock = false;
const [ name ] = project.createNewTilemapFromData(tilemap);
newAsset = project.getTilemap(name);
}
else if (newText.trim()) {
this.isGreyBlock = true;
this.valueText = newText;
}
return newAsset;
}
protected onEditorClose(newValue: pxt.ProjectTilemap) {
pxt.sprite.updateTilemapReferencesFromResult(pxt.react.getTilemapProject(), newValue);
}
protected getValueText(): string {
if (this.isGreyBlock) return pxt.Util.htmlUnescape(this.valueText);
if (this.asset) {
return pxt.getTSReferenceForAsset(this.asset);
}
return this.getInitText();
}
protected parseFieldOptions(opts: FieldTilemapOptions): ParsedFieldTilemapOptions {
return parseFieldOptions(opts);
}
}
function parseFieldOptions(opts: FieldTilemapOptions) {
const parsed: ParsedFieldTilemapOptions = {
initWidth: 16,
initHeight: 16,
disableResize: false,
tileWidth: 16,
lightMode: false
};
if (!opts) {
return parsed;
}
parsed.lightMode = opts.lightMode;
if (opts.filter) {
parsed.filter = opts.filter;
}
if (opts.tileWidth) {
if (typeof opts.tileWidth === "number") {
switch (opts.tileWidth) {
case 8:
parsed.tileWidth = 8;
break;
case 16:
parsed.tileWidth = 16;
break;
case 32:
parsed.tileWidth = 32;
break;
}
}
else {
const tw = opts.tileWidth.trim().toLowerCase();
switch (tw) {
case "8":
case "eight":
parsed.tileWidth = 8;
break;
case "16":
case "sixteen":
parsed.tileWidth = 16;
break;
case "32":
case "thirtytwo":
parsed.tileWidth = 32;
break;
}
}
}
parsed.initWidth = withDefault(opts.initWidth, parsed.initWidth);
parsed.initHeight = withDefault(opts.initHeight, parsed.initHeight);
return parsed;
function withDefault(raw: string, def: number) {
const res = parseInt(raw);
if (isNaN(res)) {
return def;
}
return res;
}
}
function checkTilemap(tilemap: pxt.sprite.TilemapData) {
if (!tilemap || !tilemap.tilemap || !tilemap.tilemap.width || !tilemap.tilemap.height) return false;
if (!tilemap.layers || tilemap.layers.width !== tilemap.tilemap.width || tilemap.layers.height !== tilemap.tilemap.height) return false;
if (!tilemap.tileset) return false;
return true;
}
}