radar-g/gi/src/pg/app.rs
2024-10-04 14:05:37 +08:00

107 lines
2.8 KiB
Rust

use log::*;
use radarg_core::{datapool::Value, radarg_data::Data};
use std::{cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc, sync::Arc};
use crate::{
errors::*,
graphics::{
colormap::linear::LinearColormap, threed::Trackball, transforms::plane::PlaneTrans,
AttaWithBuffer, AttaWithProgram, Graphics,
},
ui::{
helper::{self, Helper},
operation::{Operation, Operations},
},
utils::resources::{
ManagedResource, RcGlBuffer, RcGlFramebuffer, RcGlRcFramebuffer, RcGlRcRenderbuffer,
RcGlRcResource, RcGlRenderbuffer, RcGlResource, RcGlVertexArray, GL,
},
};
use glow::HasContext;
use super::{
layout_type::{self, ViewPort},
Module, ModuleRefs,
};
use super::{ModulePackage, Programs};
use crate::{font_manager::FontManager, graphics::font::Text};
pub struct App {
gl: GL,
pub context: Context,
pub init_modules: Option<ModulePackage>,
}
impl App {
pub fn new(gl: GL, helper: Helper) -> Result<Self> {
let programs = Programs::new(gl.clone()).unwrap();
let context = Context::new(gl.clone(), helper, programs);
Ok(Self {
gl,
context,
init_modules: None,
})
}
pub fn prepare(&mut self, setting: &radarg_core::config::Setting) -> Result<()> {
if let Err(e) = self.context.programs.prepare() {
error!("prepare failed: {:?}", e);
}
Ok(())
}
pub fn set_init_modules<T: Into<ModulePackage>>(&mut self, modules: T) {
self.init_modules = Some(modules.into());
}
pub fn render<'a>(
&'a mut self,
modules: &mut Vec<Rc<RefCell<ModulePackage>>>,
operation: &Operations,
viewport: &ViewPort,
) {
if let Some(module) = self.init_modules.as_mut() {
let programs = &mut self.context.programs;
programs.draw_modules(module, operation, viewport);
}
for module in modules {
let mut module = module.borrow_mut();
self.program()
.draw_modules(&mut *module, operation, viewport);
}
}
pub fn destroy(&mut self) {
self.context.programs.destroy().unwrap();
}
pub fn program(&mut self) -> &mut Programs {
&mut self.context.programs
}
pub fn supported_modules<'a>(
&mut self,
data: &'a Value<Data>,
) -> HashMap<&'a Arc<Data>, Vec<ModuleRefs>> {
self.program().supported_modules(data)
}
}
pub struct Context {
pub gl: GL,
pub helper: Helper,
pub programs: Programs,
}
impl Context {
fn new(gl: GL, helper: Helper, programs: Programs) -> Self {
let context = Context {
gl,
helper,
programs: programs,
};
context
}
}