Make UI components optional
This commit is contained in:
BIN
assets/DroidSans.ttf
Normal file
BIN
assets/DroidSans.ttf
Normal file
Binary file not shown.
71
src/gui.rs
71
src/gui.rs
@@ -3,11 +3,11 @@ use baseview::{
|
||||
Event, EventStatus, MouseButton, MouseEvent, WindowEvent, WindowHandler, gl::GlContext,
|
||||
};
|
||||
use ebu_dsp::Rect;
|
||||
use femtovg::{Canvas, Color, ImageFlags, ImageId, Paint, Path, renderer::OpenGl};
|
||||
use femtovg::{Canvas, Color, FontId, ImageFlags, ImageId, Paint, Path, renderer::OpenGl};
|
||||
use nih_plug::prelude::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
//const DROID_SANS_FONT: &'static [u8] = include_bytes!("../resources/fonts/DroidSans.ttf");
|
||||
const DROID_SANS_FONT: &'static [u8] = include_bytes!("../assets/DroidSans.ttf");
|
||||
|
||||
const FRESHENER_IMAGE: &'static [u8] = include_bytes!("../assets/AirFreshener/sheet.png");
|
||||
const NOT_SO_FRESH_BG_IMAGE: &'static [u8] = include_bytes!("../assets/AirFreshener/bg0.png");
|
||||
@@ -19,7 +19,7 @@ const FRESHENER_FRAME_WIDTH: f32 = 73.0;
|
||||
const FRESHENER_FRAME_HEIGHT: f32 = 144.0;
|
||||
|
||||
pub struct PluginGui {
|
||||
// font: FontId,
|
||||
font: Option<FontId>,
|
||||
params: Arc<PluginParams>,
|
||||
canvas: Option<Canvas<OpenGl>>,
|
||||
_gui_context: Arc<dyn GuiContext>,
|
||||
@@ -43,17 +43,11 @@ fn create_canvas(
|
||||
params: &PluginParams,
|
||||
scaling_factor: f32,
|
||||
) -> Result<Canvas<OpenGl>, &'static str> {
|
||||
unsafe {
|
||||
context.make_current();
|
||||
}
|
||||
let renderer = unsafe { OpenGl::new_from_function(|s| context.get_proc_address(s)) }
|
||||
.map_err(|_| "Failed to create OpenGL renderer")?;
|
||||
let mut canvas = Canvas::new(renderer).map_err(|_| "Failed to create femtovg canvas")?;
|
||||
let (width, height) = params.editor_state.size();
|
||||
canvas.set_size(width, height, scaling_factor);
|
||||
unsafe {
|
||||
context.make_not_current();
|
||||
}
|
||||
Ok(canvas)
|
||||
}
|
||||
|
||||
@@ -64,16 +58,10 @@ impl PluginGui {
|
||||
params: Arc<PluginParams>,
|
||||
scaling_factor: f32,
|
||||
) -> Self {
|
||||
let canvas = if let Some(context) = window.gl_context() {
|
||||
create_canvas(context, ¶ms, scaling_factor).ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let this = Self {
|
||||
//font,
|
||||
params,
|
||||
canvas,
|
||||
let mut this = Self {
|
||||
font: None,
|
||||
params: params.clone(),
|
||||
canvas: None,
|
||||
_gui_context: gui_context,
|
||||
scaling_factor,
|
||||
dirty: true,
|
||||
@@ -92,6 +80,33 @@ impl PluginGui {
|
||||
},
|
||||
};
|
||||
|
||||
if let Some(context) = window.gl_context() {
|
||||
unsafe {
|
||||
context.make_current();
|
||||
}
|
||||
if let Ok(mut canvas) = create_canvas(context, ¶ms, scaling_factor) {
|
||||
if let Ok(font) = canvas.add_font_mem(DROID_SANS_FONT) {
|
||||
this.font = Some(font);
|
||||
}
|
||||
if let Ok(image) = canvas.load_image_mem(FRESHENER_IMAGE, ImageFlags::empty()) {
|
||||
this.freshener_image = Some(image);
|
||||
}
|
||||
if let Ok(image) =
|
||||
canvas.load_image_mem(FRESH_DUMBLEDORE_BG_IMAGE, ImageFlags::empty())
|
||||
{
|
||||
this.fresh_dumbledore_image = Some(image);
|
||||
}
|
||||
if let Ok(image) = canvas.load_image_mem(NOT_SO_FRESH_BG_IMAGE, ImageFlags::empty())
|
||||
{
|
||||
this.not_so_fresh_image = Some(image);
|
||||
}
|
||||
this.canvas = Some(canvas);
|
||||
}
|
||||
unsafe {
|
||||
context.make_not_current();
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
if let Some(canvas) = canvas {
|
||||
this.freshener_image = canvas
|
||||
@@ -204,6 +219,24 @@ impl WindowHandler for PluginGui {
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
use femtovg::Baseline;
|
||||
if let Some(font) = self.font {
|
||||
canvas
|
||||
.fill_text(
|
||||
5.0,
|
||||
5.0,
|
||||
"Debug version",
|
||||
&Paint::color(Color::white())
|
||||
.with_font(&[font])
|
||||
.with_font_size(12.0)
|
||||
.with_text_baseline(Baseline::Top),
|
||||
)
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
|
||||
canvas.flush();
|
||||
context.swap_buffers();
|
||||
unsafe {
|
||||
|
||||
Reference in New Issue
Block a user