Fix serialization issue

This commit is contained in:
Emil Ernerfeldt 2022-03-22 09:14:29 +01:00
parent 3ff55be4be
commit 9028ce7a98
2 changed files with 13 additions and 23 deletions

View file

@ -7,24 +7,22 @@
//! * [`three-d`](https://github.com/asny/three-d)
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(unsafe_code)]
use epi::{glow, Renderer as _};
use parking_lot::Mutex;
use std::sync::Arc;
#[derive(Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Custom3dApp {
/// Behind an `Arc<Mutex<…>>` so we can pass it to [`egui::PaintCallback`] and paint later.
rotating_triangle: Arc<Mutex<RotatingTriangle>>,
angle: f32,
}
impl Custom3dApp {
pub fn new(cc: &epi::CreationContext<'_>) -> Self {
Self {
rotating_triangle: Arc::new(Mutex::new(RotatingTriangle::new(&cc.gl))),
angle: 0.0,
}
}
/// Behind an `Arc<Mutex<…>>` so we can pass it to [`egui::PaintCallback`] and paint later.
/// Behind an `Option` so we can implement `serde::Deserialize.
#[cfg_attr(feature = "serde", serde(skip))]
rotating_triangle: Arc<Mutex<Option<RotatingTriangle>>>,
}
impl epi::App for Custom3dApp {
@ -65,7 +63,10 @@ impl Custom3dApp {
rect,
callback: std::sync::Arc::new(move |render_ctx| {
if let Some(painter) = render_ctx.downcast_ref::<egui_glow::Painter>() {
rotating_triangle.lock().paint(painter.gl(), angle);
let mut rotating_triangle = rotating_triangle.lock();
let rotating_triangle = rotating_triangle
.get_or_insert_with(|| RotatingTriangle::new(painter.gl()));
rotating_triangle.paint(painter.gl(), angle);
} else {
eprintln!("Can't do custom painting because we are not using a glow context");
}

View file

@ -1,4 +1,5 @@
/// All the different demo apps.
#[derive(Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Apps {
demo: crate::apps::DemoApp,
@ -11,18 +12,6 @@ pub struct Apps {
}
impl Apps {
fn new(cc: &epi::CreationContext<'_>) -> Self {
Self {
demo: Default::default(),
easy_mark_editor: Default::default(),
#[cfg(feature = "http")]
http: Default::default(),
clock: Default::default(),
color_test: Default::default(),
custom_3d: crate::apps::Custom3dApp::new(cc),
}
}
fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &str, &mut dyn epi::App)> {
vec![
("✨ Demos", "demo", &mut self.demo as &mut dyn epi::App),