geometry.hh (3499B)
1 #ifndef __WINSYS_GEOMETRY_H_GUARD__ 2 #define __WINSYS_GEOMETRY_H_GUARD__ 3 4 #include "common.hh" 5 #include "window.hh" 6 7 #include <ostream> 8 9 namespace winsys 10 { 11 12 enum class Edge 13 { 14 Left, 15 Right, 16 Top, 17 Bottom 18 }; 19 20 enum class Corner 21 { 22 TopLeft, 23 TopRight, 24 BottomLeft, 25 BottomRight 26 }; 27 28 enum class Direction 29 { 30 Forward, 31 Backward 32 }; 33 34 struct Dim final 35 { 36 int w; 37 int h; 38 }; 39 40 inline bool 41 operator==(Dim const& lhs, Dim const& rhs) 42 { 43 return lhs.w == rhs.w && lhs.h == rhs.h; 44 } 45 46 inline std::ostream& 47 operator<<(std::ostream& os, Dim const& dim) { 48 return os << "(" << dim.w << "×" << dim.h << ")"; 49 } 50 51 struct Pos final 52 { 53 int x; 54 int y; 55 56 static Pos 57 from_center_of_dim(Dim dim) 58 { 59 return Pos { 60 static_cast<int>(dim.w / 2.f), 61 static_cast<int>(dim.h / 2.f) 62 }; 63 } 64 65 static bool 66 is_at_origin(Pos& pos) 67 { 68 return pos.x == 0 && pos.y == 0; 69 } 70 }; 71 72 inline bool 73 operator==(Pos const& lhs, Pos const& rhs) 74 { 75 return lhs.x == rhs.x && lhs.y == rhs.y; 76 } 77 78 inline std::ostream& 79 operator<<(std::ostream& os, Pos const& pos) { 80 return os << "(" << pos.x << ", " << pos.y << ")"; 81 } 82 83 inline Pos 84 operator+(Pos const& pos1, Pos const& pos2) 85 { 86 return Pos{ 87 pos1.x + pos2.x, 88 pos1.y + pos2.y 89 }; 90 } 91 92 inline Pos 93 operator-(Pos const& pos1, Pos const& pos2) 94 { 95 return Pos{ 96 pos1.x - pos2.x, 97 pos1.y - pos2.y 98 }; 99 } 100 101 inline Pos 102 operator+(Pos const& pos, Dim const& dim) 103 { 104 return Pos{ 105 pos.x + dim.w, 106 pos.y + dim.h 107 }; 108 } 109 110 inline Pos 111 operator-(Pos const& pos, Dim const& dim) 112 { 113 return Pos{ 114 pos.x - dim.w, 115 pos.y - dim.h 116 }; 117 } 118 119 struct Padding final 120 { 121 int left; 122 int right; 123 int top; 124 int bottom; 125 }; 126 127 typedef Padding Extents; 128 129 inline std::ostream& 130 operator<<(std::ostream& os, Padding const& padding) { 131 return os << "[" << padding.left 132 << "; " << padding.top 133 << "; " << padding.right 134 << "; " << padding.bottom << "]"; 135 } 136 137 struct Region final 138 { 139 Pos pos; 140 Dim dim; 141 142 void apply_minimum_dim(Dim const&); 143 void apply_extents(Extents const&); 144 void remove_extents(Extents const&); 145 146 bool contains(Pos) const; 147 bool contains(Region const&) const; 148 149 Pos center() const; 150 }; 151 152 inline bool 153 operator==(Region const& lhs, Region const& rhs) 154 { 155 return lhs.pos == rhs.pos && lhs.dim == rhs.dim; 156 } 157 158 inline std::ostream& 159 operator<<(std::ostream& os, Region const& region) { 160 return os << "[" << region.pos << " " << region.dim << "]"; 161 } 162 163 struct Distance final 164 { 165 int dx; 166 int dy; 167 }; 168 169 inline std::ostream& 170 operator<<(std::ostream& os, Distance const& dist) { 171 return os << "𝛿(" << dist.dx << ", " << dist.dy << ")"; 172 } 173 174 struct Ratio final 175 { 176 int numerator; 177 int denominator; 178 }; 179 180 struct Strut final 181 { 182 Window window; 183 int width; 184 }; 185 186 } 187 188 #endif//__WINSYS_GEOMETRY_H_GUARD__