Started canvas component work, and moving to unique network pointers (build is broken)

This commit is contained in:
Yousef Khadadeh 2025-12-11 16:05:41 +03:00
parent d3d080620a
commit 5b5aef8369
33 changed files with 750 additions and 468 deletions

View file

View file

@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
#include <cereal/archives/portable_binary.hpp>
#include <Helpers/VersionNumber.hpp>
class CanvasComponent {
public:
enum class CompType : uint8_t {
BRUSHSTROKE = 0,
RECTANGLE,
ELLIPSE,
TEXTBOX,
IMAGE
};
protected:
virtual void write_update() const;
virtual void read_update();
virtual void save_file(cereal::PortableBinaryOutputArchive& a) const;
virtual void load_file(cereal::PortableBinaryInputArchive& a, VersionNumber version);
};

View file

@ -0,0 +1,62 @@
#pragma once
#include "CanvasComponent.hpp"
#include "Helpers/SCollision.hpp"
#include <Helpers/Serializers.hpp>
#include <include/core/SkPath.h>
#include <include/core/SkPathBuilder.h>
struct DrawBrushStrokePoint {
Vector2f pos;
float width;
template <typename Archive> void serialize(Archive& a) {
a(pos, width);
}
};
class CanvasComponentBrushStroke : public CanvasComponent {
public:
constexpr static float DRAW_MINIMUM_LIMIT = 0.3;
virtual DrawComponentType get_type() const override;
virtual void write_update(cereal::PortableBinaryOutputArchive& a) const override;
virtual void read_update(cereal::PortableBinaryInputArchive& a) override;
virtual void save_file(cereal::PortableBinaryOutputArchive& a) const override;
virtual void load_file(cereal::PortableBinaryInputArchive& a, VersionNumber version) override;
struct Data {
std::vector<DrawBrushStrokePoint> points;
Vector4f color;
bool hasRoundCaps;
};
std::shared_ptr<Data> d = std::make_shared<Data>(); // It's a pointer here since brush strokes cant be edited
#ifndef IS_SERVER
virtual std::shared_ptr<DrawComponent> copy(DrawingProgram& drawP) const override;
virtual std::shared_ptr<DrawComponent> deep_copy(DrawingProgram& drawP) const override;
virtual void draw(SkCanvas* canvas, const DrawData& drawData) override;
virtual void initialize_draw_data(DrawingProgram& drawP) override;
virtual void update(DrawingProgram& drawP) override;
virtual bool collides_within_coords(const SCollision::ColliderCollection<float>& checkAgainst) override;
void create_collider();
virtual void update_from_delayed_ptr(const std::shared_ptr<DrawComponent>& delayedUpdatePtr) override;
virtual SCollision::AABB<float> get_obj_coord_bounds() const override;
std::shared_ptr<SkPath> brushPath;
SCollision::AABB<float> bounds;
std::array<std::shared_ptr<SkPath>, 2> brushPathLOD;
private:
std::vector<DrawBrushStrokePoint> every_nth_point_include_front_and_back(const std::vector<DrawBrushStrokePoint>& pts, size_t n);
void add_precheck_aabb_level(size_t level, const std::vector<SCollision::BVHContainer<float>>& levelArray);
std::vector<SCollision::AABB<float>> precheckAABBLevels;
void create_triangles(const std::function<bool(Vector2f, Vector2f, Vector2f)>& passTriangleFunc, const std::vector<DrawBrushStrokePoint>& smoothedPoints, size_t skipVertexCount, std::shared_ptr<SkPathBuilder> bPath);
std::vector<size_t> get_wedge_indices(const std::vector<DrawBrushStrokePoint>& points);
std::vector<DrawBrushStrokePoint> smooth_points(size_t beginIndex, size_t endIndex, unsigned numOfDivisions);
std::vector<DrawBrushStrokePoint> smooth_points_avg(size_t beginIndex, size_t endIndex, unsigned numOfDivisions);
#endif
};

View file

@ -0,0 +1,41 @@
#include "CanvasComponentContainer.hpp"
CanvasComponentContainer::CanvasComponentContainer() { }
CanvasComponentContainer::CanvasComponentContainer(CanvasComponent::CompType type) {
allocate_comp(type);
}
void CanvasComponentContainer::register_class(NetworkingObjects::NetObjManagerTypeList& t) {
t.register_class<CanvasComponentContainer, CanvasComponentContainer, CanvasComponentContainer, CanvasComponentContainer>({
});
}
std::unique_ptr<CanvasComponent> CanvasComponentContainer::allocate_comp(CanvasComponent::CompType type) {
switch(type) {
case CanvasComponent::CompType::BRUSHSTROKE:
return std::make_unique<CanvasComponentBrushStroke>();
case CanvasComponent::CompType::RECTANGLE:
return std::make_unique<CanvasComponentRectangle>();
case CanvasComponent::CompType::ELLIPSE:
return std::make_unique<CanvasComponentEllipse>();
case CanvasComponent::CompType::TEXTBOX:
return std::make_unique<CanvasComponentTextBox>();
case CanvasComponent::CompType::IMAGE:
return std::make_unique<CanvasComponentImage>();
}
}
void CanvasComponentContainer::save_file(cereal::PortableBinaryOutputArchive& a) const {
}
void CanvasComponentContainer::load_file(cereal::PortableBinaryInputArchive& a, VersionNumber version) {
}
const std::unique_ptr<CanvasComponent>& CanvasComponentContainer::get_comp() {
return comp;
}
const CoordSpaceHelper& CanvasComponentContainer::get_coords() {
return coords;
}

View file

@ -0,0 +1,20 @@
#pragma once
#include "../CoordSpaceHelper.hpp"
#include "CanvasComponent.hpp"
#include <Helpers/NetworkingObjects/NetObjManagerTypeList.hpp>
#include "../VersionConstants.hpp"
class CanvasComponentContainer {
public:
CanvasComponentContainer();
CanvasComponentContainer(CanvasComponent::CompType type);
static void register_class(NetworkingObjects::NetObjManagerTypeList& t);
void save_file(cereal::PortableBinaryOutputArchive& a) const;
void load_file(cereal::PortableBinaryInputArchive& a, VersionNumber version);
const std::unique_ptr<CanvasComponent>& get_comp();
const CoordSpaceHelper& get_coords();
private:
std::unique_ptr<CanvasComponent> allocate_comp(CanvasComponent::CompType type);
std::unique_ptr<CanvasComponent> comp;
CoordSpaceHelper coords;
};