@@ -60,6 +60,73 @@ pub enum InputValue {
6060 } ,
6161}
6262
63+ /// Returns a value between -1.0 and 1.0 based on the given value with its
64+ /// minimum and maximum values.
65+ pub fn normalize_signed_value ( raw_value : f64 , min : f64 , max : f64 ) -> f64 {
66+ let mid = ( max + min) / 2.0 ;
67+ let event_value = raw_value - mid;
68+
69+ // Normalize the value
70+ if event_value >= 0.0 {
71+ let maximum = max - mid;
72+ event_value / maximum
73+ } else {
74+ let minimum = min - mid;
75+ let value = event_value / minimum;
76+ -value
77+ }
78+ }
79+
80+ /// Convert the given normalized signed value to the real value based on the given
81+ /// minimum and maximum axis range.
82+ pub fn denormalize_signed_value_i16 ( normal_value : f64 , min : f64 , max : f64 ) -> i16 {
83+ let mid = ( max + min) / 2.0 ;
84+ let normal_value_abs = normal_value. abs ( ) ;
85+ if normal_value >= 0.0 {
86+ let maximum = max - mid;
87+ let value = normal_value * maximum + mid;
88+ value as i16
89+ } else {
90+ let minimum = min - mid;
91+ let value = normal_value_abs * minimum + mid;
92+ value as i16
93+ }
94+ }
95+
96+ /// Convert the given normalized signed value to the real value based on the given
97+ /// minimum and maximum axis range.
98+ pub fn denormalize_signed_value_u8 ( normal_value : f64 , min : f64 , max : f64 ) -> u8 {
99+ let mid = ( max + min) / 2.0 ;
100+ let normal_value_abs = normal_value. abs ( ) ;
101+ if normal_value >= 0.0 {
102+ let maximum = max - mid;
103+ let value = normal_value * maximum + mid;
104+ value as u8
105+ } else {
106+ let minimum = min - mid;
107+ let value = normal_value_abs * minimum + mid;
108+ value as u8
109+ }
110+ }
111+
112+ // Returns a value between 0.0 and 1.0 based on the given value with its
113+ // maximum.
114+ pub fn normalize_unsigned_value ( raw_value : f64 , max : f64 ) -> f64 {
115+ raw_value / max
116+ }
117+
118+ /// De-normalizes the given value from 0.0 - 1.0 into a real value based on
119+ /// the maximum axis range.
120+ pub fn denormalize_unsigned_value_u16 ( normal_value : f64 , max : f64 ) -> u16 {
121+ ( normal_value * max) . round ( ) as u16
122+ }
123+
124+ /// De-normalizes the given value from 0.0 - 1.0 into a real value based on
125+ /// the maximum axis range.
126+ pub fn denormalize_unsigned_value_u8 ( normal_value : f64 , max : f64 ) -> u8 {
127+ ( normal_value * max) . round ( ) as u8
128+ }
129+
63130impl InputValue {
64131 /// Returns whether or not the value is "pressed"
65132 pub fn pressed ( & self ) -> bool {
0 commit comments