HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
hitbox.hpp
1// Copyright Take Vos 2020-2021.
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/geometry.hpp"
8#include "../utility/utility.hpp"
9#include "widget_id.hpp"
10#include "../macros.hpp"
11#include <limits>
12#include <cstdint>
13#include <compare>
14#include <utility>
15#include <exception>
16
17hi_export_module(hikogui.GUI : hitbox);
18
19hi_export namespace hi::inline v1 {
20
21enum class hitbox_type : uint8_t {
22 outside,
23 _default,
24 button,
25 scroll_bar,
26 text_edit,
27 move_area,
28 bottom_resize_border,
29 top_resize_border,
30 left_resize_border,
31 right_resize_border,
32 bottom_left_resize_corner,
33 bottom_right_resize_corner,
34 top_left_resize_corner,
35 top_right_resize_corner,
36 application_icon
37};
38
39class hitbox {
40public:
41 hitbox_type type;
42 widget_id widget_id;
43
44 constexpr hitbox(hitbox const&) noexcept = default;
45 constexpr hitbox(hitbox&&) noexcept = default;
46 constexpr hitbox& operator=(hitbox const&) noexcept = default;
47 constexpr hitbox& operator=(hitbox&&) noexcept = default;
48
49 constexpr hitbox() noexcept : widget_id(), _elevation(-std::numeric_limits<float>::max()), type(hitbox_type::outside) {}
50
51 constexpr hitbox(
52 hi::widget_id widget_id,
53 float elevation = -std::numeric_limits<float>::max(),
54 hitbox_type type = hitbox_type::_default) noexcept :
55 widget_id(widget_id), _elevation(elevation), type(type)
56 {
57 }
58
59 [[nodiscard]] constexpr friend std::strong_ordering operator<=>(hitbox const& lhs, hitbox const& rhs) noexcept
60 {
61 if ((lhs.widget_id == 0) == (rhs.widget_id == 0)) {
62 // Either both are widgets, or both are not widgets.
63 auto const elevation_ordering = lhs._elevation <=> rhs._elevation;
64 if (elevation_ordering == std::partial_ordering::equivalent) {
65 return std::to_underlying(lhs.type) <=> std::to_underlying(rhs.type);
66 } else if (elevation_ordering == std::partial_ordering::less) {
67 return std::strong_ordering::less;
68 } else if (elevation_ordering == std::partial_ordering::greater) {
69 return std::strong_ordering::greater;
70 } else {
71 hi_no_default();
72 }
73 } else if (lhs.widget_id == 0) {
74 // If lhs is not a widget than it is less.
75 return std::strong_ordering::less;
76 } else {
77 // Otherwise the lhs is greater.
78 return std::strong_ordering::greater;
79 }
80 }
81
82private:
83 float _elevation;
84};
85
86} // namespace hi::inline v1
@ outside
The border is drawn outside the edge of a quad.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition hitbox.hpp:39