|
|
@ -1,12 +1,12 @@
|
|
|
|
use std::sync::mpsc;
|
|
|
|
use crate::error::Error;
|
|
|
|
|
|
|
|
use crate::layouts::Entry;
|
|
|
|
|
|
|
|
use crate::CompiledShaders;
|
|
|
|
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
|
|
|
|
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
|
|
|
|
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
use std::sync::mpsc;
|
|
|
|
|
|
|
|
use std::sync::mpsc::{Receiver, Sender};
|
|
|
|
use std::thread;
|
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
use crate::layouts::Entry;
|
|
|
|
|
|
|
|
use crate::CompiledShaders;
|
|
|
|
|
|
|
|
use std::sync::mpsc::{Sender, Receiver};
|
|
|
|
|
|
|
|
use crate::error::Error;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Watch {
|
|
|
|
pub struct Watch {
|
|
|
|
_handler: Handler,
|
|
|
|
_handler: Handler,
|
|
|
@ -29,15 +29,21 @@ impl Watch {
|
|
|
|
where
|
|
|
|
where
|
|
|
|
T: AsRef<Path>,
|
|
|
|
T: AsRef<Path>,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let (handler, rx) = create_watch(vertex.as_ref().to_path_buf(), fragment.as_ref().to_path_buf());
|
|
|
|
let (handler, rx) = create_watch(
|
|
|
|
Watch{_handler: handler, rx}
|
|
|
|
vertex.as_ref().to_path_buf(),
|
|
|
|
|
|
|
|
fragment.as_ref().to_path_buf(),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
Watch {
|
|
|
|
|
|
|
|
_handler: handler,
|
|
|
|
|
|
|
|
rx,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Loader {
|
|
|
|
impl Loader {
|
|
|
|
fn create(vertex: PathBuf, fragment: PathBuf) -> (Self, Receiver<Result<Message, Error>>) {
|
|
|
|
fn create(vertex: PathBuf, fragment: PathBuf) -> (Self, Receiver<Result<Message, Error>>) {
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
let loader = Loader{
|
|
|
|
let loader = Loader {
|
|
|
|
vertex,
|
|
|
|
vertex,
|
|
|
|
fragment,
|
|
|
|
fragment,
|
|
|
|
tx,
|
|
|
|
tx,
|
|
|
@ -50,8 +56,8 @@ impl Loader {
|
|
|
|
match crate::load(&self.vertex, &self.fragment) {
|
|
|
|
match crate::load(&self.vertex, &self.fragment) {
|
|
|
|
Ok(shaders) => {
|
|
|
|
Ok(shaders) => {
|
|
|
|
let entry = crate::parse(&shaders);
|
|
|
|
let entry = crate::parse(&shaders);
|
|
|
|
self.tx.send(Ok(Message{ shaders, entry })).ok()
|
|
|
|
self.tx.send(Ok(Message { shaders, entry })).ok()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Err(e) => self.tx.send(Err(e)).ok(),
|
|
|
|
Err(e) => self.tx.send(Err(e)).ok(),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -72,8 +78,10 @@ impl Drop for Handler {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn create_watch(
|
|
|
|
fn create_watch(vert_path: PathBuf, frag_path: PathBuf) -> (Handler, mpsc::Receiver<Result<Message, Error>>) {
|
|
|
|
vert_path: PathBuf,
|
|
|
|
|
|
|
|
frag_path: PathBuf,
|
|
|
|
|
|
|
|
) -> (Handler, mpsc::Receiver<Result<Message, Error>>) {
|
|
|
|
let (notify_tx, notify_rx) = mpsc::channel();
|
|
|
|
let (notify_tx, notify_rx) = mpsc::channel();
|
|
|
|
let (thread_tx, thread_rx) = mpsc::channel();
|
|
|
|
let (thread_tx, thread_rx) = mpsc::channel();
|
|
|
|
let mut watcher: RecommendedWatcher =
|
|
|
|
let mut watcher: RecommendedWatcher =
|
|
|
@ -86,14 +94,15 @@ fn create_watch(vert_path: PathBuf, frag_path: PathBuf) -> (Handler, mpsc::Recei
|
|
|
|
.watch(&frag_path, RecursiveMode::NonRecursive)
|
|
|
|
.watch(&frag_path, RecursiveMode::NonRecursive)
|
|
|
|
.expect("failed to add fragment shader to notify");
|
|
|
|
.expect("failed to add fragment shader to notify");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let (loader, rx) = Loader::create(vert_path, frag_path);
|
|
|
|
let (loader, rx) = Loader::create(vert_path, frag_path);
|
|
|
|
|
|
|
|
|
|
|
|
let handle = thread::spawn(move || 'watch_loop: loop {
|
|
|
|
let handle = thread::spawn(move || 'watch_loop: loop {
|
|
|
|
if let Ok(_) = thread_rx.try_recv() {
|
|
|
|
if let Ok(_) = thread_rx.try_recv() {
|
|
|
|
break 'watch_loop;
|
|
|
|
break 'watch_loop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Ok(notify::DebouncedEvent::Create(_)) = notify_rx.recv_timeout(Duration::from_secs(1)) {
|
|
|
|
if let Ok(notify::DebouncedEvent::Create(_)) =
|
|
|
|
|
|
|
|
notify_rx.recv_timeout(Duration::from_secs(1))
|
|
|
|
|
|
|
|
{
|
|
|
|
loader.reload();
|
|
|
|
loader.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|