Skip to content

Commit

Permalink
Animations implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 14, 2024
1 parent 3df5e14 commit 1962ae7
Show file tree
Hide file tree
Showing 27 changed files with 979 additions and 15 deletions.
63 changes: 62 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,67 @@
"files.associations": {
"utility": "cpp",
"__locale": "cpp",
"*.inc": "cpp"
"*.inc": "cpp",
"functional": "cpp",
"__bit_reference": "cpp",
"__hash_table": "cpp",
"__node_handle": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__verbose_abort": "cpp",
"array": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"complex": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"execution": "cpp",
"memory": "cpp",
"forward_list": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"locale": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"print": "cpp",
"queue": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"variant": "cpp",
"vector": "cpp",
"algorithm": "cpp",
"__assertion_handler": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__mutex_base": "cpp",
"atomic": "cpp",
"exception": "cpp",
"system_error": "cpp",
"any": "cpp"
}
}
8 changes: 8 additions & 0 deletions Submodules/UIKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ add_definitions(
)

add_library(UIKit
lib/CABasicAnimation.cpp
lib/CABasicAnimationPrototype.cpp
lib/CASpringAnimation.cpp
lib/CASpringAnimationPrototype.cpp
lib/CAMediaTimingFunction.cpp
lib/CATransaction.cpp
lib/ContentsGravityTransformation.cpp
lib/platforms/SkiaCtx.cpp
lib/Application.cpp
Expand All @@ -22,6 +28,8 @@ add_library(UIKit
lib/UIImageView.cpp
lib/UILabel.cpp
lib/UIView.cpp
lib/UIViewAnimationGroup.cpp
lib/UIViewAnimationOptions.cpp
lib/UIWindow.cpp
lib/NXAffineTransform.cpp
lib/NXTransform3D.cpp
Expand Down
44 changes: 44 additions & 0 deletions Submodules/UIKit/include/CABasicAnimation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <CABasicAnimationPrototype.h>
#include <CAMediaTimingFunction.h>
#include <Timer.h>
#include <string>
#include <optional>

namespace NXKit {

class CAAction {};

class CABasicAnimation: public CAAction {
public:
/// animation duration in seconds
double duration = 0;

/// animation delay in seconds
double delay = 0;

std::optional<std::string> keyPath;
std::optional<std::string> fillMode;
bool isRemovedOnCompletion = true;
std::shared_ptr<CAMediaTimingFunction> timingFunction;

std::optional<AnimatableProperty> fromValue;
std::optional<AnimatableProperty> toValue;

std::shared_ptr<UIViewAnimationGroup> animationGroup;
Timer creationTime;

CABasicAnimation(std::string keyPath);
CABasicAnimation(std::shared_ptr<CABasicAnimationPrototype> prototype, std::string keyPath, AnimatableProperty fromValue, std::shared_ptr<CAMediaTimingFunction> timingFunction);
CABasicAnimation(CABasicAnimation* animation);

bool wasCreatedInUIAnimateBlock();
float progressFor(Timer currentTime);

private:
float ease(float x);
};

}

24 changes: 24 additions & 0 deletions Submodules/UIKit/include/CABasicAnimationPrototype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <UIViewAnimationGroup.h>
#include <tools/SharedBase.hpp>
#include <memory>
#include <any>

namespace NXKit {

using AnimatableProperty = std::any;

class CABasicAnimation;
class CABasicAnimationPrototype: public enable_shared_from_this<CABasicAnimationPrototype> {
public:
const double duration;
const double delay;
const std::shared_ptr<UIViewAnimationGroup> animationGroup;

CABasicAnimationPrototype(double duration, double delay, std::shared_ptr<UIViewAnimationGroup> animationGroup);

virtual std::shared_ptr<CABasicAnimation> createAnimation(std::string keyPath, AnimatableProperty fromValue);
};

}
35 changes: 35 additions & 0 deletions Submodules/UIKit/include/CALayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#include <NXTransform3D.h>
#include <NXAffineTransform.h>
#include <ContentsGravityTransformation.h>
#include <CABasicAnimation.h>
#include <CGImage.h>
#include <UIColor.h>

#include <tools/SharedBase.hpp>
#include <optional>
#include <vector>
#include <map>

namespace NXKit {

Expand All @@ -24,8 +26,11 @@ class CALayerDelegate {
class CALayer: public enable_shared_from_this<CALayer> {
public:
CALayer();
CALayer(CALayer* layer);
~CALayer() {}

std::weak_ptr<CALayerDelegate> delegate;

// Getter Setters
void setContentsGravity(CALayerContentsGravity contentsGravity) { _contentsGravity = contentsGravity; }
[[nodiscard]] CALayerContentsGravity contentsGravity() const { return _contentsGravity; }
Expand Down Expand Up @@ -85,10 +90,29 @@ class CALayer: public enable_shared_from_this<CALayer> {

void removeFromSuperlayer();

CALayer* copy();

std::shared_ptr<CAAction> actionForKey(std::string event);
static std::shared_ptr<CABasicAnimation> defaultActionForKey(std::string event);
static NXFloat defaultAnimationDuration;

std::shared_ptr<CALayer> createPresentation();
std::shared_ptr<CALayer> presentation() { return _presentation; }
std::shared_ptr<CALayer> presentationOrSelf();

// Animations
void add(std::shared_ptr<CABasicAnimation> animation, std::string keyPath);
void removeAnimation(std::string forKey);
void removeAllAnimations();
void onWillSet(std::string keyPath);
void onDidSetAnimations(bool wasEmpty);
std::optional<AnimatableProperty> value(std::string forKeyPath);

void animateAt(Timer currentTime);

void skiaRender(SkCanvas* canvas);
private:
friend class UIView;

/// Defaults to 1.0 but if the layer is associated with a view,
/// the view sets this value to match the screen.
Expand Down Expand Up @@ -123,6 +147,17 @@ class CALayer: public enable_shared_from_this<CALayer> {
often enough for us to care about it.
**/
static bool layerTreeIsDirty;

std::shared_ptr<CALayer> _presentation;
std::map<std::string, std::shared_ptr<CABasicAnimation>> animations;

bool isPresentationForAnotherLayer = false;

/// We disable animation on parameters of views / layers that haven't been rendered yet.
/// This is both a performance optimization (avoids lots of animations at the start)
/// as well as a correctness fix (matches iOS behaviour). Maybe there's a better way though?
bool hasBeenRenderedInThisPartOfOverallLayerHierarchy = false;
void update(std::shared_ptr<CALayer> presentation, std::shared_ptr<CABasicAnimation> animation, float progress);
};

}
45 changes: 45 additions & 0 deletions Submodules/UIKit/include/CAMediaTimingFunction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <string>
#include <memory>
#include <functional>
#include <UIViewAnimationOptions.h>

namespace NXKit {

// Note these are actually acceleration rates (explains why Fast is a smaller value than Normal)
const double UIScrollViewDecelerationRateNormal = 0.998;
const double UIScrollViewDecelerationRateFast = 0.99;

const std::string kCAMediaTimingFunctionLinear = "linear";
const std::string kCAMediaTimingFunctionEaseIn = "easeIn";
const std::string kCAMediaTimingFunctionEaseOut = "easeOut";
const std::string kCAMediaTimingFunctionEaseInEaseOut = "easeInEaseOut";
const std::string kCAMediaTimingFunctionDefault = "default";
const std::string kCAMediaTimingFunctionCustomEaseOut = "customEaseOut";
const std::string kCAMediaTimingFunctionEaseOutElastic = "easeOutElastic";

class CAMediaTimingFunction {
public:
CAMediaTimingFunction(std::string name);
CAMediaTimingFunction(std::function<double(double)> timing);

static double linear(double x);
static double easeInCubic(double x);
static double easeOutCubic(double x);
static double easeInQuad(double x);
static double easeOutQuad(double x);
static double easeInOutCubic(double x);
static double easeOutElastic(double x);

// from CubicBezier1D optimising away constant terms
static double customEaseOut(double x);
static std::shared_ptr<CAMediaTimingFunction> timingFunctionFrom(UIViewAnimationOptions options);

float at(float x);
private:
std::function<double(double)> timing;
};

}

19 changes: 19 additions & 0 deletions Submodules/UIKit/include/CASpringAnimation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <CABasicAnimation.h>
#include <CASpringAnimationPrototype.h>

namespace NXKit {

class CASpringAnimation: public CABasicAnimation {
public:
std::optional<double> damping;
std::optional<double> initialSpringVelocity;

CASpringAnimation(CASpringAnimation* animation);
CASpringAnimation(std::shared_ptr<CASpringAnimationPrototype> prototype,
std::string keyPath,
AnimatableProperty fromValue);
};

}
23 changes: 23 additions & 0 deletions Submodules/UIKit/include/CASpringAnimationPrototype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <CABasicAnimationPrototype.h>

namespace NXKit {

class CASpringAnimation;
class CASpringAnimationPrototype: public CABasicAnimationPrototype {
public:
const double damping;
const double initialSpringVelocity;

CASpringAnimationPrototype(double duration,
double delay,
double damping,
double initialSpringVelocity,
std::shared_ptr<UIViewAnimationGroup> animationGroup);

std::shared_ptr<CABasicAnimation> createAnimation(std::string keyPath, AnimatableProperty fromValue) override;

};

}
28 changes: 28 additions & 0 deletions Submodules/UIKit/include/CATransaction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <CALayer.h>

namespace NXKit {

struct CATransaction {
private:
bool disableActions_ = false;
float animationDuration_ = CALayer::defaultAnimationDuration;

static std::vector<CATransaction> transactionStack;

public:
static void begin();

static void commit();

static bool disableActions();

static void setDisableActions(bool newValue);

static float animationDuration();

static void setAnimationDuration(float newValue);
};

}
4 changes: 4 additions & 0 deletions Submodules/UIKit/include/UIColor.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <Geometry.h>

namespace NXKit {

class UIColor {
Expand All @@ -15,6 +17,8 @@ class UIColor {

bool operator==(const UIColor& rhs) const;

UIColor interpolationTo(UIColor endResult, NXFloat progress);

static UIColor clear;
static UIColor red;
static UIColor green;
Expand Down
Loading

0 comments on commit 1962ae7

Please sign in to comment.