aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/render/MeasureRequirement.hpp
blob: 661f40a90aa1ea383be1deace87299f0c7000067 (plain)
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
#pragma once
#include "Base.hpp"

#include <limits>

namespace cru::ui::render {
class MeasureLength {
 public:
  struct tag_not_specify_t {};
  constexpr static tag_not_specify_t tag_not_specify{};

  constexpr MeasureLength() : MeasureLength(0) {}
  constexpr MeasureLength(tag_not_specify_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 NotSpecify() {
    return MeasureLength{tag_not_specify};
  }

  // What not specify means depends on situation.
  // For max size, it means no limit.
  constexpr bool IsNotSpecify() const { return length_ < 0; }

  // If not specify max value of float is returned.
  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.IsNotSpecify() && max_width.GetLength() < size.width)
      return false;
    if (!max_height.IsNotSpecify() && 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::NotSpecify(),
                              MeasureLength::NotSpecify()};
  }
};
}  // namespace cru::ui::render