From e52c11783098b63bdebe4b654a9c6f02409aa4a0 Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Thu, 26 Feb 2026 19:57:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(renderer):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E5=99=A8=E6=A8=A1=E5=9D=97=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将渲染器相关代码从graphics目录移动到renderer目录,包括Camera、Renderer等核心类 删除过渡动画相关代码,简化场景切换逻辑 移除Tween动画系统,减少代码复杂度 重命名ShapeNode为Shape,保持命名一致性 更新所有引用路径以反映新的目录结构 --- include/animation/sprite_frame.h | 73 --- include/animation/sprite_frame_cache.h | 201 -------- include/animation/tween.h | 168 ------- include/animation/tween_easing.h | 91 ---- include/app/application.h | 4 +- include/extra2d.h | 26 +- include/graphics/opengl/gl_renderer.h | 2 +- include/{graphics => renderer}/camera.h | 0 .../{graphics => renderer}/render_command.h | 0 .../{graphics => renderer}/render_target.h | 0 include/{graphics => renderer}/renderer.h | 0 .../{graphics => renderer}/shader_preset.h | 0 .../{graphics => renderer}/shader_system.h | 0 include/scene/node.h | 86 +--- include/scene/scene.h | 7 +- include/scene/scene_manager.h | 36 -- include/scene/{shape_node.h => shape.h} | 0 include/scene/transition_box_scene.h | 34 -- include/scene/transition_fade_scene.h | 55 --- include/scene/transition_flip_scene.h | 37 -- include/scene/transition_scale_scene.h | 29 -- include/scene/transition_scene.h | 130 ----- include/scene/transition_slide_scene.h | 35 -- src/animation/sprite_frame.cpp | 8 - src/animation/sprite_frame_cache.cpp | 8 - src/animation/tween.cpp | 451 ------------------ src/animation/tween_easing.cpp | 236 --------- src/app/application.cpp | 11 +- src/{graphics => renderer}/camera.cpp | 2 +- src/{graphics => renderer}/render_backend.cpp | 2 +- src/{graphics => renderer}/render_command.cpp | 2 +- src/{graphics => renderer}/render_target.cpp | 2 +- src/{graphics => renderer}/shader_preset.cpp | 2 +- src/{graphics => renderer}/shader_system.cpp | 2 +- src/scene/node.cpp | 142 +----- src/scene/scene.cpp | 4 +- src/scene/scene_manager.cpp | 367 +------------- src/scene/shape_node.cpp | 19 +- src/scene/sprite.cpp | 4 +- src/scene/transition_box_scene.cpp | 71 --- src/scene/transition_fade_scene.cpp | 89 ---- src/scene/transition_flip_scene.cpp | 91 ---- src/scene/transition_scale_scene.cpp | 72 --- src/scene/transition_scene.cpp | 81 ---- src/scene/transition_slide_scene.cpp | 123 ----- src/ui/button.cpp | 2 +- src/ui/check_box.cpp | 2 +- src/ui/label.cpp | 2 +- src/ui/progress_bar.cpp | 2 +- src/ui/radio_button.cpp | 2 +- src/ui/slider.cpp | 2 +- src/ui/text.cpp | 2 +- 52 files changed, 49 insertions(+), 2768 deletions(-) delete mode 100644 include/animation/sprite_frame.h delete mode 100644 include/animation/sprite_frame_cache.h delete mode 100644 include/animation/tween.h delete mode 100644 include/animation/tween_easing.h rename include/{graphics => renderer}/camera.h (100%) rename include/{graphics => renderer}/render_command.h (100%) rename include/{graphics => renderer}/render_target.h (100%) rename include/{graphics => renderer}/renderer.h (100%) rename include/{graphics => renderer}/shader_preset.h (100%) rename include/{graphics => renderer}/shader_system.h (100%) rename include/scene/{shape_node.h => shape.h} (100%) delete mode 100644 include/scene/transition_box_scene.h delete mode 100644 include/scene/transition_fade_scene.h delete mode 100644 include/scene/transition_flip_scene.h delete mode 100644 include/scene/transition_scale_scene.h delete mode 100644 include/scene/transition_scene.h delete mode 100644 include/scene/transition_slide_scene.h delete mode 100644 src/animation/sprite_frame.cpp delete mode 100644 src/animation/sprite_frame_cache.cpp delete mode 100644 src/animation/tween.cpp delete mode 100644 src/animation/tween_easing.cpp rename src/{graphics => renderer}/camera.cpp (99%) rename src/{graphics => renderer}/render_backend.cpp (89%) rename src/{graphics => renderer}/render_command.cpp (99%) rename src/{graphics => renderer}/render_target.cpp (99%) rename src/{graphics => renderer}/shader_preset.cpp (99%) rename src/{graphics => renderer}/shader_system.cpp (99%) delete mode 100644 src/scene/transition_box_scene.cpp delete mode 100644 src/scene/transition_fade_scene.cpp delete mode 100644 src/scene/transition_flip_scene.cpp delete mode 100644 src/scene/transition_scale_scene.cpp delete mode 100644 src/scene/transition_scene.cpp delete mode 100644 src/scene/transition_slide_scene.cpp diff --git a/include/animation/sprite_frame.h b/include/animation/sprite_frame.h deleted file mode 100644 index c122156..0000000 --- a/include/animation/sprite_frame.h +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace extra2d { - -// ============================================================================ -// SpriteFrame - 精灵帧(纹理 + 区域 + 偏移的中间抽象) -// 借鉴 Cocos2d-x SpriteFrame:解耦纹理物理存储与逻辑帧 -// 一个纹理图集可包含多个 SpriteFrame,减少纹理切换提升渲染性能 -// ============================================================================ -class SpriteFrame { -public: - SpriteFrame() = default; - - SpriteFrame(Ptr texture, const Rect &rect) - : texture_(std::move(texture)), rect_(rect), originalSize_(rect.size) {} - - SpriteFrame(Ptr texture, const Rect &rect, const Vec2 &offset, - const Size &originalSize) - : texture_(std::move(texture)), rect_(rect), offset_(offset), - originalSize_(originalSize) {} - - // ------ 静态创建 ------ - static Ptr create(Ptr texture, const Rect &rect) { - return makePtr(std::move(texture), rect); - } - - static Ptr create(Ptr texture, const Rect &rect, - const Vec2 &offset, const Size &originalSize) { - return makePtr(std::move(texture), rect, offset, originalSize); - } - - // ------ 纹理信息 ------ - void setTexture(Ptr texture) { texture_ = std::move(texture); } - Ptr getTexture() const { return texture_; } - - // ------ 矩形区域(在纹理图集中的位置)------ - void setRect(const Rect &rect) { rect_ = rect; } - const Rect &getRect() const { return rect_; } - - // ------ 偏移(图集打包时的裁剪偏移)------ - void setOffset(const Vec2 &offset) { offset_ = offset; } - const Vec2 &getOffset() const { return offset_; } - - // ------ 原始尺寸(裁剪前的完整尺寸)------ - void setOriginalSize(const Size &size) { originalSize_ = size; } - const Size &getOriginalSize() const { return originalSize_; } - - // ------ 旋转标志(图集工具可能旋转90度)------ - void setRotated(bool rotated) { rotated_ = rotated; } - bool isRotated() const { return rotated_; } - - // ------ 名称(用于缓存索引)------ - void setName(const std::string &name) { name_ = name; } - const std::string &name() const { return name_; } - - // ------ 有效性检查 ------ - bool isValid() const { return texture_ != nullptr; } - -private: - Ptr texture_; - Rect rect_; - Vec2 offset_; - Size originalSize_; - bool rotated_ = false; - std::string name_; -}; - -} // namespace extra2d diff --git a/include/animation/sprite_frame_cache.h b/include/animation/sprite_frame_cache.h deleted file mode 100644 index db66ec4..0000000 --- a/include/animation/sprite_frame_cache.h +++ /dev/null @@ -1,201 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -namespace extra2d { - -// ============================================================================ -// SpriteFrameCache - 精灵帧全局缓存(借鉴 Cocos SpriteFrameCache) -// 全局单例管理所有精灵帧,避免重复创建,支持图集自动切割 -// ============================================================================ -class SpriteFrameCache { -public: - static SpriteFrameCache &getInstance() { - static SpriteFrameCache instance; - return instance; - } - - // ------ 添加帧 ------ - - /// 添加单个精灵帧 - void addSpriteFrame(Ptr frame, const std::string &name) { - std::lock_guard lock(mutex_); - frames_[name] = std::move(frame); - } - - /// 从纹理和矩形区域创建并添加帧 - void addSpriteFrameFromTexture(Ptr texture, const Rect &rect, - const std::string &name) { - auto frame = SpriteFrame::create(std::move(texture), rect); - frame->setName(name); - addSpriteFrame(std::move(frame), name); - } - - /// 从纹理图集批量切割添加(等宽等高网格) - void addSpriteFramesFromGrid(const std::string &texturePath, int frameWidth, - int frameHeight, int frameCount = -1, - int spacing = 0, int margin = 0) { - auto texture = loadTextureFromFile(texturePath); - if (!texture) - return; - addSpriteFramesFromGrid(texture, texturePath, frameWidth, frameHeight, - frameCount, spacing, margin); - } - - /// 从纹理对象批量切割添加(等宽等高网格,无需走 TexturePool) - void addSpriteFramesFromGrid(Ptr texture, - const std::string &keyPrefix, int frameWidth, - int frameHeight, int frameCount = -1, - int spacing = 0, int margin = 0) { - if (!texture) - return; - - int texW = texture->width(); - int texH = texture->height(); - int usableW = texW - 2 * margin; - int usableH = texH - 2 * margin; - int cols = (usableW + spacing) / (frameWidth + spacing); - int rows = (usableH + spacing) / (frameHeight + spacing); - int total = (frameCount > 0) ? frameCount : cols * rows; - - std::lock_guard lock(mutex_); - for (int i = 0; i < total; ++i) { - int col = i % cols; - int row = i / cols; - if (row >= rows) - break; - - Rect rect(static_cast(margin + col * (frameWidth + spacing)), - static_cast(margin + row * (frameHeight + spacing)), - static_cast(frameWidth), - static_cast(frameHeight)); - - std::string name = keyPrefix + "#" + std::to_string(i); - auto frame = SpriteFrame::create(texture, rect); - frame->setName(name); - frames_[name] = std::move(frame); - } - } - - // ------ 获取帧 ------ - - /// 按名称获取 - Ptr getSpriteFrame(const std::string &name) const { - std::lock_guard lock(mutex_); - auto it = frames_.find(name); - if (it != frames_.end()) - return it->second; - return nullptr; - } - - /// 通过路径+索引获取或创建(ANI 格式的定位方式) - Ptr getOrCreateFromFile(const std::string &texturePath, - int index = 0) { - std::string key = texturePath + "#" + std::to_string(index); - - { - std::lock_guard lock(mutex_); - auto it = frames_.find(key); - if (it != frames_.end()) - return it->second; - } - - // 缓存未命中,加载纹理并创建 SpriteFrame - auto texture = loadTextureFromFile(texturePath); - if (!texture) - return nullptr; - - // 默认整张纹理作为一帧(index=0),或用整张纹理 - Rect rect(0.0f, 0.0f, static_cast(texture->width()), - static_cast(texture->height())); - - auto frame = SpriteFrame::create(texture, rect); - frame->setName(key); - - std::lock_guard lock(mutex_); - frames_[key] = frame; - return frame; - } - - // ------ 缓存管理 ------ - - bool has(const std::string &name) const { - std::lock_guard lock(mutex_); - return frames_.find(name) != frames_.end(); - } - - void removeSpriteFrame(const std::string &name) { - std::lock_guard lock(mutex_); - frames_.erase(name); - } - - /// 移除未被外部引用的精灵帧(use_count == 1 表示仅缓存自身持有) - void removeUnusedSpriteFrames() { - std::lock_guard lock(mutex_); - for (auto it = frames_.begin(); it != frames_.end();) { - if (it->second.use_count() == 1) { - it = frames_.erase(it); - } else { - ++it; - } - } - } - - void clear() { - std::lock_guard lock(mutex_); - frames_.clear(); - } - - size_t count() const { - std::lock_guard lock(mutex_); - return frames_.size(); - } - -private: - SpriteFrameCache() = default; - ~SpriteFrameCache() = default; - SpriteFrameCache(const SpriteFrameCache &) = delete; - SpriteFrameCache &operator=(const SpriteFrameCache &) = delete; - - /** - * @brief 从文件加载纹理(使用 ResourceManager 的 LRU 缓存) - * @param filepath 纹理文件路径 - * @return 纹理对象,失败返回nullptr - */ - Ptr loadTextureFromFile(const std::string &filepath) { - // 使用 ResourceManager 的纹理缓存机制 - // 这样可以享受 LRU 缓存、自动清理和缓存统计等功能 - auto &resources = ResourceManager::getInstance(); - - // 先检查缓存中是否已有该纹理 - auto texture = resources.getTexture(filepath); - if (texture) { - E2D_TRACE("SpriteFrameCache: 使用缓存纹理: {}", filepath); - return texture; - } - - // 缓存未命中,通过 ResourceManager 加载 - texture = resources.loadTexture(filepath); - if (!texture) { - E2D_ERROR("SpriteFrameCache: 加载纹理失败: {}", filepath); - return nullptr; - } - - E2D_TRACE("SpriteFrameCache: 加载新纹理: {}", filepath); - return texture; - } - - mutable std::mutex mutex_; - std::unordered_map> frames_; -}; - -// 便捷宏 -#define E2D_SPRITE_FRAME_CACHE() ::extra2d::SpriteFrameCache::getInstance() - -} // namespace extra2d diff --git a/include/animation/tween.h b/include/animation/tween.h deleted file mode 100644 index 6f042f1..0000000 --- a/include/animation/tween.h +++ /dev/null @@ -1,168 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace extra2d { - -class Node; - -struct TweenOptions { - TweenEasing easing = TweenEasing::Linear; - std::function onStart = nullptr; - std::function onUpdate = nullptr; - std::function onComplete = nullptr; -}; - -struct TweenProperty { - std::optional position; - std::optional scale; - std::optional rotation; - std::optional opacity; - std::optional color; -}; - -class TweenAction { -public: - enum class Type : uint8_t { - Interval, - Delay, - Call, - Parallel, - }; - - virtual ~TweenAction() = default; - virtual void update(float dt) = 0; - virtual bool isFinished() const = 0; - virtual float getDuration() const = 0; - virtual float getElapsed() const = 0; - virtual void reset() = 0; - - Type getType() const { return type_; } - -protected: - TweenAction(Type type) : type_(type) {} - Type type_; -}; - -class Tween { -public: - using Ptr = std::shared_ptr; - using Callback = std::function; - - explicit Tween(Node *target); - Tween(Node *target, const std::string &name); - ~Tween(); - - Tween(const Tween &) = delete; - Tween &operator=(const Tween &) = delete; - Tween(Tween &&) = default; - Tween &operator=(Tween &&) = default; - - static Tween create(Node *target); - static Tween create(Node *target, const std::string &name); - - Tween &to(float duration, const TweenProperty &props, - const TweenOptions &options = {}); - Tween &by(float duration, const TweenProperty &props, - const TweenOptions &options = {}); - - Tween &set(const TweenProperty &props); - Tween &delay(float seconds); - Tween &call(Callback callback); - - Tween ¶llel(); - Tween &endParallel(); - - Tween &union_(); - Tween &repeat(int times); - Tween &repeatForever(); - Tween &yoyo(bool enabled = true); - - void start(); - void stop(); - void pause(); - void resume(); - - void update(float dt); - - bool isFinished() const; - bool isPlaying() const; - bool isPaused() const; - float getTotalDuration() const; - - void setName(const std::string &name) { name_ = name; } - const std::string &name() const { return name_; } - -private: - Node *target_ = nullptr; - std::string name_; - std::vector> actions_; - size_t currentActionIndex_ = 0; - - bool playing_ = false; - bool paused_ = false; - bool finished_ = false; - - int repeatCount_ = 1; - int currentRepeat_ = 0; - bool yoyo_ = false; - bool reverse_ = false; - bool repeatForever_ = false; - - bool inParallel_ = false; - std::vector> parallelActions_; - - void addAction(std::unique_ptr action); - void advanceToNextAction(); - void resetAllActions(); -}; - -namespace tween { - -inline TweenProperty pos(const Vec2 &p) { - TweenProperty props; - props.position = p; - return props; -} - -inline TweenProperty pos(float x, float y) { return pos(Vec2(x, y)); } - -inline TweenProperty scl(const Vec2 &s) { - TweenProperty props; - props.scale = s; - return props; -} - -inline TweenProperty scl(float s) { return scl(Vec2(s, s)); } - -inline TweenProperty rot(float degrees) { - TweenProperty props; - props.rotation = degrees; - return props; -} - -inline TweenProperty opa(float opacity) { - TweenProperty props; - props.opacity = opacity; - return props; -} - -inline TweenProperty col(const Color &c) { - TweenProperty props; - props.color = c; - return props; -} - -TweenProperty combine(std::initializer_list props); - -} // namespace tween - -} // namespace extra2d diff --git a/include/animation/tween_easing.h b/include/animation/tween_easing.h deleted file mode 100644 index 39e9dd3..0000000 --- a/include/animation/tween_easing.h +++ /dev/null @@ -1,91 +0,0 @@ -#pragma once - -#include - -namespace extra2d { - -enum class TweenEasing : uint8_t { - Linear, - QuadIn, - QuadOut, - QuadInOut, - CubicIn, - CubicOut, - CubicInOut, - QuartIn, - QuartOut, - QuartInOut, - QuintIn, - QuintOut, - QuintInOut, - SineIn, - SineOut, - SineInOut, - ExpoIn, - ExpoOut, - ExpoInOut, - CircIn, - CircOut, - CircInOut, - ElasticIn, - ElasticOut, - ElasticInOut, - BackIn, - BackOut, - BackInOut, - BounceIn, - BounceOut, - BounceInOut, - Smooth, - Fade, -}; - -class EasingFunctions { -public: - using EasingFunc = float (*)(float); - - static float apply(float t, TweenEasing easing); - static EasingFunc get(TweenEasing easing); - - static float linear(float t); - static float quadIn(float t); - static float quadOut(float t); - static float quadInOut(float t); - static float cubicIn(float t); - static float cubicOut(float t); - static float cubicInOut(float t); - static float quartIn(float t); - static float quartOut(float t); - static float quartInOut(float t); - static float quintIn(float t); - static float quintOut(float t); - static float quintInOut(float t); - static float sineIn(float t); - static float sineOut(float t); - static float sineInOut(float t); - static float expoIn(float t); - static float expoOut(float t); - static float expoInOut(float t); - static float circIn(float t); - static float circOut(float t); - static float circInOut(float t); - static float elasticIn(float t); - static float elasticOut(float t); - static float elasticInOut(float t); - static float backIn(float t); - static float backOut(float t); - static float backInOut(float t); - static float bounceIn(float t); - static float bounceOut(float t); - static float bounceInOut(float t); - static float smooth(float t); - static float fade(float t); - -private: - static constexpr float PI = 3.14159265358979323846f; - static constexpr float HALF_PI = 1.57079632679489661923f; - static constexpr float BACK_CONST = 1.70158f; - static constexpr float ELASTIC_CONST = 2.0f * PI / 3.0f; -}; - -} // namespace extra2d diff --git a/include/app/application.h b/include/app/application.h index abdf0e3..253858c 100644 --- a/include/app/application.h +++ b/include/app/application.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -86,8 +86,6 @@ public: // 便捷方法 // ------------------------------------------------------------------------ void enterScene(Ptr scene); - void enterScene(Ptr scene, - Ptr transitionScene); float deltaTime() const { return deltaTime_; } float totalTime() const { return totalTime_; } diff --git a/include/extra2d.h b/include/extra2d.h index 82159ca..965a533 100644 --- a/include/extra2d.h +++ b/include/extra2d.h @@ -14,33 +14,23 @@ #include // Graphics -#include #include -#include -#include #include - -#include #include +// Renderer +#include +#include +#include +#include + + // Scene #include #include #include -#include +#include #include -#include -#include -#include -#include -#include -#include - -// Animation -#include -#include -#include -#include // UI #include diff --git a/include/graphics/opengl/gl_renderer.h b/include/graphics/opengl/gl_renderer.h index fef9e4e..536512f 100644 --- a/include/graphics/opengl/gl_renderer.h +++ b/include/graphics/opengl/gl_renderer.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include diff --git a/include/graphics/camera.h b/include/renderer/camera.h similarity index 100% rename from include/graphics/camera.h rename to include/renderer/camera.h diff --git a/include/graphics/render_command.h b/include/renderer/render_command.h similarity index 100% rename from include/graphics/render_command.h rename to include/renderer/render_command.h diff --git a/include/graphics/render_target.h b/include/renderer/render_target.h similarity index 100% rename from include/graphics/render_target.h rename to include/renderer/render_target.h diff --git a/include/graphics/renderer.h b/include/renderer/renderer.h similarity index 100% rename from include/graphics/renderer.h rename to include/renderer/renderer.h diff --git a/include/graphics/shader_preset.h b/include/renderer/shader_preset.h similarity index 100% rename from include/graphics/shader_preset.h rename to include/renderer/shader_preset.h diff --git a/include/graphics/shader_system.h b/include/renderer/shader_system.h similarity index 100% rename from include/graphics/shader_system.h rename to include/renderer/shader_system.h diff --git a/include/scene/node.h b/include/scene/node.h index 238068d..532dd26 100644 --- a/include/scene/node.h +++ b/include/scene/node.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include @@ -17,8 +17,6 @@ namespace extra2d { class Scene; class Renderer; struct RenderCommand; -class Tween; -enum class TweenEasing : uint8_t; // ============================================================================ // 节点基类 - 场景图的基础 @@ -155,85 +153,6 @@ public: // ------------------------------------------------------------------------ EventDispatcher &getEventDispatcher() { return eventDispatcher_; } - // ------------------------------------------------------------------------ - // Tween 动画系统 - // ------------------------------------------------------------------------ - - /** - * @brief 创建 Tween 动画 - * @return Tween 对象引用,支持链式调用 - */ - Tween &tween(); - - /** - * @brief 创建具名 Tween 动画 - */ - Tween &tween(const std::string &name); - - /** - * @brief 移动到目标位置 - */ - Tween &moveTo(const Vec2 &pos, float duration, - TweenEasing easing = static_cast(0)); - - /** - * @brief 移动指定距离 - */ - Tween &moveBy(const Vec2 &delta, float duration, - TweenEasing easing = static_cast(0)); - - /** - * @brief 缩放到目标值 - */ - Tween &scaleTo(float scale, float duration, - TweenEasing easing = static_cast(0)); - - /** - * @brief 缩放指定增量 - */ - Tween &scaleBy(float delta, float duration, - TweenEasing easing = static_cast(0)); - - /** - * @brief 旋转到目标角度 - */ - Tween &rotateTo(float degrees, float duration, - TweenEasing easing = static_cast(0)); - - /** - * @brief 旋转指定角度 - */ - Tween &rotateBy(float degrees, float duration, - TweenEasing easing = static_cast(0)); - - /** - * @brief 淡入(透明度从当前到1) - */ - Tween &fadeIn(float duration, - TweenEasing easing = static_cast(0)); - - /** - * @brief 淡出(透明度从当前到0) - */ - Tween &fadeOut(float duration, - TweenEasing easing = static_cast(0)); - - /** - * @brief 透明度变化到目标值 - */ - Tween &fadeTo(float opacity, float duration, - TweenEasing easing = static_cast(0)); - - /** - * @brief 停止所有 Tween 动画 - */ - void stopAllTweens(); - - /** - * @brief 更新所有 Tween 动画 - */ - void updateTweens(float dt); - // ------------------------------------------------------------------------ // 内部方法 // ------------------------------------------------------------------------ @@ -318,8 +237,7 @@ private: bool visible_ = true; // 1 byte bool running_ = false; // 1 byte - // 10. Tween 动画列表 - std::vector> tweens_; + }; } // namespace extra2d diff --git a/include/scene/scene.h b/include/scene/scene.h index d18ac87..e529eb5 100644 --- a/include/scene/scene.h +++ b/include/scene/scene.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include @@ -69,12 +69,7 @@ protected: void onEnter() override; void onExit() override; - // 过渡场景生命周期回调(供 TransitionScene 使用) - virtual void onExitTransitionDidStart() {} - virtual void onEnterTransitionDidFinish() {} - friend class SceneManager; - friend class TransitionScene; private: Color backgroundColor_ = Colors::Black; diff --git a/include/scene/scene_manager.h b/include/scene/scene_manager.h index d66c317..76d17cc 100644 --- a/include/scene/scene_manager.h +++ b/include/scene/scene_manager.h @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -14,15 +13,12 @@ namespace extra2d { // 前向声明 struct RenderCommand; -class TransitionScene; // ============================================================================ // 场景管理器 - 管理场景的生命周期和切换 // ============================================================================ class SceneManager { public: - using TransitionCallback = std::function; - // ------------------------------------------------------------------------ // 单例访问 // ------------------------------------------------------------------------ @@ -37,26 +33,18 @@ public: // 替换当前场景 void replaceScene(Ptr scene); - void replaceScene(Ptr scene, TransitionType transition, - float duration = 0.5f); // 压入新场景(当前场景暂停) void pushScene(Ptr scene); - void pushScene(Ptr scene, TransitionType transition, - float duration = 0.5f); // 弹出当前场景(恢复上一个场景) void popScene(); - void popScene(TransitionType transition, float duration = 0.5f); // 弹出到根场景 void popToRootScene(); - void popToRootScene(TransitionType transition, float duration = 0.5f); // 弹出到指定场景 void popToScene(const std::string &name); - void popToScene(const std::string &name, TransitionType transition, - float duration = 0.5f); // ------------------------------------------------------------------------ // 获取场景 @@ -82,14 +70,6 @@ public: void render(Renderer &renderer); void collectRenderCommands(std::vector &commands); - // ------------------------------------------------------------------------ - // 过渡控制 - // ------------------------------------------------------------------------ - bool isTransitioning() const { return isTransitioning_; } - void setTransitionCallback(TransitionCallback callback) { - transitionCallback_ = callback; - } - // ------------------------------------------------------------------------ // 清理 // ------------------------------------------------------------------------ @@ -104,30 +84,14 @@ public: // 场景切换(供 Application 使用) void enterScene(Ptr scene); - void enterScene(Ptr scene, Ptr transitionScene); private: void doSceneSwitch(); - void startTransition(Ptr from, Ptr to, TransitionType type, - float duration, Function stackAction); - void finishTransition(); void dispatchPointerEvents(Scene &scene); - // 创建过渡场景 - Ptr createTransitionScene(TransitionType type, - float duration, - Ptr inScene); - std::stack> sceneStack_; std::unordered_map> namedScenes_; - // Transition state - bool isTransitioning_ = false; - TransitionType currentTransition_ = TransitionType::None; - Ptr activeTransitionScene_; - Function transitionStackAction_; - TransitionCallback transitionCallback_; - // Next scene to switch to (queued during transition) Ptr nextScene_; bool sendCleanupToScene_ = false; diff --git a/include/scene/shape_node.h b/include/scene/shape.h similarity index 100% rename from include/scene/shape_node.h rename to include/scene/shape.h diff --git a/include/scene/transition_box_scene.h b/include/scene/transition_box_scene.h deleted file mode 100644 index 048fb48..0000000 --- a/include/scene/transition_box_scene.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include - -namespace extra2d { - -// ============================================================================ -// 方块/马赛克过渡场景 -// 实现原理: -// 1. 将屏幕分成多个方块 -// 2. 方块逐个消失,显示新场景 -// ============================================================================ -class TransitionBoxScene : public TransitionScene { -public: - /** - * @brief 创建方块过渡场景 - * @param duration 过渡持续时间(秒) - * @param inScene 要进入的目标场景 - * @param divisions 方块分割数(默认为 8,表示 8x8 网格) - */ - TransitionBoxScene(float duration, Ptr inScene, int divisions = 8); - - static Ptr create(float duration, Ptr inScene, - int divisions = 8); - -protected: - void onTransitionStart() override; - void renderContent(Renderer &renderer) override; - -private: - int divisions_; -}; - -} // namespace extra2d diff --git a/include/scene/transition_fade_scene.h b/include/scene/transition_fade_scene.h deleted file mode 100644 index f5fbecd..0000000 --- a/include/scene/transition_fade_scene.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include -#include -#include - - -namespace extra2d { - -// ============================================================================ -// 淡入淡出过渡场景 -// 实现原理: -// 1. 创建一个纯色精灵作为遮罩层 -// 2. 第一阶段:遮罩从透明淡入到不透明(黑屏),同时显示旧场景 -// 3. 切换显示新场景 -// 4. 第二阶段:遮罩从不透明淡出到透明,显示新场景 -// ============================================================================ -class TransitionFadeScene : public TransitionScene { -public: - /** - * @brief 创建淡入淡出过渡场景 - * @param duration 过渡持续时间(秒) - * @param inScene 要进入的目标场景 - * @param color 遮罩颜色(默认为黑色) - */ - TransitionFadeScene(float duration, Ptr inScene, - const Color &color = Colors::Black); - - static Ptr create(float duration, Ptr inScene, - const Color &color = Colors::Black); - -protected: - /** - * @brief 启动过渡动画 - * 创建遮罩层并运行动作序列 - */ - void onTransitionStart() override; - - /** - * @brief 渲染内容 - * 根据进度控制新旧场景的显示 - */ - void renderContent(Renderer &renderer) override; - -private: - /** - * @brief 隐藏退出场景,显示进入场景 - */ - void hideOutShowIn(); - - Color maskColor_; // 遮罩颜色 - bool hasSwitched_ = false; // 是否已经切换场景 -}; - -} // namespace extra2d diff --git a/include/scene/transition_flip_scene.h b/include/scene/transition_flip_scene.h deleted file mode 100644 index 768b969..0000000 --- a/include/scene/transition_flip_scene.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include - -namespace extra2d { - -// ============================================================================ -// 翻页过渡场景 -// 实现原理: -// 1. 前半段:旧场景翻转消失 -// 2. 后半段:新场景翻转出现 -// ============================================================================ -class TransitionFlipScene : public TransitionScene { -public: - enum class Axis { Horizontal, Vertical }; - - /** - * @brief 创建翻页过渡场景 - * @param duration 过渡持续时间(秒) - * @param inScene 要进入的目标场景 - * @param axis 翻转轴(水平或垂直) - */ - TransitionFlipScene(float duration, Ptr inScene, - Axis axis = Axis::Horizontal); - - static Ptr create(float duration, Ptr inScene, - Axis axis = Axis::Horizontal); - -protected: - void onTransitionStart() override; - void renderContent(Renderer &renderer) override; - -private: - Axis axis_; -}; - -} // namespace extra2d diff --git a/include/scene/transition_scale_scene.h b/include/scene/transition_scale_scene.h deleted file mode 100644 index dded968..0000000 --- a/include/scene/transition_scale_scene.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include - -namespace extra2d { - -// ============================================================================ -// 缩放过渡场景 -// 实现原理: -// 1. 旧场景缩小消失 -// 2. 新场景放大出现 -// ============================================================================ -class TransitionScaleScene : public TransitionScene { -public: - /** - * @brief 创建缩放过渡场景 - * @param duration 过渡持续时间(秒) - * @param inScene 要进入的目标场景 - */ - TransitionScaleScene(float duration, Ptr inScene); - - static Ptr create(float duration, Ptr inScene); - -protected: - void onTransitionStart() override; - void renderContent(Renderer &renderer) override; -}; - -} // namespace extra2d diff --git a/include/scene/transition_scene.h b/include/scene/transition_scene.h deleted file mode 100644 index f66e9f8..0000000 --- a/include/scene/transition_scene.h +++ /dev/null @@ -1,130 +0,0 @@ -#pragma once - -#include -#include - - -namespace extra2d { - -// ============================================================================ -// 过渡方向 -// ============================================================================ -enum class TransitionDirection { Left, Right, Up, Down }; - -// ============================================================================ -// 过渡效果类型 -// ============================================================================ -enum class TransitionType { - None, - Fade, - SlideLeft, - SlideRight, - SlideUp, - SlideDown, - Scale, - Flip, - Box -}; - -// ============================================================================ -// 过渡场景基类 - 继承自 Scene,作为中介场景管理过渡效果 -// 设计参考 Cocos2d-x 的 TransitionScene -// ============================================================================ -class TransitionScene : public Scene { -public: - using FinishCallback = std::function; - - /** - * @brief 创建过渡场景 - * @param duration 过渡持续时间(秒) - * @param inScene 要进入的目标场景 - */ - TransitionScene(float duration, Ptr inScene); - ~TransitionScene() override = default; - - // ------------------------------------------------------------------------ - // 场景管理 - // ------------------------------------------------------------------------ - - /** - * @brief 获取要进入的场景 - */ - Ptr getInScene() const { return inScene_; } - - /** - * @brief 获取要退出的场景 - */ - Ptr getOutScene() const { return outScene_; } - - /** - * @brief 设置要退出的场景(由 SceneManager 调用) - */ - void setOutScene(Ptr outScene) { outScene_ = outScene; } - - /** - * @brief 设置过渡完成回调 - */ - void setFinishCallback(FinishCallback callback) { - finishCallback_ = callback; - } - - /** - * @brief 获取过渡持续时间 - */ - float getDuration() const { return duration_; } - - /** - * @brief 获取当前进度 [0, 1] - */ - float getProgress() const { return progress_; } - - /** - * @brief 是否已完成 - */ - bool isFinished() const { return isFinished_; } - - /** - * @brief 完成过渡,通知 SceneManager 切换到目标场景 - */ - void finish(); - - // ------------------------------------------------------------------------ - // 渲染 - 在 TransitionScene 上渲染新旧两个子场景 - // ------------------------------------------------------------------------ - void renderContent(Renderer &renderer) override; - - // ------------------------------------------------------------------------ - // 生命周期 - // ------------------------------------------------------------------------ - void onEnter() override; - void onExit() override; - -protected: - /** - * @brief 子类实现具体的过渡逻辑 - * 在 onEnter 中设置动画,动画完成后调用 finish() - */ - virtual void onTransitionStart() = 0; - - /** - * @brief 绘制源场景(旧场景) - */ - virtual void drawOutScene(Renderer &renderer); - - /** - * @brief 绘制目标场景(新场景) - */ - virtual void drawInScene(Renderer &renderer); - - float duration_; - float elapsed_ = 0.0f; - float progress_ = 0.0f; - bool isFinished_ = false; - - Ptr inScene_; // 要进入的场景 - Ptr outScene_; // 要退出的场景 - - FinishCallback finishCallback_; -}; - -} // namespace extra2d diff --git a/include/scene/transition_slide_scene.h b/include/scene/transition_slide_scene.h deleted file mode 100644 index 9733bb5..0000000 --- a/include/scene/transition_slide_scene.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include - -namespace extra2d { - -// ============================================================================ -// 滑动过渡场景 -// 实现原理: -// 1. 旧场景向指定方向滑出 -// 2. 新场景从相反方向滑入 -// ============================================================================ -class TransitionSlideScene : public TransitionScene { -public: - /** - * @brief 创建滑动过渡场景 - * @param duration 过渡持续时间(秒) - * @param inScene 要进入的目标场景 - * @param direction 滑动方向 - */ - TransitionSlideScene(float duration, Ptr inScene, - TransitionDirection direction); - - static Ptr create(float duration, Ptr inScene, - TransitionDirection direction); - -protected: - void onTransitionStart() override; - void renderContent(Renderer &renderer) override; - -private: - TransitionDirection direction_; -}; - -} // namespace extra2d diff --git a/src/animation/sprite_frame.cpp b/src/animation/sprite_frame.cpp deleted file mode 100644 index ae39e55..0000000 --- a/src/animation/sprite_frame.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include - -namespace extra2d { - -// SpriteFrame 的实现全部在头文件中以内联方式完成 -// 此文件保留用于未来可能需要的非内联实现 - -} // namespace extra2d diff --git a/src/animation/sprite_frame_cache.cpp b/src/animation/sprite_frame_cache.cpp deleted file mode 100644 index 33e8c99..0000000 --- a/src/animation/sprite_frame_cache.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include - -namespace extra2d { - -// SpriteFrameCache 的实现全部在头文件中以内联方式完成 -// 此文件保留用于未来可能需要的非内联实现 - -} // namespace extra2d diff --git a/src/animation/tween.cpp b/src/animation/tween.cpp deleted file mode 100644 index 85a6150..0000000 --- a/src/animation/tween.cpp +++ /dev/null @@ -1,451 +0,0 @@ -#include -#include -#include -#include - -namespace extra2d { - -class IntervalAction : public TweenAction { -public: - IntervalAction(Node *target, float duration, const TweenProperty &props, - const TweenOptions &options, bool isRelative) - : TweenAction(Type::Interval), target_(target), duration_(duration), - props_(props), options_(options), isRelative_(isRelative) {} - - void update(float dt) override { - if (finished_) - return; - - elapsed_ += dt; - float t = std::min(elapsed_ / duration_, 1.0f); - float easedT = EasingFunctions::apply(t, options_.easing); - - applyProperties(easedT); - - if (options_.onUpdate) { - options_.onUpdate(t); - } - - if (t >= 1.0f) { - finished_ = true; - if (options_.onComplete) { - options_.onComplete(); - } - } - } - - bool isFinished() const override { return finished_; } - float getDuration() const override { return duration_; } - float getElapsed() const override { return elapsed_; } - - void reset() override { - elapsed_ = 0.0f; - finished_ = false; - started_ = false; - } - - void captureStartValues() { - if (started_) - return; - started_ = true; - - if (target_) { - startPos_ = target_->pos(); - startScale_ = target_->scale(); - startRotation_ = target_->rot(); - startOpacity_ = target_->opacity(); - } - } - -private: - Node *target_; - float duration_; - TweenProperty props_; - TweenOptions options_; - bool isRelative_; - bool started_ = false; - - float elapsed_ = 0.0f; - bool finished_ = false; - - Vec2 startPos_; - Vec2 startScale_; - float startRotation_ = 0.0f; - float startOpacity_ = 1.0f; - - void applyProperties(float t) { - if (!target_) - return; - - if (props_.position.has_value()) { - Vec2 targetPos = isRelative_ ? startPos_ + props_.position.value() - : props_.position.value(); - Vec2 newPos = Vec2::lerp(startPos_, targetPos, t); - target_->setPosition(newPos); - } - - if (props_.scale.has_value()) { - Vec2 targetScale = - isRelative_ ? startScale_ + props_.scale.value() - : props_.scale.value(); - Vec2 newScale = Vec2::lerp(startScale_, targetScale, t); - target_->setScale(newScale); - } - - if (props_.rotation.has_value()) { - float targetRot = isRelative_ ? startRotation_ + props_.rotation.value() - : props_.rotation.value(); - float newRot = startRotation_ + (targetRot - startRotation_) * t; - target_->setRotation(newRot); - } - - if (props_.opacity.has_value()) { - float targetOpa = isRelative_ ? startOpacity_ + props_.opacity.value() - : props_.opacity.value(); - float newOpa = startOpacity_ + (targetOpa - startOpacity_) * t; - target_->setOpacity(newOpa); - } - } -}; - -class DelayAction : public TweenAction { -public: - DelayAction(float duration) - : TweenAction(Type::Delay), duration_(duration) {} - - void update(float dt) override { - elapsed_ += dt; - finished_ = elapsed_ >= duration_; - } - - bool isFinished() const override { return finished_; } - float getDuration() const override { return duration_; } - float getElapsed() const override { return elapsed_; } - void reset() override { - elapsed_ = 0.0f; - finished_ = false; - } - -private: - float duration_; - float elapsed_ = 0.0f; - bool finished_ = false; -}; - -class CallAction : public TweenAction { -public: - CallAction(std::function callback) - : TweenAction(Type::Call), callback_(std::move(callback)) {} - - void update(float) override { - if (!called_) { - if (callback_) { - callback_(); - } - called_ = true; - } - finished_ = true; - } - - bool isFinished() const override { return finished_; } - float getDuration() const override { return 0.0f; } - float getElapsed() const override { return 0.0f; } - void reset() override { - called_ = false; - finished_ = false; - } - -private: - std::function callback_; - bool called_ = false; - bool finished_ = false; -}; - -class SetAction : public TweenAction { -public: - SetAction(Node *target, const TweenProperty &props) - : TweenAction(Type::Call), target_(target), props_(props) {} - - void update(float) override { - if (!applied_ && target_) { - if (props_.position.has_value()) - target_->setPosition(props_.position.value()); - if (props_.scale.has_value()) - target_->setScale(props_.scale.value()); - if (props_.rotation.has_value()) - target_->setRotation(props_.rotation.value()); - if (props_.opacity.has_value()) - target_->setOpacity(props_.opacity.value()); - applied_ = true; - } - finished_ = true; - } - - bool isFinished() const override { return finished_; } - float getDuration() const override { return 0.0f; } - float getElapsed() const override { return 0.0f; } - void reset() override { - applied_ = false; - finished_ = false; - } - -private: - Node *target_; - TweenProperty props_; - bool applied_ = false; - bool finished_ = false; -}; - -class ParallelAction : public TweenAction { -public: - ParallelAction() : TweenAction(Type::Parallel) {} - - void addAction(std::unique_ptr action) { - actions_.push_back(std::move(action)); - } - - void update(float dt) override { - if (finished_) - return; - - bool allFinished = true; - for (auto &action : actions_) { - if (!action->isFinished()) { - action->update(dt); - allFinished = false; - } - } - - finished_ = allFinished; - } - - bool isFinished() const override { return finished_; } - float getDuration() const override { - float maxDur = 0.0f; - for (const auto &action : actions_) { - maxDur = std::max(maxDur, action->getDuration()); - } - return maxDur; - } - float getElapsed() const override { - float maxElapsed = 0.0f; - for (const auto &action : actions_) { - maxElapsed = std::max(maxElapsed, action->getElapsed()); - } - return maxElapsed; - } - void reset() override { - for (auto &action : actions_) { - action->reset(); - } - finished_ = false; - } - -private: - std::vector> actions_; - bool finished_ = false; -}; - -Tween::Tween(Node *target) : target_(target) {} - -Tween::Tween(Node *target, const std::string &name) - : target_(target), name_(name) {} - -Tween::~Tween() = default; - -Tween Tween::create(Node *target) { return Tween(target); } - -Tween Tween::create(Node *target, const std::string &name) { - return Tween(target, name); -} - -Tween &Tween::to(float duration, const TweenProperty &props, - const TweenOptions &options) { - auto action = - std::make_unique(target_, duration, props, options, false); - addAction(std::move(action)); - return *this; -} - -Tween &Tween::by(float duration, const TweenProperty &props, - const TweenOptions &options) { - auto action = - std::make_unique(target_, duration, props, options, true); - addAction(std::move(action)); - return *this; -} - -Tween &Tween::set(const TweenProperty &props) { - auto action = std::make_unique(target_, props); - addAction(std::move(action)); - return *this; -} - -Tween &Tween::delay(float seconds) { - auto action = std::make_unique(seconds); - addAction(std::move(action)); - return *this; -} - -Tween &Tween::call(Callback callback) { - auto action = std::make_unique(std::move(callback)); - addAction(std::move(action)); - return *this; -} - -Tween &Tween::parallel() { - inParallel_ = true; - parallelActions_.clear(); - return *this; -} - -Tween &Tween::endParallel() { - if (inParallel_ && !parallelActions_.empty()) { - auto parallel = std::make_unique(); - for (auto &action : parallelActions_) { - parallel->addAction(std::move(action)); - } - actions_.push_back(std::move(parallel)); - parallelActions_.clear(); - } - inParallel_ = false; - return *this; -} - -Tween &Tween::union_() { return *this; } - -Tween &Tween::repeat(int times) { - repeatCount_ = times; - return *this; -} - -Tween &Tween::repeatForever() { - repeatForever_ = true; - return *this; -} - -Tween &Tween::yoyo(bool enabled) { - yoyo_ = enabled; - return *this; -} - -void Tween::start() { - if (actions_.empty()) - return; - - playing_ = true; - paused_ = false; - finished_ = false; - currentActionIndex_ = 0; - currentRepeat_ = 0; - - for (auto &action : actions_) { - action->reset(); - } - - if (auto *interval = - dynamic_cast(actions_[0].get())) { - interval->captureStartValues(); - } -} - -void Tween::stop() { - playing_ = false; - finished_ = true; -} - -void Tween::pause() { paused_ = true; } - -void Tween::resume() { paused_ = false; } - -void Tween::update(float dt) { - if (!playing_ || paused_ || finished_) - return; - - if (currentActionIndex_ >= actions_.size()) { - if (repeatForever_ || currentRepeat_ < repeatCount_ - 1) { - currentRepeat_++; - resetAllActions(); - currentActionIndex_ = 0; - if (yoyo_) { - reverse_ = !reverse_; - } - } else { - finished_ = true; - playing_ = false; - return; - } - } - - auto &action = actions_[currentActionIndex_]; - action->update(dt); - - if (action->isFinished()) { - currentActionIndex_++; - if (currentActionIndex_ < actions_.size()) { - actions_[currentActionIndex_]->reset(); - if (auto *interval = dynamic_cast( - actions_[currentActionIndex_].get())) { - interval->captureStartValues(); - } - } - } -} - -bool Tween::isFinished() const { return finished_; } - -bool Tween::isPlaying() const { return playing_; } - -bool Tween::isPaused() const { return paused_; } - -float Tween::getTotalDuration() const { - float total = 0.0f; - for (const auto &action : actions_) { - total += action->getDuration(); - } - return total; -} - -void Tween::addAction(std::unique_ptr action) { - if (inParallel_) { - parallelActions_.push_back(std::move(action)); - } else { - actions_.push_back(std::move(action)); - } -} - -void Tween::advanceToNextAction() { - currentActionIndex_++; - if (currentActionIndex_ < actions_.size()) { - actions_[currentActionIndex_]->reset(); - } -} - -void Tween::resetAllActions() { - for (auto &action : actions_) { - action->reset(); - } -} - -namespace tween { - -TweenProperty combine(std::initializer_list props) { - TweenProperty result; - for (const auto &p : props) { - if (p.position.has_value()) - result.position = p.position; - if (p.scale.has_value()) - result.scale = p.scale; - if (p.rotation.has_value()) - result.rotation = p.rotation; - if (p.opacity.has_value()) - result.opacity = p.opacity; - if (p.color.has_value()) - result.color = p.color; - } - return result; -} - -} // namespace tween - -} // namespace extra2d diff --git a/src/animation/tween_easing.cpp b/src/animation/tween_easing.cpp deleted file mode 100644 index eda757c..0000000 --- a/src/animation/tween_easing.cpp +++ /dev/null @@ -1,236 +0,0 @@ -#include -#include - -namespace extra2d { - -float EasingFunctions::apply(float t, TweenEasing easing) { - return get(easing)(t); -} - -EasingFunctions::EasingFunc EasingFunctions::get(TweenEasing easing) { - switch (easing) { - case TweenEasing::Linear: return linear; - case TweenEasing::QuadIn: return quadIn; - case TweenEasing::QuadOut: return quadOut; - case TweenEasing::QuadInOut: return quadInOut; - case TweenEasing::CubicIn: return cubicIn; - case TweenEasing::CubicOut: return cubicOut; - case TweenEasing::CubicInOut: return cubicInOut; - case TweenEasing::QuartIn: return quartIn; - case TweenEasing::QuartOut: return quartOut; - case TweenEasing::QuartInOut: return quartInOut; - case TweenEasing::QuintIn: return quintIn; - case TweenEasing::QuintOut: return quintOut; - case TweenEasing::QuintInOut: return quintInOut; - case TweenEasing::SineIn: return sineIn; - case TweenEasing::SineOut: return sineOut; - case TweenEasing::SineInOut: return sineInOut; - case TweenEasing::ExpoIn: return expoIn; - case TweenEasing::ExpoOut: return expoOut; - case TweenEasing::ExpoInOut: return expoInOut; - case TweenEasing::CircIn: return circIn; - case TweenEasing::CircOut: return circOut; - case TweenEasing::CircInOut: return circInOut; - case TweenEasing::ElasticIn: return elasticIn; - case TweenEasing::ElasticOut: return elasticOut; - case TweenEasing::ElasticInOut: return elasticInOut; - case TweenEasing::BackIn: return backIn; - case TweenEasing::BackOut: return backOut; - case TweenEasing::BackInOut: return backInOut; - case TweenEasing::BounceIn: return bounceIn; - case TweenEasing::BounceOut: return bounceOut; - case TweenEasing::BounceInOut: return bounceInOut; - case TweenEasing::Smooth: return smooth; - case TweenEasing::Fade: return fade; - default: return linear; - } -} - -float EasingFunctions::linear(float t) { - return t; -} - -float EasingFunctions::quadIn(float t) { - return t * t; -} - -float EasingFunctions::quadOut(float t) { - return t * (2.0f - t); -} - -float EasingFunctions::quadInOut(float t) { - if (t < 0.5f) { - return 2.0f * t * t; - } - return -1.0f + (4.0f - 2.0f * t) * t; -} - -float EasingFunctions::cubicIn(float t) { - return t * t * t; -} - -float EasingFunctions::cubicOut(float t) { - float t1 = t - 1.0f; - return t1 * t1 * t1 + 1.0f; -} - -float EasingFunctions::cubicInOut(float t) { - if (t < 0.5f) { - return 4.0f * t * t * t; - } - float t1 = 2.0f * t - 2.0f; - return 0.5f * t1 * t1 * t1 + 1.0f; -} - -float EasingFunctions::quartIn(float t) { - return t * t * t * t; -} - -float EasingFunctions::quartOut(float t) { - float t1 = t - 1.0f; - return 1.0f - t1 * t1 * t1 * t1; -} - -float EasingFunctions::quartInOut(float t) { - if (t < 0.5f) { - return 8.0f * t * t * t * t; - } - float t1 = t - 1.0f; - return 1.0f - 8.0f * t1 * t1 * t1 * t1; -} - -float EasingFunctions::quintIn(float t) { - return t * t * t * t * t; -} - -float EasingFunctions::quintOut(float t) { - float t1 = t - 1.0f; - return t1 * t1 * t1 * t1 * t1 + 1.0f; -} - -float EasingFunctions::quintInOut(float t) { - if (t < 0.5f) { - return 16.0f * t * t * t * t * t; - } - float t1 = 2.0f * t - 2.0f; - return 0.5f * t1 * t1 * t1 * t1 * t1 + 1.0f; -} - -float EasingFunctions::sineIn(float t) { - return 1.0f - std::cos(t * HALF_PI); -} - -float EasingFunctions::sineOut(float t) { - return std::sin(t * HALF_PI); -} - -float EasingFunctions::sineInOut(float t) { - return -0.5f * (std::cos(PI * t) - 1.0f); -} - -float EasingFunctions::expoIn(float t) { - return t == 0.0f ? 0.0f : std::pow(2.0f, 10.0f * (t - 1.0f)); -} - -float EasingFunctions::expoOut(float t) { - return t == 1.0f ? 1.0f : 1.0f - std::pow(2.0f, -10.0f * t); -} - -float EasingFunctions::expoInOut(float t) { - if (t == 0.0f) return 0.0f; - if (t == 1.0f) return 1.0f; - if (t < 0.5f) { - return 0.5f * std::pow(2.0f, 20.0f * t - 10.0f); - } - return 1.0f - 0.5f * std::pow(2.0f, -20.0f * t + 10.0f); -} - -float EasingFunctions::circIn(float t) { - return 1.0f - std::sqrt(1.0f - t * t); -} - -float EasingFunctions::circOut(float t) { - return std::sqrt(2.0f * t - t * t); -} - -float EasingFunctions::circInOut(float t) { - if (t < 0.5f) { - return 0.5f * (1.0f - std::sqrt(1.0f - 4.0f * t * t)); - } - return 0.5f * (std::sqrt(4.0f * t - 4.0f * t * t - 3.0f) + 1.0f); -} - -float EasingFunctions::elasticIn(float t) { - if (t == 0.0f) return 0.0f; - if (t == 1.0f) return 1.0f; - return -std::pow(2.0f, 10.0f * (t - 1.0f)) * std::sin((t - 1.1f) * ELASTIC_CONST); -} - -float EasingFunctions::elasticOut(float t) { - if (t == 0.0f) return 0.0f; - if (t == 1.0f) return 1.0f; - return std::pow(2.0f, -10.0f * t) * std::sin((t - 0.1f) * ELASTIC_CONST) + 1.0f; -} - -float EasingFunctions::elasticInOut(float t) { - if (t == 0.0f) return 0.0f; - if (t == 1.0f) return 1.0f; - if (t < 0.5f) { - return -0.5f * std::pow(2.0f, 20.0f * t - 10.0f) * std::sin((20.0f * t - 11.125f) * PI / 4.5f); - } - return std::pow(2.0f, -20.0f * t + 10.0f) * std::sin((20.0f * t - 11.125f) * PI / 4.5f) * 0.5f + 1.0f; -} - -float EasingFunctions::backIn(float t) { - return t * t * ((BACK_CONST + 1.0f) * t - BACK_CONST); -} - -float EasingFunctions::backOut(float t) { - float t1 = t - 1.0f; - return t1 * t1 * ((BACK_CONST + 1.0f) * t1 + BACK_CONST) + 1.0f; -} - -float EasingFunctions::backInOut(float t) { - float s = BACK_CONST * 1.525f; - if (t < 0.5f) { - return 0.5f * (t * 2.0f) * (t * 2.0f) * ((s + 1.0f) * 2.0f * t - s); - } - float t1 = 2.0f * t - 2.0f; - return 0.5f * (t1 * t1 * ((s + 1.0f) * t1 + s) + 2.0f); -} - -float EasingFunctions::bounceIn(float t) { - return 1.0f - bounceOut(1.0f - t); -} - -float EasingFunctions::bounceOut(float t) { - if (t < 1.0f / 2.75f) { - return 7.5625f * t * t; - } else if (t < 2.0f / 2.75f) { - float t1 = t - 1.5f / 2.75f; - return 7.5625f * t1 * t1 + 0.75f; - } else if (t < 2.5f / 2.75f) { - float t1 = t - 2.25f / 2.75f; - return 7.5625f * t1 * t1 + 0.9375f; - } else { - float t1 = t - 2.625f / 2.75f; - return 7.5625f * t1 * t1 + 0.984375f; - } -} - -float EasingFunctions::bounceInOut(float t) { - if (t < 0.5f) { - return 0.5f * bounceIn(t * 2.0f); - } - return 0.5f * bounceOut(t * 2.0f - 1.0f) + 0.5f; -} - -float EasingFunctions::smooth(float t) { - return t * t * (3.0f - 2.0f * t); -} - -float EasingFunctions::fade(float t) { - return t * t * t * (t * (6.0f * t - 15.0f) + 10.0f); -} - -} // namespace extra2d diff --git a/src/app/application.cpp b/src/app/application.cpp index 93674a7..5924d0f 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -2,8 +2,8 @@ #include