From 26382438566cfdc36131dfbcc99581d2b626b147 Mon Sep 17 00:00:00 2001 From: tsuki Date: Mon, 22 Jul 2024 23:37:14 +0800 Subject: [PATCH] sync --- src/graphics/mod.rs | 4 +- src/graphics/ppi.rs | 3 +- src/graphics/threed.rs | 2 +- src/pg.rs | 105 +++++++++++++++++++++++++++++++++++++++-- src/ui.rs | 5 +- 5 files changed, 109 insertions(+), 10 deletions(-) diff --git a/src/graphics/mod.rs b/src/graphics/mod.rs index acbf9d8..5b5ffb2 100644 --- a/src/graphics/mod.rs +++ b/src/graphics/mod.rs @@ -61,8 +61,8 @@ pub enum Config { config_for_everyitem!({PPIConfig => PPI},); -pub trait AttachWithMouse { - fn attach_with_mouse(&mut self, state: MouseState); +pub trait AttachWithMouse: AttaWithProgram { + fn attach_with_mouse(&mut self, state: &MouseState); } #[derive(Debug, Clone)] diff --git a/src/graphics/ppi.rs b/src/graphics/ppi.rs index b365e42..01b97f7 100644 --- a/src/graphics/ppi.rs +++ b/src/graphics/ppi.rs @@ -7,7 +7,7 @@ use crate::errors::*; use super::colormap::ColorMap; use super::threed::ThreeD; use super::transforms::viewport::Viewport; -use super::{transforms, AttaWithBuffer, Config, Graphics}; +use super::{transforms, AttaWithBuffer, AttaWithProgram, AttachWithMouse, Config, Graphics}; pub struct PPI { program: Program, @@ -250,7 +250,6 @@ pub struct PPIConfig { pub layer: usize, pub rdpi: f32, pub adpi: f32, - pub trackball: ThreeD, } mod test { diff --git a/src/graphics/threed.rs b/src/graphics/threed.rs index 3f110e4..967a3c2 100644 --- a/src/graphics/threed.rs +++ b/src/graphics/threed.rs @@ -73,7 +73,7 @@ impl Transform for ThreeD { } impl AttachWithMouse for ThreeD { - fn attach_with_mouse(&mut self, state: super::MouseState) { + fn attach_with_mouse(&mut self, state: &super::MouseState) { if let super::MouseState::Drag { from, delta } = state { self.trackball .on_mouse_drag(from[0], from[1], delta[0], delta[1]); diff --git a/src/pg.rs b/src/pg.rs index 17c3310..2deb62d 100644 --- a/src/pg.rs +++ b/src/pg.rs @@ -1,11 +1,13 @@ +use crate::components::Program; use crate::data_loader::Data; use crate::graphics::colormap::linear::LinearColormap; use crate::graphics::ppi::PPIConfig; +use crate::graphics::threed::ThreeD; use crate::graphics::transforms::position::Position; use crate::graphics::transforms::viewport::Viewport; use crate::graphics::transforms::ChainedTransform; use crate::graphics::{ppi::PPI, Graphics}; -use crate::graphics::{AttaWithBuffer, AttaWithProgram, Config, MouseState}; +use crate::graphics::{AttaWithBuffer, AttaWithProgram, AttachWithMouse, Config, MouseState}; use crate::{errors::*, ui::base}; use glow::{HasContext, NativeBuffer, NativeFramebuffer, NativeTexture, NativeVertexArray}; use imgui::{ImStr, ImString, Textures, Ui}; @@ -106,6 +108,10 @@ impl<'a> App<'a> { window_info.re_init = false; } + if let Some(motion) = window_info.modifer.as_ref() { + motion.attach_with_program(&self.gl, p.program_ref()).unwrap(); + } + unsafe { self.gl .bind_framebuffer(glow::FRAMEBUFFER, window_info.framebuffer); @@ -421,6 +427,10 @@ impl<'a> App<'a> { from: window.last_mouse_position, delta: delta, }); + window.modifer.as_mut().map(|v| { + v.exec(window.motion.as_ref().unwrap()); + }); + window.need_redraw = true; } if ui.is_mouse_released(imgui::MouseButton::Left) { if window.size != ui.window_size() { @@ -528,7 +538,7 @@ impl<'a> App<'a> { pub fn create_render_window(&mut self, title: &str, size: [f32; 2]) -> Result<()> { // Insert the window data into the windows hashmap let id = ImString::new(title); - let mut data = WindowData::new(id.clone(), size); + let mut data = WindowData::new(id.clone(), size, None); let (fb, tex) = self.create_framebuffer(title, (size[0].floor() as i32, size[1].floor() as i32))?; @@ -701,10 +711,11 @@ pub struct WindowData { mouse_position: [f32; 2], motion: Option, config: Option, + modifer: Option, } impl WindowData { - pub fn new(title: ImString, size: [f32; 2]) -> Self { + pub fn new(title: ImString, size: [f32; 2], modifer: Option) -> Self { Self { title, open: true, @@ -723,6 +734,7 @@ impl WindowData { attach: None, re_init: false, config: None, + modifer, } } @@ -737,4 +749,91 @@ impl WindowData { fn set_mouse_postion(&mut self, pos: [f32; 2]) { self.mouse_position = pos; } + + fn set_motion(&mut self, motion: MouseState) { + self.motion = Some(motion); + } + + fn set_need_redraw(&mut self) { + self.need_redraw = true; + } + + fn set_config(&mut self, config: Config) { + self.config = Some(config); + } + + fn set_re_init(&mut self) { + self.re_init = true; + } + + fn on_mouse_drag(&mut self) { + let state = MouseState::Drag { + from: self.last_mouse_position, + delta: self.last_mouse_delta, + }; + self.set_motion(state.clone()); + + self.modifer.as_mut().map(|m| {}); + + self.set_need_redraw(); + } } + +macro_rules! modifer_exec { + ($(($t:ty => $b:tt),)+) => { + impl ModiferType { + pub fn exec(&mut self, motion: &MouseState) { + match self { + $( + + ModiferType::$b(b) => { + b.attach_with_mouse(motion); + } + + )+ + + + _ => {} + } + } + } + + impl AttaWithProgram for ModiferType { + fn attach_with_program(&self, gl: &glow::Context, program: &Program) -> Result<()> { + match self { + $( + + ModiferType::$b(b) => { + b.attach_with_program(gl, program)?; + } + + )+ + _ => { + } + } + + Ok(()) + + } + + } + + $( + impl From<$t> for ModiferType { + fn from(t: $t) -> Self { + ModiferType::$b(t) + } + } + )+ + + + } + +} + +pub enum ModiferType { + ThreeD(ThreeD), + TwoD, +} + +modifer_exec!((ThreeD => ThreeD),); diff --git a/src/ui.rs b/src/ui.rs index e0ecbf4..9fd53f1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,4 +1,5 @@ use crate::errors::*; +use crate::graphics::threed::ThreeD; use crate::graphics::Config; use crate::pg::App; use crate::{data_loader::Data, pg::WindowData}; @@ -11,7 +12,7 @@ pub fn base(ui: &Ui, window: &winit::window::Window, run: &mut bool, app: &mut A .build(|| { if ui.button("PPI") { let data = - load_data(r#"/Users/tsuki/Desktop/ZJSXAA_20230113070200_R.dat.gz"#).unwrap(); + load_data(r#"C:\Users\qwin7\Downloads\ZJSXAA_20230113070200_R.dat.gz"#).unwrap(); app.create_render_window("ppi", [300.0, 300.0]).unwrap(); app.create_ppi_render("ppi", None); app.bind_data("ppi", &data).unwrap(); @@ -35,7 +36,7 @@ pub fn base(ui: &Ui, window: &winit::window::Window, run: &mut bool, app: &mut A } fn create_display_window(title: &str, size: [f32; 2], copy_from: Option) -> WindowData { - WindowData::new(ImString::new(title), size) + WindowData::new(ImString::new(title), size, Some(ThreeD::default().into())) } fn load_data(path: impl AsRef) -> Result {