Extra2D/Easy2D/include/easy2d/effects/custom_effect_manager.h

319 lines
10 KiB
C
Raw Normal View History

#pragma once
#include <easy2d/core/types.h>
#include <easy2d/core/color.h>
#include <easy2d/effects/particle_system.h>
#include <easy2d/effects/post_process.h>
#include <easy2d/graphics/shader_system.h>
#include <string>
#include <unordered_map>
#include <functional>
#include <vector>
namespace easy2d {
// ============================================================================
// 自定义特效类型
// ============================================================================
enum class CustomEffectType {
Particle, // 粒子特效
PostProcess, // 后处理特效
Shader, // Shader特效
Combined // 组合特效
};
// ============================================================================
// 自定义特效配置
// ============================================================================
struct CustomEffectConfig {
std::string name; // 特效名称
CustomEffectType type; // 特效类型
std::string description; // 描述
// 粒子特效配置
EmitterConfig emitterConfig;
// 后处理特效配置
std::string shaderVertPath; // 顶点着色器路径
std::string shaderFragPath; // 片段着色器路径
std::unordered_map<std::string, float> shaderParams; // Shader参数
// 通用配置
float duration; // 持续时间(-1表示无限)
bool loop; // 是否循环
float delay; // 延迟启动时间
};
// ============================================================================
// 自定义特效基类
// ============================================================================
class CustomEffect {
public:
explicit CustomEffect(const CustomEffectConfig& config);
virtual ~CustomEffect() = default;
// ------------------------------------------------------------------------
// 生命周期
// ------------------------------------------------------------------------
virtual bool init();
virtual void update(float dt);
virtual void render(RenderBackend& renderer);
virtual void shutdown();
// ------------------------------------------------------------------------
// 控制
// ------------------------------------------------------------------------
void play();
void pause();
void stop();
void reset();
bool isPlaying() const { return playing_; }
bool isFinished() const { return finished_; }
float getElapsedTime() const { return elapsedTime_; }
// ------------------------------------------------------------------------
// 配置
// ------------------------------------------------------------------------
const std::string& getName() const { return config_.name; }
const CustomEffectConfig& getConfig() const { return config_; }
void setPosition(const Vec2& pos) { position_ = pos; }
void setRotation(float rot) { rotation_ = rot; }
void setScale(float scale) { scale_ = scale; }
Vec2 getPosition() const { return position_; }
float getRotation() const { return rotation_; }
float getScale() const { return scale_; }
protected:
CustomEffectConfig config_;
Vec2 position_ = Vec2::Zero();
float rotation_ = 0.0f;
float scale_ = 1.0f;
bool playing_ = false;
bool paused_ = false;
bool finished_ = false;
float elapsedTime_ = 0.0f;
float delayTimer_ = 0.0f;
};
// ============================================================================
// 自定义粒子特效
// ============================================================================
class CustomParticleEffect : public CustomEffect {
public:
explicit CustomParticleEffect(const CustomEffectConfig& config);
bool init() override;
void update(float dt) override;
void render(RenderBackend& renderer) override;
void shutdown() override;
void play();
void stop();
Ptr<ParticleEmitter> getEmitter() { return emitter_; }
private:
Ptr<ParticleSystem> particleSystem_;
Ptr<ParticleEmitter> emitter_;
};
// ============================================================================
// 自定义后处理特效
// ============================================================================
class CustomPostProcessEffect : public CustomEffect, public PostProcessEffect {
public:
explicit CustomPostProcessEffect(const CustomEffectConfig& config);
bool init() override;
void update(float dt) override;
void shutdown() override;
void onShaderBind(GLShader& shader) override;
void setParam(const std::string& name, float value);
float getParam(const std::string& name) const;
private:
std::unordered_map<std::string, float> runtimeParams_;
};
// ============================================================================
// 自定义特效工厂
// ============================================================================
class CustomEffectFactory {
public:
using EffectCreator = std::function<Ptr<CustomEffect>(const CustomEffectConfig&)>;
static CustomEffectFactory& getInstance();
// 注册自定义特效创建器
void registerEffect(const std::string& typeName, EffectCreator creator);
// 创建特效
Ptr<CustomEffect> create(const std::string& typeName, const CustomEffectConfig& config);
// 检查是否已注册
bool isRegistered(const std::string& typeName) const;
// 获取所有已注册的类型
std::vector<std::string> getRegisteredTypes() const;
private:
CustomEffectFactory() = default;
~CustomEffectFactory() = default;
CustomEffectFactory(const CustomEffectFactory&) = delete;
CustomEffectFactory& operator=(const CustomEffectFactory&) = delete;
std::unordered_map<std::string, EffectCreator> creators_;
};
// ============================================================================
// 自定义特效管理器
// ============================================================================
class CustomEffectManager {
public:
static CustomEffectManager& getInstance();
// ------------------------------------------------------------------------
// 初始化和关闭
// ------------------------------------------------------------------------
bool init();
void shutdown();
// ------------------------------------------------------------------------
// 特效管理
// ------------------------------------------------------------------------
/**
* @brief JSON和文本格式
* JSON格式优先退
*/
bool loadFromFile(const std::string& filepath);
/**
* @brief
* EMISSION 100
*/
bool loadFromTextFile(const std::string& filepath);
/**
* @brief
* @param useJson true=JSON格式, false=
*/
bool saveToFile(const std::string& name, const std::string& filepath, bool useJson = true);
/**
* @brief JSON文件
*/
bool saveAllToFile(const std::string& filepath);
/**
* @brief
*/
void registerConfig(const std::string& name, const CustomEffectConfig& config);
/**
* @brief
*/
CustomEffectConfig* getConfig(const std::string& name);
/**
* @brief
*/
void removeConfig(const std::string& name);
/**
* @brief
*/
std::vector<std::string> getConfigNames() const;
// ------------------------------------------------------------------------
// 特效实例管理
// ------------------------------------------------------------------------
/**
* @brief
*/
Ptr<CustomEffect> createEffect(const std::string& name);
/**
* @brief
*/
Ptr<CustomEffect> createEffectFromConfig(const CustomEffectConfig& config);
/**
* @brief
*/
void destroyEffect(Ptr<CustomEffect> effect);
/**
* @brief
*/
void update(float dt);
/**
* @brief
*/
void render(RenderBackend& renderer);
/**
* @brief
*/
void stopAll();
// ------------------------------------------------------------------------
// 便捷方法
// ------------------------------------------------------------------------
/**
* @brief
*/
Ptr<CustomEffect> play(const std::string& name, const Vec2& position);
/**
* @brief
*/
void playOneShot(const std::string& name, const Vec2& position);
private:
CustomEffectManager() = default;
~CustomEffectManager() = default;
CustomEffectManager(const CustomEffectManager&) = delete;
CustomEffectManager& operator=(const CustomEffectManager&) = delete;
std::unordered_map<std::string, CustomEffectConfig> configs_;
std::vector<Ptr<CustomEffect>> activeEffects_;
};
// ============================================================================
// 便捷宏
// ============================================================================
#define E2D_CUSTOM_EFFECT_MANAGER() ::easy2d::CustomEffectManager::getInstance()
#define E2D_CUSTOM_EFFECT_FACTORY() ::easy2d::CustomEffectFactory::getInstance()
// ============================================================================
// 预设特效快速创建
// ============================================================================
class EffectBuilder {
public:
// 粒子特效
static CustomEffectConfig Particle(const std::string& name);
static CustomEffectConfig Fire(const std::string& name);
static CustomEffectConfig Smoke(const std::string& name);
static CustomEffectConfig Explosion(const std::string& name);
static CustomEffectConfig Magic(const std::string& name);
static CustomEffectConfig Sparkle(const std::string& name);
// 后处理特效
static CustomEffectConfig Bloom(const std::string& name);
static CustomEffectConfig Blur(const std::string& name);
static CustomEffectConfig Vignette(const std::string& name);
static CustomEffectConfig ColorGrading(const std::string& name);
};
} // namespace easy2d