Extra2D/include/scene/sprite.h

56 lines
1.5 KiB
C++

#pragma once
#include <graphics/texture.h>
#include <scene/node.h>
namespace extra2d {
// ============================================================================
// 精灵节点
// ============================================================================
class Sprite : public Node {
public:
Sprite();
explicit Sprite(IntrusivePtr<Texture> texture);
~Sprite() override = default;
// 纹理
void setTexture(IntrusivePtr<Texture> texture);
IntrusivePtr<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);
bool flipX() const { return flipX_; }
bool flipY() const { return flipY_; }
// 静态创建方法
static IntrusivePtr<Sprite> create();
static IntrusivePtr<Sprite> create(IntrusivePtr<Texture> texture);
static IntrusivePtr<Sprite> create(IntrusivePtr<Texture> texture, const Rect &rect);
Rect boundingBox() const override;
protected:
void onDraw(Renderer &renderer) override;
void generateRenderCommand(std::vector<RenderCommand> &commands,
int zOrder) override;
private:
IntrusivePtr<Texture> texture_;
Rect textureRect_;
Color color_ = Colors::White;
bool flipX_ = false;
bool flipY_ = false;
};
} // namespace extra2d