Extra2D/include/scene/components/transform_component.h

168 lines
3.4 KiB
C
Raw Normal View History

#pragma once
#include <scene/component.h>
#include <types/math/transform.h>
#include <types/math/vec2.h>
#include <types/math/mat4.h>
namespace extra2d {
/**
* @brief
*
*
*
*/
class TransformComponent : public Component {
public:
static constexpr const char* TYPE_NAME = "Transform";
/**
* @brief
*/
TransformComponent();
/**
* @brief
* @return
*/
const char* getTypeName() const override { return TYPE_NAME; }
// ========================================
// 本地变换
// ========================================
/**
* @brief
* @param pos
*/
void setPosition(const Vec2& pos);
/**
* @brief
* @param rot
*/
void setRotation(float rot);
/**
* @brief
* @param scale
*/
void setScale(const Vec2& scale);
/**
* @brief
* @return
*/
Vec2 getPosition() const { return local_.pos; }
/**
* @brief
* @return
*/
float getRotation() const { return local_.rot; }
/**
* @brief
* @return
*/
Vec2 getScale() const { return local_.scale; }
// ========================================
// 锚点
// ========================================
/**
* @brief
* @param anchor [0,1]
*/
void setAnchor(const Vec2& anchor);
/**
* @brief
* @param x X锚点
* @param y Y锚点
*/
void setAnchorPoint(float x, float y);
/**
* @brief
* @return
*/
Vec2 getAnchor() const { return anchor_; }
// ========================================
// 尺寸
// ========================================
/**
* @brief
* @param size
*/
void setSize(const Vec2& size);
/**
* @brief
* @param width
* @param height
*/
void setSize(float width, float height);
/**
* @brief
* @return
*/
Vec2 getSize() const { return size_; }
/**
* @brief
* @return
*/
Vec2 getAnchorOffset() const;
// ========================================
// 世界变换
// ========================================
/**
* @brief
* @return
*/
Transform getWorldTransform() const;
/**
* @brief
* @return
*/
Mat4 getWorldMatrix() const;
// ========================================
// 脏标记管理
// ========================================
/**
* @brief
*/
void setDirty();
/**
* @brief
* @return
*/
bool isDirty() const { return dirty_; }
private:
/**
* @brief
*/
void updateWorldTransform() const;
Transform local_;
mutable Transform world_;
mutable bool dirty_ = true;
Vec2 anchor_ = Vec2(0.5f, 0.5f);
Vec2 size_ = Vec2(100.0f, 100.0f);
};
} // namespace extra2d