HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
translate2.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version float{1}.
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 "point2.hpp"
8#include "aarectangle.hpp"
9#include "../macros.hpp"
10#include <concepts>
11#include <exception>
12#include <compare>
13
14hi_export_module(hikogui.geometry : translate2);
15
16hi_export namespace hi { inline namespace v1 {
17
19public:
20 using array_type = simd<float, 4>;
21 using value_type = array_type::value_type;
22
23 constexpr translate2(translate2 const&) noexcept = default;
24 constexpr translate2(translate2&&) noexcept = default;
25 constexpr translate2& operator=(translate2 const&) noexcept = default;
26 constexpr translate2& operator=(translate2&&) noexcept = default;
27
28 [[nodiscard]] constexpr translate2() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
29
30 [[nodiscard]] constexpr explicit operator array_type() const noexcept
31 {
32 return _v;
33 }
34
35 [[nodiscard]] constexpr explicit translate2(array_type const& other) noexcept : _v(other)
36 {
37 hi_axiom(holds_invariant());
38 }
39
40 [[nodiscard]] constexpr explicit translate2(aarectangle const& other) noexcept :
41 _v(static_cast<array_type>(get<0>(other)).xy00())
42 {
43 }
44
45 [[nodiscard]] constexpr explicit translate2(aarectangle const& other, float z) noexcept :
46 _v(static_cast<array_type>(get<0>(other)).xy00())
47 {
48 _v.z() = z;
49 }
50
51 [[nodiscard]] constexpr explicit translate2(vector2 const& other) noexcept : _v(static_cast<array_type>(other)) {}
52
53 [[nodiscard]] constexpr explicit translate2(point2 const& other) noexcept : _v(static_cast<array_type>(other).xy00()) {}
54
55 [[nodiscard]] constexpr translate2(float x, float y) noexcept : _v(x, y, 0.0f, 0.0f) {}
56
57 [[nodiscard]] constexpr float x() const noexcept
58 {
59 return _v.x();
60 }
61
62 [[nodiscard]] constexpr float y() const noexcept
63 {
64 return _v.y();
65 }
66
67 [[nodiscard]] constexpr float& x() noexcept
68 {
69 return _v.x();
70 }
71
72 [[nodiscard]] constexpr float& y() noexcept
73 {
74 return _v.y();
75 }
76
83 [[nodiscard]] constexpr static translate2
84 align(aarectangle src_rectangle, aarectangle dst_rectangle, alignment alignment) noexcept
85 {
86 auto x = float{0};
88 x = dst_rectangle.left();
89
91 x = dst_rectangle.right() - src_rectangle.width();
92
94 x = dst_rectangle.center() - src_rectangle.width() * 0.5f;
95
96 } else {
97 hi_no_default();
98 }
99
100 auto y = float{0};
102 y = dst_rectangle.bottom();
103
104 } else if (alignment == vertical_alignment::top) {
105 y = dst_rectangle.top() - src_rectangle.height();
106
108 y = dst_rectangle.middle() - src_rectangle.height() * 0.5f;
109
110 } else {
111 hi_no_default();
112 }
113
114 return translate2{x - src_rectangle.left(), y - src_rectangle.bottom()};
115 }
116
117 [[nodiscard]] constexpr friend bool operator==(translate2 const& lhs, translate2 const& rhs) noexcept
118 {
119 return equal(lhs._v, rhs._v);
120 }
121
122 [[nodiscard]] constexpr translate2 operator~() const noexcept
123 {
124 return translate2{-_v};
125 }
126
127 [[nodiscard]] constexpr bool holds_invariant() const noexcept
128 {
129 return _v.z() == 0.0f and _v.w() == 0.0f;
130 }
131
132 [[nodiscard]] friend constexpr translate2 round(translate2 const& rhs) noexcept
133 {
134 return translate2{round(rhs._v)};
135 }
136
137private:
138 array_type _v;
139};
140
141}} // namespace hi::v1
@ middle
Align to the vertical-middle.
@ bottom
Align to the bottom.
@ top
Align to the top.
@ right
Align the text to the right side.
@ left
Align the text to the left side.
@ center
Align the text in the center.
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition simd_intf.hpp:18
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
Horizontal/Vertical alignment combination.
Definition alignment.hpp:244
Definition translate2.hpp:18
static constexpr translate2 align(aarectangle src_rectangle, aarectangle dst_rectangle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition translate2.hpp:84
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:27