libs/base/src/base/rect.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "assert/assert.h" | ||
| 4 | |||
| 5 | #include "base/vec2.h" | ||
| 6 | #include "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 | |||
| 12 | namespace eu | ||
| 13 | { | ||
| 14 | enum class RectCut { left, right, top, bottom }; | ||
| 15 | |||
| 16 | /// offset from sides | ||
| 17 | /// \note unlike css this follows the (left right) (top bottom) order | ||
| 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 | |||
| 33 | Rect(); | ||
| 34 | |||
| 35 | bool operator==(const Rect& rhs) = delete; | ||
| 36 | |||
| 37 | |||
| 38 | [[nodiscard]] static Rect from_left_right_bottom_top(float left_side, float right_side, float bottom_side, float top_side); | ||
| 39 | [[nodiscard]] static Rect from_left_right_top_bottom(float left_side, float right_side, float top_side, float bottom_side); | ||
| 40 | [[nodiscard]] static Rect from_bottom_left_size(const v2& bl, const v2& size); | ||
| 41 | [[nodiscard]] static Rect from_top_left_size(const v2& top_left, const v2& size); | ||
| 42 | [[nodiscard]] static Rect from_size(const v2& size); | ||
| 43 | [[nodiscard]] static Rect from_point(const v2& point); | ||
| 44 | |||
| 45 | // extend/include/union current rect with another point/rect | ||
| 46 | void extend(const Rect& o); | ||
| 47 | |||
| 48 | void translate(const v2& v); | ||
| 49 | void inset(const Lrtb& lrtb); // moves each side towards the center | ||
| 50 | |||
| 51 | void set_empty(); | ||
| 52 | |||
| 53 | /// centers the self rectangle inside the other rectangle | ||
| 54 | [[nodiscard]] static Rect from_center_inside_other(const Rect& self, const Rect& other); | ||
| 55 | |||
| 56 | /// scaled copy around rect center | ||
| 57 | [[nodiscard]] static Rect from_scaled(const Rect& self, float scale); | ||
| 58 | |||
| 59 | /// does this contains the argument? | ||
| 60 | [[nodiscard]] bool contains_exclusive(const Rect& r) const; | ||
| 61 | |||
| 62 | [[nodiscard]] Rect with_inset(const Lrtb& lrtb) const; | ||
| 63 | [[nodiscard]] Rect with_translate(const v2& v) const; | ||
| 64 | [[nodiscard]] Rect with_top_left_at(const v2& p) const; | ||
| 65 | [[nodiscard]] Rect with_bottom_left_at(const v2& p) const; | ||
| 66 | [[nodiscard]] Rect with_offset(const v2& p) const; | ||
| 67 | [[nodiscard]] Rect with_scale(float hor, float vert) const; | ||
| 68 | |||
| 69 | [[nodiscard]] v2 get_bottom_left() const; | ||
| 70 | [[nodiscard]] v2 get_center_pos() const; | ||
| 71 | |||
| 72 | // position is from bottom left | ||
| 73 | [[nodiscard]] v2 get_relative_center_pos() const; | ||
| 74 | |||
| 75 | // Returns true if the rectangle is empty (left >= right or top <= bottom) | ||
| 76 | [[nodiscard]] bool is_empty() const; | ||
| 77 | |||
| 78 | // a 0 width/height is also considered valid | ||
| 79 | [[nodiscard]] bool is_valid() const; | ||
| 80 | |||
| 81 | [[nodiscard]] v2 get_size() const; | ||
| 82 | |||
| 83 | [[nodiscard]] R<float> get_range_y() const; | ||
| 84 | [[nodiscard]] R<float> get_range_x() const; | ||
| 85 | [[nodiscard]] v2 get_top_left() const; | ||
| 86 | [[nodiscard]] v2 get_top_right() const; | ||
| 87 | [[nodiscard]] v2 get_bottom_right() const; | ||
| 88 | |||
| 89 | Rect cut_left(float amount, float h_spacing = 0.0f); | ||
| 90 | Rect cut_right(float amount, float h_spacing = 0.0f); | ||
| 91 | Rect cut_bottom(float amount, float v_spacing = 0.0f); | ||
| 92 | Rect cut_top(float amount, float v_spacing = 0.0f); | ||
| 93 | |||
| 94 | Rect cut(RectCut side, float amount, float spacing = 0.0f); | ||
| 95 | |||
| 96 | // Same as cut, except they keep the input rect intact. | ||
| 97 | // Useful for decorations | ||
| 98 | Rect get_left(float amount) const; | ||
| 99 | Rect get_right(float amount) const; | ||
| 100 | Rect get_bottom(float amount) const; | ||
| 101 | Rect get_top(float amount) const; | ||
| 102 | |||
| 103 | // These will add a rectangle outside of the input rectangle. | ||
| 104 | // Useful for tooltips and other overlay elements. | ||
| 105 | Rect add_left(float amount) const; | ||
| 106 | Rect add_right(float amount) const; | ||
| 107 | Rect add_bottom(float amount) const; | ||
| 108 | Rect add_top(float amount) const; | ||
| 109 | |||
| 110 | private: | ||
| 111 | Rect(float left_side, float right_side, float top_side, float bottom_side); | ||
| 112 | }; | ||
| 113 | |||
| 114 | [[nodiscard]] v2 to_01(const Rect& r, const v2& from); | ||
| 115 | [[nodiscard]] v2 from_01(const Rect& r, const v2& from); | ||
| 116 | |||
| 117 | /// is the point contained within the rect? | ||
| 118 | /// \note on the border is considered within | ||
| 119 | bool is_within(const v2& p, const Rect& r); | ||
| 120 | |||
| 121 | std::string to_string(const Rect& r); | ||
| 122 | } | ||
| 123 | |||
| 124 | ADD_DEFAULT_FORMATTER(eu::Rect, std::string, eu::to_string); | ||
| 125 |