HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
box_shape.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "box_constraints.hpp"
8#include "../geometry/module.hpp"
9#include "../utility/module.hpp"
10#include <limits>
11#include <optional>
12
13namespace hi { inline namespace v1 {
14
15struct box_shape {
16 aarectangle rectangle;
17 std::optional<float> baseline = {};
18 std::optional<float> centerline = {};
19
20 constexpr box_shape() noexcept = default;
21 constexpr box_shape(box_shape const&) noexcept = default;
22 constexpr box_shape(box_shape&&) noexcept = default;
23 constexpr box_shape& operator=(box_shape const&) noexcept = default;
24 constexpr box_shape& operator=(box_shape&&) noexcept = default;
25 [[nodiscard]] constexpr friend bool operator==(box_shape const&, box_shape const&) noexcept = default;
26
27 constexpr box_shape(extent2 size) noexcept : rectangle(size), baseline(), centerline()
28 {
29 hi_axiom(holds_invariant());
30 }
31
32 constexpr box_shape(
34 box_constraints const& constraints,
35 aarectangle const& rectangle,
36 float baseline_adjustment) noexcept :
37 rectangle(rectangle),
38 baseline(make_guideline(
39 constraints.alignment.vertical(),
40 rectangle.bottom(),
41 rectangle.top(),
42 baseline_adjustment)),
43 centerline(make_guideline(
44 constraints.alignment.horizontal(),
45 rectangle.left(),
46 rectangle.right()))
47 {
48 hi_axiom(holds_invariant());
49 }
50
51 constexpr box_shape(box_constraints const& constraints, aarectangle rectangle, float baseline_adjustment) noexcept :
52 box_shape(override_t{}, constraints, rectangle, baseline_adjustment)
53 {
54 hi_axiom(rectangle.size() >= constraints.minimum);
55 hi_axiom(holds_invariant());
56 }
57
58 [[nodiscard]] constexpr float x() const noexcept
59 {
60 return rectangle.x();
61 }
62
63 [[nodiscard]] constexpr float y() const noexcept
64 {
65 return rectangle.y();
66 }
67
68 [[nodiscard]] constexpr extent2 size() const noexcept
69 {
70 return rectangle.size();
71 }
72
73 [[nodiscard]] constexpr float width() const noexcept
74 {
75 return rectangle.width();
76 }
77
78 [[nodiscard]] constexpr float height() const noexcept
79 {
80 return rectangle.height();
81 }
82
83 [[nodiscard]] constexpr bool holds_invariant() const noexcept
84 {
85 if (not(is_integral_value(rectangle.x()) and is_integral_value(rectangle.y()) and is_integral_value(rectangle.width()) and
86 is_integral_value(rectangle.height()))) {
87 return false;
88 }
89 if (baseline and not is_integral_value(*baseline)) {
90 return false;
91 }
92 if (centerline and not is_integral_value(*centerline)) {
93 return false;
94 }
95 return true;
96 }
97};
98
99}} // namespace hi::v1
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
constexpr bool is_integral_value(T const &rhs) noexcept
Check if a value is integral.
Definition value_traits.hpp:18
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:27
2D constraints.
Definition box_constraints.hpp:22
Definition box_shape.hpp:15
Tag used for special functions or constructions to do a override compared to another function of the ...
Definition utility.hpp:235