Extra2D/src/ui/widget.cpp

44 lines
1.1 KiB
C++

#include <cmath>
#include <ui/widget.h>
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::boundingBox() const {
if (size_.empty()) {
return Rect();
}
auto position = convertToWorldSpace(extra2d::Vec2::Zero());
auto anchorPt = anchor();
auto scaleVal = scale();
float w = size_.width * scaleVal.x;
float h = size_.height * scaleVal.y;
float x0 = position.x - size_.width * anchorPt.x * scaleVal.x;
float y0 = position.y - size_.height * anchorPt.y * scaleVal.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));
}
// ------------------------------------------------------------------------
// 重写 onDraw
// ------------------------------------------------------------------------
void Widget::onDraw(Renderer &renderer) { onDrawWidget(renderer); }
} // namespace extra2d