Extra2D/Extra2D/include/extra2d/base/Scheduler.h

163 lines
3.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <extra2d/core/types.h>
#include <functional>
#include <unordered_map>
#include <vector>
#include <string>
#include <mutex>
namespace extra2d {
using SchedulerCallback = std::function<void(f32)>;
/**
* @brief 定时器结构
*/
struct Timer {
SchedulerCallback callback;
f32 interval{0.0f};
f32 delay{0.0f};
u32 repeat{0};
u32 elapsed{0};
bool paused{false};
std::string key;
void* target{nullptr};
f32 accumulator{0.0f};
};
/**
* @brief 调度器
*
* 管理定时任务和回调调度。
* 参考 Cocos Creator 的 Scheduler 设计。
*/
class Scheduler {
public:
Scheduler();
~Scheduler();
/**
* @brief 每帧更新
* @param dt 帧间隔时间
*/
void update(f32 dt);
/**
* @brief 调度定时回调
* @param callback 回调函数
* @param target 目标对象指针
* @param interval 间隔时间(秒)
* @param repeat 重复次数0 = 无限)
* @param delay 延迟时间(秒)
* @param paused 是否暂停
* @param key 标识键
*/
void schedule(const SchedulerCallback& callback, void* target,
f32 interval, u32 repeat, f32 delay,
bool paused, const std::string& key);
/**
* @brief 调度定时回调(简化版)
* @param callback 回调函数
* @param target 目标对象指针
* @param interval 间隔时间(秒)
* @param paused 是否暂停
* @param key 标识键
*/
void schedule(const SchedulerCallback& callback, void* target,
f32 interval, bool paused, const std::string& key);
/**
* @brief 调度单次回调
* @param callback 回调函数
* @param target 目标对象指针
* @param delay 延迟时间(秒)
* @param key 标识键
*/
void scheduleOnce(const SchedulerCallback& callback, void* target,
f32 delay, const std::string& key);
/**
* @brief 取消调度
* @param key 标识键
* @param target 目标对象指针
*/
void unschedule(const std::string& key, void* target);
/**
* @brief 取消目标对象的所有调度
* @param target 目标对象指针
*/
void unscheduleAllForTarget(void* target);
/**
* @brief 取消所有调度
*/
void unscheduleAll();
/**
* @brief 检查是否已调度
* @param key 标识键
* @param target 目标对象指针
* @return 如果已调度返回 true
*/
bool isScheduled(const std::string& key, void* target);
/**
* @brief 暂停目标对象的所有调度
* @param target 目标对象指针
*/
void pauseTarget(void* target);
/**
* @brief 恢复目标对象的所有调度
* @param target 目标对象指针
*/
void resumeTarget(void* target);
/**
* @brief 检查目标对象是否暂停
* @param target 目标对象指针
* @return 如果暂停返回 true
*/
bool isTargetPaused(void* target);
/**
* @brief 在主线程执行函数
* @param func 要执行的函数
*/
void performFunctionInMainThread(const std::function<void()>& func);
/**
* @brief 执行所有待执行的主线程函数
*/
void runFunctionsToBePerformed();
/**
* @brief 获取时间缩放因子
* @return 时间缩放因子
*/
f32 getTimeScale() const;
/**
* @brief 设置时间缩放因子
* @param scale 时间缩放因子
*/
void setTimeScale(f32 scale);
private:
struct TargetEntry {
std::vector<Timer*> timers;
bool paused{false};
};
std::unordered_map<void*, TargetEntry> targets_;
std::vector<std::function<void()>> functionsToPerform_;
std::mutex performMutex_;
f32 timeScale_{1.0f};
bool updateLocked_{false};
};
} // namespace extra2d