aboutsummaryrefslogtreecommitdiff
path: root/src/math_util.hpp
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2018-11-27 21:12:10 +0800
committerGitHub <noreply@github.com>2018-11-27 21:12:10 +0800
commitee22597122612cd75fe62f5d808cb51478373fad (patch)
tree19b39da16f155451d5817e82e045d69d7410acbe /src/math_util.hpp
parent30333294fcd5917a9f3572f0c4c6dfc2ec429a3c (diff)
parent5b770e3bf0f3f9e22454d9e092630b22f5916ebe (diff)
downloadcru-ee22597122612cd75fe62f5d808cb51478373fad.tar.gz
cru-ee22597122612cd75fe62f5d808cb51478373fad.tar.bz2
cru-ee22597122612cd75fe62f5d808cb51478373fad.zip
Merge pull request #26 from crupest/3-scrollview
Develop scrollview.
Diffstat (limited to 'src/math_util.hpp')
-rw-r--r--src/math_util.hpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/math_util.hpp b/src/math_util.hpp
new file mode 100644
index 00000000..b9830d6b
--- /dev/null
+++ b/src/math_util.hpp
@@ -0,0 +1,69 @@
+#pragma once
+
+// ReSharper disable once CppUnusedIncludeDirective
+#include "pre.hpp"
+
+// ReSharper disable once CppUnusedIncludeDirective
+#include <type_traits>
+#include <optional>
+
+namespace cru
+{
+ template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
+ float Coerce(const T n, const std::optional<T> min, const std::optional<T> max)
+ {
+ if (min.has_value() && n < min.value())
+ return min.value();
+ if (max.has_value() && n > max.value())
+ return max.value();
+ return n;
+ }
+
+ template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
+ float Coerce(const T n, const T min, const T max)
+ {
+ if (n < min)
+ return min;
+ if (n > max)
+ return max;
+ return n;
+ }
+
+ template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
+ float Coerce(const T n, const std::nullopt_t, const std::optional<T> max)
+ {
+ if (max.has_value() && n > max.value())
+ return max.value();
+ return n;
+ }
+
+ template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
+ float Coerce(const T n, const std::optional<T> min, const std::nullopt_t)
+ {
+ if (min.has_value() && n < min.value())
+ return min.value();
+ return n;
+ }
+
+ template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
+ float Coerce(const T n, const std::nullopt_t, const T max)
+ {
+ if (n > max)
+ return max;
+ return n;
+ }
+
+ template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
+ float Coerce(const T n, const T min, const std::nullopt_t)
+ {
+ if (n < min)
+ return min;
+ return n;
+ }
+
+ template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
+ T AtLeast0(const T value)
+ {
+ return value < static_cast<T>(0) ? static_cast<T>(0) : value;
+ }
+}