Added proper hit test with rotation and pivoting
This commit is contained in:
+37
-17
@@ -15,6 +15,7 @@ struct Element {
|
||||
Type type;
|
||||
Vec2<float> position = {0, 0};
|
||||
float rotation = 0;
|
||||
Vec2<float> pivot = {0, 0};
|
||||
Vec2<float> scale = {1, 1};
|
||||
float alpha = 1.0f;
|
||||
float z = 0;
|
||||
@@ -24,7 +25,7 @@ struct Element {
|
||||
Element(Type type) : type(type) {}
|
||||
virtual ~Element() = default;
|
||||
virtual void render(Window &window, uint64_t abs_time) = 0;
|
||||
virtual Vec2<float> size(Window *window) = 0;
|
||||
virtual Vec2<float> size() = 0;
|
||||
|
||||
void click() {
|
||||
on_click.call();
|
||||
@@ -43,11 +44,11 @@ struct EImage : Element {
|
||||
animation_pool.release(animation_id);
|
||||
}
|
||||
|
||||
Vec2<float> size(Window *window) override {
|
||||
Vec2<float> size() override {
|
||||
Animation &animation = *animation_pool[animation_id];
|
||||
if (animation.frames.empty())
|
||||
return {0, 0};
|
||||
return window->get_image_size(animation.frames[0]);
|
||||
return window.get_image_size(animation.frames[0]);
|
||||
}
|
||||
|
||||
void render(Window &window, uint64_t abs_time) override {
|
||||
@@ -55,7 +56,7 @@ struct EImage : Element {
|
||||
if (animation.frames.empty())
|
||||
return;
|
||||
int frame_index = (int)((abs_time * animation.frame_rate) / 1e9) % animation.frames.size();
|
||||
window.draw(animation.frames[frame_index], position, z, rotation, scale, alpha);
|
||||
window.draw(animation.frames[frame_index], position, z, rotation, pivot, scale, alpha);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -86,10 +87,10 @@ struct EText : Element {
|
||||
last_key = UINT16_MAX;
|
||||
}
|
||||
|
||||
Vec2<float> size(Window *window) override {
|
||||
Vec2<float> size() override {
|
||||
if (text_id == UINT64_MAX)
|
||||
return {0, 0};
|
||||
return window->get_text_size(text_id);
|
||||
return window.get_text_size(text_id);
|
||||
};
|
||||
|
||||
void render(Window &window, uint64_t) override {
|
||||
@@ -106,7 +107,7 @@ struct EText : Element {
|
||||
last_key = key;
|
||||
last_color = color;
|
||||
}
|
||||
window.write(text_id, position, z, rotation, scale, alpha);
|
||||
window.write(text_id, position, z, rotation, pivot, scale, alpha);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -119,14 +120,14 @@ private:
|
||||
struct ERect : Element {
|
||||
Vec2<float> shape = {0, 0};
|
||||
Color color = {255, 255, 255};
|
||||
Vec2<float> size(Window *) override {
|
||||
Vec2<float> size() override {
|
||||
return shape;
|
||||
};
|
||||
|
||||
ERect(Vec2<float> shape, Color color) : Element(Type::RECT), shape(shape), color(color) {}
|
||||
|
||||
void render(Window &window, uint64_t) override {
|
||||
window.draw_rect(Rect::from_size(position, shape), color, z, rotation, scale, alpha);
|
||||
window.draw_rect(Rect::from(position, shape), color, z, rotation, pivot, scale, alpha);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -177,16 +178,19 @@ struct Scene {
|
||||
element_pool[id]->click();
|
||||
}
|
||||
|
||||
#warning "This does not take rotation into account \
|
||||
reference and use reverse-transform on point then AABB check instead"
|
||||
void render(Window &window, uint64_t abs_time) {
|
||||
for (const uint64_t &e : element_ids)
|
||||
element_pool[e]->render(window, abs_time);
|
||||
}
|
||||
|
||||
std::vector<uint64_t> elements_at(Vec2<float> position) {
|
||||
std::vector<uint64_t> result;
|
||||
|
||||
for (auto it = element_ids.rbegin(); it != element_ids.rend(); ++it) {
|
||||
Element *e = element_pool[*it];
|
||||
Vec2<float> size = e->size(&window);
|
||||
Rect rect = Rect::from_size(e->position, size * e->scale);
|
||||
if (rect.contains(position)) {
|
||||
Vec2<float> size = e->size();
|
||||
Rect rect = Rect::from(size);
|
||||
if (rect.contains(world_to_local(position, e))) {
|
||||
result.push_back(*it);
|
||||
if (e->click_through)
|
||||
continue;
|
||||
@@ -198,9 +202,25 @@ reference and use reverse-transform on point then AABB check instead"
|
||||
return result;
|
||||
}
|
||||
|
||||
void render(Window &window, uint64_t abs_time) {
|
||||
for (const uint64_t &e : element_ids)
|
||||
element_pool[e]->render(window, abs_time);
|
||||
Vec2<float> world_to_local(Vec2<float> p, Element *e) {
|
||||
Vec2<float> size = e->size();
|
||||
Vec2<float> pivot = e->pivot * size * e->scale;
|
||||
p = p - e->position;
|
||||
|
||||
float rad = -e->rotation * (M_PI / 180.0f);
|
||||
float c = cos(rad);
|
||||
float s = sin(rad);
|
||||
|
||||
p = p - pivot;
|
||||
p = {
|
||||
p.x * c - p.y * s,
|
||||
p.x * s + p.y * c
|
||||
};
|
||||
|
||||
p = p + pivot;
|
||||
p = p / e->scale;
|
||||
|
||||
return p;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user