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
|
use crate::math::{self, Vec2};
use crate::transform::Transform;
use raylib::drawing::RaylibDraw;
use raylib::ffi::Color;
/// The internal grid length which will be used to snap things to it.
pub const SNAP_SIZE: f32 = 0.5;
pub const LINE_COLOUR: Color = Color {
r: 255,
g: 255,
b: 255,
a: 75,
};
/// 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_to_grid(mut vec: Vec2<f32>, snap_fraction: f32) -> Vec2<f32> {
vec.x = math::round(vec.x, snap_fraction);
vec.y = math::round(vec.y, snap_fraction);
vec
}
/// Draw an infinite grid that can be moved around on the screen and zoomed in and out of.
pub fn draw_grid<D>(rld: &mut D, screen_width: i32, screen_height: i32, transform: &Transform)
where
D: RaylibDraw,
{
/* Calculate the actual screen offset of the grid, by modulo-ing the translation of the
* transform.
*/
let translation_x_px: i32 =
transform.translation_px().x as i32 % transform.pixels_per_m() as i32;
let translation_y_px: i32 =
transform.translation_px().y as i32 % transform.pixels_per_m() as i32;
/*
let mut row = 0;
loop {
let line_y = translation_y_px + (transform.pixels_per_m() * row as f32) as i32;
if line_y > screen_height {
break;
}
rld.draw_line(0, line_y as i32, screen_width, line_y as i32, LINE_COLOUR);
row += 1;
}
let mut column = 0;
loop {
let line_x = translation_x_px + (transform.pixels_per_m() * column as f32) as i32;
if line_x > screen_width {
break;
}
rld.draw_line(line_x as i32, 0, line_x as i32, screen_height, LINE_COLOUR);
column += 1;
}
*/
// Draw the row lines.
let mut line_y: f32 = translation_y_px as f32;
while line_y <= screen_height as f32 {
rld.draw_line(0, line_y as i32, screen_width, line_y as i32, LINE_COLOUR);
line_y += transform.pixels_per_m();
}
// Draw the column lines.
let mut line_x: f32 = translation_x_px as f32;
while line_x <= screen_width as f32 {
rld.draw_line(line_x as i32, 0, line_x as i32, screen_height, LINE_COLOUR);
line_x += transform.pixels_per_m();
}
}
|