-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRect.hpp
More file actions
264 lines (244 loc) · 10.9 KB
/
Rect.hpp
File metadata and controls
264 lines (244 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*##########################################################################################
PAX SAPIENTICA Library 💀🌿🌏
[Planning] 2023 As Project
[Production] 2023 As Project
[Contact Us] wanotaitei@gmail.com https://github.com/AsPJT/PAX_SAPIENTICA
[License] Distributed under the CC0 1.0. https://creativecommons.org/publicdomain/zero/1.0/
##########################################################################################*/
#ifndef PAX_GRAPHICA_RECT_HPP
#define PAX_GRAPHICA_RECT_HPP
/*##########################################################################################
##########################################################################################*/
#if defined(PAXS_USING_SIV3D)
#include <Siv3D.hpp>
#elif defined(PAXS_USING_SFML)
#include <SFML/Graphics.hpp>
#endif
#include <PAX_GRAPHICA/IDrawable.hpp>
#include <PAX_GRAPHICA/Vec2.hpp>
#include <PAX_GRAPHICA/Window.hpp>
namespace paxg {
struct Line {
constexpr Line() = default;
#if defined(PAXS_USING_SIV3D)
s3d::Line line{};
constexpr Line(const float sx, const float sy, const float ex, const float ey)
: line(sx, sy, ex, ey) {}
constexpr Line(const float sx, const float sy, const Vec2i& e)
: line(sx, sy, e.x(), e.y()) {}
constexpr Line(const Vec2i& s, const Vec2i& e)
: line(s.x(), s.y(), e.x(), e.y()) {}
void draw(const double thickness, const paxg::Color& color) const {
line.draw(thickness, color.color);
}
#elif defined(PAXS_USING_DXLIB)
float x0{}, y0{}, w0{}, h0{};
constexpr Line(const float x, const float y, const float w, const float h) :
x0(x), y0(y), w0(w), h0(h) {}
constexpr Line(const Vec2i& pos, const Vec2i& size)
: x0(static_cast<float>(pos.x())), y0(static_cast<float>(pos.y())),
w0(static_cast<float>(size.x())), h0(static_cast<float>(size.y())) {}
constexpr Line(const Vec2i& pos, const float w, const float h) :
x0(static_cast<float>(pos.x())), y0(static_cast<float>(pos.y())), w0(w), h0(h) {}
constexpr Line(const float x, const float y, const Vec2i& size)
: x0(x), y0(y), w0(static_cast<float>(size.x())), h0(static_cast<float>(size.y())) {}
void draw(const double thickness, const paxg::Color& color) const {
// 直線の場合
if (y0 == h0 || x0 == w0) {
const int x1 = static_cast<int>(x0 - thickness / 2);
const int y1 = static_cast<int>(y0 - thickness / 2);
const int w1 = static_cast<int>(w0 + thickness / 2);
const int h1 = static_cast<int>(h0 + thickness / 2);
DxLib::DrawBox(
x1,
y1,
(w1 == x1) ? w1 + 1 : w1,
(h1 == y1) ? h1 + 1 : h1,
DxLib::GetColor(color.r, color.g, color.b), TRUE);
}
}
#else
float sx0{}, sy0{}, ex0{}, ey0{};
constexpr Line(const float sx, const float sy, const float ex, const float ey)
: sx0(sx), sy0(sy), ex0(ex), ey0(ey) {}
constexpr Line(const float sx, const float sy, const Vec2i& e)
: sx0(sx), sy0(sy), ex0(e.x()), ey0(e.y()) {}
constexpr Line(const Vec2i& s, const Vec2i& e)
: sx0(s.x()), sy0(s.y()), ex0(e.x()), ey0(e.y()) {}
void draw(const double thickness, const paxg::Color& color) const {
}
#endif
};
struct Rect : public paxg::IDrawable {
#if defined(PAXS_USING_SIV3D)
s3d::RectF rect{};
constexpr Rect() = default;
constexpr Rect(const float x, const float y, const float w, const float h) : rect(x, y, w, h) {}
constexpr Rect(const Vec2i& pos, const Vec2i& size) : rect(pos.x(), pos.y(), size.x(), size.y()) {}
constexpr Rect(const Vec2i& pos, const float w, const float h) : rect(pos.x(), pos.y(), w, h) {}
constexpr Rect(const float x, const float y, const Vec2i& size) : rect(x, y, size.x(), size.y()) {}
constexpr operator s3d::RectF() const { return rect; }
void setX(const float x_) { rect.x = x_; }
void setY(const float y_) { rect.y = y_; }
void setW(const float w_) { rect.w = w_; }
void setH(const float h_) { rect.h = h_; }
float x() const { return static_cast<float>(rect.x); }
float y() const { return static_cast<float>(rect.y); }
float w() const { return static_cast<float>(rect.w); }
float h() const { return static_cast<float>(rect.h); }
Vec2i pos() const { return Vec2i(static_cast<int>(rect.x), static_cast<int>(rect.y)); }
Vec2i size() const { return Vec2i(static_cast<int>(rect.w), static_cast<int>(rect.h)); }
void setPos(const float x_, const float y_) {
rect.x = x_;
rect.y = y_;
}
void setSize(const float w_, const float h_) {
rect.w = w_;
rect.h = h_;
}
void setPos(const Vec2i pos_) {
rect.x = pos_.x();
rect.y = pos_.y();
}
void setSize(const Vec2i size_) {
rect.w = size_.x();
rect.h = size_.y();
}
#elif defined(PAXS_USING_SFML)
sf::RectangleShape rect{};
constexpr Rect() = default;
Rect(const float x, const float y, const float w, const float h) : rect(sf::Vector2f(w, h)) { rect.setPosition(x, y); }
Rect(const sf::Vector2i& pos, const sf::Vector2i& size) : rect(sf::Vector2f(size.x, size.y)) { rect.setPosition(pos.x, pos.y); }
Rect(const sf::Vector2i& pos, const float w, const float h) : rect(sf::Vector2f(w, h)) { rect.setPosition(pos.x, pos.y); }
Rect(const float x, const float y, const sf::Vector2i& size) : rect(sf::Vector2f(size.x, size.y)) { rect.setPosition(x, y); }
operator sf::RectangleShape() const { return rect; }
void setX(const float x_) { rect.x = x_; }
void setY(const float y_) { rect.y = y_; }
void setW(const float w_) { rect.w = w_; }
void setH(const float h_) { rect.h = h_; }
float x() const { return rect.x; }
float y() const { return rect.y; }
float w() const { return rect.w; }
float h() const { return rect.h; }
Vec2i pos() const { return Vec2i(rect.x, rect.y); }
Vec2i size() const { return Vec2i(rect.w, rect.h); }
void setPos(const float x_, const float y_) {
rect.x = x_;
rect.y = y_;
}
void setSize(const float w_, const float h_) {
rect.w = w_;
rect.h = h_;
}
void setPos(const Vec2i pos_) {
rect.x = pos_.x();
rect.y = pos_.y();
}
void setSize(const Vec2i size_) {
rect.w = size_.x();
rect.h = size_.y();
}
#else
float x0{}, y0{}, w0{}, h0{};
constexpr Rect() = default;
constexpr Rect(const float x, const float y, const float w, const float h) :
x0(x), y0(y), w0(w), h0(h) {}
constexpr Rect(const Vec2i& pos, const Vec2i& size)
: x0(static_cast<float>(pos.x())), y0(static_cast<float>(pos.y())),
w0(static_cast<float>(size.x())), h0(static_cast<float>(size.y())) {}
constexpr Rect(const Vec2i& pos, const float w, const float h) :
x0(static_cast<float>(pos.x())), y0(static_cast<float>(pos.y())), w0(w), h0(h) {}
constexpr Rect(const float x, const float y, const Vec2i& size)
: x0(x), y0(y), w0(static_cast<float>(size.x())), h0(static_cast<float>(size.y())) {}
void setX(const float x_) { x0 = x_; }
void setY(const float y_) { y0 = y_; }
void setW(const float w_) { w0 = w_; }
void setH(const float h_) { h0 = h_; }
float x() const { return x0; }
float y() const { return y0; }
float w() const { return w0; }
float h() const { return h0; }
Vec2i pos() const { return Vec2i(x0, y0); }
Vec2i size() const { return Vec2i(w0, h0); }
void setPos(const float x_, const float y_) {
x0 = x_;
y0 = y_;
}
void setSize(const float w_, const float h_) {
w0 = w_;
h0 = h_;
}
void setPos(const Vec2i pos_) {
x0 = static_cast<float>(pos_.x());
y0 = static_cast<float>(pos_.y());
}
void setSize(const Vec2i size_) {
w0 = static_cast<float>(size_.x());
h0 = static_cast<float>(size_.y());
}
#endif
void draw() const override {
#if defined(PAXS_USING_SIV3D)
rect.draw();
#elif defined(PAXS_USING_SFML)
Window::window.draw(rect);
#elif defined(PAXS_USING_DXLIB)
DxLib::DrawBox(x0, y0, x0 + w0, y0 + h0, DxLib::GetColor(255, 255, 255), TRUE);
#endif
}
void draw(const paxg::Color& c_) const {
#if defined(PAXS_USING_SIV3D)
rect.draw(c_.color);
#elif defined(PAXS_USING_SFML)
Window::window.draw(c_);
#elif defined(PAXS_USING_DXLIB)
DxLib::DrawBox(x0, y0, x0 + w0, y0 + h0, DxLib::GetColor(c_.r, c_.g, c_.b), TRUE);
#endif
}
void drawFrame(const double inner_thickness, const double outer_thickness, const paxg::Color& color_) const {
#if defined(PAXS_USING_SIV3D)
rect.drawFrame(inner_thickness, outer_thickness, color_.color);
#elif defined(PAXS_USING_SFML)
Window::window.draw(color_);
#elif defined(PAXS_USING_DXLIB)
DxLib::DrawBox(
x0 - outer_thickness, y0 - outer_thickness,
x0 + w0 + outer_thickness, y0 + inner_thickness,
DxLib::GetColor(color_.r, color_.g, color_.b), TRUE);
DxLib::DrawBox(
x0 - outer_thickness, y0 + h0 - inner_thickness,
x0 + w0 + outer_thickness, y0 + h0 + outer_thickness,
DxLib::GetColor(color_.r, color_.g, color_.b), TRUE);
DxLib::DrawBox(
x0 - outer_thickness, y0 - outer_thickness,
x0 + inner_thickness, y0 + h0 + outer_thickness,
DxLib::GetColor(color_.r, color_.g, color_.b), TRUE);
DxLib::DrawBox(
x0 + w0 - inner_thickness, y0 - outer_thickness,
x0 + w0 + outer_thickness, y0 + h0 + outer_thickness,
DxLib::GetColor(color_.r, color_.g, color_.b), TRUE);
#endif
}
bool leftClicked() const {
#if defined(PAXS_USING_SIV3D)
return rect.leftClicked();
#else
return false;
#endif
}
bool mouseOver() const {
#if defined(PAXS_USING_SIV3D)
return rect.mouseOver();
#elif defined(PAXS_USING_DXLIB)
int mx = 0, my = 0;
DxLib::GetMousePoint(&mx, &my);
return (mx >= x0 && my >= y0 && mx < x0 + w0 && my < y0 + h0);
#else
return false;
#endif
}
void drawAt(const Vec2f&) const override {}
void drawAt(const Vec2i&) const override {}
};
}
#endif // !PAX_GRAPHICA_RECT_HPP