windows-troll 0.1.0

Modular Windows prank library
//! Grid cell structure, ported from the `screen-messy-fun` project.

use super::capture::ScreenCapture;

/// A single captured screen tile that eases toward a target position.
pub struct GridCell {
    pub capture: ScreenCapture,
    pub original_x: i32,
    pub original_y: i32,
    pub current_x: i32,
    pub current_y: i32,
    pub target_x: i32,
    pub target_y: i32,
    pub speed: f32,
}

impl GridCell {
    pub fn new(capture: ScreenCapture, x: i32, y: i32, speed: f32) -> Self {
        GridCell {
            capture,
            original_x: x,
            original_y: y,
            current_x: x,
            current_y: y,
            target_x: x,
            target_y: y,
            speed,
        }
    }

    pub fn update(&mut self) {
        let dx = self.target_x - self.current_x;
        let dy = self.target_y - self.current_y;

        if dx != 0 || dy != 0 {
            self.current_x += (dx as f32 * self.speed) as i32;
            self.current_y += (dy as f32 * self.speed) as i32;

            if let Err(e) = self.capture.render_at(self.current_x, self.current_y) {
                eprintln!("Failed to render: {}", e);
            }
        }
    }

    pub fn reset(&self) -> Result<(), String> {
        self.capture.render_at(self.original_x, self.original_y)
    }
}