Skip to content

Commit

Permalink
Base UIKit
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 8, 2024
1 parent 775da6f commit c7bf11d
Show file tree
Hide file tree
Showing 15 changed files with 840 additions and 194 deletions.
5 changes: 4 additions & 1 deletion Submodules/UIKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ add_definitions(
add_library(UIKit
lib/platforms/SkiaCtx.cpp
lib/Application.cpp
lib/NXSize.cpp
lib/Geometry.cpp
lib/NXLayer.cpp
lib/NXColor.cpp
lib/UIView.cpp
)

# APPLE
Expand Down
7 changes: 5 additions & 2 deletions Submodules/UIKit/include/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

#include <SDL.h>
#include "include/gpu/ganesh/GrDirectContext.h"
#import "tools/window/WindowContext.h"
#import "SkiaCtx.h"
#include "tools/window/WindowContext.h"
#include "SkiaCtx.h"
#include "UIView.h"

namespace NXKit {

Expand All @@ -16,6 +17,8 @@ class Application {
sk_sp<SkTypeface> typeface;
float fRotationAngle = 0;

std::shared_ptr<UIView> keyWindow;

std::unique_ptr<SkiaCtx> skiaCtx;

static Application* shared;
Expand Down
85 changes: 85 additions & 0 deletions Submodules/UIKit/include/Geometry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#pragma once

namespace NXKit {

typedef double NXFloat;

struct NXPoint {
NXFloat x, y;

NXPoint();
NXPoint(NXFloat x, NXFloat y);

bool operator==(const NXPoint& rhs) const;
NXPoint operator+(const NXPoint& first) const;
NXPoint operator-(const NXPoint& first) const;
NXPoint& operator+=(const NXPoint& rhs);
NXPoint& operator-=(const NXPoint& rhs);
NXPoint operator/(const NXFloat& rhs);
NXPoint operator*(const NXFloat& rhs);

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

bool valid();
NXFloat magnitude();
};

struct NXSize {
NXFloat width, height;

NXSize();
NXSize(NXFloat width, NXFloat height);

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

NXSize operator+(const NXSize &first) const;
NXSize operator-(const NXSize &first) const;
NXSize &operator+=(const NXSize &rhs);
NXSize &operator-=(const NXSize &rhs);

NXSize operator*(const NXFloat &first) const;
NXSize operator/(const NXFloat &first) const;
NXSize &operator*=(const NXFloat &rhs);
NXSize &operator/=(const NXFloat &rhs);

bool valid();
// NXSize inset(UIEdgeInsets inset) const;
};

struct NXRect {
NXPoint origin;
NXSize size;

NXRect();
NXRect(NXPoint origin, NXSize size);
NXRect(NXFloat x, NXFloat y, NXFloat width, NXFloat height);

NXFloat width() const;
NXFloat height() const;

NXFloat minX() const;
NXFloat midX() const;
NXFloat maxX() const;

NXFloat minY() const;
NXFloat midY() const;
NXFloat maxY() const;

bool contains(NXPoint point);
bool intersects(const NXRect& other) const;
NXRect intersection(const NXRect& other) const;
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;

bool operator==(const NXRect& rhs);

bool valid();

static NXRect zero;
static NXRect null;
};

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

namespace NXKit {

class NXColor {
public:
NXColor();
NXColor(unsigned char red, unsigned char green, unsigned char blue);
NXColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha);

unsigned char r();
unsigned char g();
unsigned char b();
unsigned char a();

private:
friend class NXLayer;
int color;
};

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

#include "include/core/SkCanvas.h"

#include <Geometry.h>
#include <NXColor.h>

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

namespace NXKit {

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

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

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

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

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

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


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

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

void removeFromSuperlayer();

void skiaRender(SkCanvas* canvas);
private:

std::weak_ptr<NXLayer> superlayer;
std::vector<std::shared_ptr<NXLayer>> _sublayers;

// Animatable
NXPoint _anchorPoint = NXPoint(0.5f, 0.5f);
NXPoint _position;
NXRect _bounds;
NXFloat _cornerRadius = 0;
std::optional<NXColor> _backgroundColor;

/**
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;
};

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

namespace NXKit {

typedef double NXFloat;

struct NXSize {
NXFloat width;
NXFloat height;

NXSize();

NXSize(NXFloat width, NXFloat height);

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

NXSize operator+(const NXSize &first) const;

NXSize operator-(const NXSize &first) const;

NXSize &operator+=(const NXSize &rhs);

NXSize &operator-=(const NXSize &rhs);

NXSize operator*(const NXFloat &first) const;

NXSize operator/(const NXFloat &first) const;

NXSize &operator*=(const NXFloat &rhs);

NXSize &operator/=(const NXFloat &rhs);
};

}
1 change: 1 addition & 0 deletions Submodules/UIKit/include/SkiaCtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "include/core/SkSurface.h"
#include <functional>
#include "include/core/SkFontMgr.h"
#include <Geometry.h>

namespace NXKit {

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

#include "NXLayer.h"

namespace NXKit {

class UIView: public enable_shared_from_this<UIView> {
public:
UIView();

virtual void addSubview(std::shared_ptr<UIView> view);
void insertSubviewAt(std::shared_ptr<UIView> view, int index);
void insertSubviewBelow(std::shared_ptr<UIView> view, std::shared_ptr<UIView> belowSubview);
void removeFromSuperview();

// virtual std::shared_ptr<UIWindow> window();

const std::vector<std::shared_ptr<UIView>>& subviews() const { return _subviews; }
std::weak_ptr<UIView> superview() const { return _superview; }

std::shared_ptr<NXLayer> layer() const { return _layer; };
private:
std::vector<std::shared_ptr<UIView>> _subviews;
std::weak_ptr<UIView> _superview;
std::shared_ptr<NXLayer> _layer;

void setSuperview(std::shared_ptr<UIView> superview);
};

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

#pragma once

#include <memory>

namespace NXKit {

class enable_shared_from_this_pointer_holder {
public:
mutable std::shared_ptr<void> __strong_this_ctor_;
};

template <class Base>
class enable_shared_from_this: public enable_shared_from_this_pointer_holder {
public:
// mutable std::shared_ptr<Base> __strong_this_ctor_;
mutable std::weak_ptr<Base> __weak_this_;

enable_shared_from_this() {
__strong_this_ctor_ = std::shared_ptr<Base>(static_cast<Base*>(this));
__weak_this_ = std::static_pointer_cast<Base>(__strong_this_ctor_);
}


protected:
std::shared_ptr<Base> shared_from_this() {
return std::shared_ptr<Base>(__weak_this_);
}

std::weak_ptr<Base> weak_from_this() {
return __weak_this_;
}

template <class Derived>
std::shared_ptr<Derived> shared_from_base() {
return std::dynamic_pointer_cast<Derived>(this->shared_from_this());
}

template <class Derived>
std::weak_ptr<Derived> weak_from_base() {
if (!this->weak_from_this().expired()) {
return std::dynamic_pointer_cast<Derived>(this->weak_from_this().lock());
}
return std::weak_ptr<Derived>();
}
};

template<class _Tp, class ..._Args>
typename std::enable_if<std::is_base_of<enable_shared_from_this_pointer_holder, _Tp>::value, std::shared_ptr<_Tp>>::type new_shared(_Args&& ...__args) {
std::allocator<_Tp> alloc;
using traits_t = std::allocator_traits<decltype(alloc)>;
auto obj = alloc.allocate(1);
traits_t::construct(alloc, obj, std::forward<_Args>(__args)...);

auto ptr = std::static_pointer_cast<_Tp>(obj->__strong_this_ctor_);
obj->__strong_this_ctor_ = nullptr;
return ptr;
}

template<class _Tp, class ..._Args>
typename std::enable_if<!std::is_base_of<enable_shared_from_this_pointer_holder, _Tp>::value, std::shared_ptr<_Tp>>::type new_shared(_Args&& ...__args) {
auto res = std::make_shared<_Tp>(std::forward<_Args>(__args)...);
return res;
}

}
Loading

0 comments on commit c7bf11d

Please sign in to comment.