HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
hitbox.hpp
1// Copyright Take Vos 2020.
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 "../geometry/point.hpp"
8#include "../assert.hpp"
9#include <limits>
10#include <cstdint>
11#include <compare>
12
13namespace hi::inline v1 {
14class widget;
15
16class hitbox {
17public:
18 enum class Type : uint8_t {
19 Outside,
20 Default,
21 Button,
22 TextEdit,
23 MoveArea,
24 BottomResizeBorder,
25 TopResizeBorder,
26 LeftResizeBorder,
27 RightResizeBorder,
28 BottomLeftResizeCorner,
29 BottomRightResizeCorner,
30 TopLeftResizeCorner,
31 TopRightResizeCorner,
32 ApplicationIcon
33 };
34
35 Type type;
36 widget const *widget;
37
38 constexpr hitbox(hitbox const &) noexcept = default;
39 constexpr hitbox(hitbox &&) noexcept = default;
40 constexpr hitbox &operator=(hitbox const &) noexcept = default;
41 constexpr hitbox &operator=(hitbox &&) noexcept = default;
42
43 constexpr hitbox() noexcept : widget(nullptr), _elevation(-std::numeric_limits<float>::max()), type(Type::Outside) {}
44
45 constexpr hitbox(
46 hi::widget const *widget,
47 float elevation = -std::numeric_limits<float>::max(),
48 Type type = Type::Default) noexcept :
49 widget(widget), _elevation(elevation), type(type)
50 {
51 }
52
53 constexpr hitbox(hi::widget const *widget, point3 position, Type type = Type::Default) noexcept :
54 widget(widget), _elevation(-position.z()), type(type)
55 {
56 }
57
58 [[nodiscard]] constexpr friend std::strong_ordering operator<=>(hitbox const &lhs, hitbox const &rhs) noexcept
59 {
60 if ((lhs.widget == nullptr) == (rhs.widget == nullptr)) {
61 // Either both are widgets, or both are not widgets.
62 hilet elevation_cmp = lhs._elevation <=> rhs._elevation;
63 if (elevation_cmp == 0) {
64 return static_cast<int>(lhs.type) <=> static_cast<int>(rhs.type);
65 } else if (elevation_cmp < 0) {
66 return std::strong_ordering::less;
67 } else if (elevation_cmp > 0) {
68 return std::strong_ordering::greater;
69 } else {
70 hi_no_default();
71 }
72 } else if (lhs.widget == nullptr) {
73 // If lhs is not a widget than it is less.
74 return std::strong_ordering::less;
75 } else {
76 // Otherwise the lhs is greater.
77 return std::strong_ordering::greater;
78 }
79 }
80
81private:
82 float _elevation;
83};
84
85} // namespace hi::inline v1
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
constexpr float & z() noexcept
Access the z element from the point.
Definition point.hpp:112
Definition hitbox.hpp:16
An interactive graphical object as part of the user-interface.
Definition widget.hpp:39