Files
pixelpwnr/src/painter/handle.rs
2017-12-31 00:17:03 +01:00

49 lines
1.1 KiB
Rust

extern crate image;
use std::sync::mpsc::Sender;
use std::thread::JoinHandle;
use image::DynamicImage;
use rect::Rect;
/// A handle to a painter thread.
///
/// This also holds a channel to the painter thread,
/// to allow image updates to be pushed to the thread.
pub struct Handle {
#[allow(dead_code)]
thread: JoinHandle<u32>,
area: Rect,
image_sender: Sender<DynamicImage>,
}
impl Handle {
/// Create a new handle from the given properties.
pub fn new(thread: JoinHandle<u32>, area: Rect, image_sender: Sender<DynamicImage>) -> Handle {
Handle {
thread,
area,
image_sender,
}
}
/// Push an image update.
pub fn update_image(&self, full_image: &mut DynamicImage) {
// Crop the image to the area
let image = full_image.crop(
self.area.x,
self.area.y,
self.area.w,
self.area.h,
);
// Push a new image to the thread
// TODO: return this result
self.image_sender.send(image)
.expect("Failed to send image update to painter");
}
}