bevy_map_editor 0.1.6

Full-featured map editor for Bevy games with autotile support
Documentation
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! SpriteSheet Editor - Asset setup for spritesheets
//!
//! This module provides a standalone editor for configuring spritesheet assets:
//! - Loading spritesheet images
//! - Configuring grid dimensions (frame width/height, columns/rows)
//! - Setting pivot points
//! - Preview grid overlay

use bevy_egui::egui;
use bevy_map_animation::SpriteData;
use uuid::Uuid;

/// State for the SpriteSheet Editor
#[derive(Default, Clone)]
pub struct SpriteSheetEditorState {
    /// Asset ID being edited (project-level asset)
    pub asset_id: Option<Uuid>,
    /// Copy of sprite data being edited
    pub sprite_data: SpriteData,
    /// Path input buffer for text field
    pub sheet_path_input: String,
    /// Last loaded sheet path (for detecting changes)
    pub loaded_sheet_path: Option<String>,
    /// Texture ID for the loaded spritesheet
    pub spritesheet_texture_id: Option<egui::TextureId>,
    /// Size of the loaded spritesheet in pixels
    pub spritesheet_size: Option<(f32, f32)>,
    /// Zoom level for grid view
    pub zoom: f32,
    /// Scroll position for grid view
    pub scroll_offset: egui::Vec2,
    /// Hovered frame index for preview highlighting
    pub hovered_frame: Option<usize>,
}

impl SpriteSheetEditorState {
    pub fn new() -> Self {
        Self {
            zoom: 2.0, // Start at 2x zoom for better visibility
            ..Default::default()
        }
    }

    /// Create editor state from sprite data for editing a project-level asset
    pub fn from_sprite_data(sprite_data: SpriteData, asset_id: Uuid) -> Self {
        Self {
            asset_id: Some(asset_id),
            sprite_data: sprite_data.clone(),
            sheet_path_input: sprite_data.sheet_path,
            loaded_sheet_path: None,
            spritesheet_texture_id: None,
            spritesheet_size: None,
            zoom: 2.0,
            scroll_offset: egui::Vec2::ZERO,
            hovered_frame: None,
        }
    }

    /// Check if the spritesheet needs to be (re)loaded
    pub fn needs_texture_load(&self) -> bool {
        let current_path = &self.sprite_data.sheet_path;
        !current_path.is_empty()
            && (self.loaded_sheet_path.as_ref() != Some(current_path)
                || self.spritesheet_texture_id.is_none())
    }

    /// Set the loaded texture info
    pub fn set_texture(&mut self, texture_id: egui::TextureId, width: f32, height: f32) {
        self.spritesheet_texture_id = Some(texture_id);
        self.spritesheet_size = Some((width, height));
        self.loaded_sheet_path = Some(self.sprite_data.sheet_path.clone());
    }

    /// Clear the loaded texture
    pub fn clear_texture(&mut self) {
        self.spritesheet_texture_id = None;
        self.spritesheet_size = None;
        self.loaded_sheet_path = None;
    }

    /// Get the edited sprite data
    pub fn get_sprite_data(&self) -> SpriteData {
        self.sprite_data.clone()
    }
}

/// Result from SpriteSheet Editor rendering
#[derive(Default)]
pub struct SpriteSheetEditorResult {
    /// Whether sprite data was changed and should be saved
    pub changed: bool,
    /// Whether the editor should close
    pub close: bool,
    /// Whether to open file browser for spritesheet selection
    pub browse_spritesheet: bool,
    /// Whether the spritesheet path changed and needs reloading
    pub reload_spritesheet: bool,
}

