Skip to content

Commit a27b7ed

Browse files
committed
feat: add state serialization and restoration for voxel editing context
1 parent a44fd8b commit a27b7ed

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/layer/vox/index.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ import { TrackableValue, WatchableValue } from "#src/trackable_value.js";
3939
import type { UserLayerWithAnnotations } from "#src/ui/annotations.js";
4040
import { randomUint64 } from "#src/util/bigint.js";
4141
import { RefCounted } from "#src/util/disposable.js";
42-
import { parseUint64, verifyFiniteFloat, verifyInt } from "#src/util/json.js";
42+
import {
43+
parseUint64,
44+
verifyFiniteFloat,
45+
verifyInt,
46+
verifyOptionalObjectProperty,
47+
} from "#src/util/json.js";
4348
import { TrackableEnum } from "#src/util/trackable_enum.js";
4449
import { VoxelPreviewMultiscaleSource } from "#src/voxel_annotation/PreviewMultiscaleChunkSource.js";
4550
import type { VoxelEditControllerHost } from "#src/voxel_annotation/edit_controller.js";
@@ -50,6 +55,12 @@ export enum BrushShape {
5055
SPHERE = 1,
5156
}
5257

58+
const BRUSH_SIZE_JSON_KEY = "brushSize";
59+
const ERASE_MODE_JSON_KEY = "eraseMode";
60+
const BRUSH_SHAPE_JSON_KEY = "brushShape";
61+
const FLOOD_FILL_MAX_VOXELS_JSON_KEY = "floodFillMaxVoxels";
62+
const PAINT_VALUE_JSON_KEY = "paintValue";
63+
5364
export class VoxelEditingContext
5465
extends RefCounted
5566
implements VoxelEditControllerHost
@@ -196,6 +207,25 @@ export function UserLayerWithVoxelEditingMixin<
196207
});
197208
}
198209

210+
toJSON() {
211+
const json = super.toJSON();
212+
json[BRUSH_SIZE_JSON_KEY] = this.voxBrushRadius.toJSON();
213+
json[ERASE_MODE_JSON_KEY] = this.voxEraseMode.toJSON();
214+
json[BRUSH_SHAPE_JSON_KEY] = this.voxBrushShape.toJSON();
215+
json[FLOOD_FILL_MAX_VOXELS_JSON_KEY] = this.voxFloodMaxVoxels.toJSON();
216+
json[PAINT_VALUE_JSON_KEY] = this.paintValue.toJSON();
217+
return json;
218+
}
219+
220+
restoreState(specification: any) {
221+
super.restoreState(specification);
222+
verifyOptionalObjectProperty(specification, BRUSH_SIZE_JSON_KEY, v => this.voxBrushRadius.restoreState(v));
223+
verifyOptionalObjectProperty(specification, ERASE_MODE_JSON_KEY, v => this.voxEraseMode.restoreState(v));
224+
verifyOptionalObjectProperty(specification, BRUSH_SHAPE_JSON_KEY, v => this.voxBrushShape.restoreState(v));
225+
verifyOptionalObjectProperty(specification, FLOOD_FILL_MAX_VOXELS_JSON_KEY, v => this.voxFloodMaxVoxels.restoreState(v));
226+
verifyOptionalObjectProperty(specification, PAINT_VALUE_JSON_KEY, v => this.paintValue.restoreState(v));
227+
}
228+
199229
getVoxelPaintValue(erase: boolean): bigint {
200230
if (erase) return 0n;
201231
return this.paintValue.value;

0 commit comments

Comments
 (0)