Skip to content

Commit

Permalink
UIImageView
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 14, 2024
1 parent bc48a8f commit 3df5e14
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Submodules/UIKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ add_library(UIKit
lib/UIApplicationMain.cpp
lib/UIColor.cpp
lib/UIImage.cpp
lib/UIImageView.cpp
lib/UILabel.cpp
lib/UIView.cpp
lib/UIWindow.cpp
lib/NXAffineTransform.cpp
Expand Down
Empty file.
6 changes: 6 additions & 0 deletions Submodules/UIKit/include/CALayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

namespace NXKit {

class CALayerDelegate {
public:
virtual std::shared_ptr<CABasicAnimation> actionForKey(std::string event) = 0;
virtual void display(std::shared_ptr<CALayer> layer) = 0;
};

class CALayer: public enable_shared_from_this<CALayer> {
public:
CALayer();
Expand Down
2 changes: 1 addition & 1 deletion Submodules/UIKit/include/NXData.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class NXData {
NXData(const std::byte* bytes, std::size_t count, bool freeSource = false);
~NXData();

static std::optional<NXData> fromPath(const std::string& path);
static std::shared_ptr<NXData> fromPath(const std::string& path);
private:
std::vector<std::byte> _data;
};
Expand Down
20 changes: 17 additions & 3 deletions Submodules/UIKit/include/UIImage.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
#pragma once

#include <CGImage.h>
#include <NXData.h>
#include <SkiaCtx.h>

namespace NXKit {

class UIImage {
public:
UIImage();
UIImage(std::shared_ptr<CGImage> cgImage, NXFloat scale);

static std::shared_ptr<UIImage> fromPath(std::string path);
static std::shared_ptr<UIImage> fromData(std::shared_ptr<NXData> data, NXFloat scale = SkiaCtx::main()->getScaleFactor());

std::shared_ptr<CGImage> cgImage() { return _cgImage; }
NXSize size() const { return _size; }
NXFloat scale() const { return _scale; }

private:
std::shared_ptr<CGImage> _cgImage;
NXSize _size;
NXFloat _scale;
};

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

#include <UIView.h>
#include <UIImage.h>

namespace NXKit {

class UIImageView: public UIView {
public:
static std::shared_ptr<UIImageView> init();

UIImageView(std::shared_ptr<UIImage> image = nullptr);
UIImageView(NXRect frame);

void setImage(std::shared_ptr<UIImage> image);
std::shared_ptr<UIImage> image() { return _image; }

// void sizeToFit() override;
// Size sizeThatFits(Size size) override;
// bool applyXMLAttribute(std::string name, std::string value) override;

private:
std::shared_ptr<UIImage> _image;
void updateTextureFromImage();
};

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

#include <UIView.h>

namespace NXKit {

class UILabel: public UIView {
public:
private:
int _numberOfLines = 1;
std::string _text;
UIColor _textColor = UIColor::black;
};

}
30 changes: 21 additions & 9 deletions Submodules/UIKit/include/UIView.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
#pragma once

#include "CALayer.h"
#include <UIViewContentMode.h>

namespace NXKit {

class UIView: public enable_shared_from_this<UIView> {
public:
UIView();
UIView(): UIView(NXRect()) {}
UIView(NXRect frame);

void setFrame(NXRect frame);
NXRect frame() const { return _layer->getFrame(); }
[[nodiscard]] NXRect frame() const { return _layer->getFrame(); }

void setBounds(NXRect bounds);
NXRect bounds() const { return _layer->bounds(); }
[[nodiscard]] NXRect bounds() const { return _layer->bounds(); }

void setCenter(NXPoint position);
NXPoint center() const;
[[nodiscard]] NXPoint center() const;

void setAlpha(NXFloat alpha) { _layer->setOpacity(alpha); }
NXFloat alpha() const { return _layer->opacity(); }
[[nodiscard]] NXFloat alpha() const { return _layer->opacity(); }

void setHidden(bool hidden) { _layer->setHidden(hidden); }
bool isHidden() const { return _layer->isHidden(); }
[[nodiscard]] bool isHidden() const { return _layer->isHidden(); }

void setClipsToBounds(bool clipsToBounds) { _layer->setMasksToBounds(clipsToBounds); }
bool clipsToBounds() const { return _layer->masksToBounds(); }
[[nodiscard]] bool clipsToBounds() const { return _layer->masksToBounds(); }

void setTransform(NXAffineTransform transform) { _layer->setAffineTransform(transform); }
NXAffineTransform transform() const { return _layer->affineTransform(); }
[[nodiscard]] NXAffineTransform transform() const { return _layer->affineTransform(); }

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

void setUserInteractionEnabled(bool isUserInteractionEnabled) { _isUserInteractionEnabled = isUserInteractionEnabled; }
[[nodiscard]] bool isUserInteractionEnabled() const { return _isUserInteractionEnabled; }

void setContentMode(UIViewContentMode mode);
[[nodiscard]] UIViewContentMode contentMode() const { return _contentMode; }

virtual void addSubview(std::shared_ptr<UIView> view);
void insertSubviewAt(std::shared_ptr<UIView> view, int index);
Expand All @@ -42,11 +50,15 @@ class UIView: public enable_shared_from_this<UIView> {
const std::vector<std::shared_ptr<UIView>>& subviews() const { return _subviews; }
std::weak_ptr<UIView> superview() const { return _superview; }

virtual std::shared_ptr<CALayer> initLayer();
std::shared_ptr<CALayer> layer() const { return _layer; };
private:
std::vector<std::shared_ptr<UIView>> _subviews;
std::weak_ptr<UIView> _superview;
std::shared_ptr<CALayer> _layer;
UIViewContentMode _contentMode;

bool _isUserInteractionEnabled = true;

void setSuperview(std::shared_ptr<UIView> superview);
};
Expand Down
33 changes: 33 additions & 0 deletions Submodules/UIKit/include/UIViewContentMode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

namespace NXKit {

enum class UIViewContentMode {
scaleToFill = 0,

scaleAspectFit = 1, // contents scaled to fit with fixed aspect. remainder is transparent

scaleAspectFill = 2, // contents scaled to fill with fixed aspect. some portion of content may be clipped.

redraw = 3, // redraw on bounds change (calls -setNeedsDisplay)

center = 4, // contents remain same size. positioned adjusted.

top = 5,

bottom = 6,

left = 7,

right = 8,

topLeft = 9,

topRight = 10,

bottomLeft = 11,

bottomRight = 12,
};

}
35 changes: 28 additions & 7 deletions Submodules/UIKit/lib/CALayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,38 @@ void CALayer::removeFromSuperlayer() {
}

void CALayer::skiaRender(SkCanvas* canvas) {
// Do not render is hidden
if (_isHidden || _opacity < 0.001f) return;

// Initial save 1
canvas->save();

auto matrix = CATransform3DMakeTranslation(_position.x, _position.y, _zPosition)
.concat(_transform);

// Set Origin matrix
canvas->concat(matrix.toSkM44());

// Masks To Bounds
if (_masksToBounds) {
SkRect rect = SkRect::MakeXYWH(0, 0, _bounds.width(), _bounds.height());
float radii = _cornerRadius;
SkVector corners[] = {{radii, radii}, {radii, radii}, {radii, radii}, {radii, radii}};
SkRRect rrect;
rrect.setRectRadii(rect, corners);
canvas->clipRRect(rrect, true);
}

// Origin matrix save 2
canvas->save();

// Set Anchor matrix
canvas->concat(CATransform3DMakeTranslation(-_bounds.width() * _anchorPoint.x, -_bounds.height() * _anchorPoint.y, 0).toSkM44());

SkPaint paint;
paint.setAntiAlias(true);
SkRect rect = SkRect::MakeXYWH(0, 0, _bounds.width(), _bounds.height());

// Opacity enable
// Opacity save 3
if (_opacity < 1) {
canvas->saveLayerAlphaf(nullptr, _opacity);
}
Expand All @@ -158,21 +173,23 @@ void CALayer::skiaRender(SkCanvas* canvas) {
if (_backgroundColor.has_value()) {
paint.setColor(_backgroundColor->color);

SkRect rect = SkRect::MakeXYWH(0, 0, _bounds.width(), _bounds.height());
SkRRect rrect;
float radii = _cornerRadius;
SkVector corners[] = {{radii, radii}, {radii, radii}, {radii, radii}, {radii, radii}};
rrect.setRectRadii(rect, corners);
canvas->drawRRect(rrect, paint);
}

// Content
// Contents
if (_contents) {
auto contentsGravity = ContentsGravityTransformation(this);
auto width = _contents->size().width * contentsGravity.scale.width / _contentsScale;
auto height = _contents->size().height * contentsGravity.scale.height / _contentsScale;
auto x = (_bounds.size.width - width) / 2.0 + contentsGravity.offset.x;
auto y = (_bounds.size.height - height) / 2.0 + contentsGravity.offset.y;

// Contents maxrix save 4
canvas->save();
canvas->translate(x, y);

Expand All @@ -183,19 +200,23 @@ void CALayer::skiaRender(SkCanvas* canvas) {
float(height)
}, SkSamplingOptions(), nullptr);

// Contents maxrix save 4 // restore
canvas->restore();
}

// Opacity save 3 // restore
if (_opacity < 1) {
canvas->restore();
}

// Reset Anchor to Origin matrix
// Origin matrix save 2 // restore
canvas->restore();
for (const auto& sublayer: _sublayers) {
sublayer->skiaRender(canvas);
}
// Opacity disable
if (_opacity < 1) {
canvas->restore();
}

// Initial save 1 // restore
canvas->restore();
}

Expand Down
6 changes: 3 additions & 3 deletions Submodules/UIKit/lib/NXData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const std::byte* NXData::data() const {
return _data.data();
}

std::optional<NXData> NXData::fromPath(const std::string& path) {
std::shared_ptr<NXData> NXData::fromPath(const std::string& path) {
//#ifdef USE_LIBROMFS
// auto file = romfs::get(path);
// auto fileReader = SDL_RWFromConstMem(file.data(), (int) file.size());
Expand All @@ -45,10 +45,10 @@ std::optional<NXData> NXData::fromPath(const std::string& path) {
fileReader->close(fileReader);

if (bytesRead == fileSize) {
return NXData(buffer, fileSize, true);
return make_shared<NXData>(buffer, fileSize, true);
} else {
delete[] buffer;
return std::nullopt;
return nullptr;
}
}

Expand Down
16 changes: 14 additions & 2 deletions Submodules/UIKit/lib/UIImage.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#include "UIImage.h"
#include <tools/SharedBase.hpp>

using namespace NXKit;

UIImage::UIImage() {

UIImage::UIImage(std::shared_ptr<CGImage> cgImage, NXFloat scale):
_cgImage(cgImage), _scale(scale), _size(cgImage->size())
{ }

std::shared_ptr<UIImage> UIImage::fromPath(std::string path) {
auto imageData = NXData::fromPath(path);
if (!imageData) { return nullptr; }
return fromData(imageData);
}

std::shared_ptr<UIImage> UIImage::fromData(std::shared_ptr<NXData> data, NXFloat scale) {
auto image = new_shared<CGImage>(data);
return new_shared<UIImage>(image, scale);
}
Loading

0 comments on commit 3df5e14

Please sign in to comment.