2022-02-06 10:22:33 +11:00
|
|
|
//! [egui](https://github.com/emilk/egui) editor support for NIH plug.
|
|
|
|
//!
|
2022-02-09 09:25:56 +11:00
|
|
|
//! TODO: Proper usage example, for now check out the gain_gui example
|
2022-02-06 10:22:33 +11:00
|
|
|
|
2022-07-01 23:50:23 +10:00
|
|
|
// See the comment in the main `nih_plug` crate
|
|
|
|
#![allow(clippy::type_complexity)]
|
|
|
|
|
2022-02-09 06:38:10 +11:00
|
|
|
use crossbeam::atomic::AtomicCell;
|
2022-02-28 02:49:18 +11:00
|
|
|
use egui::Context;
|
2022-10-20 21:26:12 +11:00
|
|
|
use nih_plug::params::persist::PersistentField;
|
2022-11-16 02:28:53 +11:00
|
|
|
use nih_plug::prelude::{Editor, ParamSetter};
|
2022-02-07 03:12:57 +11:00
|
|
|
use parking_lot::RwLock;
|
2022-07-14 07:13:15 +10:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-02-09 06:16:39 +11:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2022-02-06 10:22:33 +11:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-03-13 09:15:33 +11:00
|
|
|
#[cfg(not(feature = "opengl"))]
|
|
|
|
compile_error!("There's currently no software rendering support for egui");
|
|
|
|
|
2022-02-06 10:22:33 +11:00
|
|
|
/// Re-export for convenience.
|
2022-02-06 10:54:13 +11:00
|
|
|
pub use egui;
|
2022-02-06 10:22:33 +11:00
|
|
|
|
2022-11-16 02:28:53 +11:00
|
|
|
mod editor;
|
2022-02-09 09:25:56 +11:00
|
|
|
pub mod widgets;
|
|
|
|
|
2022-03-04 09:05:01 +11:00
|
|
|
/// Create an [`Editor`] instance using an [`egui`][::egui] GUI. Using the user state parameter is
|
|
|
|
/// optional, but it can be useful for keeping track of some temporary GUI-only settings. See the
|
|
|
|
/// `gui_gain` example for more information on how to use this. The [`EguiState`] passed to this
|
|
|
|
/// function contains the GUI's intitial size, and this is kept in sync whenever the GUI gets
|
|
|
|
/// resized. You can also use this to know if the GUI is open, so you can avoid performing
|
|
|
|
/// potentially expensive calculations while the GUI is not open. If you want this size to be
|
|
|
|
/// persisted when restoring a plugin instance, then you can store it in a `#[persist = "key"]`
|
|
|
|
/// field on your parameters struct.
|
2022-02-09 06:16:39 +11:00
|
|
|
///
|
2022-03-04 09:05:01 +11:00
|
|
|
/// See [`EguiState::from_size()`].
|
2022-10-20 23:03:55 +11:00
|
|
|
pub fn create_egui_editor<T, B, U>(
|
2022-02-09 06:16:39 +11:00
|
|
|
egui_state: Arc<EguiState>,
|
|
|
|
user_state: T,
|
2022-10-20 23:03:55 +11:00
|
|
|
build: B,
|
2022-02-07 03:12:57 +11:00
|
|
|
update: U,
|
2022-02-07 00:02:55 +11:00
|
|
|
) -> Option<Box<dyn Editor>>
|
2022-02-06 10:22:33 +11:00
|
|
|
where
|
2022-02-07 03:12:57 +11:00
|
|
|
T: 'static + Send + Sync,
|
2022-10-20 23:03:55 +11:00
|
|
|
B: Fn(&Context, &mut T) + 'static + Send + Sync,
|
2022-02-28 02:49:18 +11:00
|
|
|
U: Fn(&Context, &ParamSetter, &mut T) + 'static + Send + Sync,
|
2022-02-06 10:22:33 +11:00
|
|
|
{
|
2022-11-16 02:28:53 +11:00
|
|
|
Some(Box::new(editor::EguiEditor {
|
2022-02-09 06:16:39 +11:00
|
|
|
egui_state,
|
|
|
|
user_state: Arc::new(RwLock::new(user_state)),
|
2022-10-20 23:03:55 +11:00
|
|
|
build: Arc::new(build),
|
2022-02-07 03:12:57 +11:00
|
|
|
update: Arc::new(update),
|
2022-03-05 23:37:35 +11:00
|
|
|
|
2022-04-28 02:33:08 +10:00
|
|
|
// TODO: We can't get the size of the window when baseview does its own scaling, so if the
|
|
|
|
// host does not set a scale factor on Windows or Linux we should just use a factor of
|
|
|
|
// 1. That may make the GUI tiny but it also prevents it from getting cut off.
|
|
|
|
#[cfg(target_os = "macos")]
|
2022-03-05 23:37:35 +11:00
|
|
|
scaling_factor: AtomicCell::new(None),
|
2022-04-28 02:33:08 +10:00
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
scaling_factor: AtomicCell::new(Some(1.0)),
|
2022-02-07 03:12:57 +11:00
|
|
|
}))
|
2022-02-06 10:22:33 +11:00
|
|
|
}
|
|
|
|
|
2022-07-14 07:13:15 +10:00
|
|
|
/// State for an `nih_plug_egui` editor.
|
2023-03-02 05:19:54 +11:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2022-02-09 06:16:39 +11:00
|
|
|
pub struct EguiState {
|
2022-07-14 07:13:15 +10:00
|
|
|
/// The window's size in logical pixels before applying `scale_factor`.
|
2022-10-20 21:26:12 +11:00
|
|
|
#[serde(with = "nih_plug::params::persist::serialize_atomic_cell")]
|
2022-02-09 06:16:39 +11:00
|
|
|
size: AtomicCell<(u32, u32)>,
|
2022-07-14 07:13:15 +10:00
|
|
|
/// Whether the editor's window is currently open.
|
|
|
|
#[serde(skip)]
|
2022-02-09 06:16:39 +11:00
|
|
|
open: AtomicBool,
|
|
|
|
}
|
|
|
|
|
2022-07-14 07:13:15 +10:00
|
|
|
impl<'a> PersistentField<'a, EguiState> for Arc<EguiState> {
|
|
|
|
fn set(&self, new_value: EguiState) {
|
|
|
|
self.size.store(new_value.size.load());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map<F, R>(&self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: Fn(&EguiState) -> R,
|
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 06:16:39 +11:00
|
|
|
impl EguiState {
|
2022-03-05 23:37:35 +11:00
|
|
|
/// Initialize the GUI's state. This value can be passed to [`create_egui_editor()`]. The window
|
|
|
|
/// size is in logical pixels, so before it is multiplied by the DPI scaling factor.
|
2022-02-09 06:16:39 +11:00
|
|
|
pub fn from_size(width: u32, height: u32) -> Arc<EguiState> {
|
|
|
|
Arc::new(EguiState {
|
|
|
|
size: AtomicCell::new((width, height)),
|
|
|
|
open: AtomicBool::new(false),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-27 03:39:03 +10:00
|
|
|
/// Returns a `(width, height)` pair for the current size of the GUI in logical pixels.
|
2022-02-09 06:16:39 +11:00
|
|
|
pub fn size(&self) -> (u32, u32) {
|
|
|
|
self.size.load()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether the GUI is currently visible.
|
2022-02-09 06:33:08 +11:00
|
|
|
// Called `is_open()` instead of `open()` to avoid the ambiguity.
|
|
|
|
pub fn is_open(&self) -> bool {
|
2022-02-09 06:16:39 +11:00
|
|
|
self.open.load(Ordering::Acquire)
|
|
|
|
}
|
|
|
|
}
|