TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_numeric_field_config.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cfloat>
5#include <cmath>
6#include <limits>
7#include <stdint.h>
8
9namespace tachyon
10{
13
14 namespace detail
15 {
19 template<typename T>
20 T DragInteger(double delta, T startValue, T minVal, T maxVal)
21 {
22 if (std::isnan(delta))
23 return std::max(minVal, std::min(startValue, maxVal));
24 constexpr double kExact = 9007199254740992.0; // 2^53
25 double result;
26 if (std::fabs(double(startValue)) <= kExact)
27 result = double(startValue) + delta;
28 else
29 {
30 // Only 64-bit types get here. In unsigned math, which wraps as two's complement
31 // does: the room left on the side moved to, and the move within it.
32 const double step = std::trunc(delta);
33 const uint64_t from = uint64_t(startValue);
34 const uint64_t room = step >= 0 ? uint64_t(std::numeric_limits<T>::max()) - from
35 : from - uint64_t(std::numeric_limits<T>::lowest());
36 if (std::fabs(step) >= double(room))
37 return step >= 0 ? std::max(minVal, maxVal) : minVal;
38 const uint64_t by = uint64_t(std::fabs(step));
39 const T moved = T(step >= 0 ? from + by : from - by);
40 return std::max(minVal, std::min(moved, maxVal));
41 }
42 // double(max) of a 64-bit type rounds up past it: compare before converting.
43 if (result <= double(minVal)) return minVal;
44 if (result >= double(maxVal)) return std::max(minVal, maxVal);
45 return T(result);
46 }
47 }
48
53 template<typename T>
55
57 template<>
58 struct NumericFieldConfig<double>
59 {
60 double stepPerPixel = 0.01;
61 double minVal = -DBL_MAX;
62 double maxVal = DBL_MAX;
63 int decimals = -1;
64
67 double Calculate(int32_t offset, double speedScale, double startValue) const
68 {
69 double result = startValue + offset * speedScale * stepPerPixel;
70 if (std::isnan(result)) return 0.0;
71 return std::max(minVal, std::min(result, maxVal));
72 }
73 };
74
76 template<>
77 struct NumericFieldConfig<float>
78 {
79 double stepPerPixel = 0.01;
80 float minVal = -FLT_MAX;
81 float maxVal = FLT_MAX;
82 int decimals = -1;
83
86 float Calculate(int32_t offset, double speedScale, float startValue) const
87 {
88 double result = double(startValue) + offset * speedScale * stepPerPixel;
89 if (std::isnan(result)) return 0.0f;
90 return float(std::max(double(minVal), std::min(result, double(maxVal))));
91 }
92 };
93
95 template<>
96 struct NumericFieldConfig<uint64_t>
97 {
98 double stepPerPixel = 1.00;
99 uint64_t minVal = 0;
100 uint64_t maxVal = UINT64_MAX;
103 uint64_t Calculate(int32_t offset, double speedScale, uint64_t startValue) const
104 {
105 return detail::DragInteger(offset * speedScale * stepPerPixel, startValue, minVal, maxVal);
106 }
107 };
108
110 template<>
111 struct NumericFieldConfig<uint32_t>
112 {
113 double stepPerPixel = 1.00;
114 uint32_t minVal = 0;
115 uint32_t maxVal = UINT32_MAX;
118 uint32_t Calculate(int32_t offset, double speedScale, uint32_t startValue) const
119 {
120 return detail::DragInteger(offset * speedScale * stepPerPixel, startValue, minVal, maxVal);
121 }
122 };
123
125 template<>
126 struct NumericFieldConfig<uint16_t>
127 {
128 double stepPerPixel = 1.00;
129 uint16_t minVal = 0;
130 uint16_t maxVal = UINT16_MAX;
133 uint16_t Calculate(int32_t offset, double speedScale, uint16_t startValue) const
134 {
135 return detail::DragInteger(offset * speedScale * stepPerPixel, startValue, minVal, maxVal);
136 }
137 };
138
140 template<>
141 struct NumericFieldConfig<uint8_t>
142 {
143 double stepPerPixel = 1.00;
144 uint8_t minVal = 0;
145 uint8_t maxVal = UINT8_MAX;
148 uint8_t Calculate(int32_t offset, double speedScale, uint8_t startValue) const
149 {
150 return detail::DragInteger(offset * speedScale * stepPerPixel, startValue, minVal, maxVal);
151 }
152 };
153
155 template<>
156 struct NumericFieldConfig<int64_t>
157 {
158 double stepPerPixel = 1.00;
159 int64_t minVal = INT64_MIN;
160 int64_t maxVal = INT64_MAX;
163 int64_t Calculate(int32_t offset, double speedScale, int64_t startValue) const
164 {
165 return detail::DragInteger(offset * speedScale * stepPerPixel, startValue, minVal, maxVal);
166 }
167 };
168
170 template<>
171 struct NumericFieldConfig<int32_t>
172 {
173 double stepPerPixel = 1.00;
174 int32_t minVal = INT32_MIN;
175 int32_t maxVal = INT32_MAX;
178 int32_t Calculate(int32_t offset, double speedScale, int32_t startValue) const
179 {
180 return detail::DragInteger(offset * speedScale * stepPerPixel, startValue, minVal, maxVal);
181 }
182 };
183
185 template<>
186 struct NumericFieldConfig<int16_t>
187 {
188 double stepPerPixel = 1.00;
189 int16_t minVal = INT16_MIN;
190 int16_t maxVal = INT16_MAX;
193 int16_t Calculate(int32_t offset, double speedScale, int16_t startValue) const
194 {
195 return detail::DragInteger(offset * speedScale * stepPerPixel, startValue, minVal, maxVal);
196 }
197 };
198
200 template<>
201 struct NumericFieldConfig<int8_t>
202 {
203 double stepPerPixel = 1.00;
204 int8_t minVal = INT8_MIN;
205 int8_t maxVal = INT8_MAX;
208 int8_t Calculate(int32_t offset, double speedScale, int8_t startValue) const
209 {
210 return detail::DragInteger(offset * speedScale * stepPerPixel, startValue, minVal, maxVal);
211 }
212 };
213
214}
Definition gui_logging.h:24
double Calculate(int32_t offset, double speedScale, double startValue) const
The value after dragging offset pixels from startValue; speedScale is the modifier factor (Ctrl 0....
Definition gui_numeric_field_config.h:67
double minVal
lowest allowed value
Definition gui_numeric_field_config.h:61
int decimals
digits shown after the point (2: "1.00"); -1: as few as the value needs
Definition gui_numeric_field_config.h:63
double maxVal
highest allowed value
Definition gui_numeric_field_config.h:62
double stepPerPixel
value change per pixel dragged
Definition gui_numeric_field_config.h:60
int decimals
digits shown after the point (2: "1.00"); -1: as few as the value needs
Definition gui_numeric_field_config.h:82
float Calculate(int32_t offset, double speedScale, float startValue) const
The value after dragging offset pixels from startValue; speedScale is the modifier factor (Ctrl 0....
Definition gui_numeric_field_config.h:86
double stepPerPixel
value change per pixel dragged
Definition gui_numeric_field_config.h:79
float minVal
lowest allowed value
Definition gui_numeric_field_config.h:80
float maxVal
highest allowed value
Definition gui_numeric_field_config.h:81
double stepPerPixel
value change per pixel dragged
Definition gui_numeric_field_config.h:188
int16_t Calculate(int32_t offset, double speedScale, int16_t startValue) const
The value after dragging offset pixels from startValue; speedScale is the modifier factor (Ctrl 0....
Definition gui_numeric_field_config.h:193
int16_t minVal
lowest allowed value
Definition gui_numeric_field_config.h:189
int16_t maxVal
highest allowed value
Definition gui_numeric_field_config.h:190
int32_t minVal
lowest allowed value
Definition gui_numeric_field_config.h:174
int32_t maxVal
highest allowed value
Definition gui_numeric_field_config.h:175
int32_t Calculate(int32_t offset, double speedScale, int32_t startValue) const
The value after dragging offset pixels from startValue; speedScale is the modifier factor (Ctrl 0....
Definition gui_numeric_field_config.h:178
double stepPerPixel
value change per pixel dragged
Definition gui_numeric_field_config.h:173
int64_t minVal
lowest allowed value
Definition gui_numeric_field_config.h:159
double stepPerPixel
value change per pixel dragged
Definition gui_numeric_field_config.h:158
int64_t maxVal
highest allowed value
Definition gui_numeric_field_config.h:160
int64_t Calculate(int32_t offset, double speedScale, int64_t startValue) const
The value after dragging offset pixels from startValue; speedScale is the modifier factor (Ctrl 0....
Definition gui_numeric_field_config.h:163
double stepPerPixel
value change per pixel dragged
Definition gui_numeric_field_config.h:203
int8_t maxVal
highest allowed value
Definition gui_numeric_field_config.h:205
int8_t Calculate(int32_t offset, double speedScale, int8_t startValue) const
The value after dragging offset pixels from startValue; speedScale is the modifier factor (Ctrl 0....
Definition gui_numeric_field_config.h:208
int8_t minVal
lowest allowed value
Definition gui_numeric_field_config.h:204
uint16_t minVal
lowest allowed value
Definition gui_numeric_field_config.h:129
uint16_t maxVal
highest allowed value
Definition gui_numeric_field_config.h:130
uint16_t Calculate(int32_t offset, double speedScale, uint16_t startValue) const
The value after dragging offset pixels from startValue; speedScale is the modifier factor (Ctrl 0....
Definition gui_numeric_field_config.h:133
double stepPerPixel
value change per pixel dragged
Definition gui_numeric_field_config.h:128
uint32_t maxVal
highest allowed value
Definition gui_numeric_field_config.h:115
double stepPerPixel
value change per pixel dragged
Definition gui_numeric_field_config.h:113
uint32_t Calculate(int32_t offset, double speedScale, uint32_t startValue) const
The value after dragging offset pixels from startValue; speedScale is the modifier factor (Ctrl 0....
Definition gui_numeric_field_config.h:118
uint32_t minVal
lowest allowed value
Definition gui_numeric_field_config.h:114
uint64_t maxVal
highest allowed value
Definition gui_numeric_field_config.h:100
uint64_t Calculate(int32_t offset, double speedScale, uint64_t startValue) const
The value after dragging offset pixels from startValue; speedScale is the modifier factor (Ctrl 0....
Definition gui_numeric_field_config.h:103
double stepPerPixel
value change per pixel dragged
Definition gui_numeric_field_config.h:98
uint64_t minVal
lowest allowed value
Definition gui_numeric_field_config.h:99
uint8_t minVal
lowest allowed value
Definition gui_numeric_field_config.h:144
uint8_t Calculate(int32_t offset, double speedScale, uint8_t startValue) const
The value after dragging offset pixels from startValue; speedScale is the modifier factor (Ctrl 0....
Definition gui_numeric_field_config.h:148
double stepPerPixel
value change per pixel dragged
Definition gui_numeric_field_config.h:143
uint8_t maxVal
highest allowed value
Definition gui_numeric_field_config.h:145
Drag speed and range of a numeric Field, specialized per value type.
Definition gui_numeric_field_config.h:54