radar-g/gi/src/graphics/mod.rs
2024-08-31 20:59:21 +08:00

148 lines
3.3 KiB
Rust

pub mod collections;
pub mod colormap;
mod colormesh;
pub mod font;
pub mod ppi;
pub mod threed;
pub mod tools;
pub mod transforms;
pub mod ty;
use crate::{
camera::Camera,
components::Program,
errors::*,
graphics::font::FontConfig,
pg::layout_type::ViewPort,
ui::{operation::Projection, typ::CameraOP},
};
use glow::{HasContext, NativeBuffer, NativeVertexArray};
pub trait Graphics {
const id: &'static str;
type Config;
fn draw(&self, gl: &glow::Context, count: i32) -> Result<()>;
fn compile(&mut self, gl: &glow::Context) -> Result<()>;
fn destroy(&mut self, gl: &glow::Context) -> Result<()>;
fn program_ref(&self) -> &Program;
fn program_mut(&mut self) -> &mut Program;
fn mount(&self, gl: &glow::Context) -> Result<()> {
unsafe {
gl.use_program(self.program_ref().native_program.clone());
}
Ok(())
}
fn unmount(&self, gl: &glow::Context) -> Result<()> {
unsafe {
gl.use_program(None);
}
Ok(())
}
fn set_config(&mut self, gl: &glow::Context, config: &Self::Config) -> Result<()>;
}
pub trait AttaWithProgram {
fn attach_with_program(&self, gl: &glow::Context, program: &Program) -> Result<()>;
}
pub trait AttaWithBuffer: Graphics {
type Data;
fn bake<'a, 'gl: 'a>(
&'a self,
gl: &'gl glow::Context,
data: &Self::Data,
config: &<Self as Graphics>::Config,
) -> Result<(Vec<f32>, Option<Vec<u32>>, i32)>;
fn init(&self, gl: &glow::Context) -> (NativeVertexArray, NativeBuffer, Option<NativeBuffer>);
}
macro_rules! config_for_everyitem {
($({$conf:ty => $name:tt},)+) => {
$(
impl From<$conf> for Config {
fn from(value: $conf) -> Self {
Self::$name(value)
}
}
impl From<Config> for $conf {
fn from(value: Config) -> Self {
if let Config::$name(value) = value {
value
} else {
panic!("error transfer");
}
}
}
impl<'a> From<&'a Config> for &'a $conf {
fn from(value: &'a Config) -> &'a $conf {
if let Config::$name(value) = value {
&value
} else {
panic!("error transfer");
}
}
}
)+
impl From<()> for Config {
fn from(_: ()) -> Self {
Self::None
}
}
};
}
// #[derive(Debug, Clone)]
// pub enum Config {
// PPI(PPIConfig),
// Font(FontConfig),
// None,
// }
// config_for_everyitem!({PPIConfig => PPI},{FontConfig => Font}, );
pub trait AttachWithIO {
type State: CameraOP;
fn attach_with_mouse(
&mut self,
state: &Self::State,
camera: &mut Camera,
projection: &mut Projection,
viewport: &ViewPort,
) -> bool;
fn reset(&mut self);
fn init_camera(&self) -> Camera;
}
#[derive(Debug, Clone)]
pub enum MouseState {
Drag { from: [f32; 2], delta: [f32; 2] },
Wheel(f32),
None,
}
#[derive(Debug, Clone)]
pub struct MouseKeyboardState {
pub mouse_state: MouseState,
pub keyboard_state: [bool; 652],
}