aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/render/MeasureRequirement.hpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-06-22 01:09:24 +0800
committercrupest <crupest@outlook.com>2020-06-22 01:09:24 +0800
commit4f0a2f32c273780c32cc3937615c2a8bbd993aab (patch)
tree6e1f45447854a40fe2d16ef9bec79f3c0fef030a /include/cru/ui/render/MeasureRequirement.hpp
parentd86a71f79afe0e4dac768f61d6bff690567aca5b (diff)
downloadcru-4f0a2f32c273780c32cc3937615c2a8bbd993aab.tar.gz
cru-4f0a2f32c273780c32cc3937615c2a8bbd993aab.tar.bz2
cru-4f0a2f32c273780c32cc3937615c2a8bbd993aab.zip
...
Diffstat (limited to 'include/cru/ui/render/MeasureRequirement.hpp')
-rw-r--r--include/cru/ui/render/MeasureRequirement.hpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/include/cru/ui/render/MeasureRequirement.hpp b/include/cru/ui/render/MeasureRequirement.hpp
new file mode 100644
index 00000000..83de255d
--- /dev/null
+++ b/include/cru/ui/render/MeasureRequirement.hpp
@@ -0,0 +1,79 @@
+#pragma once
+#include "Base.hpp"
+
+#include <limits>
+
+namespace cru::ui::render {
+class MeasureLength {
+ public:
+ struct tag_infinate_t {};
+ constexpr static tag_infinate_t tag_infinate{};
+
+ constexpr MeasureLength() : MeasureLength(0) {}
+ constexpr MeasureLength(tag_infinate_t) : length_(-1) {}
+ constexpr MeasureLength(float length) : length_(length) {
+ Expects(length >= 0);
+ }
+
+ MeasureLength(const MeasureLength& other) = default;
+ MeasureLength& operator=(const MeasureLength& other) = default;
+ MeasureLength& operator=(float length) {
+ Expects(length >= 0);
+ length_ = length;
+ return *this;
+ }
+
+ ~MeasureLength() = default;
+
+ constexpr static MeasureLength Infinate() {
+ return MeasureLength{tag_infinate};
+ }
+
+ constexpr bool IsInfinate() const { return length_ < 0; }
+ constexpr float GetLength() const {
+ return length_ < 0 ? std::numeric_limits<float>::max() : length_;
+ }
+
+ constexpr bool operator==(MeasureLength other) const {
+ return (length_ < 0 && other.length_ < 0) || length_ == other.length_;
+ }
+
+ constexpr bool operator!=(MeasureLength other) const {
+ return !operator==(other);
+ }
+
+ private:
+ // -1 for infinate
+ float length_;
+};
+
+struct MeasureRequirement {
+ MeasureLength max_width;
+ MeasureLength max_height;
+
+ constexpr MeasureRequirement() = default;
+ constexpr MeasureRequirement(MeasureLength max_width,
+ MeasureLength max_height)
+ : max_width(max_width), max_height(max_height) {}
+
+ constexpr MeasureRequirement(const Size& max_size)
+ : max_width(max_size.width), max_height(max_size.height) {}
+
+ constexpr bool Satisfy(const Size& size) const {
+ if (!max_width.IsInfinate() && max_width.GetLength() < size.width)
+ return false;
+ if (!max_height.IsInfinate() && max_height.GetLength() < size.height)
+ return false;
+ return true;
+ }
+
+ constexpr Size GetMaxSize() const {
+ return Size{max_width.GetLength(), max_height.GetLength()};
+ }
+
+ constexpr static MeasureRequirement Infinate() {
+ return MeasureRequirement{MeasureLength::Infinate(),
+ MeasureLength::Infinate()};
+ }
+};
+} // namespace cru::ui::render