Skip to content

Commit

Permalink
Egui image api (#72)
Browse files Browse the repository at this point in the history
* chore: use universal set of lints across crates

* chore: 🚨 auto-fix most warnings

* chore: disable panic and unwrap lints for now

* chore: 🚨 fix wasily fixable warnings

* refactor(image cache): ♻️ add basic egui texture loader

* fix(image cache): 🐛 handle image load errors semi-gracefully

* fix(image cache): 🐛 clear up the difference between uris and paths

* refactor(image cache): ♻️ drop the use of multiple bind groups for one monolithic bind group

* refactor(graphics): ♻️ remove pervasive passing of push constant support

* fix(tilemap): 🐛 fix various shader and bind group issues

* refactor(tilemap): ♻️ don't pass viewports around constantly

* fix(tilemap): 🐛 fix the collision shader crashing

* refactor: ♻️ remove all uses of retainedimage

* fix(image cache): 🐛 fix loading errors & textures not being srgb

* fix(image cache): 🐛 fix srgb weirdness

* fix(image cache): 🐛 properly unload textures

* refactor(image cache): 🔥 remove TextureLoader and add BytesLoader instead

* refactor: ♻️ Rename atlas cache to atlas loader

* fix: 🚨 fix compile error on wasm

* Add CJK font (#71)

* feat: add Source Han Sans (for displaying CJK characters)

* perf: compress fonts to reduce binary size

* rustfmt

* fix(tilemap): 🐛 fix collision shader on native

I don't know how I missed this, but the collision shader still expected there to be a bind group when we weren't passing one!

---------

Co-authored-by: 刘皓 <[email protected]>
  • Loading branch information
melody-rs and white-axe authored Dec 3, 2023
1 parent 3a18b13 commit 17c6c87
Show file tree
Hide file tree
Showing 67 changed files with 1,259 additions and 1,044 deletions.
24 changes: 24 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ categories.workspace = true
members = ["crates/*"]
resolver = "2"

[workspace.lints.rust]
rust_2018_idioms = "warn"
# unsafe code is sometimes fine but in general we don't want to use it.
unsafe_code = "warn"
# clearly denote where you are using unsafe code in unsafe fns. in a future rust edition this will become a hard error.
unsafe_op_in_unsafe_fn = "forbid"
# will become a hard error in a future rust edition
elided_lifetimes_in_paths = "forbid"

[workspace.lints.clippy]
all = "warn"
# we should turn these on in the future to avoid possible crashes
# panic = "warn"
# panic_in_result_fn = "warn"
# panicking_unwrap = "warn"
unnecessary_wraps = "warn"

missing_errors_doc = "allow"
doc_markdown = "allow"
missing_panics_doc = "allow"
too_many_lines = "allow"
# you must provide a safety doc.
missing_safety_doc = "forbid"

[workspace.package]
version = "0.4.0"
authors = [
Expand Down
4 changes: 3 additions & 1 deletion crates/audio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ categories.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
[lints]
workspace = true

[dependencies]
rustysynth = "1.3.1"

strum.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions crates/components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ categories.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lints]
workspace = true

[dependencies]
luminol-audio.workspace = true
luminol-core.workspace = true
Expand Down
16 changes: 4 additions & 12 deletions crates/components/src/command_view/command_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,13 @@
impl super::CommandView {
pub fn command_ui<'i, I>(
&mut self,
ui: &mut egui::Ui,
db: &luminol_config::command_db::CommandDB,
(index, command): (usize, &'i mut luminol_data::rpg::EventCommand),
iter: &mut std::iter::Peekable<I>,
_ui: &mut egui::Ui,
_db: &luminol_config::command_db::CommandDB,
(_index, _command): (usize, &'i mut luminol_data::rpg::EventCommand),
_iter: &mut std::iter::Peekable<I>,
) where
I: Iterator<Item = (usize, &'i mut luminol_data::rpg::EventCommand)>,
{
todo!()
}
}

fn parameter_label(
string: &mut String,
parameter: &luminol_data::commands::Parameter,
command: &mut luminol_data::rpg::EventCommand,
) -> std::fmt::Result {
todo!()
}
20 changes: 9 additions & 11 deletions crates/components/src/command_view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,31 @@ mod ui;
use std::collections::HashMap;

pub struct CommandView {
selected_index: usize,
window_state: WindowState,
id: egui::Id,
modals: HashMap<u64, bool>, // todo find a better way to handle modals
_selected_index: usize,
_window_state: WindowState,
_id: egui::Id,
_modals: HashMap<u64, bool>, // todo find a better way to handle modals
}

enum WindowState {
Insert { index: usize, tab: usize },
Edit { index: usize },
None,
}

impl Default for CommandView {
fn default() -> Self {
Self {
selected_index: 0,
window_state: WindowState::None,
id: egui::Id::new("command_view"),
modals: HashMap::new(),
_selected_index: 0,
_window_state: WindowState::None,
_id: egui::Id::new("command_view"),
_modals: HashMap::new(),
}
}
}

impl CommandView {
pub fn new(id: impl std::hash::Hash) -> Self {
Self {
id: egui::Id::new(id),
_id: egui::Id::new(id),
..Default::default()
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/components/src/command_view/parameter_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ impl CommandView {
#[allow(clippy::only_used_in_recursion)]
pub fn parameter_ui(
&mut self,
ui: &mut egui::Ui,
parameter: &luminol_data::commands::Parameter,
command: &mut luminol_data::rpg::EventCommand,
_ui: &mut egui::Ui,
_parameter: &luminol_data::commands::Parameter,
_command: &mut luminol_data::rpg::EventCommand,
) {
todo!()
}
Expand Down
6 changes: 3 additions & 3 deletions crates/components/src/command_view/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ impl super::CommandView {
#[allow(clippy::ptr_arg)]
pub fn ui(
&mut self,
ui: &mut egui::Ui,
db: &luminol_config::command_db::CommandDB,
commands: &mut Vec<luminol_data::rpg::EventCommand>,
_ui: &mut egui::Ui,
_db: &luminol_config::command_db::CommandDB,
_commands: &mut Vec<luminol_data::rpg::EventCommand>,
) {
todo!()
}
Expand Down
22 changes: 7 additions & 15 deletions crates/components/src/map_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use itertools::Itertools;

#[derive(Debug)]
pub struct MapView {
/// Toggle to display the visible region in-game.
pub visible_display: bool,
Expand Down Expand Up @@ -84,35 +83,28 @@ impl MapView {
|x, y, passage| passages[(x, y)] = passage,
);

let atlas = update_state.graphics.atlas_cache.load_atlas(
let atlas = update_state.graphics.atlas_loader.load_atlas(
&update_state.graphics,
update_state.filesystem,
tileset,
)?;
let events = map
.events
.iter()
.map(|(id, e)| {
.map(|(id, e)| -> anyhow::Result<_> {
let sprite = luminol_graphics::Event::new(
&update_state.graphics,
update_state.filesystem,
e,
&atlas,
update_state.graphics.push_constants_supported(),
);
)?;
let preview_sprite = luminol_graphics::Event::new(
&update_state.graphics,
update_state.filesystem,
e,
&atlas,
update_state.graphics.push_constants_supported(),
);
let Ok(sprite) = sprite else {
return Err(sprite.unwrap_err());
};
let Ok(preview_sprite) = preview_sprite else {
return Err(preview_sprite.unwrap_err());
};
)?;

Ok(if let Some(sprite) = sprite {
preview_sprite.map(|preview_sprite| (id, (sprite, preview_sprite)))
} else {
Expand All @@ -127,7 +119,6 @@ impl MapView {
&map,
tileset,
&passages,
update_state.graphics.push_constants_supported(),
)?;

Ok(Self {
Expand Down Expand Up @@ -159,7 +150,8 @@ impl MapView {
})
}

// FIXME
// FIXME lots of arguments
#[allow(clippy::too_many_arguments)]
pub fn ui(
&mut self,
ui: &mut egui::Ui,
Expand Down
2 changes: 1 addition & 1 deletion crates/components/src/sound_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl SoundTab {
.show_inside(ui, |ui| {
ui.vertical(|ui| {
ui.horizontal(|ui| {
if ui.button("Play").clicked() && !self.selected_track.as_str().is_empty() {
if ui.button("Play").clicked() && !self.selected_track.is_empty() {
let path = format!("Audio/{}/{}", self.source, &self.selected_track);
let pitch = self.pitch;
let volume = self.volume;
Expand Down
63 changes: 22 additions & 41 deletions crates/components/src/tilepicker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
// along with Luminol. If not, see <http://www.gnu.org/licenses/>.

use itertools::Itertools;
use slab::Slab;
use std::sync::Arc;
use std::time::Duration;

#[derive(Debug)]
pub struct Tilepicker {
pub selected_tiles_left: i16,
pub selected_tiles_top: i16,
Expand All @@ -30,14 +28,13 @@ pub struct Tilepicker {
drag_origin: Option<egui::Pos2>,

resources: Arc<Resources>,
viewport: Arc<luminol_graphics::viewport::Viewport>,
ani_time: Option<f64>,
}

#[derive(Debug)]
struct Resources {
tiles: luminol_graphics::tiles::Tiles,
collision: luminol_graphics::collision::Collision,
viewport: luminol_graphics::viewport::Viewport,
}

struct Callback {
Expand All @@ -47,8 +44,12 @@ struct Callback {
coll_enabled: bool,
}

// FIXME
//? SAFETY:
//? wgpu resources are not Send + Sync on wasm, but egui_wgpu::CallbackTrait requires Send + Sync (because egui::Context is Send + Sync)
//? as long as this callback does not leave the thread it was created on on wasm (which it shouldn't be) these are ok.
#[allow(unsafe_code)]
unsafe impl Send for Callback {}
#[allow(unsafe_code)]
unsafe impl Sync for Callback {}

impl luminol_egui_wgpu::CallbackTrait for Callback {
Expand All @@ -58,22 +59,14 @@ impl luminol_egui_wgpu::CallbackTrait for Callback {
render_pass: &mut wgpu::RenderPass<'a>,
_callback_resources: &'a luminol_egui_wgpu::CallbackResources,
) {
self.resources.viewport.bind(1, render_pass);
self.resources.tiles.draw(
&self.graphics_state,
&self.resources.viewport,
&[true],
None,
render_pass,
);
self.resources
.tiles
.draw(&self.graphics_state, &[true], None, render_pass);

if self.coll_enabled {
self.resources.viewport.bind(0, render_pass);
self.resources.collision.draw(
&self.graphics_state,
&self.resources.viewport,
render_pass,
);
self.resources
.collision
.draw(&self.graphics_state, render_pass);
}
}
}
Expand Down Expand Up @@ -107,8 +100,6 @@ impl Default for SelectedTile {
}
}

type ResourcesSlab = Slab<Arc<Resources>>;

impl Tilepicker {
pub fn new(
update_state: &luminol_core::UpdateState<'_>,
Expand All @@ -120,7 +111,7 @@ impl Tilepicker {
let tilesets = update_state.data.tilesets();
let tileset = &tilesets[map.tileset_id];

let atlas = update_state.graphics.atlas_cache.load_atlas(
let atlas = update_state.graphics.atlas_loader.load_atlas(
&update_state.graphics,
update_state.filesystem,
tileset,
Expand All @@ -137,24 +128,17 @@ impl Tilepicker {
tilepicker_data,
);

let viewport = luminol_graphics::viewport::Viewport::new(
let viewport = Arc::new(luminol_graphics::viewport::Viewport::new(
&update_state.graphics,
glam::Mat4::orthographic_rh(
0.0,
256.,
atlas.tileset_height as f32 + 32.,
0.0,
-1.0,
1.0,
),
update_state.graphics.push_constants_supported(),
);
256.,
atlas.tileset_height as f32 + 32.,
));

let tiles = luminol_graphics::tiles::Tiles::new(
&update_state.graphics,
viewport.clone(),
atlas,
&tilepicker_data,
update_state.graphics.push_constants_supported(),
);

let mut passages =
Expand All @@ -175,16 +159,13 @@ impl Tilepicker {
.copy_from_slice(&tileset.passages.as_slice()[384..384 + length]);
let collision = luminol_graphics::collision::Collision::new(
&update_state.graphics,
viewport.clone(),
&passages,
update_state.graphics.push_constants_supported(),
);

Ok(Self {
resources: Arc::new(Resources {
tiles,
collision,
viewport,
}),
resources: Arc::new(Resources { tiles, collision }),
viewport,
ani_time: None,
selected_tiles_left: 0,
selected_tiles_top: 0,
Expand Down Expand Up @@ -241,7 +222,7 @@ impl Tilepicker {
.intersect(scroll_rect.translate(canvas_rect.min.to_vec2()));
let scroll_rect = absolute_scroll_rect.translate(-canvas_rect.min.to_vec2());

self.resources.viewport.set_proj(
self.viewport.set_proj(
&graphics_state.render_state,
glam::Mat4::orthographic_rh(
scroll_rect.left(),
Expand Down
3 changes: 3 additions & 0 deletions crates/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ categories.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lints]
workspace = true

[dependencies]
rust-ini.workspace = true

Expand Down
3 changes: 3 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ categories.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lints]
workspace = true

[dependencies]
egui.workspace = true

Expand Down
Loading

0 comments on commit 17c6c87

Please sign in to comment.