Properly paint for restarted client, do not recreate painters on crash

This commit is contained in:
timvisee
2018-12-28 22:43:13 +01:00
parent 4f5a0b9274
commit b8fd77ebad
2 changed files with 20 additions and 11 deletions

View File

@@ -9,7 +9,7 @@ use rect::Rect;
/// A painter that paints on a pixelflut panel.
pub struct Painter {
client: Client,
client: Option<Client>,
area: Rect,
offset: (u32, u32),
image: Option<DynamicImage>,
@@ -18,7 +18,7 @@ pub struct Painter {
impl Painter {
/// Create a new painter.
pub fn new(
client: Client,
client: Option<Client>,
area: Rect,
offset: (u32, u32),
image: Option<DynamicImage>,
@@ -71,13 +71,15 @@ impl Painter {
let color = Color::from(channels[0], channels[1], channels[2]);
// Set the pixel
self.client.write_pixel(
if let Some(client) = &mut self.client {
client.write_pixel(
x + self.area.x + self.offset.0,
y + self.area.y + self.offset.1,
color,
)?;
}
}
}
// Everything seems to be ok
Ok(())
@@ -87,4 +89,9 @@ impl Painter {
pub fn set_image(&mut self, image: DynamicImage) {
self.image = Some(image);
}
/// Update the client.
pub fn set_client(&mut self, client: Option<Client>) {
self.client = client;
}
}

View File

@@ -69,9 +69,13 @@ impl Canvas {
// Create the painter thread
let thread = thread::spawn(move || {
// Create the painter
let mut painter = Painter::new(None, area, offset, None);
loop {
// The painting loop
'paint: loop {
// Create a new client
// Connect
let client = match Client::connect(host.clone()) {
Ok(client) => client,
Err(e) => {
@@ -79,9 +83,7 @@ impl Canvas {
break 'paint;
},
};
// Create a painter
let mut painter = Painter::new(client, area, offset, None);
painter.set_client(Some(client));
// Keep painting
loop {