Skip to content

Commit

Permalink
RGB / RGBA builtins for the scripting API (#92)
Browse files Browse the repository at this point in the history
* RGB / RGBA builtins for the scripting API

* Rename GetRel.. to GetFloat..

* Remove deleted keys.h

* ColorRGBA inherits from ColorRGB

* Yeah Mr. White, Yeah Math
  • Loading branch information
Deewarz authored Jan 28, 2024
1 parent 50c3088 commit eb625a1
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
107 changes: 107 additions & 0 deletions code/framework/src/scripting/engines/node/builtins/color_rgb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* MafiaHub OSS license
* Copyright (c) 2021-2023, MafiaHub. All rights reserved.
*
* This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework.
* See LICENSE file in the source repository for information regarding licensing.
*/

#pragma once

#include <glm/glm.hpp>
#include <v8pp/class.hpp>
#include <v8pp/module.hpp>

#include <iomanip>
#include <list>
#include <sstream>

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

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

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

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

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

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

std::string ToString() const {
std::ostringstream ss;
ss << "RGB{ r: " << _data.r << ", g: " << _data.g << ", b: " << _data.b << " }";
return ss.str();
}

std::list<int> ToArray() const {
return {_data.r, _data.g, _data.b};
}

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

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

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

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

static ColorRGB FromVec4(const glm::vec4 &vec) {
return ColorRGB(static_cast<int>(vec.r * 255.0f), static_cast<int>(vec.g * 255.0f), static_cast<int>(vec.b * 255.0f));
}

static void Register(v8::Isolate *isolate, v8pp::module *rootModule) {
if (!rootModule) {
return;
}

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);
cls.function("div", &ColorRGB::Div);

rootModule->class_("RGB", cls);
}
};
} // namespace Framework::Scripting::Engines::Node::Builtins
89 changes: 89 additions & 0 deletions code/framework/src/scripting/engines/node/builtins/color_rgba.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* MafiaHub OSS license
* Copyright (c) 2021-2023, MafiaHub. All rights reserved.
*
* This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework.
* See LICENSE file in the source repository for information regarding licensing.
*/

#pragma once

#include <glm/glm.hpp>
#include <v8pp/class.hpp>
#include <v8pp/module.hpp>

#include <iomanip>
#include <list>
#include <sstream>

#include "color_rgb.h"

namespace Framework::Scripting::Engines::Node::Builtins {
class ColorRGBA: public ColorRGB {
public:
ColorRGBA(int r, int g, int b, int a): ColorRGB(r, g, b) {
_data.a = a;
}

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

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

std::string ToString() const {
std::ostringstream ss;
ss << "RGBA{ r: " << _data.r << ", g: " << _data.g << ", b: " << _data.b << ", a: " << _data.a << " }";
return ss.str();
}

std::list<int> ToArray() const {
return {_data.r, _data.g, _data.b, _data.a};
}

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

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

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

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

static ColorRGBA FromVec4(const glm::vec4 &vec) {
return ColorRGBA(static_cast<int>(vec.r * 255.0f), static_cast<int>(vec.g * 255.0f), static_cast<int>(vec.b * 255.0f), static_cast<int>(vec.a * 255.0f));
}

static void Register(v8::Isolate *isolate, v8pp::module *rootModule) {
if (!rootModule) {
return;
}

v8pp::class_<ColorRGBA> cls(isolate);
cls.inherit<ColorRGB>();
cls.ctor<int, int, int, int>();
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);
cls.function("div", &ColorRGBA::Div);

rootModule->class_("RGBA", cls);
}
};
} // namespace Framework::Scripting::Engines::Node::Builtins
4 changes: 4 additions & 0 deletions code/framework/src/scripting/engines/node/sdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <string>

#include "builtins/color_rgb.h"
#include "builtins/color_rgba.h"
#include "builtins/quaternion.h"
#include "builtins/vector_2.h"
#include "builtins/vector_3.h"
Expand Down Expand Up @@ -87,6 +89,8 @@ namespace Framework::Scripting::Engines::Node {
_module->function("emit", &Emit);

// Bind the builtins
Builtins::ColorRGB::Register(isolate, _module);
Builtins::ColorRGBA::Register(isolate, _module);
Builtins::Quaternion::Register(isolate, _module);
Builtins::Vector3::Register(isolate, _module);
Builtins::Vector2::Register(isolate, _module);
Expand Down

0 comments on commit eb625a1

Please sign in to comment.