diff options
author | rtk0c <[email protected]> | 2021-05-12 13:23:56 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-05-12 13:34:43 -0700 |
commit | 765df313e065f8401319c68ba70cd41b0bc34c9d (patch) | |
tree | 44e0c781ed9f5ea0f98ac906e96c677d04befa27 /core/src/Utils | |
parent | 6ec8cc216282396ece535941ea6ca4a63d924e8f (diff) |
Start to work on actually rendering the node graph
Diffstat (limited to 'core/src/Utils')
-rw-r--r-- | core/src/Utils/Color.hpp | 191 | ||||
-rw-r--r-- | core/src/Utils/Enum.hpp | 140 | ||||
-rw-r--r-- | core/src/Utils/Macros.hpp | 2 | ||||
-rw-r--r-- | core/src/Utils/Vector.hpp | 63 | ||||
-rw-r--r-- | core/src/Utils/fwd.hpp | 14 |
5 files changed, 262 insertions, 148 deletions
diff --git a/core/src/Utils/Color.hpp b/core/src/Utils/Color.hpp new file mode 100644 index 0000000..26cfdd4 --- /dev/null +++ b/core/src/Utils/Color.hpp @@ -0,0 +1,191 @@ +#pragma once
+
+#include "Utils/Vector.hpp"
+#include "Utils/fwd.hpp"
+
+#include <imgui.h>
+#include <algorithm>
+#include <cstdint>
+#include <limits>
+
+class RgbaColor
+{
+public:
+ uint8_t r;
+ uint8_t g;
+ uint8_t b;
+ uint8_t a;
+
+public:
+ constexpr RgbaColor() noexcept
+ : r{ 255 }
+ , g{ 255 }
+ , b{ 255 }
+ , a{ 255 }
+ {
+ }
+
+ constexpr RgbaColor(float r, float g, float b, float a = 1.0f) noexcept
+ : r{ static_cast<uint8_t>(r * 255.0f) }
+ , g{ static_cast<uint8_t>(g * 255.0f) }
+ , b{ static_cast<uint8_t>(b * 255.0f) }
+ , a{ static_cast<uint8_t>(a * 255.0f) }
+ {
+ }
+
+ constexpr RgbaColor(int r, int g, int b, int a = 255) noexcept
+ : r{ static_cast<uint8_t>(r & 0xFF) }
+ , g{ static_cast<uint8_t>(g & 0xFF) }
+ , b{ static_cast<uint8_t>(b & 0xFF) }
+ , a{ static_cast<uint8_t>(a & 0xFF) }
+ {
+ }
+
+ constexpr RgbaColor(uint32_t rgba) noexcept
+ : r{ static_cast<uint8_t>((rgba >> 0) & 0xFF) }
+ , g{ static_cast<uint8_t>((rgba >> 8) & 0xFF) }
+ , b{ static_cast<uint8_t>((rgba >> 16) & 0xFF) }
+ , a{ static_cast<uint8_t>((rgba >> 24) & 0xFF) }
+ {
+ }
+
+ constexpr uint32_t GetScalar() const noexcept
+ {
+ uint32_t res = 0;
+ res |= r << 24;
+ res |= g << 16;
+ res |= b << 8;
+ res |= a;
+ return res;
+ }
+
+ constexpr void SetScalar(uint32_t scalar) noexcept
+ {
+ r = (scalar >> 0) & 0xFF;
+ g = (scalar >> 8) & 0xFF;
+ b = (scalar >> 16) & 0xFF;
+ a = (scalar >> 24) & 0xFF;
+ }
+
+ constexpr float GetNormalizedRed() const noexcept
+ {
+ return r / 255.0f;
+ }
+
+ constexpr float GetNormalizedGreen() const noexcept
+ {
+ return g / 255.0f;
+ }
+
+ constexpr float GetNormalizedBlue() const noexcept
+ {
+ return b / 255.0f;
+ }
+
+ constexpr float GetNormalizedAlpha() const noexcept
+ {
+ return a / 255.0f;
+ }
+
+ constexpr Vec4i AsVec4i() const noexcept
+ {
+ return Vec4i{ r, g, b, a };
+ }
+
+ constexpr Vec4f AsVec4f() const noexcept
+ {
+ return Vec4f{
+ GetNormalizedRed(),
+ GetNormalizedGreen(),
+ GetNormalizedBlue(),
+ GetNormalizedAlpha(),
+ };
+ }
+
+ ImVec4 AsImVec() const
+ {
+ auto v = AsVec4f();
+ return ImVec4{ v.x, v.y, v.z, v.w };
+ }
+
+ ImColor AsImColor() const
+ {
+ auto v = AsVec4f();
+ return ImColor{ v.x, v.y, v.z, v.w };
+ }
+
+ constexpr void SetVec(const Vec4f& vec) noexcept
+ {
+ r = (uint8_t)(vec.x * 255.0f);
+ g = (uint8_t)(vec.y * 255.0f);
+ b = (uint8_t)(vec.z * 255.0f);
+ a = (uint8_t)(vec.w * 255.0f);
+ }
+
+ // Forward declaring because cyclic reference between RgbaColor and HsvColor
+ constexpr HsvColor ToHsv() const noexcept;
+
+ friend constexpr bool operator==(const RgbaColor&, const RgbaColor&) noexcept = default;
+};
+
+class HsvColor
+{
+public:
+ float h;
+ float s;
+ float v;
+ float a;
+
+public:
+ constexpr HsvColor() noexcept
+ : h{ 0.0f }
+ , s{ 0.0f }
+ , v{ 1.0f }
+ , a{ 1.0f }
+ {
+ }
+
+ constexpr HsvColor(float h, float s, float v, float a) noexcept
+ : h{ h }
+ , s{ s }
+ , v{ v }
+ , a{ a }
+ {
+ }
+
+ // Forward declaring because cyclic reference between RgbaColor and HsvColor
+ constexpr RgbaColor ToRgba() const noexcept;
+};
+
+constexpr HsvColor RgbaColor::ToHsv() const noexcept
+{
+ float fr = GetNormalizedRed();
+ float fg = GetNormalizedBlue();
+ float fb = GetNormalizedGreen();
+ float fa = GetNormalizedAlpha();
+
+ auto p = fg < fb ? ImVec4(fb, fg, -1, 2.0f / 3.0f) : ImVec4(fg, fb, 0, -1.0f / 3.0f);
+ auto q = fr < p.x ? ImVec4(p.x, p.y, p.w, fr) : ImVec4(fr, p.y, p.z, p.x);
+ float c = q.x - std::min(q.w, q.y);
+ float h = std::abs((q.w - q.y) / (6 * c + std::numeric_limits<float>::epsilon()) + q.z);
+
+ Vec3f hcv{ h, c, q.x };
+ float s = hcv.y / (hcv.z + std::numeric_limits<float>::epsilon());
+ return HsvColor(hcv.x, s, hcv.z, fa);
+}
+
+constexpr RgbaColor HsvColor::ToRgba() const noexcept
+{
+ float r = std::abs(h * 6 - 3) - 1;
+ float g = 2 - std::abs(h * 6 - 2);
+ float b = 2 - std::abs(h * 6 - 4);
+
+ auto rgb = Vec3f{
+ std::clamp(r, 0.0f, 1.0f),
+ std::clamp(g, 0.0f, 1.0f),
+ std::clamp(b, 0.0f, 1.0f),
+ };
+ auto vc = (rgb - Vec3f{ 0, 0, 0 }) * s + Vec3f{ 1, 1, 1 } * v;
+
+ return RgbaColor(vc.x, vc.y, vc.z, a);
+}
diff --git a/core/src/Utils/Enum.hpp b/core/src/Utils/Enum.hpp deleted file mode 100644 index 1fb9661..0000000 --- a/core/src/Utils/Enum.hpp +++ /dev/null @@ -1,140 +0,0 @@ -#pragma once - -#include <compare> - -template <class TSelf> -struct BasicEnum -{ - using Self = TSelf; - using ValueType = int; - - int value; - - BasicEnum() - : value{ 0 } {} - BasicEnum(int value) - : value{ value } {} - - /// Comparison between 2 values of enum. Useful for situations like `a == b` where both a and b are TSelf. - friend auto operator<=>(const TSelf& a, const TSelf& b) - { - return a.value <=> b.value; - } - - /// Comparison between a enum and a raw value. Useful for situations like `a == TSelf::Option` where a is a enum and TSelf::Option is a raw value. - friend auto operator<=>(const TSelf& self, int value) - { - return self.value <=> value; - } - - operator int() const - { - return value; - } - - operator bool() const - { - return value; - } -}; - -#define ENUM(Name) struct Name : public BasicEnum<Name> -#define ENUM_MEMBERS() \ - enum Enum : int; \ - operator Enum() const \ - { \ - return (Enum)value; \ - } \ - using BasicEnum<Self>::BasicEnum; \ - enum Enum : int - -template <class TSelf> -struct BasicFlag -{ - using Self = TSelf; - using ValueType = int; - - int value; - - BasicFlag() - : value{ 0 } {} - BasicFlag(int value) - : value{ value } {} - - bool IsSet(TSelf mask) const - { - return (value & mask.value) == mask.value; - } - bool IsSetExclusive(TSelf mask) const - { - return value == mask.value; - } - void Set(TSelf mask, bool state) - { - if (state) { - value = (int)(value | mask.value); - } else { - value = (int)(value & ~mask.value); - } - } - - /// Comparsion between 2 values of flag. Useful for situations like `a == b` where both a and b are TSelf. - friend bool operator==(const TSelf& a, const TSelf& b) - { - return a.value == b.value; - } - - friend auto operator<=>(const TSelf& a, const TSelf& b) - { - return a.value <=> b.value; - } - - /// Comparsion between a flag and a raw value. Useful for situations like `a == TSelf::Option` where a is a flag and TSelf::Option is a raw value. - friend bool operator==(const TSelf& self, int value) - { - return self.value == value; - } - - friend auto operator<=>(const TSelf& self, int value) - { - return self.value <=> value; - } - - friend TSelf operator&(const TSelf& a, const TSelf& b) - { - return TSelf(a.value & b.value); - } - - friend TSelf operator|(const TSelf& a, const TSelf& b) - { - return TSelf(a.value | b.value); - } - - friend TSelf operator^(const TSelf& a, const TSelf& b) - { - return TSelf(a.value ^ b.value); - } - - TSelf operator~() const - { - return TSelf(~value); - } - - TSelf& operator&=(int that) - { - value = value & that; - return *this; - } - - TSelf& operator|=(int that) - { - value = value | that; - return *this; - } - - TSelf& operator^=(int that) - { - value = value ^ that; - return *this; - } -}; diff --git a/core/src/Utils/Macros.hpp b/core/src/Utils/Macros.hpp index 658aebf..68b93fb 100644 --- a/core/src/Utils/Macros.hpp +++ b/core/src/Utils/Macros.hpp @@ -4,3 +4,5 @@ #define CONCAT(a, b) CONCAT_IMPL(a, b) #define UNIQUE_NAME(prefix) CONCAT(prefix, __COUNTER__) +#define UNIQUE_NAME_LINE(prefix) CONCAT(prefix, __LINE__) +#define DISCARD UNIQUE_NAME(_discard) diff --git a/core/src/Utils/Vector.hpp b/core/src/Utils/Vector.hpp index 61fa520..bf75fd1 100644 --- a/core/src/Utils/Vector.hpp +++ b/core/src/Utils/Vector.hpp @@ -1,7 +1,64 @@ #pragma once -struct Vec2i +template <class T> +struct Vec2 { - int x = 0; - int y = 0; + T x = 0; + T y = 0; + + friend constexpr Vec2 operator+(const Vec2& a, const Vec2& b) { return { a.x + b.x, a.y + b.y }; } + friend constexpr Vec2 operator-(const Vec2& a, const Vec2& b) { return { a.x - b.x, a.y - b.y }; } + friend constexpr Vec2 operator*(const Vec2& a, const Vec2& b) { return { a.x * b.x, a.y * b.y }; } + friend constexpr Vec2 operator/(const Vec2& a, const Vec2& b) { return { a.x / b.x, a.y / b.y }; } + + friend constexpr Vec2 operator+(const Vec2& a, T n) { return { a.x + n, a.y + n }; } + friend constexpr Vec2 operator-(const Vec2& a, T n) { return { a.x - n, a.y - n }; } + friend constexpr Vec2 operator*(const Vec2& a, T n) { return { a.x * n, a.y * n }; } + friend constexpr Vec2 operator/(const Vec2& a, T n) { return { a.x / n, a.y / n }; } +}; + +using Vec2i = Vec2<int>; +using Vec2f = Vec2<float>; + +template <class T> +struct Vec3 +{ + T x = 0; + T y = 0; + T z = 0; + + friend constexpr Vec3 operator+(const Vec3& a, const Vec3& b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; } + friend constexpr Vec3 operator-(const Vec3& a, const Vec3& b) { return { a.x - b.x, a.y - b.y, a.z - b.z }; } + friend constexpr Vec3 operator*(const Vec3& a, const Vec3& b) { return { a.x * b.x, a.y * b.y, a.z * b.z }; } + friend constexpr Vec3 operator/(const Vec3& a, const Vec3& b) { return { a.x / b.x, a.y / b.y, a.z / b.z }; } + + friend constexpr Vec3 operator+(const Vec3& a, T n) { return { a.x + n, a.y + n, a.z + n }; } + friend constexpr Vec3 operator-(const Vec3& a, T n) { return { a.x - n, a.y - n, a.z - n }; } + friend constexpr Vec3 operator*(const Vec3& a, T n) { return { a.x * n, a.y * n, a.z * n }; } + friend constexpr Vec3 operator/(const Vec3& a, T n) { return { a.x / n, a.y / n, a.z / n }; } }; + +using Vec3i = Vec3<int>; +using Vec3f = Vec3<float>; + +template <class T> +struct Vec4 +{ + T x = 0; + T y = 0; + T z = 0; + T w = 0; + + friend constexpr Vec4 operator+(const Vec4& a, const Vec4& b) { return { a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w }; } + friend constexpr Vec4 operator-(const Vec4& a, const Vec4& b) { return { a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w }; } + friend constexpr Vec4 operator*(const Vec4& a, const Vec4& b) { return { a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w }; } + friend constexpr Vec4 operator/(const Vec4& a, const Vec4& b) { return { a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w }; } + + friend constexpr Vec4 operator+(const Vec4& a, T n) { return { a.x + n, a.y + n, a.z + n, a.w + n }; } + friend constexpr Vec4 operator-(const Vec4& a, T n) { return { a.x - n, a.y - n, a.z - n, a.w - n }; } + friend constexpr Vec4 operator*(const Vec4& a, T n) { return { a.x * n, a.y * n, a.z * n, a.w * n }; } + friend constexpr Vec4 operator/(const Vec4& a, T n) { return { a.x / n, a.y / n, a.z / n, a.w / n }; } +}; + +using Vec4i = Vec4<int>; +using Vec4f = Vec4<float>; diff --git a/core/src/Utils/fwd.hpp b/core/src/Utils/fwd.hpp index 58b2991..74e642d 100644 --- a/core/src/Utils/fwd.hpp +++ b/core/src/Utils/fwd.hpp @@ -1,10 +1,8 @@ #pragma once -// Sigslot.hpp -class SignalStub; -template <class... TArgs> -class Signal; -class SlotGuard; +// Color.hpp +class RgbaColor; +class HsvColor; // I18n.hpp class I18n; @@ -15,6 +13,12 @@ class BasicTranslation; class FormattedTranslation; class NumericTranslation; +// Sigslot.hpp +class SignalStub; +template <class... TArgs> +class Signal; +class SlotGuard; + // String.hpp class Utf8Iterator; class Utf8IterableString; |