contentpack remove feature WIP

This commit is contained in:
MihailRis
2024-02-29 23:47:07 +03:00
parent a1f313bedc
commit 3862dbda66
18 changed files with 541 additions and 433 deletions
+71 -70
View File
@@ -4,9 +4,11 @@
#include <GLFW/glfw3.h>
#include <string.h>
bool Events::_keys[KEYS_BUFFER_SIZE] = {};
uint Events::_frames[KEYS_BUFFER_SIZE] = {};
uint Events::_current = 0;
inline constexpr short _MOUSE_KEYS_OFFSET = 1024;
bool Events::keys[KEYS_BUFFER_SIZE] = {};
uint Events::frames[KEYS_BUFFER_SIZE] = {};
uint Events::currentFrame = 0;
int Events::scroll = 0;
glm::vec2 Events::delta = {};
glm::vec2 Events::cursor = {};
@@ -17,125 +19,124 @@ std::vector<keycode> Events::pressedKeys;
std::unordered_map<std::string, Binding> Events::bindings;
bool Events::pressed(keycode keycode) {
return pressed(static_cast<int>(keycode));
return pressed(static_cast<int>(keycode));
}
bool Events::pressed(int keycode) {
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) {
fprintf(stderr, "pressed %i\n", keycode); //FIXME: unreasonable cstdio usage
return false;
}
return _keys[keycode];
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) {
return false;
}
return keys[keycode];
}
bool Events::jpressed(keycode keycode) {
return jpressed(static_cast<int>(keycode));
return jpressed(static_cast<int>(keycode));
}
bool Events::jpressed(int keycode) {
return Events::pressed(keycode) && _frames[keycode] == _current;
return Events::pressed(keycode) && frames[keycode] == currentFrame;
}
bool Events::clicked(mousecode button) {
return clicked(static_cast<int>(button));
return clicked(static_cast<int>(button));
}
bool Events::clicked(int button) {
return Events::pressed(_MOUSE_KEYS_OFFSET + button);
return Events::pressed(_MOUSE_KEYS_OFFSET + button);
}
bool Events::jclicked(mousecode button) {
return jclicked(static_cast<int>(button));
return jclicked(static_cast<int>(button));
}
bool Events::jclicked(int button) {
return Events::jpressed(_MOUSE_KEYS_OFFSET + button);
return Events::jpressed(_MOUSE_KEYS_OFFSET + button);
}
void Events::toggleCursor() {
_cursor_locked = !_cursor_locked;
Window::setCursorMode(_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
_cursor_locked = !_cursor_locked;
Window::setCursorMode(_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
}
void Events::pollEvents() {
_current++;
delta.x = 0.f;
delta.y = 0.f;
scroll = 0;
codepoints.clear();
pressedKeys.clear();
glfwPollEvents();
currentFrame++;
delta.x = 0.f;
delta.y = 0.f;
scroll = 0;
codepoints.clear();
pressedKeys.clear();
glfwPollEvents();
for (auto& entry : bindings) {
auto& binding = entry.second;
binding.justChange = false;
for (auto& entry : bindings) {
auto& binding = entry.second;
binding.justChange = false;
bool newstate = false;
switch (binding.type) {
case inputtype::keyboard: newstate = pressed(binding.code); break;
case inputtype::mouse: newstate = clicked(binding.code); break;
}
bool newstate = false;
switch (binding.type) {
case inputtype::keyboard: newstate = pressed(binding.code); break;
case inputtype::mouse: newstate = clicked(binding.code); break;
}
if (newstate) {
if (!binding.state) {
binding.state = true;
binding.justChange = true;
}
}
else {
if (binding.state) {
binding.state = false;
binding.justChange = true;
}
}
}
if (newstate) {
if (!binding.state) {
binding.state = true;
binding.justChange = true;
}
}
else {
if (binding.state) {
binding.state = false;
binding.justChange = true;
}
}
}
}
void Events::bind(std::string name, inputtype type, keycode code) {
bind(name, type, static_cast<int>(code));
bind(name, type, static_cast<int>(code));
}
void Events::bind(std::string name, inputtype type, mousecode code) {
bind(name, type, static_cast<int>(code));
bind(name, type, static_cast<int>(code));
}
void Events::bind(std::string name, inputtype type, int code) {
bindings[name] = { type, code, false, false };
bindings[name] = { type, code, false, false };
}
bool Events::active(std::string name) {
const auto& found = bindings.find(name);
if (found == bindings.end()) {
return false;
}
return found->second.active();
const auto& found = bindings.find(name);
if (found == bindings.end()) {
return false;
}
return found->second.active();
}
bool Events::jactive(std::string name) {
const auto& found = bindings.find(name);
if (found == bindings.end()) {
return false;
}
return found->second.jactive();
const auto& found = bindings.find(name);
if (found == bindings.end()) {
return false;
}
return found->second.jactive();
}
void Events::setKey(int key, bool b) {
Events::_keys[key] = b;
Events::_frames[key] = Events::_current;
Events::keys[key] = b;
Events::frames[key] = currentFrame;
}
void Events::setButton(int button, bool b) {
setKey(_MOUSE_KEYS_OFFSET + button, b);
setKey(_MOUSE_KEYS_OFFSET + button, b);
}
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 {
Events::cursor_drag = true;
}
Events::cursor.x = xpos;
Events::cursor.y = ypos;
if (Events::cursor_drag) {
Events::delta.x += xpos - Events::cursor.x;
Events::delta.y += ypos - Events::cursor.y;
}
else {
Events::cursor_drag = true;
}
Events::cursor.x = xpos;
Events::cursor.y = ypos;
}