Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save and load #28

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,144 changes: 633 additions & 511 deletions Cargo.lock

Large diffs are not rendered by default.

137 changes: 72 additions & 65 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,88 +6,95 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy_pixel_buffer = { version = "0.6.0", features = ["egui"] }
bevy_pixel_buffer = { version = "0.7.0", features = ["egui"] }
colorgrad = "0.6.2"
egui_plot = "0.23.0"
egui_file = "0.16.3"
egui_plot = "0.26.2"
rand = "0.8.5"
rayon = "1.8.0"
serde = { version = "1.0.197", features = ["serde_derive"] }
serde_json = "1.0.114"
smallvec = "1.11.1"
spectrum-analyzer = "1.5.0"

[dependencies.egui]
version = "*"
features = ["serde"]

[dependencies.bevy]
version = "0.12"
version = "0.13"
# Disable the default features if there are any that you do not want
default-features = false
features = [
# Bevy functionality:
"multi-threaded", # Run with multithreading
"bevy_asset", # Assets management
# "bevy_audio", # Builtin audio
# "bevy_gilrs", # Gamepad input support
# "bevy_scene", # Scenes management
"bevy_winit", # Window management
"bevy_render", # Rendering framework core
"bevy_core_pipeline", # Common rendering abstractions
# "bevy_gizmos", # Support drawing debug lines and shapes
"bevy_sprite", # 2D (sprites) rendering
# "bevy_pbr", # 3D (physically-based) rendering
# "bevy_gltf", # GLTF 3D assets format support
"bevy_text", # Text/font rendering
"bevy_ui", # UI toolkit
# "animation", # Animation support
# "tonemapping_luts", # Support different camera Tonemapping modes (embeds extra data)
# "filesystem_watcher", # Asset hot-reloading
"default_font", # Embed a minimal default font for text/UI
# Bevy functionality:
"multi-threaded", # Run with multithreading
"bevy_asset", # Assets management
# "bevy_audio", # Builtin audio
# "bevy_gilrs", # Gamepad input support
# "bevy_scene", # Scenes management
"bevy_winit", # Window management
"bevy_render", # Rendering framework core
"bevy_core_pipeline", # Common rendering abstractions
# "bevy_gizmos", # Support drawing debug lines and shapes
"bevy_sprite", # 2D (sprites) rendering
# "bevy_pbr", # 3D (physically-based) rendering
# "bevy_gltf", # GLTF 3D assets format support
"bevy_text", # Text/font rendering
"bevy_ui", # UI toolkit
# "animation", # Animation support
# "tonemapping_luts", # Support different camera Tonemapping modes (embeds extra data)
# "filesystem_watcher", # Asset hot-reloading
"default_font", # Embed a minimal default font for text/UI

# File formats:
"png", # PNG image format for simple 2D images
# "hdr", # HDR images
# "ktx2", # Preferred format for GPU textures
# "zstd", # ZSTD compression support in KTX2 files
# "vorbis", # Audio: OGG Vorbis
# File formats:
"png", # PNG image format for simple 2D images
# "hdr", # HDR images
# "ktx2", # Preferred format for GPU textures
# "zstd", # ZSTD compression support in KTX2 files
# "vorbis", # Audio: OGG Vorbis

# Platform-specific:
"x11", # Linux: Support X11 windowing system
# "android_shared_stdcxx", # Android: use shared C++ library
# "webgl2", # Web: use WebGL2 instead of WebGPU
# Platform-specific:
"x11", # Linux: Support X11 windowing system
# "android_shared_stdcxx", # Android: use shared C++ library
# "webgl2", # Web: use WebGL2 instead of WebGPU

# These are other features that may be of interest:
# (add any of these that you need)
# These are other features that may be of interest:
# (add any of these that you need)

# Bevy functionality:
"subpixel_glyph_atlas", # Subpixel antialiasing for text/fonts
"serialize", # Support for `serde` Serialize/Deserialize
# Bevy functionality:
"subpixel_glyph_atlas", # Subpixel antialiasing for text/fonts
"serialize", # Support for `serde` Serialize/Deserialize

# File formats:###
# "dds", # Alternative DirectX format for GPU textures, instead of KTX2
# "jpeg", # JPEG lossy format for 2D photos
# "webp", # WebP image format
# "bmp", # Uncompressed BMP image format
# "tga", # Truevision Targa image format
# "exr", # OpenEXR advanced image format
# "pnm", # PNM (pam, pbm, pgm, ppm) image format
"basis-universal", # Basis Universal GPU texture compression format
# "zlib", # zlib compression support in KTX2 files
# "flac", # Audio: FLAC lossless format
# "mp3", # Audio: MP3 format (not recommended)
# "wav", # Audio: Uncompressed WAV
# "symphonia-all", # All Audio formats supported by the Symphonia library
"shader_format_glsl", # GLSL shader support
# "shader_format_spirv", # SPIR-V shader support
# File formats:###
# "dds", # Alternative DirectX format for GPU textures, instead of KTX2
# "jpeg", # JPEG lossy format for 2D photos
# "webp", # WebP image format
# "bmp", # Uncompressed BMP image format
# "tga", # Truevision Targa image format
# "exr", # OpenEXR advanced image format
# "pnm", # PNM (pam, pbm, pgm, ppm) image format
"basis-universal", # Basis Universal GPU texture compression format
# "zlib", # zlib compression support in KTX2 files
# "flac", # Audio: FLAC lossless format
# "mp3", # Audio: MP3 format (not recommended)
# "wav", # Audio: Uncompressed WAV
# "symphonia-all", # All Audio formats supported by the Symphonia library
"shader_format_glsl", # GLSL shader support
# "shader_format_spirv", # SPIR-V shader support

# Platform-specific:
"wayland", # (Linux) Support Wayland windowing system
# "accesskit_unix", # (Unix-like) AccessKit integration for UI Accessibility
# "bevy_dynamic_plugin", # (Desktop) support for loading of `DynamicPlugin`s
# Platform-specific:
"wayland", # (Linux) Support Wayland windowing system
# "accesskit_unix", # (Unix-like) AccessKit integration for UI Accessibility
# "bevy_dynamic_plugin", # (Desktop) support for loading of `DynamicPlugin`s

# Development/Debug features:
# "dynamic_linking", # Dynamic linking for faster compile-times
"trace", # Enable tracing for performance measurement
# "detailed_trace", # Make traces more verbose
# "trace_tracy", # Tracing using `tracy`
# "trace_tracy_memory", # + memory profiling
# "trace_chrome", # Tracing using the Chrome format
# "wgpu_trace", # WGPU/rendering tracing
# Development/Debug features:
# "dynamic_linking", # Dynamic linking for faster compile-times
"trace", # Enable tracing for performance measurement
# "detailed_trace", # Make traces more verbose
# "trace_tracy", # Tracing using `tracy`
# "trace_tracy_memory", # + memory profiling
# "trace_chrome", # Tracing using the Chrome format
# "wgpu_trace", # WGPU/rendering tracing
]

