2026-02-11 19:40:26 +08:00
|
|
|
#include <cmath>
|
2026-02-25 06:23:53 +08:00
|
|
|
#include <ui/widget.h>
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
|
|
|
Widget::Widget() {
|
|
|
|
|
setSpatialIndexed(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Widget::setSize(const Size &size) {
|
|
|
|
|
size_ = size;
|
|
|
|
|
updateSpatialIndex();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Widget::setSize(float width, float height) {
|
|
|
|
|
setSize(Size(width, height));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rect Widget::getBoundingBox() const {
|
|
|
|
|
if (size_.empty()) {
|
|
|
|
|
return Rect();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 00:12:54 +08:00
|
|
|
auto pos = convertToWorldSpace(extra2d::Vec2::Zero());
|
2026-02-11 19:40:26 +08:00
|
|
|
auto anchor = getAnchor();
|
|
|
|
|
auto scale = getScale();
|
|
|
|
|
|
|
|
|
|
float w = size_.width * scale.x;
|
|
|
|
|
float h = size_.height * scale.y;
|
|
|
|
|
float x0 = pos.x - size_.width * anchor.x * scale.x;
|
|
|
|
|
float y0 = pos.y - size_.height * anchor.y * scale.y;
|
|
|
|
|
float x1 = x0 + w;
|
|
|
|
|
float y1 = y0 + h;
|
|
|
|
|
|
|
|
|
|
float l = std::min(x0, x1);
|
|
|
|
|
float t = std::min(y0, y1);
|
|
|
|
|
return Rect(l, t, std::abs(w), std::abs(h));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
2026-02-26 00:12:54 +08:00
|
|
|
// 重写 onDraw
|
2026-02-11 19:40:26 +08:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void Widget::onDraw(RenderBackend &renderer) {
|
2026-02-26 00:12:54 +08:00
|
|
|
onDrawWidget(renderer);
|
2026-02-11 19:40:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace extra2d
|