agb_eb_ext 0.25.0

AGB Extension methods
use agb::fixnum::{Num, Vector2D, num, vec2};

/// Highlight/focus indicator for button that animates between buttons
///
/// # Usage
///
/// Assuming buttons at `24,24` `40,24` `56,24` all 24x40 px
///
/// ```rust,ignore
/// let mut highlight = ButtonHighlight::new(24,24);
/// //player presses right
/// highlight.set_target(40,24);
///
/// //in update
/// highlight.update();
///
/// //in show
/// sprites::BUTTON.show(highlight.pos(), frame);
/// ```
pub struct ButtonHighlight {
    pos: Vector2D<Num<i32, 8>>,
    target: Vector2D<Num<i32, 8>>,
}

impl ButtonHighlight {
    pub fn new(x: i32, y: i32) -> Self {
        let pos = vec2(Num::from(x), Num::from(y));
        Self { pos, target: pos }
    }

    /// Set target (in px)
    pub fn set_target(&mut self, x: i32, y: i32) {
        self.target = vec2(Num::from(x), Num::from(y));
    }

    /// Move button highlight towards target
    pub fn update(&mut self) {
        self.pos += (self.target - self.pos) * num!(0.35);

        if (self.target.x - self.pos.x).abs() < num!(0.5) {
            self.pos.x = self.target.x;
        }
        if (self.target.y - self.pos.y).abs() < num!(0.5) {
            self.pos.y = self.target.y;
        }
    }

    /// Position (in px) of button highlight
    pub fn pos(&self) -> Vector2D<i32> {
        self.pos.round()
    }
}