Skip to content

Commit 5be0639

Browse files
authored
add removal asset method (#18)
1 parent 27c4c68 commit 5be0639

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import runCreator from 'run'
55
import { createTextureFromSource } from 'WebGPU/getTexture'
66
import {
77
init_state,
8-
add_texture,
8+
add_asset,
9+
remove_asset,
910
connect_on_asset_update_callback,
1011
destroy_state,
1112
import_icons,
@@ -21,6 +22,7 @@ export type SerializedAsset = Omit<AssetZig, 'texture_id'> & {
2122

2223
export interface CreatorAPI {
2324
addImage: (img: HTMLImageElement, points?: PointUV[]) => void
25+
removeAsset: VoidFunction
2426
destroy: VoidFunction
2527
}
2628

@@ -78,7 +80,7 @@ export default async function initCreator(
7880
texture: createTextureFromSource(device, img, { flipY: true }),
7981
})
8082

81-
add_texture(points || getDefaultPoints(img, canvas), newTextureId)
83+
add_asset(points || getDefaultPoints(img, canvas), newTextureId)
8284
}
8385

8486
assets.forEach((asset) => {
@@ -108,8 +110,13 @@ export default async function initCreator(
108110

109111
const stopCreator = runCreator(canvas, context, device, presentationFormat, textures)
110112

113+
function removeAsset() {
114+
remove_asset()
115+
}
116+
111117
return {
112118
addImage,
119+
removeAsset,
113120
destroy: () => {
114121
stopCreator()
115122
destroy_state()

src/logic/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ type AssetZig = {
1818

1919
declare module '*.zig' {
2020
export const init_state: (width: number, height: number) => void
21-
export const add_texture: (points: PointUV[], texture_id: number) => void
21+
export const add_asset: (points: PointUV[], texture_id: number) => void
22+
export const remove_asset: () => void
2223
export const update_points: (id: number, points: PointUV[]) => void
2324

2425
export const on_update_pick: (id: number) => void

src/logic/index.zig

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ pub fn init_state(width: u32, height: u32) void {
6767
}
6868

6969
var next_asset_id: u32 = ASSET_ID_TRESHOLD;
70-
pub fn add_texture(points: [4]Types.PointUV, texture_id: u32) void {
70+
pub fn add_asset(points: [4]Types.PointUV, texture_id: u32) void {
7171
state.assets.put(next_asset_id, Texture.new(next_asset_id, points, texture_id)) catch unreachable;
7272
next_asset_id +%= 1;
73+
on_asset_update();
74+
}
75+
76+
pub fn remove_asset() void {
77+
state.assets.remove(state.active_asset_id) catch unreachable;
78+
on_asset_update();
7379
}
7480

7581
pub fn update_points(id: u32, points: [4]Types.PointUV) void {
@@ -95,13 +101,7 @@ pub fn on_pointer_down(x: f32, y: f32) void {
95101
}
96102
}
97103

98-
pub fn on_pointer_up() void {
99-
if (state.ongoing_action == .none) {
100-
state.active_asset_id = state.hovered_asset_id;
101-
}
102-
103-
state.ongoing_action = .none;
104-
104+
fn on_asset_update() void {
105105
var result = std.heap.page_allocator.alloc(AssetZig, state.assets.count()) catch unreachable;
106106
var iterator = state.assets.iterator();
107107
var i: usize = 0;
@@ -115,6 +115,16 @@ pub fn on_pointer_up() void {
115115
}
116116
}
117117

118+
pub fn on_pointer_up() void {
119+
if (state.ongoing_action == .none) {
120+
state.active_asset_id = state.hovered_asset_id;
121+
}
122+
123+
state.ongoing_action = .none;
124+
125+
on_asset_update();
126+
}
127+
118128
pub fn on_pointer_move(x: f32, y: f32) void {
119129
switch (state.ongoing_action) {
120130
.move => {

0 commit comments

Comments
 (0)