/// Render the SpriteSheet Editor window
pub fn render_spritesheet_editor(
    ctx: &egui::Context,
    state: &mut SpriteSheetEditorState,
) -> SpriteSheetEditorResult {
    let mut result = SpriteSheetEditorResult::default();
    let mut is_open = true;

    egui::Window::new("SpriteSheet Editor")
        .open(&mut is_open)
        .resizable(true)
        .default_size([900.0, 700.0])
        .show(ctx, |ui| {
            // Top toolbar
            ui.horizontal(|ui| {
                if ui.button("Save & Close").clicked() {
                    result.changed = true;
                    result.close = true;
                }
                if ui.button("Cancel").clicked() {
                    result.close = true;
                }
                ui.separator();
                ui.label("Zoom:");
                if ui.button("-").clicked() {
                    state.zoom = (state.zoom - 0.25).max(0.25);
                }
                ui.label(format!("{:.0}%", state.zoom * 100.0));
                if ui.button("+").clicked() {
                    state.zoom = (state.zoom + 0.25).min(4.0);
                }
            });

            ui.separator();

            // Main content - left: settings, right: grid preview
            ui.columns(2, |columns| {
                // Left column: Spritesheet settings
                columns[0].vertical(|ui| {
                    render_spritesheet_settings(ui, state, &mut result);
                });

                // Right column: Grid preview
                columns[1].vertical(|ui| {
                    render_spritesheet_grid_preview(ui, state);
                });
            });
        });

    // If the window close button was clicked, mark as closed
    if !is_open {
        result.close = true;
    }

    result
}

