Skip to content

Commit

Permalink
Respect the Liskov substitution principle for RGB and RGBA (#109)
Browse files Browse the repository at this point in the history
Also: Consider RGB, RGBA, Vector2, Vector3 and Quaternion as final classes
  • Loading branch information
Deewarz committed Feb 22, 2024
1 parent 1d69793 commit 3badf24
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
19 changes: 11 additions & 8 deletions code/framework/src/scripting/engines/node/builtins/color_rgb.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
#include <sstream>

namespace Framework::Scripting::Engines::Node::Builtins {
class ColorRGB {
protected:
glm::ivec4 _data;
class ColorRGB final {
private:
glm::ivec3 _data;

public:
ColorRGB(int r, int g, int b) {
_data = {r, g, b, 255};
_data = {r, g, b};
}

int GetR() const {
Expand Down Expand Up @@ -61,22 +61,22 @@ namespace Framework::Scripting::Engines::Node::Builtins {
}

void Add(int r, int g, int b) {
const glm::ivec4 newVec(r, g, b, 0);
const glm::ivec3 newVec(r, g, b);
_data += newVec;
}

void Sub(int r, int g, int b) {
const glm::ivec4 newVec(r, g, b, 0);
const glm::ivec3 newVec(r, g, b);
_data -= newVec;
}

void Mul(int r, int g, int b) {
const glm::ivec4 newVec(r, g, b, 1);
const glm::ivec3 newVec(r, g, b);
_data *= newVec;
}

void Div(int r, int g, int b) {
const glm::ivec4 newVec(r, g, b, 1);
const glm::ivec3 newVec(r, g, b);
_data /= newVec;
}

Expand All @@ -91,11 +91,14 @@ namespace Framework::Scripting::Engines::Node::Builtins {

v8pp::class_<ColorRGB> cls(isolate);
cls.ctor<int, int, int>();

cls.property("r", &ColorRGB::GetR);
cls.property("g", &ColorRGB::GetG);
cls.property("b", &ColorRGB::GetB);

cls.function("toString", &ColorRGB::ToString);
cls.function("toArray", &ColorRGB::ToArray);

cls.function("add", &ColorRGB::Add);
cls.function("sub", &ColorRGB::Sub);
cls.function("mul", &ColorRGB::Mul);
Expand Down
42 changes: 36 additions & 6 deletions code/framework/src/scripting/engines/node/builtins/color_rgba.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,44 @@
#include <list>
#include <sstream>

#include "color_rgb.h"

namespace Framework::Scripting::Engines::Node::Builtins {
class ColorRGBA: public ColorRGB {
class ColorRGBA final {
private:
glm::ivec4 _data;

public:
ColorRGBA(int r, int g, int b, int a): ColorRGB(r, g, b) {
_data.a = a;
ColorRGBA(int r, int g, int b, int a) {
_data = {r, g, b, a};
}

int GetR() const {
return _data.r;
}

int GetG() const {
return _data.g;
}

int GetB() const {
return _data.b;
}

int GetA() const {
return _data.a;
}

float GetFloatR() const {
return static_cast<float>(_data.r) / 255.0f;
}

float GetFloatG() const {
return static_cast<float>(_data.g) / 255.0f;
}

float GetFloatB() const {
return static_cast<float>(_data.b) / 255.0f;
}

float GetFloatA() const {
return static_cast<float>(_data.a) / 255.0f;
}
Expand Down Expand Up @@ -73,11 +98,16 @@ namespace Framework::Scripting::Engines::Node::Builtins {
}

v8pp::class_<ColorRGBA> cls(isolate);
cls.inherit<ColorRGB>();
cls.ctor<int, int, int, int>();

cls.property("r", &ColorRGBA::GetR);
cls.property("g", &ColorRGBA::GetG);
cls.property("b", &ColorRGBA::GetB);
cls.property("a", &ColorRGBA::GetA);

cls.function("toString", &ColorRGBA::ToString);
cls.function("toArray", &ColorRGBA::ToArray);

cls.function("add", &ColorRGBA::Add);
cls.function("sub", &ColorRGBA::Sub);
cls.function("mul", &ColorRGBA::Mul);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <v8pp/module.hpp>

namespace Framework::Scripting::Engines::Node::Builtins {
class Quaternion {
class Quaternion final {
private:
glm::quat _data;

Expand Down Expand Up @@ -118,13 +118,16 @@ namespace Framework::Scripting::Engines::Node::Builtins {

v8pp::class_<Quaternion> cls(isolate);
cls.ctor<float, float, float, float>();

cls.property("w", &Quaternion::GetW);
cls.property("x", &Quaternion::GetX);
cls.property("y", &Quaternion::GetY);
cls.property("z", &Quaternion::GetZ);
cls.property("length", &Quaternion::GetLength);

cls.function("toString", &Quaternion::ToString);
cls.function("toArray", &Quaternion::ToArray);

cls.function("add", &Quaternion::Add);
cls.function("sub", &Quaternion::Sub);
cls.function("mul", &Quaternion::Mul);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <sstream>

namespace Framework::Scripting::Engines::Node::Builtins {
class Vector2 {
class Vector2 final {
private:
glm::vec2 _data;

Expand Down Expand Up @@ -80,11 +80,14 @@ namespace Framework::Scripting::Engines::Node::Builtins {

v8pp::class_<Vector2> cls(isolate);
cls.ctor<double, double>();

cls.property("x", &Vector2::GetX);
cls.property("y", &Vector2::GetY);
cls.property("length", &Vector2::GetLength);

cls.function("toString", &Vector2::ToString);
cls.function("toArray", &Vector2::ToArray);

cls.function("add", &Vector2::Add);
cls.function("sub", &Vector2::Sub);
cls.function("mul", &Vector2::Mul);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <sstream>

namespace Framework::Scripting::Engines::Node::Builtins {
class Vector3 {
class Vector3 final {
private:
glm::vec3 _data;

Expand Down Expand Up @@ -84,12 +84,15 @@ namespace Framework::Scripting::Engines::Node::Builtins {

v8pp::class_<Vector3> cls(isolate);
cls.ctor<double, double, double>();

cls.property("x", &Vector3::GetX);
cls.property("y", &Vector3::GetY);
cls.property("z", &Vector3::GetZ);
cls.property("length", &Vector3::GetLength);

cls.function("toString", &Vector3::ToString);
cls.function("toArray", &Vector3::ToArray);

cls.function("add", &Vector3::Add);
cls.function("sub", &Vector3::Sub);
cls.function("mul", &Vector3::Mul);
Expand Down

0 comments on commit 3badf24

Please sign in to comment.