Euphoria
rect.h
Go to the documentation of this file.
1#pragma once
2
3#include "eu/assert/assert.h"
4
5#include "eu/base/vec2.h"
6#include "eu/base/range.h"
7
8// Bottom, Left of screen is (0,0)
9// X-axis is positive right, Y-axis is positive up
10
11
12namespace eu
13{
14 enum class RectCut { left, right, top, bottom };
15
18 struct Lrtb
19 {
20 float left; float right; float top; float bottom;
21 constexpr explicit Lrtb(float all) : left(all), right(all), top(all), bottom(all) {}
22 constexpr Lrtb(float lr, float tb) : left(lr), right(lr), top(tb), bottom(tb) {}
23 constexpr Lrtb(float l, float r, float t, float b) : left(l), right(r), top(t), bottom(b) {}
24 };
25
26 struct Rect
27 {
28 float left; // min x
29 float right; // max x
30 float top; // max y
31 float bottom; // min y
32
34
35 bool operator==(const Rect& rhs) = delete;
36
37
44
46 [[nodiscard]] static Rect from_bottom_left_size(const v2& bl, const v2& size);
47 [[nodiscard]] static Rect from_top_left_size(const v2& top_left, const v2& size);
48 [[nodiscard]] static Rect from_size(const v2& size);
49 [[nodiscard]] static Rect from_point(const v2& point);
50
51 // extend/include/union current rect with another point/rect
52 void extend(const Rect& o);
53
54 void translate(const v2& v);
55 void inset(const Lrtb& lrtb); // moves each side towards the center
56
57 void set_empty();
58
61
63 [[nodiscard]] static Rect from_scaled(const Rect& self, float scale);
64
66 [[nodiscard]] bool contains_exclusive(const Rect& r) const;
67
68 [[nodiscard]] Rect with_inset(const Lrtb& lrtb) const;
69 [[nodiscard]] Rect with_translate(const v2& v) const;
70 [[nodiscard]] Rect with_top_left_at(const v2& p) const;
72 [[nodiscard]] Rect with_offset(const v2& p) const;
73 [[nodiscard]] Rect with_scale(float hor, float vert) const;
74
77
78 // position is from bottom left
80
81 // Returns true if the rectangle is empty (left >= right or top <= bottom)
82 [[nodiscard]] bool is_empty() const;
83
84 // a 0 width/height is also considered valid
85 [[nodiscard]] bool is_valid() const;
86
87 [[nodiscard]] v2 get_size() const;
88
94
95 Rect cut_left(float amount, float h_spacing = 0.0f);
96 Rect cut_right(float amount, float h_spacing = 0.0f);
97 Rect cut_bottom(float amount, float v_spacing = 0.0f);
98 Rect cut_top(float amount, float v_spacing = 0.0f);
99
100 Rect cut(RectCut side, float amount, float spacing = 0.0f);
101
102 // Same as cut, except they keep the input rect intact.
103 // Useful for decorations
104 Rect get_left(float amount) const;
105 Rect get_right(float amount) const;
106 Rect get_bottom(float amount) const;
107 Rect get_top(float amount) const;
108
109 // These will add a rectangle outside of the input rectangle.
110 // Useful for tooltips and other overlay elements.
111 Rect add_left(float amount) const;
112 Rect add_right(float amount) const;
113 Rect add_bottom(float amount) const;
114 Rect add_top(float amount) const;
115
116 private:
117 constexpr Rect(float left_side, float right_side, float top_side, float bottom_side)
118 : left(left_side)
120 , top(top_side)
122 {
123 }
124 };
125
126 [[nodiscard]] v2 to_01(const Rect& r, const v2& from);
127 [[nodiscard]] v2 from_01(const Rect& r, const v2& from);
128
131 bool is_within(const v2& p, const Rect& r);
132
133 std::string to_string(const Rect& r);
134}
135
136ADD_DEFAULT_FORMATTER(eu::Rect, std::string, eu::to_string);
#define ASSERTX(x,...)
Assert that a value is true.
Definition assert.h:68
bool is_within(const R< T > &range, T value)
Returns true if a value is withing a range.
Definition range.h:108
T from_01(const R< T > &range, float value)
Converts a value in 0-1 range to a custom range.
Definition range.h:61
Definition assert.h:118
std::string to_string(const Rect &r)
RectCut
Definition rect.h:14
v2 to_01(const Rect &r, const v2 &from)
offset from sides
Definition rect.h:19
constexpr Lrtb(float lr, float tb)
Definition rect.h:22
float left
Definition rect.h:20
float bottom
Definition rect.h:20
constexpr Lrtb(float l, float r, float t, float b)
Definition rect.h:23
constexpr Lrtb(float all)
Definition rect.h:21
float top
Definition rect.h:20
float right
Definition rect.h:20
A (inclusive) range between two values.
Definition range.h:19
static Rect from_point(const v2 &point)
bool is_valid() const
static Rect from_left_right_top_bottom(float left_side, float right_side, float top_side, float bottom_side)
Rect get_top(float amount) const
void set_empty()
bool contains_exclusive(const Rect &r) const
does this contains the argument?
Rect get_right(float amount) const
static Rect from_size(const v2 &size)
v2 get_center_pos() const
Rect add_left(float amount) const
float top
Definition rect.h:30
v2 get_top_left() const
Rect cut_left(float amount, float h_spacing=0.0f)
Rect get_bottom(float amount) const
R< float > get_range_x() const
static Rect from_scaled(const Rect &self, float scale)
scaled copy around rect center
void inset(const Lrtb &lrtb)
v2 get_top_right() const
void extend(const Rect &o)
R< float > get_range_y() const
Rect with_inset(const Lrtb &lrtb) const
static Rect from_center_inside_other(const Rect &self, const Rect &other)
centers the self rectangle inside the other rectangle
v2 get_size() const
void translate(const v2 &v)
Rect with_offset(const v2 &p) const
Rect cut(RectCut side, float amount, float spacing=0.0f)
float right
Definition rect.h:29
static Rect from_top_left_size(const v2 &top_left, const v2 &size)
float left
Definition rect.h:28
v2 get_bottom_left() const
Rect with_translate(const v2 &v) const
v2 get_bottom_right() const
Rect cut_right(float amount, float h_spacing=0.0f)
Rect add_top(float amount) const
Rect add_bottom(float amount) const
bool is_empty() const
static Rect from_bottom_left_size(const v2 &bl, const v2 &size)
Rect with_top_left_at(const v2 &p) const
static constexpr Rect from_left_right_bottom_top(float left_side, float right_side, float bottom_side, float top_side)
Definition rect.h:38
Rect with_bottom_left_at(const v2 &p) const
Rect cut_top(float amount, float v_spacing=0.0f)
v2 get_relative_center_pos() const
Rect cut_bottom(float amount, float v_spacing=0.0f)
float bottom
Definition rect.h:31
Rect add_right(float amount) const
Rect with_scale(float hor, float vert) const
Rect get_left(float amount) const
bool operator==(const Rect &rhs)=delete
a 2d vector
Definition vec2.h:17