Properly paint for restarted client, do not recreate painters on crash
This commit is contained in:
@@ -9,7 +9,7 @@ use rect::Rect;
|
|||||||
|
|
||||||
/// A painter that paints on a pixelflut panel.
|
/// A painter that paints on a pixelflut panel.
|
||||||
pub struct Painter {
|
pub struct Painter {
|
||||||
client: Client,
|
client: Option<Client>,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
offset: (u32, u32),
|
offset: (u32, u32),
|
||||||
image: Option<DynamicImage>,
|
image: Option<DynamicImage>,
|
||||||
@@ -18,7 +18,7 @@ pub struct Painter {
|
|||||||
impl Painter {
|
impl Painter {
|
||||||
/// Create a new painter.
|
/// Create a new painter.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
client: Client,
|
client: Option<Client>,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
offset: (u32, u32),
|
offset: (u32, u32),
|
||||||
image: Option<DynamicImage>,
|
image: Option<DynamicImage>,
|
||||||
@@ -71,11 +71,13 @@ impl Painter {
|
|||||||
let color = Color::from(channels[0], channels[1], channels[2]);
|
let color = Color::from(channels[0], channels[1], channels[2]);
|
||||||
|
|
||||||
// Set the pixel
|
// Set the pixel
|
||||||
self.client.write_pixel(
|
if let Some(client) = &mut self.client {
|
||||||
x + self.area.x + self.offset.0,
|
client.write_pixel(
|
||||||
y + self.area.y + self.offset.1,
|
x + self.area.x + self.offset.0,
|
||||||
color,
|
y + self.area.y + self.offset.1,
|
||||||
)?;
|
color,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,4 +89,9 @@ impl Painter {
|
|||||||
pub fn set_image(&mut self, image: DynamicImage) {
|
pub fn set_image(&mut self, image: DynamicImage) {
|
||||||
self.image = Some(image);
|
self.image = Some(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update the client.
|
||||||
|
pub fn set_client(&mut self, client: Option<Client>) {
|
||||||
|
self.client = client;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,9 +69,13 @@ impl Canvas {
|
|||||||
|
|
||||||
// Create the painter thread
|
// Create the painter thread
|
||||||
let thread = thread::spawn(move || {
|
let thread = thread::spawn(move || {
|
||||||
|
// Create the painter
|
||||||
|
let mut painter = Painter::new(None, area, offset, None);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
// The painting loop
|
||||||
'paint: loop {
|
'paint: loop {
|
||||||
// Create a new client
|
// Connect
|
||||||
let client = match Client::connect(host.clone()) {
|
let client = match Client::connect(host.clone()) {
|
||||||
Ok(client) => client,
|
Ok(client) => client,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -79,9 +83,7 @@ impl Canvas {
|
|||||||
break 'paint;
|
break 'paint;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
painter.set_client(Some(client));
|
||||||
// Create a painter
|
|
||||||
let mut painter = Painter::new(client, area, offset, None);
|
|
||||||
|
|
||||||
// Keep painting
|
// Keep painting
|
||||||
loop {
|
loop {
|
||||||
|
|||||||
Reference in New Issue
Block a user