aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/render/MeasureRequirement.h
blob: 5ea22952e02a78c4a3841e0668688adc72b26352 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#pragma once
#include "../Base.h"

#include <algorithm>
#include <format>
#include <limits>
#include <string>

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

  constexpr MeasureLength() : MeasureLength(tag_not_specify) {}
  constexpr MeasureLength(tag_not_specify_t) : length_(-1) {}
  constexpr MeasureLength(float length) : length_(length) {
    if (length < 0.0f) {
      throw Exception("Measure length must not be positive.");
    }
  }
  constexpr MeasureLength& operator=(float length) {
    if (length < 0.0f) {
      throw Exception("Measure length must not be positive.");
    }
    length_ = length;
    return *this;
  }

  constexpr static MeasureLength NotSpecified() {
    return MeasureLength(tag_not_specify);
  }

  constexpr bool IsSpecified() const { return length_ >= 0.0f; }

  constexpr float GetLengthOr(float value) const {
    return length_ < 0 ? value : length_;
  }

  // If not specify max value of float is returned.
  constexpr float GetLengthOrMaxFloat() const {
    return GetLengthOr(std::numeric_limits<float>::max());
  }

  // If not specify 0 is returned.
  constexpr float GetLengthOr0() const { return GetLengthOr(0.f); }

  // If not specify, return value is undefined.
  constexpr float GetLengthOrUndefined() const { return length_; }

  constexpr MeasureLength Or(MeasureLength value) const {
    return IsSpecified() ? *this : value;
  }

  constexpr MeasureLength OverrideBy(MeasureLength value) const {
    return value.IsSpecified() ? value : *this;
  }

  /**
   * If specified, given length is added and negative value is coerced to 0.
   */
  constexpr MeasureLength Plus(float length) const {
    if (IsSpecified())
      return std::max(length_ + length, 0.f);
    else
      return NotSpecified();
  }

  /**
   * If specified, given length is minused and negative value is coerced to 0.
   */
  constexpr MeasureLength Minus(float length) const {
    if (IsSpecified())
      return std::max(length_ - length, 0.f);
    else
      return NotSpecified();
  }

  /**
   * 1. Both unspecified => unspecified.
   * 2. One is specified and the other is not => the specified one.
   * 3. Both specified => smaller one.
   */
  constexpr MeasureLength Min(MeasureLength other) const {
    if (IsSpecified()) {
      return other.IsSpecified() ? std::min(length_, other.length_) : *this;
    } else {
      return other;
    }
  }

  /**
   * 1. This is unspecified => other.
   * 2. This is specified => smaller one.
   */
  constexpr float Min(float other) const {
    return IsSpecified() ? std::min(length_, other) : other;
  }

  /**
   * 1. Both unspecified => unspecified.
   * 2. One is specified and the other is not => the specified one.
   * 3. Both specified => bigger one.
   */
  constexpr MeasureLength Max(MeasureLength other) const {
    if (IsSpecified()) {
      return other.IsSpecified() ? std::max(length_, other.length_) : *this;
    } else {
      return other;
    }
  }

  /**
   * 1. This is unspecified => other.
   * 2. This is specified => bigger one.
   */
  constexpr float Max(float other) const {
    return IsSpecified() ? std::max(length_, other) : other;
  }

  std::string ToString() const {
    return IsSpecified() ? std::to_string(GetLengthOrUndefined())
                         : "unspecified";
  }

  constexpr bool operator==(const MeasureLength& other) const = default;

 private:
  // -1 for not specify
  float length_;
};
}  // namespace cru::ui::render

template <>
struct std::formatter<cru::ui::render::MeasureLength, char>
    : cru::string::ImplementFormatterByToString<
          cru::ui::render::MeasureLength> {};

namespace cru::ui::render {
struct MeasureSize {
  MeasureLength width;
  MeasureLength height;

  constexpr MeasureSize() = default;
  constexpr MeasureSize(MeasureLength width, MeasureLength height)
      : width(width), height(height) {}
  constexpr MeasureSize(const Size& size)
      : width(size.width), height(size.height) {}

  constexpr MeasureSize& operator=(const Size& other) {
    width = other.width;
    height = other.height;
    return *this;
  }

  constexpr static MeasureSize NotSpecified() {
    return {MeasureLength::NotSpecified(), MeasureLength::NotSpecified()};
  }

  constexpr Size GetSizeOrMaxFloat() const {
    return {width.GetLengthOrMaxFloat(), height.GetLengthOrMaxFloat()};
  }

