Skip to content

Commit

Permalink
coupler-reflector integration crate
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Sep 28, 2024
1 parent 852eb44 commit b09d51b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 82 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ derive = ["coupler-derive"]
members = [
"cargo-coupler",
"coupler-derive",
"coupler-reflector",
"examples/*",
"examples/*/format/*",
]
Expand Down
11 changes: 11 additions & 0 deletions coupler-reflector/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "coupler-reflector"
version = "0.1.0"
authors = ["Micah Johnston <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
publish = false

[dependencies]
coupler = { path = ".." }
reflector = { git = "https://github.com/coupler-rs/reflector", rev = "aac3ff9a1d692052e49b23ced82fe071a667a165" }
77 changes: 77 additions & 0 deletions coupler-reflector/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use coupler::editor::{Editor, ParentWindow, RawParent, Size};
use coupler::params::{ParamId, ParamValue};
use reflector::platform::{
App, AppMode, AppOptions, Bitmap, Event, RawWindow, Response, Result, Window, WindowContext,
WindowOptions,
};

struct EditorState {
framebuffer: Vec<u32>,
}

impl EditorState {
fn new() -> EditorState {
EditorState {
framebuffer: Vec::new(),
}
}

fn handle_event(&mut self, cx: &WindowContext, event: Event) -> Response {
match event {
Event::Frame => {
let scale = cx.window().scale();
let size = cx.window().size();
let width = (size.width * scale) as usize;
let height = (size.height * scale) as usize;
self.framebuffer.resize(width * height, 0xFF000000);

cx.window().present(Bitmap::new(&self.framebuffer, width, height));
}
_ => {}
}

Response::Ignore
}
}

pub struct EditorWindow {
#[allow(unused)]
app: App,
window: Window,
}

impl EditorWindow {
pub fn open(parent: &ParentWindow, size: Size) -> Result<EditorWindow> {
let app = AppOptions::new().mode(AppMode::Guest).build()?;

let mut options = WindowOptions::new();
options.size(reflector::platform::Size::new(size.width, size.height));

let raw_parent = match parent.as_raw() {
RawParent::Win32(window) => RawWindow::Win32(window),
RawParent::Cocoa(view) => RawWindow::Cocoa(view),
RawParent::X11(window) => RawWindow::X11(window),
};
unsafe { options.raw_parent(raw_parent) };

let mut state = EditorState::new();
let window = options.open(app.handle(), move |cx, event| state.handle_event(cx, event))?;

window.show();

Ok(EditorWindow { app, window })
}
}

impl Editor for EditorWindow {
fn size(&self) -> Size {
let size = self.window.size();

Size {
width: size.width,
height: size.height,
}
}

fn param_changed(&mut self, _id: ParamId, _value: ParamValue) {}
}
2 changes: 1 addition & 1 deletion examples/gain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ formats = ["clap", "vst3"]

[dependencies]
coupler = { workspace = true, features = ["derive"] }
coupler-reflector = { path = "../../coupler-reflector" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reflector-platform = { git = "https://github.com/coupler-rs/reflector", rev = "eadfc6da67a3da807161e9f0150bea11bf9d0401" }
91 changes: 10 additions & 81 deletions examples/gain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ use std::io::{self, Read, Write};

use serde::{Deserialize, Serialize};

use reflector_platform::{
App, AppMode, AppOptions, Bitmap, Response, Window, WindowContext, WindowOptions,
};

use coupler::format::clap::*;
use coupler::format::vst3::*;
use coupler::{
buffers::*, bus::*, editor::*, events::*, host::*, params::*, plugin::*, process::*,
};

use coupler_reflector::EditorWindow;

#[derive(Params, Serialize, Deserialize, Clone)]
struct GainParams {
#[param(id = 0, name = "Gain", range = 0.0..1.0, format = "{:.2}")]
Expand All @@ -30,7 +28,7 @@ pub struct Gain {

impl Plugin for Gain {
type Processor = GainProcessor;
type Editor = GainEditor;
type Editor = EditorWindow;

fn info() -> PluginInfo {
PluginInfo {
Expand Down Expand Up @@ -88,8 +86,13 @@ impl Plugin for Gain {
}
}

fn editor(&mut self, host: EditorHost, parent: &ParentWindow) -> Self::Editor {
GainEditor::open(host, parent).unwrap()
fn editor(&mut self, _host: EditorHost, parent: &ParentWindow) -> Self::Editor {
let size = Size {
width: 512.0,
height: 512.0,
};

EditorWindow::open(parent, size).unwrap()
}
}

Expand Down Expand Up @@ -145,77 +148,3 @@ impl Processor for GainProcessor {
}
}
}

struct EditorState {
framebuffer: Vec<u32>,
_host: EditorHost,
}

impl EditorState {
fn new(host: EditorHost) -> EditorState {
EditorState {
framebuffer: Vec::new(),
_host: host,
}
}

fn handle_event(&mut self, cx: &WindowContext, event: reflector_platform::Event) -> Response {
use reflector_platform::Event;

match event {
Event::Frame => {
let scale = cx.window().scale();
let size = cx.window().size();
let width = (size.width * scale) as usize;
let height = (size.height * scale) as usize;
self.framebuffer.resize(width * height, 0xFF000000);

cx.window().present(Bitmap::new(&self.framebuffer, width, height));
}
_ => {}
}

Response::Ignore
}
}

pub struct GainEditor {
_app: App,
window: Window,
}

impl GainEditor {
fn open(host: EditorHost, parent: &ParentWindow) -> reflector_platform::Result<GainEditor> {
let app = AppOptions::new().mode(AppMode::Guest).build()?;

let mut options = WindowOptions::new();
options.size(reflector_platform::Size::new(512.0, 512.0));

let raw_parent = match parent.as_raw() {
RawParent::Win32(window) => reflector_platform::RawWindow::Win32(window),
RawParent::Cocoa(view) => reflector_platform::RawWindow::Cocoa(view),
RawParent::X11(window) => reflector_platform::RawWindow::X11(window),
};
unsafe { options.raw_parent(raw_parent) };

let mut state = EditorState::new(host);
let window = options.open(app.handle(), move |cx, event| state.handle_event(cx, event))?;

window.show();

Ok(GainEditor { _app: app, window })
}
}

impl Editor for GainEditor {
fn size(&self) -> Size {
let size = self.window.size();

Size {
width: size.width,
height: size.height,
}
}

fn param_changed(&mut self, _id: ParamId, _value: ParamValue) {}
}

0 comments on commit b09d51b

Please sign in to comment.