refactor: add GUI instance reference to UI nodes
This commit is contained in:
@@ -28,7 +28,6 @@ void Camera::rotate(float x, float y, float z) {
|
||||
}
|
||||
|
||||
glm::mat4 Camera::getProjection() const {
|
||||
constexpr float epsilon = 1e-6f;
|
||||
if (perspective) {
|
||||
return glm::perspective(fov * zoom, ar, near, far);
|
||||
} else if (flipped) {
|
||||
|
||||
+18
-28
@@ -28,7 +28,7 @@ glm::vec2 Events::cursor = {};
|
||||
|
||||
std::vector<uint> Events::codepoints;
|
||||
std::vector<keycode> Events::pressedKeys;
|
||||
std::unordered_map<std::string, Binding> Events::bindings;
|
||||
Bindings Events::bindings {};
|
||||
|
||||
int Events::getScroll() {
|
||||
return scroll;
|
||||
@@ -86,9 +86,9 @@ void Events::pollEvents() {
|
||||
pressedKeys.clear();
|
||||
glfwPollEvents();
|
||||
|
||||
for (auto& entry : bindings) {
|
||||
for (auto& entry : bindings.getAll()) {
|
||||
auto& binding = entry.second;
|
||||
if (!binding.enable) {
|
||||
if (!binding.enabled) {
|
||||
binding.state = false;
|
||||
continue;
|
||||
}
|
||||
@@ -119,12 +119,15 @@ void Events::pollEvents() {
|
||||
}
|
||||
}
|
||||
|
||||
Binding& Events::getBinding(const std::string& name) {
|
||||
const auto found = bindings.find(name);
|
||||
if (found == bindings.end()) {
|
||||
throw std::runtime_error("binding '" + name + "' does not exist");
|
||||
Binding* Events::getBinding(const std::string& name) {
|
||||
return bindings.get(name);
|
||||
}
|
||||
|
||||
Binding& Events::requireBinding(const std::string& name) {
|
||||
if (const auto found = getBinding(name)) {
|
||||
return *found;
|
||||
}
|
||||
return found->second;
|
||||
throw std::runtime_error("binding '" + name + "' does not exist");
|
||||
}
|
||||
|
||||
void Events::bind(const std::string& name, inputtype type, keycode code) {
|
||||
@@ -136,31 +139,19 @@ void Events::bind(const std::string& name, inputtype type, mousecode code) {
|
||||
}
|
||||
|
||||
void Events::bind(const std::string& name, inputtype type, int code) {
|
||||
bindings.try_emplace(name, Binding(type, code));
|
||||
bindings.bind(name, type, code);
|
||||
}
|
||||
|
||||
void Events::rebind(const std::string& name, inputtype type, int code) {
|
||||
const auto& found = bindings.find(name);
|
||||
if (found == bindings.end()) {
|
||||
throw std::runtime_error("binding '" + name + "' does not exist");
|
||||
}
|
||||
bindings[name] = Binding(type, code);
|
||||
requireBinding(name) = Binding(type, code);
|
||||
}
|
||||
|
||||
bool Events::active(const std::string& name) {
|
||||
const auto& found = bindings.find(name);
|
||||
if (found == bindings.end()) {
|
||||
return false;
|
||||
}
|
||||
return found->second.active();
|
||||
return bindings.active(name);
|
||||
}
|
||||
|
||||
bool Events::jactive(const std::string& name) {
|
||||
const auto& found = bindings.find(name);
|
||||
if (found == bindings.end()) {
|
||||
return false;
|
||||
}
|
||||
return found->second.jactive();
|
||||
return bindings.jactive(name);
|
||||
}
|
||||
|
||||
void Events::setKey(int key, bool b) {
|
||||
@@ -198,7 +189,7 @@ observer_handler Events::addKeyCallback(keycode key, KeyCallback callback) {
|
||||
|
||||
std::string Events::writeBindings() {
|
||||
auto obj = dv::object();
|
||||
for (auto& entry : Events::bindings) {
|
||||
for (auto& entry : bindings.getAll()) {
|
||||
const auto& binding = entry.second;
|
||||
std::string value;
|
||||
switch (binding.type) {
|
||||
@@ -253,9 +244,8 @@ void Events::loadBindings(
|
||||
}
|
||||
|
||||
void Events::enableBindings() {
|
||||
for (auto& entry : bindings) {
|
||||
auto& binding = entry.second;
|
||||
binding.enable = true;
|
||||
for (auto& entry : bindings.getAll()) {
|
||||
entry.second.enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace Events {
|
||||
extern glm::vec2 cursor;
|
||||
extern std::vector<uint> codepoints;
|
||||
extern std::vector<keycode> pressedKeys;
|
||||
extern std::unordered_map<std::string, Binding> bindings;
|
||||
extern Bindings bindings;
|
||||
|
||||
void pollEvents();
|
||||
|
||||
@@ -39,7 +39,8 @@ namespace Events {
|
||||
|
||||
void toggleCursor();
|
||||
|
||||
Binding& getBinding(const std::string& name);
|
||||
Binding* getBinding(const std::string& name);
|
||||
Binding& requireBinding(const std::string& name);
|
||||
void bind(const std::string& name, inputtype type, keycode code);
|
||||
void bind(const std::string& name, inputtype type, mousecode code);
|
||||
void bind(const std::string& name, inputtype type, int code);
|
||||
|
||||
+59
-9
@@ -175,7 +175,61 @@ static void glfw_error_callback(int error, const char* description) {
|
||||
|
||||
static GLFWcursor* standard_cursors[static_cast<int>(CursorShape::LAST) + 1] = {};
|
||||
|
||||
int Window::initialize(DisplaySettings* settings) {
|
||||
class GLFWInput : public Input {
|
||||
public:
|
||||
void pollEvents() override {
|
||||
Events::pollEvents();
|
||||
}
|
||||
|
||||
const char* getClipboardText() const override {
|
||||
return glfwGetClipboardString(::window);
|
||||
}
|
||||
|
||||
int getScroll() override {
|
||||
return Events::getScroll();
|
||||
}
|
||||
|
||||
Binding& requireBinding(const std::string& name) override {
|
||||
return Events::requireBinding(name);
|
||||
}
|
||||
|
||||
bool pressed(keycode keycode) const override {
|
||||
return Events::pressed(keycode);
|
||||
}
|
||||
bool jpressed(keycode keycode) const override {
|
||||
return Events::jpressed(keycode);
|
||||
}
|
||||
|
||||
bool clicked(mousecode mousecode) const override {
|
||||
return Events::clicked(mousecode);
|
||||
}
|
||||
bool jclicked(mousecode mousecode) const override {
|
||||
return Events::jclicked(mousecode);
|
||||
}
|
||||
|
||||
CursorState getCursor() const override {
|
||||
return {
|
||||
Events::isCursorLocked(),
|
||||
Events::cursor,
|
||||
Events::delta
|
||||
};
|
||||
}
|
||||
|
||||
Bindings& getBindings() override {
|
||||
return Events::bindings;
|
||||
}
|
||||
|
||||
const Bindings& getBindings() const override {
|
||||
return Events::bindings;
|
||||
}
|
||||
|
||||
observer_handler addKeyCallback(keycode key, KeyCallback callback) override {
|
||||
return Events::addKeyCallback(key, std::move(callback));
|
||||
}
|
||||
};
|
||||
static_assert(!std::is_abstract<GLFWInput>());
|
||||
|
||||
std::unique_ptr<Input> Window::initialize(DisplaySettings* settings) {
|
||||
::settings = settings;
|
||||
Window::width = settings->width.get();
|
||||
Window::height = settings->height.get();
|
||||
@@ -190,7 +244,7 @@ int Window::initialize(DisplaySettings* settings) {
|
||||
glfwSetErrorCallback(glfw_error_callback);
|
||||
if (glfwInit() == GLFW_FALSE) {
|
||||
logger.error() << "failed to initialize GLFW";
|
||||
return -1;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
@@ -209,7 +263,7 @@ int Window::initialize(DisplaySettings* settings) {
|
||||
if (window == nullptr) {
|
||||
logger.error() << "failed to create GLFW window";
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
return nullptr;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
@@ -224,7 +278,7 @@ int Window::initialize(DisplaySettings* settings) {
|
||||
} else {
|
||||
logger.error() << "failed to initialize GLEW:\n"
|
||||
<< glewGetErrorString(glewErr);
|
||||
return -1;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,7 +337,7 @@ int Window::initialize(DisplaySettings* settings) {
|
||||
}
|
||||
standard_cursors[i] = glfwCreateStandardCursor(cursor);
|
||||
}
|
||||
return 0;
|
||||
return std::make_unique<GLFWInput>();
|
||||
}
|
||||
|
||||
void Window::clear() {
|
||||
@@ -467,10 +521,6 @@ std::unique_ptr<ImageData> Window::takeScreenshot() {
|
||||
);
|
||||
}
|
||||
|
||||
const char* Window::getClipboardText() {
|
||||
return glfwGetClipboardString(window);
|
||||
}
|
||||
|
||||
void Window::setClipboardText(const char* text) {
|
||||
glfwSetClipboardString(window, text);
|
||||
}
|
||||
|
||||
@@ -9,13 +9,14 @@
|
||||
#include "typedefs.hpp"
|
||||
|
||||
class ImageData;
|
||||
class Input;
|
||||
struct DisplaySettings;
|
||||
|
||||
namespace Window {
|
||||
extern uint width;
|
||||
extern uint height;
|
||||
|
||||
int initialize(DisplaySettings* settings);
|
||||
std::unique_ptr<Input> initialize(DisplaySettings* settings);
|
||||
void terminate();
|
||||
|
||||
void viewport(int x, int y, int width, int height);
|
||||
@@ -41,7 +42,6 @@ namespace Window {
|
||||
void setBgColor(glm::vec3 color);
|
||||
void setBgColor(glm::vec4 color);
|
||||
double time();
|
||||
const char* getClipboardText();
|
||||
void setClipboardText(const char* text);
|
||||
DisplaySettings* getSettings();
|
||||
void setIcon(const ImageData* image);
|
||||
|
||||
+76
-1
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <glm/vec2.hpp>
|
||||
|
||||
#include "util/HandlersList.hpp"
|
||||
|
||||
@@ -143,7 +144,7 @@ struct Binding {
|
||||
int code;
|
||||
bool state = false;
|
||||
bool justChange = false;
|
||||
bool enable = true;
|
||||
bool enabled = true;
|
||||
|
||||
Binding() = default;
|
||||
Binding(inputtype type, int code) : type(type), code(code) {
|
||||
@@ -173,3 +174,77 @@ struct Binding {
|
||||
return "<unknown input type>";
|
||||
}
|
||||
};
|
||||
|
||||
class Bindings {
|
||||
std::unordered_map<std::string, Binding> bindings;
|
||||
public:
|
||||
bool active(const std::string& name) const {
|
||||
const auto& found = bindings.find(name);
|
||||
if (found == bindings.end()) {
|
||||
return false;
|
||||
}
|
||||
return found->second.active();
|
||||
}
|
||||
|
||||
bool jactive(const std::string& name) const {
|
||||
const auto& found = bindings.find(name);
|
||||
if (found == bindings.end()) {
|
||||
return false;
|
||||
}
|
||||
return found->second.jactive();
|
||||
}
|
||||
|
||||
Binding* get(const std::string& name) {
|
||||
const auto found = bindings.find(name);
|
||||
if (found == bindings.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return &found->second;
|
||||
}
|
||||
|
||||
void bind(const std::string& name, inputtype type, int code) {
|
||||
bindings.try_emplace(name, Binding(type, code));
|
||||
}
|
||||
|
||||
auto& getAll() {
|
||||
return bindings;
|
||||
}
|
||||
};
|
||||
|
||||
struct CursorState {
|
||||
bool locked = false;
|
||||
glm::vec2 pos {};
|
||||
glm::vec2 delta {};
|
||||
};
|
||||
|
||||
class Input {
|
||||
public:
|
||||
virtual ~Input() = default;
|
||||
|
||||
virtual void pollEvents() = 0;
|
||||
|
||||
virtual const char* getClipboardText() const = 0;
|
||||
|
||||
virtual int getScroll() = 0;
|
||||
|
||||
virtual Binding& requireBinding(const std::string& name) = 0;
|
||||
|
||||
virtual bool pressed(keycode keycode) const = 0;
|
||||
virtual bool jpressed(keycode keycode) const = 0;
|
||||
|
||||
virtual bool clicked(mousecode mousecode) const = 0;
|
||||
virtual bool jclicked(mousecode mousecode) const = 0;
|
||||
|
||||
virtual CursorState getCursor() const = 0;
|
||||
|
||||
virtual Bindings& getBindings() = 0;
|
||||
|
||||
virtual const Bindings& getBindings() const = 0;
|
||||
|
||||
virtual observer_handler addKeyCallback(keycode key, KeyCallback callback) = 0;
|
||||
|
||||
observer_handler addCallback(const std::string& name, KeyCallback callback) {
|
||||
return requireBinding(name).onactived.add(callback);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user