aboutsummaryrefslogtreecommitdiff
path: root/src/snapping.rs
blob: 325b62e42a4313c29c0bd1c9a568dd5e1967199a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! Responsible for snapping a position with a granularity
//!
//! Most of us are not capable of adjusting everything with sub-pixel accuracy. For us filthy casuals,
//! Snapping was invented. However I hate programs where there is only one option for granularity, so
//! I thought it should be changeable. This module is responsible for snapping and managing the user
//! instructions telling the program what granularity should currently be used, if any.

use crate::gui::DecimalNumBox;
use crate::math::{self, Vec2};
use raylib::drawing::RaylibDrawHandle;
use raylib::ffi::KeyboardKey;
use raylib::RaylibHandle;

/// The struct containing the current snapping information of the program.
pub struct Snapper {
    grain: f64,
    grain_gui: DecimalNumBox<f64>,
}

impl Snapper {
    /// Create a new snapper with the default granularity.
    pub fn new() -> Self {
        Self::default()
    }

    /// Update the grain according to the input the program receives.
    pub fn update(&mut self, rl: &mut RaylibHandle) {
        if !self.grain_gui.active() && rl.is_key_pressed(KeyboardKey::KEY_G) {
            self.grain_gui.set_active(true);
        }

        if !self.grain_gui.active() {
            return;
        }

        self.grain_gui.update(rl);

        if rl.is_key_pressed(KeyboardKey::KEY_ENTER) {
            self.grain_gui.set_active(false);
            self.grain = self.grain_gui.value();
            self.grain_gui.set_value(self.grain);
        } else if rl.is_key_pressed(KeyboardKey::KEY_ESCAPE) {
            self.grain_gui.set_active(false);
            self.grain_gui.set_value(self.grain);
        }
    }

    /// Draw the snapper gui
    pub fn draw(&self, rld: &mut RaylibDrawHandle) {
        self.grain_gui.draw(
            rld,
            "m",
            &Vec2::new(15., (rld.get_screen_height() - 25) as f64),
        );
    }

    /// Get the current granularity of the world snapping in meters. Snapping always starts at (0, 0)
    pub fn grain(&self) -> f64 {
        self.grain
    }

    /// Snap a vector to the grid with the factor being the sub-grid accuracy. For instance, 0.5 will
    /// snap to half a grid cell, while 2.0 would snap to every second grid cell.
    pub fn snap(&self, pos: Vec2<f64>) -> Vec2<f64> {
        Vec2::new(
            math::round(pos.x, self.grain),
            math::round(pos.y, self.grain),
        )
    }
}

impl Default for Snapper {
    fn default() -> Self {
        Self {
            grain: 0.5,
            grain_gui: DecimalNumBox::new(0.5),
        }
    }
}