# Enable a small amount of optimization in debug mode
Expand Down
1 change: 1 addition & 0 deletions examples/slit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"sources":[{"x":400,"y":400,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":0}],"mics":[{"x":399,"y":90,"id":0,"record":[[0.0,0.0]],"spectrum":[]}],"wallblocks":[{"rect":{"min":{"x":403.0,"y":174.0},"max":{"x":699.0,"y":211.0}},"center":{"x":403.0,"y":174.0},"reflection_factor":1.0,"calc_rect":{"min":{"x":403.0,"y":174.0},"max":{"x":699.0,"y":211.0}},"calc_rect_with_boundaries":{"min":{"x":453.0,"y":224.0},"max":{"x":798.0,"y":261.0}},"id":1},{"rect":{"min":{"x":-8.5,"y":170.0},"max":{"x":393.0,"y":212.0}},"center":{"x":5.0,"y":170.0},"reflection_factor":1.0,"calc_rect":{"min":{"x":0.0,"y":170.0},"max":{"x":393.0,"y":212.0}},"calc_rect_with_boundaries":{"min":{"x":41.5,"y":220.0},"max":{"x":443.0,"y":262.0}},"id":0}]}
1 change: 1 addition & 0 deletions examples/wave_wall.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"sources":[{"x":212,"y":27,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":20},{"x":266,"y":266,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":1},{"x":208,"y":99,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":2},{"x":208,"y":133,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":3},{"x":208,"y":165,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":4},{"x":208,"y":205,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":5},{"x":208,"y":228,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":6},{"x":206,"y":272,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":7},{"x":206,"y":306,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":8},{"x":206,"y":336,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":9},{"x":206,"y":361,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":10},{"x":198,"y":421,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":11},{"x":198,"y":477,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":12},{"x":198,"y":504,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":13},{"x":198,"y":525,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":14},{"x":197,"y":600,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":15},{"x":196,"y":681,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":16},{"x":195,"y":568,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":17},{"x":195,"y":630,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":18},{"x":212,"y":63,"phase":0.0,"frequency":10000.0,"amplitude":10.0,"source_type":"Sin","id":19}],"mics":[{"x":250,"y":250,"id":0,"record":[[0.0,0.0]],"spectrum":[]},{"x":100,"y":100,"id":1,"record":[[0.0,0.0]],"spectrum":[]},{"x":650,"y":650,"id":2,"record":[[0.0,0.0]],"spectrum":[]}],"wallblocks":[]}
3 changes: 2 additions & 1 deletion src/components/microphone.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use bevy::prelude::*;
use bevy_pixel_buffer::bevy_egui::egui::emath::Pos2;
use serde::{Deserialize, Serialize};

use super::states::Gizmo;
use crate::grid::plugin::ComponentIDs;
use crate::math::constants::*;
use crate::math::transformations::f32_map_range;

#[derive(Debug, Default, Component)]
#[derive(Debug, Default, Component, Serialize, Deserialize, Clone)]
/// A microphone on the grid that records the pressure at its position
pub struct Microphone {
pub x: u32,
Expand Down
5 changes: 3 additions & 2 deletions src/components/source.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::f32::consts::PI;

use bevy::prelude::*;
use serde::{Deserialize, Serialize};

use crate::grid::plugin::ComponentIDs;
use crate::math::constants::*;

#[derive(Debug, Default, Component)]
#[derive(Debug, Default, Component, Serialize, Deserialize, Clone)]
/// A sound source on the grid
pub struct Source {
pub x: u32,
Expand All @@ -21,7 +22,7 @@ pub struct Source {
pub id: usize,
}

#[derive(Debug, Default, PartialEq)]
#[derive(Debug, Default, PartialEq, Serialize, Deserialize, Clone)]
pub enum SourceType {
#[default]
Sin,
Expand Down
3 changes: 2 additions & 1 deletion src/components/wall.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::prelude::*;
use bevy_pixel_buffer::bevy_egui::egui::{Pos2, Rect};
use serde::{Deserialize, Serialize};

use crate::math::constants::{SIMULATION_HEIGHT, SIMULATION_WIDTH};
use crate::math::transformations::true_rect_from_rect;
Expand Down Expand Up @@ -38,7 +39,7 @@ impl WallCell {

impl Wall for WallCell {}

#[derive(Debug, Component)]
#[derive(Debug, Component, Serialize, Deserialize)]
pub struct WallBlock {
pub rect: Rect,
pub center: Pos2,
Expand Down
4 changes: 2 additions & 2 deletions src/input/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::math::transformations::{screen_to_grid, screen_to_nearest_grid};
use crate::render::state::{ToolType, UiState, WallBrush};

pub fn button_input(
mouse_buttons: Res<Input<MouseButton>>,
keys: Res<Input<KeyCode>>,
mouse_buttons: Res<ButtonInput<MouseButton>>,
keys: Res<ButtonInput<KeyCode>>,
q_windows: Query<&Window, With<PrimaryWindow>>,
sources: Query<(Entity, &Source), Without<Drag>>,
mut drag_sources: Query<(Entity, &mut Source), With<Drag>>,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod components;
pub mod grid;
pub mod input;
pub mod loading;
pub mod math;
pub mod render;
pub mod saving;
19 changes: 19 additions & 0 deletions src/loading.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::path::Path;

use serde::Deserialize;

use crate::components::microphone::Microphone;
use crate::components::source::Source;
use crate::components::wall::WallBlock;

#[derive(Deserialize)]
pub struct SaveData {
pub sources: Vec<Source>,
pub mics: Vec<Microphone>,
pub wallblocks: Vec<WallBlock>,
}

pub fn load(path: &Path) -> SaveData {
let file = std::fs::File::open(path).unwrap();
serde_json::from_reader(file).unwrap()
}
7 changes: 6 additions & 1 deletion src/render/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::prelude::*;
use bevy_pixel_buffer::bevy_egui::egui::{self, Vec2};
use bevy_pixel_buffer::bevy_egui::egui::Vec2;
use egui_file::FileDialog;

#[derive(Default, Resource)]
pub struct GameTicks {
Expand Down Expand Up @@ -58,6 +59,8 @@ pub struct UiState {
pub wall_brush: WallBrush,
pub wall_brush_radius: u32,
pub tools_enabled: bool,
pub open_file_dialog: Option<FileDialog>,
pub save_file_dialog: Option<FileDialog>,
}

impl Default for UiState {
Expand All @@ -80,6 +83,8 @@ impl Default for UiState {
wall_brush: WallBrush::Rectangle,
wall_brush_radius: 10,
tools_enabled: true,
open_file_dialog: None,
save_file_dialog: None,
}
}
}
Expand Down
Loading
Loading