2026-02-11 19:40:26 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
2026-02-25 06:23:53 +08:00
|
|
|
#include <graphics/texture.h>
|
|
|
|
|
#include <scene/node.h>
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// 精灵节点
|
|
|
|
|
// ============================================================================
|
|
|
|
|
class Sprite : public Node {
|
|
|
|
|
public:
|
|
|
|
|
Sprite();
|
|
|
|
|
explicit Sprite(Ptr<Texture> texture);
|
|
|
|
|
~Sprite() override = default;
|
|
|
|
|
|
|
|
|
|
// 纹理
|
|
|
|
|
void setTexture(Ptr<Texture> texture);
|
|
|
|
|
Ptr<Texture> getTexture() const { return texture_; }
|
|
|
|
|
|
|
|
|
|
// 纹理矩形 (用于图集)
|
|
|
|
|
void setTextureRect(const Rect &rect);
|
|
|
|
|
Rect getTextureRect() const { return textureRect_; }
|
|
|
|
|
|
|
|
|
|
// 颜色混合
|
|
|
|
|
void setColor(const Color &color);
|
|
|
|
|
Color getColor() const { return color_; }
|
|
|
|
|
|
|
|
|
|
// 翻转
|
|
|
|
|
void setFlipX(bool flip);
|
|
|
|
|
void setFlipY(bool flip);
|
2026-02-26 00:38:31 +08:00
|
|
|
bool flipX() const { return flipX_; }
|
|
|
|
|
bool flipY() const { return flipY_; }
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
// 静态创建方法
|
|
|
|
|
static Ptr<Sprite> create();
|
|
|
|
|
static Ptr<Sprite> create(Ptr<Texture> texture);
|
|
|
|
|
static Ptr<Sprite> create(Ptr<Texture> texture, const Rect &rect);
|
|
|
|
|
|
2026-02-26 00:55:13 +08:00
|
|
|
Rect boundingBox() const override;
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
protected:
|
2026-02-26 00:59:16 +08:00
|
|
|
void onDraw(Renderer &renderer) override;
|
2026-02-11 19:40:26 +08:00
|
|
|
void generateRenderCommand(std::vector<RenderCommand> &commands,
|
|
|
|
|
int zOrder) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Ptr<Texture> texture_;
|
|
|
|
|
Rect textureRect_;
|
|
|
|
|
Color color_ = Colors::White;
|
|
|
|
|
bool flipX_ = false;
|
|
|
|
|
bool flipY_ = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace extra2d
|