-
Notifications
You must be signed in to change notification settings - Fork 5
/
basic.rs
104 lines (87 loc) · 2.47 KB
/
basic.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! Minimal VST plugin with an editor window.
//!
//! The editor window is blank. Clicking anywhere in the window will print "Click!" to stdout.
use core::ffi::c_void;
use vst::{
editor::Editor,
plugin::{HostCallback, Info, Plugin},
plugin_main,
};
use vst_window::{setup, EventSource, WindowEvent};
#[derive(Default)]
struct BasicPlugin {
editor_placeholder: Option<MyPluginEditor>,
}
impl Plugin for BasicPlugin {
fn get_info(&self) -> Info {
Info {
name: "Basic Plugin with Editor".to_string(),
unique_id: 13579,
..Default::default()
}
}
fn new(_host: HostCallback) -> Self {
Self {
editor_placeholder: Some(MyPluginEditor::default()),
}
}
fn get_editor(&mut self) -> Option<Box<dyn Editor>> {
self.editor_placeholder
.take()
.map(|editor| Box::new(editor) as Box<dyn Editor>)
}
}
plugin_main!(BasicPlugin);
#[derive(Default)]
struct MyPluginEditor {
renderer: Option<MyRenderer>,
window_events: Option<EventSource>,
}
const WINDOW_DIMENSIONS: (i32, i32) = (300, 200);
impl Editor for MyPluginEditor {
fn size(&self) -> (i32, i32) {
(WINDOW_DIMENSIONS.0 as i32, WINDOW_DIMENSIONS.1 as i32)
}
fn position(&self) -> (i32, i32) {
(0, 0)
}
fn open(&mut self, parent: *mut c_void) -> bool {
if self.window_events.is_none() {
let (window_handle, event_source) = setup(parent, WINDOW_DIMENSIONS);
self.renderer = Some(MyRenderer::new(window_handle));
self.window_events = Some(event_source);
true
} else {
false
}
}
fn is_open(&mut self) -> bool {
self.window_events.is_some()
}
fn close(&mut self) {
drop(self.renderer.take());
drop(self.window_events.take());
}
fn idle(&mut self) {
if let Some(window_events) = &mut self.window_events {
while let Some(event) = window_events.poll_event() {
match event {
WindowEvent::MouseClick(_) => println!("Click!"),
_ => (),
}
}
}
if let Some(renderer) = &mut self.renderer {
renderer.draw_frame();
}
}
}
struct MyRenderer;
impl MyRenderer {
pub fn new<W: raw_window_handle::HasRawWindowHandle>(_handle: W) -> Self {
Self
}
pub fn draw_frame(&mut self) {
/* ... */
}
}