diff --git a/crates/suitcase/src/components/draw_background.rs b/crates/suitcase/src/components/draw_background.rs new file mode 100644 index 0000000..27ccfcd --- /dev/null +++ b/crates/suitcase/src/components/draw_background.rs @@ -0,0 +1,41 @@ +use eframe::egui; +use eframe::egui::{Color32, Ui}; + +pub fn draw_background(ui: &mut Ui, colors: &[Color32; 4]) { + let rect = ui.available_rect_before_wrap(); + let painter = ui.painter_at(rect); + + let top_left = rect.left_top(); + let top_right = rect.right_top(); + let bottom_left = rect.left_bottom(); + let bottom_right = rect.right_bottom(); + + let mut mesh = egui::epaint::Mesh::default(); + + let i0 = mesh.vertices.len() as u32; + mesh.vertices.push(egui::epaint::Vertex { + pos: top_left, + uv: egui::epaint::WHITE_UV, + color: colors[0], + }); + mesh.vertices.push(egui::epaint::Vertex { + pos: top_right, + uv: egui::epaint::WHITE_UV, + color: colors[1], + }); + mesh.vertices.push(egui::epaint::Vertex { + pos: bottom_right, + uv: egui::epaint::WHITE_UV, + color: colors[3], + }); + mesh.vertices.push(egui::epaint::Vertex { + pos: bottom_left, + uv: egui::epaint::WHITE_UV, + color: colors[2], + }); + + mesh.indices + .extend_from_slice(&[i0, i0 + 1, i0 + 2, i0, i0 + 2, i0 + 3]); + + painter.add(egui::Shape::mesh(mesh)); +} diff --git a/crates/suitcase/src/components/mod.rs b/crates/suitcase/src/components/mod.rs index 7621ed8..f74ba84 100644 --- a/crates/suitcase/src/components/mod.rs +++ b/crates/suitcase/src/components/mod.rs @@ -1,10 +1,11 @@ pub mod bottom_bar; pub mod buttons; pub mod dialogs; +pub mod draw_background; +pub(crate) mod file_picker; pub mod file_tree; +pub mod greeting; pub mod menu_bar; pub mod menu_item; pub mod tab_viewer; pub mod toolbar; -pub mod greeting; -pub(crate) mod file_picker; diff --git a/crates/suitcase/src/tabs/icn_viewer.rs b/crates/suitcase/src/tabs/icn_viewer.rs index 04735e6..ee696e8 100644 --- a/crates/suitcase/src/tabs/icn_viewer.rs +++ b/crates/suitcase/src/tabs/icn_viewer.rs @@ -1,3 +1,4 @@ +use crate::components::draw_background::draw_background; use crate::data::state::AppState; use crate::rendering::icn_renderer::ICNRenderer; use crate::tabs::PS2RgbaInterface; @@ -9,7 +10,10 @@ use crate::{ }; use cgmath::Vector3; use eframe::egui::load::SizedTexture; -use eframe::egui::{vec2, ColorImage, ComboBox, Grid, Id, ImageData, ImageSource, Stroke, TextureId, TextureOptions, WidgetText}; +use eframe::egui::{ + vec2, ColorImage, ComboBox, Grid, Id, ImageData, ImageSource, Stroke, TextureId, + TextureOptions, WidgetText, +}; use eframe::{ egui, egui::{include_image, menu, Color32, Ui}, @@ -64,9 +68,11 @@ impl<'a> TabViewer for ICNTabViewer<'a> { } ui.end_row(); ui.label("Compression"); - ComboBox::from_id_salt("compression").selected_text("On").show_ui(ui, |ui| { - ui.selectable_label(true, "On"); - }) + ComboBox::from_id_salt("compression") + .selected_text("On") + .show_ui(ui, |ui| { + ui.selectable_label(true, "On"); + }) }); } ICNTab::IconSysProperties => { @@ -139,25 +145,45 @@ impl ICNViewer { impl ICNViewer { pub fn new(file: &VirtualFile, state: &AppState) -> Self { let mut background_colors = [Color32::DARK_GRAY; 4]; - let mut light_colors = [ColorF{r: 0.0, g: 0.0, b: 0.0, a: 0.0}; 3]; - let mut light_positions = [Vector{x: 0.0, y: 0.0, z: 0.0, w: 0.0}; 3]; - let mut ambient_color = ColorF{r: 0.1, g: 0.1, b: 0.1, a: 0.0}; + let mut light_colors = [ColorF { + r: 0.0, + g: 0.0, + b: 0.0, + a: 0.0, + }; 3]; + let mut light_positions = [Vector { + x: 0.0, + y: 0.0, + z: 0.0, + w: 0.0, + }; 3]; + let mut ambient_color = ColorF { + r: 0.1, + g: 0.1, + b: 0.1, + a: 0.0, + }; let buf = std::fs::read(&file.file_path).expect("File not found"); let icon_sys = file.file_path.clone().parent().unwrap().join("icon.sys"); if icon_sys.exists() { let icon_sys = IconSys::new(std::fs::read(icon_sys).unwrap()); - background_colors = icon_sys.background_colors.map(|c| PS2RgbaInterface::build_from_color(c).into()); + background_colors = icon_sys + .background_colors + .map(|c| PS2RgbaInterface::build_from_color(c).into()); light_colors = icon_sys.light_colors; light_positions = icon_sys.light_directions; } let icn = ps2_filetypes::ICNParser::read(&buf.clone()).unwrap(); - let mut dock_state = - DockState::new(vec![ICNTab::IconProperties]); + let mut dock_state = DockState::new(vec![ICNTab::IconProperties]); - dock_state.main_surface_mut().split_below(NodeIndex::root(), 0.5, vec![ICNTab::IconSysProperties]); + dock_state.main_surface_mut().split_below( + NodeIndex::root(), + 0.5, + vec![ICNTab::IconSysProperties], + ); Self { dock_state, @@ -218,8 +244,7 @@ impl ICNViewer { let mut delta_pitch = 0.0; let mut delta_zoom = 0.0; - if response.dragged() - { + if response.dragged() { let delta = response.drag_delta(); delta_yaw -= delta.x * 0.01; delta_pitch += delta.y * 0.01; @@ -257,7 +282,15 @@ impl ICNViewer { if closing { renderer.drop(painter.gl()) } else { - renderer.paint(painter.gl(), aspect_ratio, camera, frame, light_colors, light_positions, ambient_color); + renderer.paint( + painter.gl(), + aspect_ratio, + camera, + frame, + light_colors, + light_positions, + ambient_color, + ); } })), }; @@ -358,10 +391,13 @@ impl ICNViewer { ui.vertical(|ui| { ui.set_height(ui.available_size_before_wrap().y - 28.0); - egui::Frame::canvas(ui.style()).stroke(Stroke::NONE).corner_radius(0).show(ui, |ui| { - draw_background(ui, &self.background_colors); - self.custom_painting(ui); - }); + egui::Frame::canvas(ui.style()) + .stroke(Stroke::NONE) + .corner_radius(0) + .show(ui, |ui| { + draw_background(ui, &self.background_colors); + self.custom_painting(ui); + }); }); if self.icn.animation_header.frame_length > 1 { @@ -415,42 +451,3 @@ impl Tab for ICNViewer { self.modified = false; } } - -fn draw_background(ui: &mut Ui, colors: &[Color32; 4]) { - let rect = ui.available_rect_before_wrap(); - let painter = ui.painter_at(rect); - - let top_left = rect.left_top(); - let top_right = rect.right_top(); - let bottom_left = rect.left_bottom(); - let bottom_right = rect.right_bottom(); - - let mut mesh = egui::epaint::Mesh::default(); - - let i0 = mesh.vertices.len() as u32; - mesh.vertices.push(egui::epaint::Vertex { - pos: top_left, - uv: egui::epaint::WHITE_UV, - color: colors[0], - }); - mesh.vertices.push(egui::epaint::Vertex { - pos: top_right, - uv: egui::epaint::WHITE_UV, - color: colors[1], - }); - mesh.vertices.push(egui::epaint::Vertex { - pos: bottom_right, - uv: egui::epaint::WHITE_UV, - color: colors[3], - }); - mesh.vertices.push(egui::epaint::Vertex { - pos: bottom_left, - uv: egui::epaint::WHITE_UV, - color: colors[2], - }); - - mesh.indices - .extend_from_slice(&[i0, i0 + 1, i0 + 2, i0, i0 + 2, i0 + 3]); - - painter.add(egui::Shape::mesh(mesh)); -} diff --git a/crates/suitcase/src/tabs/icon_sys_viewer.rs b/crates/suitcase/src/tabs/icon_sys_viewer.rs index dfdb78d..72ed24e 100644 --- a/crates/suitcase/src/tabs/icon_sys_viewer.rs +++ b/crates/suitcase/src/tabs/icon_sys_viewer.rs @@ -1,3 +1,4 @@ +use crate::components::draw_background::draw_background; use crate::tabs::Tab; use crate::{AppState, VirtualFile}; use eframe::egui; @@ -181,7 +182,7 @@ impl IconSysViewer { const SPACING: f32 = 40.0; ui.add_sized(vec2(SPACING * 3.0, SPACING * 3.0), |ui: &mut Ui| { - draw_background(ui, &self.background_colors); + draw_background(ui, &self.background_colors.map(|color| color.into())); ui.spacing_mut().interact_size = vec2(SPACING, SPACING); ui.spacing_mut().item_spacing = vec2(0.0, 0.0); @@ -369,42 +370,3 @@ fn file_select(ui: &mut Ui, name: impl Into, value: &mut String, files: }); }); } - -fn draw_background(ui: &mut Ui, colors: &[PS2RgbaInterface; 4]) { - let rect = ui.available_rect_before_wrap(); - let painter = ui.painter_at(rect); - - let top_left = rect.left_top(); - let top_right = rect.right_top(); - let bottom_left = rect.left_bottom(); - let bottom_right = rect.right_bottom(); - - let mut mesh = egui::epaint::Mesh::default(); - - let i0 = mesh.vertices.len() as u32; - mesh.vertices.push(egui::epaint::Vertex { - pos: top_left, - uv: egui::epaint::WHITE_UV, - color: colors[0].into(), - }); - mesh.vertices.push(egui::epaint::Vertex { - pos: top_right, - uv: egui::epaint::WHITE_UV, - color: colors[1].into(), - }); - mesh.vertices.push(egui::epaint::Vertex { - pos: bottom_right, - uv: egui::epaint::WHITE_UV, - color: colors[3].into(), - }); - mesh.vertices.push(egui::epaint::Vertex { - pos: bottom_left, - uv: egui::epaint::WHITE_UV, - color: colors[2].into(), - }); - - mesh.indices - .extend_from_slice(&[i0, i0 + 1, i0 + 2, i0, i0 + 2, i0 + 3]); - - painter.add(egui::Shape::mesh(mesh)); -}