  constexpr Size GetSizeOr0() const {
    return {width.GetLengthOr0(), height.GetLengthOr0()};
  }

  constexpr Size GetSizeOr(const Size& other) const {
    return {width.GetLengthOr(other.width), height.GetLengthOr(other.height)};
  }

  constexpr MeasureSize Or(const MeasureSize& other) const {
    return {width.Or(other.width), height.Or(other.height)};
  }

  constexpr MeasureSize OverrideBy(const MeasureSize& other) const {
    return {width.OverrideBy(other.width), height.OverrideBy(other.height)};
  }

  constexpr MeasureSize Plus(const Size& size) const {
    return {width.Plus(size.width), height.Plus(size.height)};
  }

  constexpr MeasureSize Minus(const Size& size) const {
    return {width.Minus(size.width), height.Minus(size.height)};
  }

  constexpr MeasureSize Min(const MeasureSize& other) const {
    return {width.Min(other.width), height.Min(other.height)};
  }

  constexpr Size Min(const Size& other) const {
    return {width.Min(other.width), height.Min(other.height)};
  }

  constexpr MeasureSize Max(const MeasureSize& other) const {
    return {width.Max(other.width), height.Max(other.height)};
  }

  constexpr Size Max(const Size& other) const {
    return {width.Max(other.width), height.Max(other.height)};
  }

  std::string ToString() const { return std::format("{}x{}", width, height); }

  constexpr bool operator==(const MeasureSize& other) const = default;
};
}  // namespace cru::ui::render

template <>
struct std::formatter<cru::ui::render::MeasureSize, char>
    : cru::string::ImplementFormatterByToString<cru::ui::render::MeasureSize> {
};

namespace cru::ui::render {
struct MeasureRequirement {
  MeasureSize max;
  MeasureSize min;
  MeasureSize suggest;

  constexpr MeasureRequirement() = default;
  constexpr MeasureRequirement(const MeasureSize& max, const MeasureSize& min,
                               const MeasureSize& suggest,
                               bool allow_coerce_suggest = true)
      : max(max), min(min), suggest(Coerce(suggest)) {
    if (max.width.GetLengthOrMaxFloat() < min.width.GetLengthOr0()) {
      throw Exception(
          "Measure requirement's max width is smaller than min width.");
    }

    if (max.height.GetLengthOrMaxFloat() < min.height.GetLengthOr0()) {
      throw Exception(
          "Measure requirement's max height is smaller than min height.");
    }

    if (!allow_coerce_suggest && this->suggest != suggest) {
      throw Exception(
          "Measure requirement's suggest size is in invalid range.");
    }
  }

  constexpr bool Satisfy(const Size& size) const {
    return max.width.GetLengthOrMaxFloat() >= size.width &&
           max.height.GetLengthOrMaxFloat() >= size.height &&
           min.width.GetLengthOr0() <= size.width &&
           min.height.GetLengthOr0() <= size.height;
  }

  constexpr Size Coerce(const Size& size) const {
    return max.Min(min.Max(size));
  }

  constexpr Size ExpandToSuggestAndCoerce(const Size& size) const {
    return max.Min(min.Max(suggest.Max(size)));
  }

  constexpr MeasureSize Coerce(const MeasureSize& size) const {
    MeasureSize result = size;
    if (result.width.IsSpecified())
      result.width =
          max.width.Min(min.width.Max(result.width.GetLengthOrUndefined()));

    if (result.height.IsSpecified())
      result.height =
          max.height.Min(min.height.Max(result.height.GetLengthOrUndefined()));

    return result;
  }

  constexpr MeasureRequirement Minus(const Size& size) const {
    return {max.Minus(size), min.Minus(size), suggest.Minus(size)};
  }

  /**
   * Suggest size will use the other's one if this doesn't specify one but the
   * other does.
   */
  constexpr MeasureRequirement Merge(const MeasureRequirement& other) const {
    return {max.Min(other.max), min.Max(other.min),
            suggest.OverrideBy(other.suggest)};
  }

  std::string ToString() const {
    return std::format("(min: {}, max: {}, suggest: {})", min, max, suggest);
  }

  constexpr bool operator==(const MeasureRequirement& other) const = default;
};
}  // namespace cru::ui::render

template <>
struct std::formatter<cru::ui::render::MeasureRequirement, char>
    : cru::string::ImplementFormatterByToString<
          cru::ui::render::MeasureRequirement> {};