/// Render spritesheet settings (path, frame size, grid config, pivot)
fn render_spritesheet_settings(
    ui: &mut egui::Ui,
    state: &mut SpriteSheetEditorState,
    result: &mut SpriteSheetEditorResult,
) {
    ui.heading("Spritesheet Settings");

    // Sheet path with Browse button
    ui.horizontal(|ui| {
        ui.label("Sheet Path:");
    });
    ui.horizontal(|ui| {
        let text_response = ui.add(
            egui::TextEdit::singleline(&mut state.sheet_path_input)
                .desired_width(200.0)
                .hint_text("path/to/spritesheet.png"),
        );
        if text_response.changed() {
            state.sprite_data.sheet_path = state.sheet_path_input.clone();
            result.changed = true;
            result.reload_spritesheet = true;
        }

        // Browse button (native only via rfd)
        #[cfg(not(target_arch = "wasm32"))]
        if ui.button("Browse...").clicked() {
            result.browse_spritesheet = true;
        }

        // Reload button
        if ui.button("").on_hover_text("Reload spritesheet").clicked() {
            state.clear_texture();
            result.reload_spritesheet = true;
        }
    });

    // Show spritesheet info if loaded
    if let Some((width, height)) = state.spritesheet_size {
        ui.label(format!("Image: {}x{} px", width as u32, height as u32));

        // If frame dimensions aren't set yet and image is loaded, suggest auto-detecting
        if state.sprite_data.frame_width == 0 || state.sprite_data.frame_height == 0 {
            ui.colored_label(
                egui::Color32::from_rgb(200, 200, 0),
                "ℹ Set frame size or use auto-detect",
            );
        }
    } else if !state.sprite_data.sheet_path.is_empty() {
        ui.colored_label(egui::Color32::YELLOW, "⏳ Image loading...");
    }

    ui.add_space(8.0);

    // Grid configuration section
    ui.group(|ui| {
        ui.label("Grid Configuration:");

        ui.horizontal(|ui| {
            ui.label("Frame Width:");
            let mut width = state.sprite_data.frame_width as i32;
            if ui
                .add(egui::DragValue::new(&mut width).range(1..=1024))
                .changed()
            {
                state.sprite_data.frame_width = width.max(1) as u32;
                result.changed = true;
            }

            ui.label("Height:");
            let mut height = state.sprite_data.frame_height as i32;
            if ui
                .add(egui::DragValue::new(&mut height).range(1..=1024))
                .changed()
            {
                state.sprite_data.frame_height = height.max(1) as u32;
                result.changed = true;
            }
        });

        ui.horizontal(|ui| {
            ui.label("Columns:");
            let mut cols = state.sprite_data.columns as i32;
            if ui
                .add(egui::DragValue::new(&mut cols).range(1..=100))
                .changed()
            {
                state.sprite_data.columns = cols.max(1) as u32;
                result.changed = true;
            }

            ui.label("Rows:");
            let mut rows = state.sprite_data.rows as i32;
            if ui
                .add(egui::DragValue::new(&mut rows).range(1..=100))
                .changed()
            {
                state.sprite_data.rows = rows.max(1) as u32;
                result.changed = true;
            }
        });

        // Auto-detect buttons
        if let Some((img_width, img_height)) = state.spritesheet_size {
            ui.horizontal(|ui| {
                if ui
                    .button("🔍 Auto-detect")
                    .on_hover_text("Calculate rows/columns from frame size")
                    .clicked()
                {
                    if state.sprite_data.frame_width > 0 && state.sprite_data.frame_height > 0 {
                        state.sprite_data.columns =
                            (img_width as u32) / state.sprite_data.frame_width;
                        state.sprite_data.rows =
                            (img_height as u32) / state.sprite_data.frame_height;
                        result.changed = true;
                    }
                }

                // Quick preset buttons for common grid layouts
                if ui.button("1×1").on_hover_text("Single sprite").clicked() {
                    state.sprite_data.columns = 1;
                    state.sprite_data.rows = 1;
                    result.changed = true;
                }
                if ui
                    .button("4×4")
                    .on_hover_text("4 columns, 4 rows")
                    .clicked()
                {
                    state.sprite_data.columns = 4;
                    state.sprite_data.rows = 4;
                    result.changed = true;
                }
                if ui.button("8×1").on_hover_text("8 columns, 1 row").clicked() {
                    state.sprite_data.columns = 8;
                    state.sprite_data.rows = 1;
                    result.changed = true;
                }
            });
        }
    });

    ui.add_space(8.0);

    // Pivot configuration
    ui.group(|ui| {
        ui.label("Pivot Point:");
        ui.horizontal(|ui| {
            ui.label("X:");
            let mut px = state.sprite_data.pivot_x;
            if ui
                .add(egui::DragValue::new(&mut px).range(0.0..=1.0).speed(0.01))
                .changed()
            {
                state.sprite_data.pivot_x = px;
                result.changed = true;
            }

            ui.label("Y:");
            let mut py = state.sprite_data.pivot_y;
            if ui
                .add(egui::DragValue::new(&mut py).range(0.0..=1.0).speed(0.01))
                .changed()
            {
                state.sprite_data.pivot_y = py;
                result.changed = true;
            }

            // Preset pivot buttons
            if ui.button("Center").on_hover_text("0.5, 0.5").clicked() {
                state.sprite_data.pivot_x = 0.5;
                state.sprite_data.pivot_y = 0.5;
                result.changed = true;
            }
            if ui.button("Bottom").on_hover_text("0.5, 1.0").clicked() {
                state.sprite_data.pivot_x = 0.5;
                state.sprite_data.pivot_y = 1.0;
                result.changed = true;
            }
        });
    });

    ui.add_space(8.0);

    let total_frames = state.sprite_data.total_frames();
    ui.label(format!("Total frames: {}", total_frames));

    // Sprite data name
    ui.add_space(8.0);
    ui.horizontal(|ui| {
        ui.label("Name:");
        if ui
            .text_edit_singleline(&mut state.sprite_data.name)
            .changed()
        {
            result.changed = true;
        }
    });
}

/// Open file dialog for selecting a spritesheet image
#[cfg(not(target_arch = "wasm32"))]
pub fn open_spritesheet_dialog() -> Option<String> {
    rfd::FileDialog::new()
        .add_filter("Images", &["png", "jpg", "jpeg", "webp", "gif", "bmp"])
        .pick_file()
        .map(|p| p.to_string_lossy().to_string())
}

