Skip to content

Commit

Permalink
UIApplication foundation
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 14, 2024
1 parent c7bf11d commit 2781952
Show file tree
Hide file tree
Showing 47 changed files with 2,035 additions and 282 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"files.associations": {
"utility": "cpp"
"utility": "cpp",
"__locale": "cpp",
"*.inc": "cpp"
}
}
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ set(CMAKE_CXX_STANDARD 17)

add_executable(${PROJECT_NAME}
app/main.cpp
app/AppDelegate.cpp
)

setup_project()

target_link_libraries(${PROJECT_NAME} PRIVATE
UIKit
)

setup_project()

#list(APPEND google_angle_libs
# ${EXTERN_PATH}/angle/mac/libEGL.dylib
Expand Down
18 changes: 16 additions & 2 deletions Submodules/UIKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@ add_definitions(
)

add_library(UIKit
lib/ContentsGravityTransformation.cpp
lib/platforms/SkiaCtx.cpp
lib/Application.cpp
lib/DispatchQueue.cpp
lib/Timer.cpp
lib/Geometry.cpp
lib/NXLayer.cpp
lib/NXColor.cpp
lib/CGImage.cpp
lib/CADisplayLink.cpp
lib/CALayer.cpp
lib/UIApplication.cpp
lib/UIApplicationDelegateImpl.cpp
lib/UIApplicationMain.cpp
lib/UIColor.cpp
lib/UIImage.cpp
lib/UIView.cpp
lib/UIWindow.cpp
lib/NXAffineTransform.cpp
lib/NXTransform3D.cpp
lib/NXData.cpp
)

# APPLE
Expand Down Expand Up @@ -55,6 +68,7 @@ endif ()
target_include_directories(UIKit PUBLIC
${EXTERN_PATH}/SDL/include
${EXTERN_PATH}/skia
${EXTERN_PATH}/libromfs/lib/include
include
lib
)
Expand Down
22 changes: 22 additions & 0 deletions Submodules/UIKit/include/CADisplayLink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <vector>
#include <functional>
#include <tools/SharedBase.hpp>

