76 lines
2.3 KiB
C++
76 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include <core/rect.h>
|
|
#include <core/types.h>
|
|
#include <core/vec2.h>
|
|
#include <platform/input.h>
|
|
#include <scene/node.h>
|
|
|
|
namespace extra2d {
|
|
|
|
// ============================================================================
|
|
// 鼠标事件结构
|
|
// ============================================================================
|
|
struct MouseEvent {
|
|
MouseButton button;
|
|
float x;
|
|
float y;
|
|
int mods; // 修饰键状态
|
|
};
|
|
|
|
// ============================================================================
|
|
// Widget 基类 - UI 组件的基础
|
|
// ============================================================================
|
|
class Widget : public Node {
|
|
public:
|
|
Widget();
|
|
~Widget() override = default;
|
|
|
|
void setSize(const Size &size);
|
|
void setSize(float width, float height);
|
|
Size size() const { return size_; }
|
|
|
|
Rect boundingBox() const override;
|
|
|
|
// ------------------------------------------------------------------------
|
|
// 鼠标事件处理(子类可重写)
|
|
// ------------------------------------------------------------------------
|
|
virtual bool onMousePress(const MouseEvent &event) { return false; }
|
|
virtual bool onMouseRelease(const MouseEvent &event) { return false; }
|
|
virtual bool onMouseMove(const MouseEvent &event) { return false; }
|
|
virtual void onMouseEnter() {}
|
|
virtual void onMouseLeave() {}
|
|
|
|
// ------------------------------------------------------------------------
|
|
// 启用/禁用状态
|
|
// ------------------------------------------------------------------------
|
|
void setEnabled(bool enabled) { enabled_ = enabled; }
|
|
bool isEnabled() const { return enabled_; }
|
|
|
|
// ------------------------------------------------------------------------
|
|
// 焦点状态
|
|
// ------------------------------------------------------------------------
|
|
void setFocused(bool focused) { focused_ = focused; }
|
|
bool isFocused() const { return focused_; }
|
|
|
|
protected:
|
|
// 供子类使用的辅助方法
|
|
bool isPointInside(float x, float y) const {
|
|
return boundingBox().containsPoint(Point(x, y));
|
|
}
|
|
|
|
// 子类重写此方法以支持自定义渲染
|
|
virtual void onDrawWidget(Renderer &renderer) {}
|
|
|
|
// 重写 Node 的 onDraw
|
|
void onDraw(Renderer &renderer) override;
|
|
|
|
private:
|
|
Size size_ = Size::Zero();
|
|
bool enabled_ = true;
|
|
bool focused_ = false;
|
|
bool hovered_ = false;
|
|
};
|
|
|
|
} // namespace extra2d
|