/// Render the spritesheet grid preview (hover only, no click selection)
fn render_spritesheet_grid_preview(ui: &mut egui::Ui, state: &mut SpriteSheetEditorState) {
    ui.heading("Grid Preview");
    ui.label("Hover over frames to see frame numbers");

    let frame_w = state.sprite_data.frame_width as f32 * state.zoom;
    let frame_h = state.sprite_data.frame_height as f32 * state.zoom;
    let cols = state.sprite_data.columns.max(1);
    let rows = state.sprite_data.rows.max(1);

    let total_w = frame_w * cols as f32;
    let total_h = frame_h * rows as f32;

    // Scroll area for the grid
    egui::ScrollArea::both()
        .auto_shrink([false, false])
        .show(ui, |ui| {
            let (response, painter) =
                ui.allocate_painter(egui::vec2(total_w, total_h), egui::Sense::hover());

            let rect = response.rect;

            // Draw background
            painter.rect_filled(rect, 0.0, egui::Color32::from_gray(40));

            // Draw spritesheet image if loaded
            if let (Some(texture_id), Some((img_width, img_height))) =
                (state.spritesheet_texture_id, state.spritesheet_size)
            {
                // Calculate the portion of the image to show based on grid settings
                let grid_width =
                    state.sprite_data.columns as f32 * state.sprite_data.frame_width as f32;
                let grid_height =
                    state.sprite_data.rows as f32 * state.sprite_data.frame_height as f32;

                // UV coordinates for the grid portion
                let u_max = (grid_width / img_width).min(1.0);
                let v_max = (grid_height / img_height).min(1.0);

                let mesh = egui::Mesh {
                    texture_id,
                    indices: vec![0, 1, 2, 0, 2, 3],
                    vertices: vec![
                        egui::epaint::Vertex {
                            pos: rect.min,
                            uv: egui::pos2(0.0, 0.0),
                            color: egui::Color32::WHITE,
                        },
                        egui::epaint::Vertex {
                            pos: egui::pos2(rect.max.x, rect.min.y),
                            uv: egui::pos2(u_max, 0.0),
                            color: egui::Color32::WHITE,
                        },
                        egui::epaint::Vertex {
                            pos: rect.max,
                            uv: egui::pos2(u_max, v_max),
                            color: egui::Color32::WHITE,
                        },
                        egui::epaint::Vertex {
                            pos: egui::pos2(rect.min.x, rect.max.y),
                            uv: egui::pos2(0.0, v_max),
                            color: egui::Color32::WHITE,
                        },
                    ],
                };
                painter.add(egui::Shape::mesh(mesh));
            }

            // Detect hovered frame
            let mut hovered_idx: Option<usize> = None;
            if let Some(pos) = response.hover_pos() {
                let local_x = pos.x - rect.min.x;
                let local_y = pos.y - rect.min.y;

                let col = (local_x / frame_w) as u32;
                let row = (local_y / frame_h) as u32;

                if col < cols && row < rows {
                    hovered_idx = Some(state.sprite_data.grid_to_frame(col, row));
                }
            }
            state.hovered_frame = hovered_idx;

            // Draw frame grid overlay
            for row in 0..rows {
                for col in 0..cols {
                    let frame_idx = state.sprite_data.grid_to_frame(col, row);
                    let x = rect.min.x + col as f32 * frame_w;
                    let y = rect.min.y + row as f32 * frame_h;
                    let frame_rect =
                        egui::Rect::from_min_size(egui::pos2(x, y), egui::vec2(frame_w, frame_h));

                    // Highlight hovered frame
                    let is_hovered = hovered_idx == Some(frame_idx);
                    if is_hovered {
                        painter.rect_filled(
                            frame_rect,
                            0.0,
                            egui::Color32::from_rgba_unmultiplied(255, 200, 100, 80),
                        );
                    }

                    // Draw grid lines
                    painter.rect_stroke(
                        frame_rect,
                        0.0,
                        egui::Stroke::new(
                            1.0,
                            egui::Color32::from_rgba_unmultiplied(200, 200, 200, 150),
                        ),
                        egui::StrokeKind::Middle,
                    );

                    // Draw frame number
                    let text_color = if is_hovered {
                        egui::Color32::WHITE
                    } else {
                        egui::Color32::from_rgba_unmultiplied(255, 255, 255, 200)
                    };
                    painter.text(
                        egui::pos2(x + 4.0, y + 4.0),
                        egui::Align2::LEFT_TOP,
                        format!("{}", frame_idx),
                        egui::FontId::proportional(10.0 * state.zoom.max(0.5)),
                        text_color,
                    );
                }
            }
        });
}