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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use num::{Float, NumCast, PrimInt, ToPrimitive};
use position::{Dimensions, Point};
use vecmath::vec2_sub;
pub fn partial_min<T: PartialOrd>(a: T, b: T) -> T {
if a <= b { a } else { b }
}
pub fn partial_max<T: PartialOrd>(a: T, b: T) -> T {
if a >= b { a } else { b }
}
pub fn clamp<T: PartialOrd>(n: T, min: T, max: T) -> T {
if n < min { min } else if n > max { max } else { n }
}
pub fn is_over_rect(rect_point: Point, mouse_point: Point, rect_dim: Dimensions) -> bool {
let point = vec2_sub(rect_point, mouse_point);
if point[0].abs() < rect_dim[0] / 2.0 && point[1].abs() < rect_dim[1] / 2.0 { true }
else { false }
}
pub fn percentage<T: Float + NumCast>(value: T, min: T, max: T) -> f32 {
let v: f32 = NumCast::from(value).unwrap();
let mn: f32 = NumCast::from(min).unwrap();
let mx: f32 = NumCast::from(max).unwrap();
(v - mn) / (mx - mn)
}
pub fn value_from_perc<T: Float + NumCast + ToPrimitive>(perc: f32, min: T, max: T) -> T {
let f: f32 = (max - min).to_f32().unwrap() * perc;
min + NumCast::from(f).unwrap()
}
pub fn map_range<X: Float + NumCast, Y: Float + NumCast>
(val: X, in_min: X, in_max: X, out_min: Y, out_max: Y) -> Y {
let val_f: f64 = NumCast::from(val).unwrap();
let in_min_f: f64 = NumCast::from(in_min).unwrap();
let in_max_f: f64 = NumCast::from(in_max).unwrap();
let out_min_f: f64 = NumCast::from(out_min).unwrap();
let out_max_f: f64 = NumCast::from(out_max).unwrap();
NumCast::from(
(val_f - in_min_f) / (in_max_f - in_min_f) * (out_max_f - out_min_f) + out_min_f
).unwrap()
}
pub fn val_to_string<T: ToString + NumCast>
(val: T, max: T, val_rng: T, pixel_range: usize) -> String {
let mut s = val.to_string();
let decimal = s.chars().position(|ch| ch == '.');
match decimal {
None => s,
Some(idx) => {
let val_rng_f: f64 = NumCast::from(val_rng).unwrap();
let max_f: f64 = NumCast::from(max).unwrap();
let mut n: f64 = 0.0;
let mut pow_ten = 0.0;
while pow_ten < val_rng_f || pow_ten < max_f {
pow_ten = (10.0).powf(n);
n += 1.0
}
let min_string_len = n as usize + 1;
let mut n = 1;
while 10.pow(n) < pixel_range { n += 1 }
let precision = n as usize;
let mut truncate_len = min_string_len + (precision - 1);
if idx + precision < truncate_len { truncate_len = idx + precision }
if s.len() > truncate_len { s.truncate(truncate_len) }
s
}
}
}