format: reformat project

Signed-off-by: Vyacheslav Ivanov <islavaivanov76@gmail.com>
This commit is contained in:
Vyacheslav Ivanov
2024-08-03 19:53:48 +03:00
committed by Pugemon
parent 736cd175d5
commit bbf33e8e4d
202 changed files with 7389 additions and 5609 deletions
+22 -22
View File
@@ -1,60 +1,60 @@
#include "Camera.hpp"
#include "Window.hpp"
#include <glm/ext.hpp>
#include "Window.hpp"
Camera::Camera(glm::vec3 position, float fov) : fov(fov), position(position) {
updateVectors();
}
void Camera::updateVectors(){
front = glm::vec3(rotation * glm::vec4(0,0,-1,1));
right = glm::vec3(rotation * glm::vec4(1,0,0,1));
up = glm::vec3(rotation * glm::vec4(0,1,0,1));
dir = glm::vec3(rotation * glm::vec4(0,0,-1,1));
void Camera::updateVectors() {
front = glm::vec3(rotation * glm::vec4(0, 0, -1, 1));
right = glm::vec3(rotation * glm::vec4(1, 0, 0, 1));
up = glm::vec3(rotation * glm::vec4(0, 1, 0, 1));
dir = glm::vec3(rotation * glm::vec4(0, 0, -1, 1));
dir.y = 0;
float len = glm::length(dir);
if (len > 0.0f){
if (len > 0.0f) {
dir.x /= len;
dir.z /= len;
}
}
void Camera::rotate(float x, float y, float z){
rotation = glm::rotate(rotation, y, glm::vec3(0,1,0));
rotation = glm::rotate(rotation, x, glm::vec3(1,0,0));
rotation = glm::rotate(rotation, z, glm::vec3(0,0,1));
void Camera::rotate(float x, float y, float z) {
rotation = glm::rotate(rotation, y, glm::vec3(0, 1, 0));
rotation = glm::rotate(rotation, x, glm::vec3(1, 0, 0));
rotation = glm::rotate(rotation, z, glm::vec3(0, 0, 1));
updateVectors();
}
glm::mat4 Camera::getProjection(){
glm::mat4 Camera::getProjection() {
float aspect = this->aspect;
if (aspect == 0.0f){
if (aspect == 0.0f) {
aspect = (float)Window::width / (float)Window::height;
}
if (perspective)
return glm::perspective(fov*zoom, aspect, 0.05f, 1500.0f);
return glm::perspective(fov * zoom, aspect, 0.05f, 1500.0f);
else if (flipped)
return glm::ortho(0.0f, fov * aspect, fov, 0.0f);
else
if (flipped)
return glm::ortho(0.0f, fov*aspect, fov, 0.0f);
else
return glm::ortho(0.0f, fov*aspect, 0.0f, fov);
return glm::ortho(0.0f, fov * aspect, 0.0f, fov);
}
glm::mat4 Camera::getView(bool pos){
glm::mat4 Camera::getView(bool pos) {
glm::vec3 position = this->position;
if (!pos) {
position = glm::vec3(0.0f);
}
if (perspective) {
return glm::lookAt(position, position+front, up);
return glm::lookAt(position, position + front, up);
} else {
return glm::translate(glm::mat4(1.0f), position);
}
}
glm::mat4 Camera::getProjView(bool pos){
return getProjection()*getView(pos);
glm::mat4 Camera::getProjView(bool pos) {
return getProjection() * getView(pos);
}
void Camera::setFov(float fov) {
+3 -3
View File
@@ -29,11 +29,11 @@ public:
void rotate(float x, float y, float z);
glm::mat4 getProjection();
glm::mat4 getView(bool position=true);
glm::mat4 getProjView(bool position=true);
glm::mat4 getView(bool position = true);
glm::mat4 getProjView(bool position = true);
void setFov(float fov);
float getFov() const;
};
#endif // WINDOW_CAMERA_HPP_
#endif // WINDOW_CAMERA_HPP_
+32 -18
View File
@@ -1,12 +1,13 @@
#include "Events.hpp"
#include "Window.hpp"
#include "../debug/Logger.hpp"
#include "../util/stringutil.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string.h>
#include "../debug/Logger.hpp"
#include "../util/stringutil.hpp"
#include "Window.hpp"
static debug::Logger logger("events");
inline constexpr short _MOUSE_KEYS_OFFSET = 1024;
@@ -61,7 +62,9 @@ bool Events::jclicked(int button) {
void Events::toggleCursor() {
cursor_drag = false;
_cursor_locked = !_cursor_locked;
Window::setCursorMode(_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
Window::setCursorMode(
_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL
);
}
void Events::pollEvents() {
@@ -79,8 +82,12 @@ void Events::pollEvents() {
bool newstate = false;
switch (binding.type) {
case inputtype::keyboard: newstate = pressed(binding.code); break;
case inputtype::mouse: newstate = clicked(binding.code); break;
case inputtype::keyboard:
newstate = pressed(binding.code);
break;
case inputtype::mouse:
newstate = clicked(binding.code);
break;
}
if (newstate) {
@@ -101,7 +108,7 @@ void Events::pollEvents() {
Binding& Events::getBinding(const std::string& name) {
auto found = bindings.find(name);
if (found == bindings.end()) {
throw std::runtime_error("binding '"+name+"' does not exists");
throw std::runtime_error("binding '" + name + "' does not exists");
}
return found->second;
}
@@ -151,17 +158,16 @@ void Events::setPosition(float xpos, float ypos) {
if (Events::cursor_drag) {
Events::delta.x += xpos - Events::cursor.x;
Events::delta.y += ypos - Events::cursor.y;
}
else {
} else {
Events::cursor_drag = true;
}
Events::cursor.x = xpos;
Events::cursor.y = ypos;
}
#include "../data/dynamic.hpp"
#include "../coders/json.hpp"
#include "../coders/toml.hpp"
#include "../data/dynamic.hpp"
std::string Events::writeBindings() {
dynamic::Map obj;
@@ -169,20 +175,27 @@ std::string Events::writeBindings() {
const auto& binding = entry.second;
std::string value;
switch (binding.type) {
case inputtype::keyboard:
value = "key:"+input_util::get_name(static_cast<keycode>(binding.code));
case inputtype::keyboard:
value =
"key:" +
input_util::get_name(static_cast<keycode>(binding.code));
break;
case inputtype::mouse:
value = "mouse:"+input_util::get_name(static_cast<mousecode>(binding.code));
case inputtype::mouse:
value =
"mouse:" +
input_util::get_name(static_cast<mousecode>(binding.code));
break;
default: throw std::runtime_error("unsupported control type");
default:
throw std::runtime_error("unsupported control type");
}
obj.put(entry.first, value);
}
return toml::stringify(obj);
}
void Events::loadBindings(const std::string& filename, const std::string& source) {
void Events::loadBindings(
const std::string& filename, const std::string& source
) {
auto map = toml::parse(filename, source);
for (auto& entry : map->values) {
if (auto value = std::get_if<std::string>(&entry.second)) {
@@ -196,8 +209,9 @@ void Events::loadBindings(const std::string& filename, const std::string& source
type = inputtype::mouse;
code = static_cast<int>(input_util::mousecode_from(codename));
} else {
logger.error() << "unknown input type: " << prefix
<< " (binding " << util::quote(entry.first) << ")";
logger.error()
<< "unknown input type: " << prefix << " (binding "
<< util::quote(entry.first) << ")";
continue;
}
Events::bind(entry.first, type, code);
+8 -6
View File
@@ -1,12 +1,12 @@
#ifndef WINDOW_EVENTS_HPP_
#define WINDOW_EVENTS_HPP_
#include "input.hpp"
#include "../typedefs.hpp"
#include <string>
#include <vector>
#include <unordered_map>
#include <vector>
#include "../typedefs.hpp"
#include "input.hpp"
inline constexpr short KEYS_BUFFER_SIZE = 1036;
@@ -52,7 +52,9 @@ public:
static void setPosition(float xpos, float ypos);
static std::string writeBindings();
static void loadBindings(const std::string& filename, const std::string& source);
static void loadBindings(
const std::string& filename, const std::string& source
);
};
#endif // WINDOW_EVENTS_HPP_
#endif // WINDOW_EVENTS_HPP_
+107 -69
View File
@@ -1,16 +1,18 @@
#include <iostream>
#include "Window.hpp"
#include "Events.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <chrono>
#include <iostream>
#include <thread>
#include "../debug/Logger.hpp"
#include "../graphics/core/ImageData.hpp"
#include "../graphics/core/Texture.hpp"
#include "../settings.hpp"
#include "../util/ObjectsKeeper.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <thread>
#include <chrono>
#include "Events.hpp"
static debug::Logger logger("window");
@@ -36,16 +38,16 @@ void mouse_button_callback(GLFWwindow*, int button, int action, int) {
Events::setButton(button, action == GLFW_PRESS);
}
void key_callback(GLFWwindow*, int key, int /*scancode*/, int action, int /*mode*/) {
void key_callback(
GLFWwindow*, int key, int /*scancode*/, int action, int /*mode*/
) {
if (key == GLFW_KEY_UNKNOWN) return;
if (action == GLFW_PRESS) {
Events::setKey(key, true);
Events::pressedKeys.push_back(static_cast<keycode>(key));
}
else if (action == GLFW_RELEASE) {
} else if (action == GLFW_RELEASE) {
Events::setKey(key, false);
}
else if (action == GLFW_REPEAT) {
} else if (action == GLFW_REPEAT) {
Events::pressedKeys.push_back(static_cast<keycode>(key));
}
}
@@ -82,24 +84,36 @@ void window_size_callback(GLFWwindow*, int width, int height) {
Window::resetScissor();
}
void character_callback(GLFWwindow*, unsigned int codepoint){
void character_callback(GLFWwindow*, unsigned int codepoint) {
Events::codepoints.push_back(codepoint);
}
const char* glfwErrorName(int error) {
switch (error) {
case GLFW_NO_ERROR: return "no error";
case GLFW_NOT_INITIALIZED: return "not initialized";
case GLFW_NO_CURRENT_CONTEXT: return "no current context";
case GLFW_INVALID_ENUM: return "invalid enum";
case GLFW_INVALID_VALUE: return "invalid value";
case GLFW_OUT_OF_MEMORY: return "out of memory";
case GLFW_API_UNAVAILABLE: return "api unavailable";
case GLFW_VERSION_UNAVAILABLE: return "version unavailable";
case GLFW_PLATFORM_ERROR: return "platform error";
case GLFW_FORMAT_UNAVAILABLE: return "format unavailable";
case GLFW_NO_WINDOW_CONTEXT: return "no window context";
default: return "unknown error";
case GLFW_NO_ERROR:
return "no error";
case GLFW_NOT_INITIALIZED:
return "not initialized";
case GLFW_NO_CURRENT_CONTEXT:
return "no current context";
case GLFW_INVALID_ENUM:
return "invalid enum";
case GLFW_INVALID_VALUE:
return "invalid value";
case GLFW_OUT_OF_MEMORY:
return "out of memory";
case GLFW_API_UNAVAILABLE:
return "api unavailable";
case GLFW_VERSION_UNAVAILABLE:
return "version unavailable";
case GLFW_PLATFORM_ERROR:
return "platform error";
case GLFW_FORMAT_UNAVAILABLE:
return "format unavailable";
case GLFW_NO_WINDOW_CONTEXT:
return "no window context";
default:
return "unknown error";
}
}
@@ -111,14 +125,14 @@ void error_callback(int error, const char* description) {
}
}
int Window::initialize(DisplaySettings* settings){
int Window::initialize(DisplaySettings* settings) {
Window::settings = settings;
Window::width = settings->width.get();
Window::height = settings->height.get();
std::string title = "VoxelEngine-Cpp v" +
std::to_string(ENGINE_VERSION_MAJOR) + "." +
std::to_string(ENGINE_VERSION_MINOR);
std::string title = "VoxelEngine-Cpp v" +
std::to_string(ENGINE_VERSION_MAJOR) + "." +
std::to_string(ENGINE_VERSION_MINOR);
glfwSetErrorCallback(error_callback);
if (glfwInit() == GLFW_FALSE) {
@@ -139,7 +153,7 @@ int Window::initialize(DisplaySettings* settings){
glfwWindowHint(GLFW_SAMPLES, settings->samples.get());
window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
if (window == nullptr){
if (window == nullptr) {
logger.error() << "failed to create GLFW window";
glfwTerminate();
return -1;
@@ -147,24 +161,26 @@ int Window::initialize(DisplaySettings* settings){
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
GLenum glewErr = glewInit();
if (glewErr != GLEW_OK){
if (glewErr != GLEW_OK) {
if (glewErr == GLEW_ERROR_NO_GLX_DISPLAY) {
// see issue #240
logger.warning() << "glewInit() returned GLEW_ERROR_NO_GLX_DISPLAY; ignored";
logger.warning()
<< "glewInit() returned GLEW_ERROR_NO_GLX_DISPLAY; ignored";
} else {
logger.error() << "failed to initialize GLEW:\n" << glewGetErrorString(glewErr);
logger.error() << "failed to initialize GLEW:\n"
<< glewGetErrorString(glewErr);
return -1;
}
}
glViewport(0,0, width, height);
glClearColor(0.0f,0.0f,0.0f, 1);
glViewport(0, 0, width, height);
glClearColor(0.0f, 0.0f, 0.0f, 1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GLint maxTextureSize[1]{static_cast<GLint>(Texture::MAX_RESOLUTION)};
GLint maxTextureSize[1] {static_cast<GLint>(Texture::MAX_RESOLUTION)};
glGetIntegerv(GL_MAX_TEXTURE_SIZE, maxTextureSize);
if (maxTextureSize[0] > 0) {
Texture::MAX_RESOLUTION = maxTextureSize[0];
@@ -179,11 +195,14 @@ int Window::initialize(DisplaySettings* settings){
glfwSetScrollCallback(window, scroll_callback);
observers_keeper = util::ObjectsKeeper();
observers_keeper.keepAlive(settings->fullscreen.observe([=](bool value) {
if (value != isFullscreen()) {
toggleFullscreen();
}
}, true));
observers_keeper.keepAlive(settings->fullscreen.observe(
[=](bool value) {
if (value != isFullscreen()) {
toggleFullscreen();
}
},
true
));
glfwSwapInterval(1);
setFramerate(settings->framerate.get());
@@ -213,11 +232,11 @@ void Window::setBgColor(glm::vec4 color) {
glClearColor(color.r, color.g, color.b, color.a);
}
void Window::viewport(int x, int y, int width, int height){
void Window::viewport(int x, int y, int width, int height) {
glViewport(x, y, width, height);
}
void Window::setCursorMode(int mode){
void Window::setCursorMode(int mode) {
glfwSetInputMode(window, GLFW_CURSOR, mode);
}
@@ -245,9 +264,12 @@ void Window::pushScissor(glm::vec4 area) {
if (area.z < 0.0f || area.w < 0.0f) {
glScissor(0, 0, 0, 0);
} else {
glScissor(area.x, Window::height-area.w,
std::max(0, int(area.z-area.x)),
std::max(0, int(area.w-area.y)));
glScissor(
area.x,
Window::height - area.w,
std::max(0, int(area.z - area.x)),
std::max(0, int(area.w - area.y))
);
}
scissorArea = area;
}
@@ -262,9 +284,12 @@ void Window::popScissor() {
if (area.z < 0.0f || area.w < 0.0f) {
glScissor(0, 0, 0, 0);
} else {
glScissor(area.x, Window::height-area.w,
std::max(0, int(area.z-area.x)),
std::max(0, int(area.w-area.y)));
glScissor(
area.x,
Window::height - area.w,
std::max(0, int(area.z - area.x)),
std::max(0, int(area.w - area.y))
);
}
if (scissorStack.empty()) {
glDisable(GL_SCISSOR_TEST);
@@ -272,16 +297,16 @@ void Window::popScissor() {
scissorArea = area;
}
void Window::terminate(){
void Window::terminate() {
observers_keeper = util::ObjectsKeeper();
glfwTerminate();
}
bool Window::isShouldClose(){
bool Window::isShouldClose() {
return glfwWindowShouldClose(window);
}
void Window::setShouldClose(bool flag){
void Window::setShouldClose(bool flag) {
glfwSetWindowShouldClose(window, flag);
}
@@ -292,7 +317,7 @@ void Window::setFramerate(int framerate) {
Window::framerate = framerate;
}
void Window::toggleFullscreen(){
void Window::toggleFullscreen() {
fullscreen = !fullscreen;
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
@@ -302,12 +327,17 @@ void Window::toggleFullscreen(){
if (fullscreen) {
glfwGetWindowPos(window, &posX, &posY);
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE);
}
else {
glfwSetWindowMonitor(window, nullptr,
posX, posY,
settings->width.get(), settings->height.get(),
glfwSetWindowMonitor(
window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE
);
} else {
glfwSetWindowMonitor(
window,
nullptr,
posX,
posY,
settings->width.get(),
settings->height.get(),
GLFW_DONT_CARE
);
glfwSetWindowAttrib(window, GLFW_MAXIMIZED, GLFW_FALSE);
@@ -327,8 +357,9 @@ void Window::swapBuffers() {
Window::resetScissor();
double currentTime = time();
if (framerate > 0 && currentTime - prevSwap < (1.0 / framerate)) {
std::this_thread::sleep_for(std::chrono::milliseconds(
static_cast<int>((1.0/framerate - (currentTime-prevSwap))*1000)));
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(
(1.0 / framerate - (currentTime - prevSwap)) * 1000
)));
}
prevSwap = time();
}
@@ -361,18 +392,26 @@ void Window::setClipboardText(const char* text) {
bool Window::tryToMaximize(GLFWwindow* window, GLFWmonitor* monitor) {
glm::ivec4 windowFrame(0);
glm::ivec4 workArea(0);
glfwGetWindowFrameSize(window, &windowFrame.x, &windowFrame.y, &windowFrame.z, &windowFrame.w);
glfwGetMonitorWorkarea(monitor, &workArea.x, &workArea.y, &workArea.z, &workArea.w);
glfwGetWindowFrameSize(
window, &windowFrame.x, &windowFrame.y, &windowFrame.z, &windowFrame.w
);
glfwGetMonitorWorkarea(
monitor, &workArea.x, &workArea.y, &workArea.z, &workArea.w
);
if (Window::width > (uint)workArea.z) Window::width = (uint)workArea.z;
if (Window::height > (uint)workArea.w) Window::height = (uint)workArea.w;
if (Window::width >= (uint)(workArea.z - (windowFrame.x + windowFrame.z)) &&
Window::height >= (uint)(workArea.w - (windowFrame.y + windowFrame.w))) {
Window::height >=
(uint)(workArea.w - (windowFrame.y + windowFrame.w))) {
glfwMaximizeWindow(window);
return true;
}
glfwSetWindowSize(window, Window::width, Window::height);
glfwSetWindowPos(window, workArea.x + (workArea.z - Window::width) / 2,
workArea.y + (workArea.w - Window::height) / 2 + windowFrame.y / 2);
glfwSetWindowPos(
window,
workArea.x + (workArea.z - Window::width) / 2,
workArea.y + (workArea.w - Window::height) / 2 + windowFrame.y / 2
);
return false;
}
@@ -380,7 +419,6 @@ void Window::setIcon(const ImageData* image) {
GLFWimage icon {
static_cast<int>(image->getWidth()),
static_cast<int>(image->getHeight()),
image->getData()
};
image->getData()};
glfwSetWindowIcon(window, 1, &icon);
}
+5 -5
View File
@@ -1,12 +1,12 @@
#ifndef WINDOW_WINDOW_HPP_
#define WINDOW_WINDOW_HPP_
#include "../typedefs.hpp"
#include <glm/glm.hpp>
#include <memory>
#include <stack>
#include <vector>
#include <memory>
#include <glm/glm.hpp>
#include "../typedefs.hpp"
class ImageData;
struct DisplaySettings;
@@ -64,4 +64,4 @@ public:
static std::unique_ptr<ImageData> takeScreenshot();
};
#endif // WINDOW_WINDOW_HPP_
#endif // WINDOW_WINDOW_HPP_
+109 -57
View File
@@ -1,10 +1,12 @@
#include "input.hpp"
#include <GLFW/glfw3.h>
#include <unordered_map>
#ifdef _WIN32
#include <windows.h>
#endif // _WIN32
#endif // _WIN32
static std::unordered_map<std::string, int> keycodes {
{"enter", GLFW_KEY_ENTER},
@@ -44,10 +46,14 @@ static std::unordered_map<int, std::string> keynames {};
std::string input_util::get_name(mousecode code) {
switch (code) {
case mousecode::BUTTON_1: return "left";
case mousecode::BUTTON_2: return "right";
case mousecode::BUTTON_3: return "middle";
default: return "unknown";
case mousecode::BUTTON_1:
return "left";
case mousecode::BUTTON_2:
return "right";
case mousecode::BUTTON_3:
return "middle";
default:
return "unknown";
}
}
@@ -74,13 +80,13 @@ void Binding::reset(mousecode code) {
void input_util::initialize() {
for (int i = 0; i <= 9; i++) {
keycodes[std::to_string(i)] = GLFW_KEY_0+i;
keycodes[std::to_string(i)] = GLFW_KEY_0 + i;
}
for (int i = 0; i < 25; i++) {
keycodes["f"+std::to_string(i+1)] = GLFW_KEY_F1+i;
keycodes["f" + std::to_string(i + 1)] = GLFW_KEY_F1 + i;
}
for (char i = 'a'; i <= 'z'; i++) {
keycodes[std::string({i})] = GLFW_KEY_A-'a'+i;
keycodes[std::string({i})] = GLFW_KEY_A - 'a' + i;
}
for (const auto& entry : keycodes) {
keynames[entry.second] = entry.first;
@@ -107,66 +113,112 @@ std::string input_util::to_string(keycode code) {
int icode_repr = static_cast<int>(code);
#ifdef _WIN32
char name[64];
int result = GetKeyNameTextA(glfwGetKeyScancode(icode_repr) << 16, name, 64);
int result =
GetKeyNameTextA(glfwGetKeyScancode(icode_repr) << 16, name, 64);
if (result == NULL) return "Unknown";
return std::string(name);
#else
const char* name = glfwGetKeyName(icode_repr, glfwGetKeyScancode(icode_repr));
const char* name =
glfwGetKeyName(icode_repr, glfwGetKeyScancode(icode_repr));
if (name == nullptr) {
switch (icode_repr) {
case GLFW_KEY_TAB: return "Tab";
case GLFW_KEY_LEFT_CONTROL: return "Left Ctrl";
case GLFW_KEY_RIGHT_CONTROL: return "Right Ctrl";
case GLFW_KEY_LEFT_ALT: return "Left Alt";
case GLFW_KEY_RIGHT_ALT: return "Right Alt";
case GLFW_KEY_LEFT_SHIFT: return "Left Shift";
case GLFW_KEY_RIGHT_SHIFT: return "Right Shift";
case GLFW_KEY_CAPS_LOCK: return "Caps-Lock";
case GLFW_KEY_SPACE: return "Space";
case GLFW_KEY_ESCAPE: return "Esc";
case GLFW_KEY_ENTER: return "Enter";
case GLFW_KEY_UP: return "Up";
case GLFW_KEY_DOWN: return "Down";
case GLFW_KEY_LEFT: return "Left";
case GLFW_KEY_RIGHT: return "Right";
case GLFW_KEY_BACKSPACE: return "Backspace";
case GLFW_KEY_F1: return "F1";
case GLFW_KEY_F2: return "F2";
case GLFW_KEY_F3: return "F3";
case GLFW_KEY_F4: return "F4";
case GLFW_KEY_F5: return "F5";
case GLFW_KEY_F6: return "F6";
case GLFW_KEY_F7: return "F7";
case GLFW_KEY_F8: return "F8";
case GLFW_KEY_F9: return "F9";
case GLFW_KEY_F10: return "F10";
case GLFW_KEY_F11: return "F11";
case GLFW_KEY_F12: return "F12";
case GLFW_KEY_DELETE: return "Delete";
case GLFW_KEY_HOME: return "Home";
case GLFW_KEY_END: return "End";
case GLFW_KEY_LEFT_SUPER: return "Left Super";
case GLFW_KEY_RIGHT_SUPER: return "Right Super";
case GLFW_KEY_PAGE_UP: return "Page Up";
case GLFW_KEY_PAGE_DOWN: return "Page Down";
case GLFW_KEY_INSERT: return "Insert";
case GLFW_KEY_PRINT_SCREEN: return "Print Screen";
case GLFW_KEY_NUM_LOCK: return "Num Lock";
case GLFW_KEY_MENU: return "Menu";
case GLFW_KEY_PAUSE: return "Pause";
default:
return "Unknown";
case GLFW_KEY_TAB:
return "Tab";
case GLFW_KEY_LEFT_CONTROL:
return "Left Ctrl";
case GLFW_KEY_RIGHT_CONTROL:
return "Right Ctrl";
case GLFW_KEY_LEFT_ALT:
return "Left Alt";
case GLFW_KEY_RIGHT_ALT:
return "Right Alt";
case GLFW_KEY_LEFT_SHIFT:
return "Left Shift";
case GLFW_KEY_RIGHT_SHIFT:
return "Right Shift";
case GLFW_KEY_CAPS_LOCK:
return "Caps-Lock";
case GLFW_KEY_SPACE:
return "Space";
case GLFW_KEY_ESCAPE:
return "Esc";
case GLFW_KEY_ENTER:
return "Enter";
case GLFW_KEY_UP:
return "Up";
case GLFW_KEY_DOWN:
return "Down";
case GLFW_KEY_LEFT:
return "Left";
case GLFW_KEY_RIGHT:
return "Right";
case GLFW_KEY_BACKSPACE:
return "Backspace";
case GLFW_KEY_F1:
return "F1";
case GLFW_KEY_F2:
return "F2";
case GLFW_KEY_F3:
return "F3";
case GLFW_KEY_F4:
return "F4";
case GLFW_KEY_F5:
return "F5";
case GLFW_KEY_F6:
return "F6";
case GLFW_KEY_F7:
return "F7";
case GLFW_KEY_F8:
return "F8";
case GLFW_KEY_F9:
return "F9";
case GLFW_KEY_F10:
return "F10";
case GLFW_KEY_F11:
return "F11";
case GLFW_KEY_F12:
return "F12";
case GLFW_KEY_DELETE:
return "Delete";
case GLFW_KEY_HOME:
return "Home";
case GLFW_KEY_END:
return "End";
case GLFW_KEY_LEFT_SUPER:
return "Left Super";
case GLFW_KEY_RIGHT_SUPER:
return "Right Super";
case GLFW_KEY_PAGE_UP:
return "Page Up";
case GLFW_KEY_PAGE_DOWN:
return "Page Down";
case GLFW_KEY_INSERT:
return "Insert";
case GLFW_KEY_PRINT_SCREEN:
return "Print Screen";
case GLFW_KEY_NUM_LOCK:
return "Num Lock";
case GLFW_KEY_MENU:
return "Menu";
case GLFW_KEY_PAUSE:
return "Pause";
default:
return "Unknown";
}
}
return std::string(name);
#endif // _WIN32
#endif // _WIN32
}
std::string input_util::to_string(mousecode code) {
switch (code) {
case mousecode::BUTTON_1: return "LMB";
case mousecode::BUTTON_2: return "RMB";
case mousecode::BUTTON_3: return "MMB";
default: return "unknown button";
case mousecode::BUTTON_1:
return "LMB";
case mousecode::BUTTON_2:
return "RMB";
case mousecode::BUTTON_3:
return "MMB";
default:
return "unknown button";
}
}
+17 -20
View File
@@ -1,10 +1,10 @@
#ifndef WINDOW_INPUT_HPP_
#define WINDOW_INPUT_HPP_
#include "../util/RunnablesList.hpp"
#include <string>
#include "../util/RunnablesList.hpp"
/// @brief Represents glfw3 keycode values.
enum class keycode : int {
SPACE = 32,
@@ -100,17 +100,14 @@ enum class keycode : int {
/// @brief Represents glfw3 mouse button IDs.
/// @details There is a subset of glfw3 mouse button IDs.
enum class mousecode : int {
BUTTON_1 = 0, // Left mouse button
BUTTON_2 = 1, // Right mouse button
BUTTON_3 = 2, // Middle mouse button
BUTTON_1 = 0, // Left mouse button
BUTTON_2 = 1, // Right mouse button
BUTTON_3 = 2, // Middle mouse button
UNKNOWN = -1,
};
inline mousecode MOUSECODES_ALL[] {
mousecode::BUTTON_1,
mousecode::BUTTON_2,
mousecode::BUTTON_3
};
mousecode::BUTTON_1, mousecode::BUTTON_2, mousecode::BUTTON_3};
namespace input_util {
void initialize();
@@ -141,9 +138,10 @@ struct Binding {
int code;
bool state = false;
bool justChange = false;
Binding() = default;
Binding(inputtype type, int code) : type(type), code(code) {}
Binding(inputtype type, int code) : type(type), code(code) {
}
bool active() const {
return state;
@@ -152,23 +150,22 @@ struct Binding {
bool jactive() const {
return state && justChange;
}
void reset(inputtype, int);
void reset(keycode);
void reset(mousecode);
inline std::string text() const {
switch (type) {
case inputtype::keyboard: {
return input_util::to_string(static_cast<keycode>(code));
}
case inputtype::mouse: {
return input_util::to_string(static_cast<mousecode>(code));
}
case inputtype::keyboard: {
return input_util::to_string(static_cast<keycode>(code));
}
case inputtype::mouse: {
return input_util::to_string(static_cast<mousecode>(code));
}
}
return "<unknown input type>";
}
};
#endif // WINDOW_INPUT_HPP_
#endif // WINDOW_INPUT_HPP_