Fixed OS EventHandler compilation issue

This commit is contained in:
Ryzerth
2021-05-03 02:08:49 +02:00
parent 6e4f502454
commit c6c15a446b
17 changed files with 121 additions and 63 deletions

50
core/src/utils/color.h Normal file
View File

@ -0,0 +1,50 @@
#pragma once
#include <algorithm>
#include <math.h>
namespace color {
inline void RGBtoHSL(float r, float g, float b, float& h, float& s, float& l) {
// Calculate minimum, maximum and delta of components
float cmax = std::max<float>(std::max<float>(r, g), b);
float cmin = std::min<float>(std::min<float>(r, g), b);
float delta = cmax - cmin;
// Calculate the hue
if (delta == 0) { h = 0; }
else if (r > g && r > b) { h = 60.0f * fmodf((g - b) / delta, 6.0f); }
else if (g > r && g > b) { h = 60.0f * (((b - r) / delta) + 2.0f); }
else { h = 60.0f * (((r - g) / delta) + 4.0f); }
// Calculate lightness
l = (cmin + cmax) / 2.0f;
// Calculate saturation
s = (delta == 0) ? 0 : (delta / (1.0f - fabsf((2.0f * l) - 1.0f)));
}
inline void HSLtoRGB(float h, float s, float l, float& r, float& g, float& b) {
// Calculate coefficients
float c = s * (1.0f - fabsf((2.0f * l) - 1.0f));
float x = c * (1.0f - fabsf(fmodf(h / 60.0f, 2.0f) - 1.0f));
float m = l - (c / 2.0f);
// Affect coefficients to R, G or B depending on hue
if (h < 60) { r = c; g = x; b = 0; }
else if (h < 120) { r = x; g = c; b = 0; }
else if (h < 180) { r = 0; g = c; b = x; }
else if (h < 240) { r = 0; g = x; b = c; }
else if (h < 300) { r = x; g = 0; b = c; }
else { r = c; g = 0; b = x; }
// Add m
r += m;
g += m;
b += m;
}
inline void interpRGB(float ar, float ag, float ab, float br, float bg, float bb, float& or, float& og, float& ob, float ratio) {
or = ar + (br - ar) * ratio;
og = ag + (bg - ag) * ratio;
ob = ab + (bb - ab) * ratio;
}
}

44
core/src/utils/event.h Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#include <vector>
#include <spdlog/spdlog.h>
template <class T>
struct EventHandler {
EventHandler() {}
EventHandler(void (*handler)(T, void*), void* ctx) {
this->handler = handler;
this->ctx = ctx;
}
void (*handler)(T, void*);
void* ctx;
};
template <class T>
class Event {
public:
Event() {}
~Event() {}
void emit(T value) {
for (auto const& handler : handlers) {
handler.handler(value, handler.ctx);
}
}
void bindHandler(const EventHandler<T>& handler) {
handlers.push_back(handler);
}
void unbindHandler(const EventHandler<T>& handler) {
if (handlers.find(handler) == handlers.end()) {
spdlog::error("Tried to remove a non-existant event handler");
return;
}
handlers.erase(std::remove(handlers.begin(), handlers.end(), handler), handlers.end());
}
private:
std::vector<EventHandler<T>> handlers;
};