namespace NXKit {

class CADisplayLink {
public:
explicit CADisplayLink(std::function<void()> func);
~CADisplayLink();

void invalidate();
private:
bool isRunning = true;
std::function<void()> func;
static std::vector<CADisplayLink*> activeLinks;
friend class DispatchQueue;
};

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

#include "include/core/SkCanvas.h"

#include <Geometry.h>
#include <NXTransform3D.h>
#include <NXAffineTransform.h>
#include <ContentsGravityTransformation.h>
#include <CGImage.h>
#include <UIColor.h>

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

namespace NXKit {

class CALayer: public enable_shared_from_this<CALayer> {
public:
CALayer();
~CALayer() {}

// Getter Setters
void setContentsGravity(CALayerContentsGravity contentsGravity) { _contentsGravity = contentsGravity; }
[[nodiscard]] CALayerContentsGravity contentsGravity() const { return _contentsGravity; }

void setContentsScale(NXFloat contentsScale) { _contentsScale = contentsScale; }
[[nodiscard]] NXFloat contentsScale() const { return _contentsScale; }

void setAnchorPoint(NXPoint anchorPoint);
[[nodiscard]] NXPoint anchorPoint() const { return _anchorPoint; }

void setOpacity(NXFloat opacity);
[[nodiscard]] NXFloat opacity() const { return _opacity; }

void setBounds(NXRect bounds);
[[nodiscard]] NXRect bounds() const { return _bounds; }

void setPosition(NXPoint position);
[[nodiscard]] NXPoint position() const { return _position; }

void setZPosition(NXFloat position);
[[nodiscard]] NXFloat zPosition() { return _zPosition; }

void setBackgroundColor(std::optional<UIColor> backgroundColor);
[[nodiscard]] std::optional<UIColor> backgroundColor() const { return _backgroundColor; }

void setCornerRadius(NXFloat cornerRadius);
[[nodiscard]] NXFloat cornerRadius() const { return _cornerRadius; }

void setTransform(NXTransform3D transform);
[[nodiscard]] NXTransform3D transform() const { return _transform; }

void setMask(std::shared_ptr<CALayer> mask);
[[nodiscard]] std::shared_ptr<CALayer> mask() const { return _mask; }

void setContents(std::shared_ptr<CGImage> contents);
[[nodiscard]] std::shared_ptr<CGImage> contents() { return _contents; }

NXRect getFrame();
void setFrame(NXRect frame);

// Layers
[[nodiscard]] std::vector<std::shared_ptr<CALayer>> sublayers() { return _sublayers; }

void addSublayer(const std::shared_ptr<CALayer>& layer);
void insertSublayerAt(const std::shared_ptr<CALayer>& layer, int index);
void insertSublayerAbove(const std::shared_ptr<CALayer>& layer, const std::shared_ptr<CALayer>& sibling);
void insertSublayerBelow(const std::shared_ptr<CALayer>& layer, const std::shared_ptr<CALayer>& sibling);

void removeFromSuperlayer();

void onWillSet(std::string keyPath);

void skiaRender(SkCanvas* canvas);
private:

/// Defaults to 1.0 but if the layer is associated with a view,
/// the view sets this value to match the screen.
NXFloat _contentsScale = 1.0f;
CALayerContentsGravity _contentsGravity = CALayerContentsGravity::resize;

std::weak_ptr<CALayer> _superlayer;
std::vector<std::shared_ptr<CALayer>> _sublayers;
std::shared_ptr<CALayer> _mask;

std::shared_ptr<CGImage> _contents;

// Animatable
NXPoint _anchorPoint = NXPoint(0.5, 0.5);
NXPoint _position;
NXFloat _opacity = 1;
NXFloat _zPosition = 0.0;
NXRect _bounds;
NXFloat _cornerRadius = 0;
std::optional<UIColor> _backgroundColor;
NXTransform3D _transform = NXTransform3D::identity;

/**
Indicates whether a layer somewhere has changed since the last render pass.
The current implementation of this is quite simple and doesn't check whether the layer is actually in
the layer hierarchy or not. In theory this means that we're wasting render passes if users frequently
update layers that aren't in the tree. In practice it's not expected that UIKit users would do that
often enough for us to care about it.
**/
static bool layerTreeIsDirty;
};

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

#include <SDL.h>
//#include <SDL_gpu.h>
#include <optional>
#include <memory>
#include <NXData.h>
#include <Geometry.h>

#include "include/core/SkImage.h"

namespace NXKit {

class CGImage {
public:
sk_sp<SkImage> pointee;

// CGImage(NXSize size);
CGImage(const NXData& sourceData);
// CGImage(SDL_Surface* surface);
CGImage(sk_sp<SkImage> image, std::optional<NXData> sourceData);
CGImage(sk_sp<SkImage> image): CGImage(image, std::nullopt) {}
~CGImage();

[[nodiscard]] NXSize size() const;
private:
std::optional<NXData> sourceData;
};
}
26 changes: 26 additions & 0 deletions Submodules/UIKit/include/ContentsGravityTransformation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <Geometry.h>

namespace NXKit {

enum class CALayerContentsGravity {
bottom, bottomLeft, bottomRight,
center, left, right,
top, topLeft, topRight,
resize, resizeAspect, resizeAspectFill
};

class CALayer;
struct ContentsGravityTransformation {
NXPoint offset;
NXSize scale;

ContentsGravityTransformation(CALayer* layer);

private:
NXSize defaultScale = NXSize(1.0, 1.0);
};

}

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

#include <queue>
#include <string>
#include <functional>
#include <thread>
#include <mutex>

#ifdef main
#undef main
#endif

namespace NXKit {

class DispatchQueue {
public:
DispatchQueue(const std::string& tag);
~DispatchQueue();

static std::shared_ptr<DispatchQueue> main();
static std::shared_ptr<DispatchQueue> global();

std::string tag() const { return _tag; }
void async(const std::function<void()>& task);

bool isActive() { return _task_loop_active; }
private:
static std::shared_ptr<DispatchQueue> _main;
static std::shared_ptr<DispatchQueue> _global;
std::queue<std::function<void()>> _queue;
std::string _tag;

pthread_t _task_loop_thread = nullptr;
std::mutex _m_async_mutex;
volatile bool _task_loop_active = true;
static void* task_loop(void* a);

void performAll();

friend bool applicationRunLoop();
};

}
13 changes: 9 additions & 4 deletions Submodules/UIKit/include/Geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace NXKit {

typedef double NXFloat;

struct NXAffineTransform;
struct NXTransform3D;

struct NXPoint {
NXFloat x, y;

Expand All @@ -18,10 +21,12 @@ struct NXPoint {
NXPoint operator/(const NXFloat& rhs);
NXPoint operator*(const NXFloat& rhs);

// Point applying(const NXAffineTransform& t) const;
NXPoint applying(const NXAffineTransform& t) const;

bool valid();
NXFloat magnitude();

static NXPoint zero;
};

struct NXSize {
Expand Down Expand Up @@ -71,8 +76,8 @@ struct NXRect {
NXRect& offsetBy(const NXPoint& offset);
NXRect& offsetBy(const NXFloat& offsetX, const NXFloat& offsetY);
// NXRect& insetBy(const UIEdgeInsets& insets);
// NXRect applying(const NXAffineTransform& t) const;
// NXRect applying(const NXTransform3D& t) const;
NXRect applying(const NXAffineTransform& t) const;
NXRect applying(const NXTransform3D& t) const;

bool operator==(const NXRect& rhs);

Expand All @@ -82,4 +87,4 @@ struct NXRect {
static NXRect null;
};

}
}
36 changes: 36 additions & 0 deletions Submodules/UIKit/include/NXAffineTransform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// NXAffineTransform.h
// NXKit
//
// Created by Даниил Виноградов on 19.07.2022.
//

#pragma once

#include "Geometry.h"
#include <optional>

namespace NXKit {

struct NXAffineTransform {
public:
float m11, m12, m21, m22, tX, tY;

NXAffineTransform();
NXAffineTransform(NXFloat m11, NXFloat m12, NXFloat m21, NXFloat m22, NXFloat tX, NXFloat tY);

static NXAffineTransform translationBy(NXFloat x, NXFloat y);
static NXAffineTransform scaleBy(NXFloat x, NXFloat y);
static NXAffineTransform scale(NXFloat factor);
static NXAffineTransform rotationBy(NXFloat angle);

std::optional<NXAffineTransform> inverted() const;
bool isIdentity() const;

static const NXAffineTransform identity;

bool operator==(const NXAffineTransform& rhs) const;
NXAffineTransform operator*(const NXAffineTransform& rhs) const;
};

}
Loading

0 comments on commit 2781952

Please sign in to comment.