-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.js
More file actions
168 lines (153 loc) · 4.13 KB
/
Copy pathscene.js
File metadata and controls
168 lines (153 loc) · 4.13 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
163
164
165
166
167
168
// The document model. A Scene is a stack of Layers (bottom-first). Terrain layers
// store paint as vector brush *strokes* (the raster is a runtime-derived cache),
// which keeps undo snapshots and save files tiny. Object layers hold sprites,
// shapes, and text.
import { GRID_DEFAULT } from '../constants';
import { DEFAULT_THEME } from '../themes';
let _counter = 1;
export function uid(prefix = 'o') {
_counter += 1;
const rnd = Math.floor(Math.random() * 1679616).toString(36);
return `${prefix}${_counter.toString(36)}${rnd}`;
}
export function createObjectLayer(name = 'Layer') {
return {
id: uid('L'),
name,
kind: 'object',
visible: true,
locked: false,
opacity: 1,
objects: [],
};
}
export function createTerrainLayer(name = 'Terrain') {
return {
id: uid('L'),
name,
kind: 'terrain',
visible: true,
locked: false,
opacity: 1,
strokes: [],
_raster: null, // runtime cache, never serialized
_dirty: true,
};
}
export function createScene() {
const terrain = createTerrainLayer('Terrain');
const ink = createObjectLayer('Ink');
return {
v: 1,
meta: { title: 'Untitled Map', createdAt: Date.now(), updatedAt: Date.now() },
camera: { x: 0, y: 0, zoom: 1 },
grid: { type: 'square', size: GRID_DEFAULT, visible: false, snap: false },
themeId: DEFAULT_THEME,
size: { mode: 'fixed', width: 2400, height: 1500 },
layers: [terrain, ink],
activeLayerId: ink.id,
};
}
// ---- object factories ----
export function createSprite(spriteId, x, y, opts = {}) {
return {
id: uid('s'),
kind: 'sprite',
spriteId,
x,
y,
scale: opts.scale ?? 1,
rotation: opts.rotation ?? 0,
opacity: opts.opacity ?? 1,
flipX: opts.flipX ?? false,
tint: opts.tint ?? null,
w: opts.w,
h: opts.h,
};
}
export function createShape(shapeType, points, style = {}) {
return {
id: uid('p'),
kind: 'shape',
shapeType,
points: points.map((p) => ({ x: p.x, y: p.y })),
closed: !!style.closed,
x: 0,
y: 0,
scale: 1,
rotation: 0,
opacity: style.opacity ?? 1,
style: {
stroke: style.stroke ?? '#2c2418',
fill: style.fill ?? null,
width: style.width ?? 4,
dash: style.dash ?? null,
pattern: style.pattern ?? null,
},
};
}
export function createText(text, x, y, opts = {}) {
return {
id: uid('t'),
kind: 'text',
text,
x,
y,
scale: 1,
rotation: opts.rotation ?? 0,
opacity: 1,
font: opts.font ?? 'serif',
fontSize: opts.fontSize ?? 28,
color: opts.color ?? '#2c2418',
align: opts.align ?? 'center',
italic: opts.italic ?? true,
letterSpacing: opts.letterSpacing ?? 1,
w: opts.w,
h: opts.h,
};
}
export function createStroke(material, opts = {}) {
return {
material,
size: opts.size ?? 60,
opacity: opts.opacity ?? 1,
hardness: opts.hardness ?? 0.7,
erase: opts.erase ?? false,
pts: [],
};
}
// ---- accessors / mutators ----
export const getLayer = (scene, id) => scene.layers.find((l) => l.id === id);
export const getActiveLayer = (scene) => getLayer(scene, scene.activeLayerId);
export function findObject(scene, objId) {
for (const l of scene.layers) {
if (l.kind !== 'object') continue;
const o = l.objects.find((x) => x.id === objId);
if (o) return { layer: l, object: o };
}
return null;
}
export function addObject(scene, obj, layerId = scene.activeLayerId) {
let layer = getLayer(scene, layerId);
if (!layer || layer.kind !== 'object') layer = scene.layers.find((l) => l.kind === 'object');
if (!layer) {
layer = createObjectLayer('Ink');
scene.layers.push(layer);
scene.activeLayerId = layer.id;
}
layer.objects.push(obj);
return obj;
}
export function removeObjects(scene, ids) {
const set = new Set(ids);
for (const l of scene.layers) {
if (l.kind === 'object') l.objects = l.objects.filter((o) => !set.has(o.id));
}
}
export function firstObjectLayerId(scene) {
const l = scene.layers.find((x) => x.kind === 'object');
return l ? l.id : null;
}
export function firstTerrainLayer(scene) {
return scene.layers.find((x) => x.kind === 'terrain');
}