More UI stuff

This commit is contained in:
MrLetsplay 2022-10-21 16:37:31 +02:00
parent 84b871e859
commit 19f791cd35
10 changed files with 130 additions and 45 deletions

View File

@ -40,12 +40,12 @@ void defaultInput(GLFWwindow *window, void *data) {
}
void init() {
keyForward = Input::createKeyMapping("Forward", GLFW_KEY_W);
keyBackward = Input::createKeyMapping("Backward", GLFW_KEY_S);
keyLeft = Input::createKeyMapping("Left", GLFW_KEY_A);
keyRight = Input::createKeyMapping("Right", GLFW_KEY_D);
keyUp = Input::createKeyMapping("Up", GLFW_KEY_SPACE);
keyDown = Input::createKeyMapping("Down", GLFW_KEY_LEFT_CONTROL);
keyForward = Input::createKeyBinding("Forward", GLFW_KEY_W);
keyBackward = Input::createKeyBinding("Backward", GLFW_KEY_S);
keyLeft = Input::createKeyBinding("Left", GLFW_KEY_A);
keyRight = Input::createKeyBinding("Right", GLFW_KEY_D);
keyUp = Input::createKeyBinding("Up", GLFW_KEY_SPACE);
keyDown = Input::createKeyBinding("Down", GLFW_KEY_LEFT_CONTROL);
Input::addPeriodicCallback(PeriodicCallback(defaultInput, nullptr));
}

View File

@ -176,7 +176,7 @@ int init() {
Defaults::init();
fpsText = new TextElement(px(0), px(0));
//fpsText = new TextObject(new Font(KEK_DEFAULT_FONT), "HELLO WORLD!");
UI::addElement(fpsText);
return KEK_SUCCESS;
}
@ -237,7 +237,6 @@ int start() {
for(Light *light : shaderLights) {
ShaderLight shLight;
//shLight.color = light->color;
memcpy(shLight.color, glm::value_ptr(light->color), sizeof(shLight.color));
switch(light->getType()) {
case LightType::POINT:
@ -270,7 +269,6 @@ int start() {
i++;
}
// TODO: Fix broken lights
glNamedBufferData(kekData.shader->lighting->ubo, kekData.shader->lighting->blockSize, &kekData.shader->lighting->buffer, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, KEK_UNIFORM_LIGHTS_BINDING, kekData.shader->lighting->ubo);
@ -290,7 +288,10 @@ int start() {
int time = (int) (glfwGetTime() * 10);
if(time != prevTime) fpsText->setText("FPS: " + std::to_string((int) floor(1.0f / kekData.lastFrameTime)) + " (" + std::to_string(kekData.lastFrameTime * 1000) + " ms)");
prevTime = time;
fpsText->draw(UIPoint(0, 0), uiProjection);
for(UIElement *uiEl : kekData.uiElements) {
uiEl->drawAll(UIPoint(0, 0), uiProjection);
}
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);

View File

@ -38,33 +38,33 @@ void removeMouseListener(InputListener listener) {
kekData.mouseCallbacks.erase(listener);
}
KeyMapping createKeyMapping(std::string name, GLFWKey defaultKey) {
if(name == KEK_INVALID_KEY_MAPPING_NAME) return KEK_INVALID_KEY_MAPPING;
KeyMapping id = nextID++;
KeyMappingData d;
KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey) {
if(name == KEK_INVALID_KEY_BINDING_NAME) return KEK_INVALID_KEY_BINDING;
KeyBinding id = nextID++;
KeyBindingData d;
d.name = name;
d.key = defaultKey;
kekData.keyMappings.emplace(id, d);
kekData.keyBindings.emplace(id, d);
return id;
}
void reassignKeyMapping(KeyMapping mapping, GLFWKey key) {
auto it = kekData.keyMappings.find(mapping);
if(it == kekData.keyMappings.end()) return;
KeyMappingData d = it->second;
void reassignKeyBinding(KeyBinding mapping, GLFWKey key) {
auto it = kekData.keyBindings.find(mapping);
if(it == kekData.keyBindings.end()) return;
KeyBindingData d = it->second;
d.key = key;
it->second = d;
}
KeyMappingData getKeyMapping(KeyMapping mapping) {
auto it = kekData.keyMappings.find(mapping);
if(it == kekData.keyMappings.end()) return {.name = KEK_INVALID_KEY_MAPPING_NAME};
KeyBindingData getKeyBinding(KeyBinding mapping) {
auto it = kekData.keyBindings.find(mapping);
if(it == kekData.keyBindings.end()) return {.name = KEK_INVALID_KEY_BINDING_NAME};
return it->second;
}
GLFWKeyState getKeyState(KeyMapping mapping) {
auto it = kekData.keyMappings.find(mapping);
if(it == kekData.keyMappings.end()) return -1;
GLFWKeyState getKeyState(KeyBinding mapping) {
auto it = kekData.keyBindings.find(mapping);
if(it == kekData.keyBindings.end()) return -1;
return glfwGetKey(kekData.window, it->second.key);
}

View File

@ -6,6 +6,32 @@
namespace kek {
constexpr UIValue &UIValue::operator+=(const UIValue& rhs) {
this->pixels += rhs.pixels;
this->parentWidth += rhs.parentWidth;
this->parentHeight += rhs.parentHeight;
this->screenWidth += rhs.screenWidth;
this->screenHeight += rhs.screenHeight;
return *this;
}
constexpr UIValue &UIValue::operator-=(const UIValue& rhs) {
this->pixels -= rhs.pixels;
this->parentWidth -= rhs.parentWidth;
this->parentHeight -= rhs.parentHeight;
this->screenWidth -= rhs.screenWidth;
this->screenHeight -= rhs.screenHeight;
return *this;
}
constexpr UIValue operator+(const UIValue& lhs, const UIValue& rhs) {
return UIValue(lhs) += rhs;
}
constexpr UIValue operator-(const UIValue& lhs, const UIValue& rhs) {
return UIValue(lhs) -= rhs;
}
Font *UIElement::defaultFont = nullptr;
UIElement::UIElement(UIValue x, UIValue y): x(x), y(y) {
@ -32,6 +58,10 @@ UIPoint UIElement::getScreenPosition() {
return pos;
}
std::vector<UIElement *> UIElement::getChildren() {
return children;
}
void UIElement::addChild(UIElement *child) {
children.push_back(child);
}
@ -182,4 +212,22 @@ void UIElement::init() {
defaultFont = new Font(KEK_DEFAULT_FONT);
}
void UI::addElement(UIElement *element) {
kekData.uiElements.push_back(element);
}
void UI::removeElement(UIElement *element) {
for(auto it = kekData.uiElements.begin(); it < kekData.uiElements.end(); it++) {
if(*it == element) {
kekData.uiElements.erase(it);
delete element;
break;
}
}
}
std::vector<UIElement *> UI::getElements() {
return kekData.uiElements;
}
}

View File

@ -137,4 +137,20 @@ void TextElement::draw(UIPoint screenPos, glm::mat4 projection) {
}
}
UIWindow::UIWindow(UIValue x, UIValue y): UIElement(x, y) {
addChild(new TextElement(x, y));
}
UIWindow::~UIWindow() {
}
UIElementType UIWindow::getType() {
return UIElementType::WINDOW;
}
void UIWindow::draw(UIPoint screenPos, glm::mat4 projection) {
}
}

View File

@ -11,7 +11,7 @@
#define KEK_UNIFORM_LIGHTS_BINDING 0
#define KEK_LIGHT_LIMIT 64 // Also in shader/mesh/fragment.glsl
#define KEK_LIGHT_LIMIT 64 // Also in shader/include/constants.glsl
#define KEK_NOCLIP_SPEED 10.0f
@ -24,8 +24,8 @@
#define KEK_LIGHT_MAX_DISTANCE 30
#define KEK_LIGHT_MAX_DISTANCE_SQUARED (KEK_LIGHT_MAX_DISTANCE * KEK_LIGHT_MAX_DISTANCE)
#define KEK_INVALID_KEY_MAPPING_NAME "INVALID"
#define KEK_INVALID_KEY_MAPPING -1
#define KEK_INVALID_KEY_BINDING_NAME "INVALID"
#define KEK_INVALID_KEY_BINDING -1
#define KEK_FONT_RESOLUTION 64
#define KEK_FONT_BITMAP_WIDTH_BLOCKS 16

View File

@ -14,10 +14,10 @@ typedef generic_callable_t<GLFWwindow *, double, double> MouseCallback; // mouse
typedef unsigned int InputListener;
typedef unsigned int GLFWKey;
typedef unsigned int GLFWKeyState;
typedef unsigned int KeyMapping;
typedef unsigned int KeyBinding;
typedef unsigned int GLFWMouseButton;
struct KeyMappingData {
struct KeyBindingData {
std::string name;
GLFWKey key;
};
@ -38,12 +38,12 @@ InputListener addMouseListener(MouseCallback callback);
void removeMouseListener(InputListener listener);
KeyMapping createKeyMapping(std::string name, GLFWKey defaultKey);
KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey);
void reassignKeyMapping(KeyMapping mapping, GLFWKey key);
void reassignKeyBinding(KeyBinding mapping, GLFWKey key);
KeyMappingData getKeyMapping(KeyMapping mapping);
KeyBindingData getKeyBinding(KeyBinding mapping);
GLFWKeyState getKeyState(KeyMapping mapping);
GLFWKeyState getKeyState(KeyBinding mapping);
}

View File

@ -9,6 +9,7 @@
#include "scene.h"
#include "texture.h"
#include "fonts.h"
#include "ui.h"
namespace kek {
@ -16,7 +17,7 @@ struct KekData {
std::map<InputListener, PeriodicCallback> periodicCallbacks;
std::map<InputListener, KeyCallback> keyCallbacks;
std::map<InputListener, MouseCallback> mouseCallbacks;
std::map<KeyMapping, KeyMappingData> keyMappings;
std::map<KeyBinding, KeyBindingData> keyBindings;
GLFWwindow *window;
Shader *shader;
@ -32,6 +33,8 @@ struct KekData {
std::map<std::string, std::weak_ptr<Texture>> loadedTextures;
FT_Library freetype;
std::vector<UIElement *> uiElements;
};
extern KekData kekData;

View File

@ -16,21 +16,13 @@ namespace kek {
struct UIPoint {
int x, y;
UIPoint(int x, int y) {
this->x = x;
this->y = y;
}
UIPoint(int x, int y): x(x), y(y) {}
};
struct UIBounds {
int x, y, w, h;
UIBounds(int x, int y, int w, int h) {
this->x = x;
this->y = y;
this->w = w;
this->h = h;
}
UIBounds(int x, int y, int w, int h): x(x), y(y), w(w), h(h) {}
inline bool contains(UIPoint pos) {
return pos.x < x || pos.y < y || pos.x >= x + w || pos.y >= y + h;
@ -38,6 +30,7 @@ struct UIBounds {
};
enum class UIElementType {
WINDOW,
RECTANGLE,
TEXT,
BUTTON,
@ -153,6 +146,8 @@ public:
// Returns the element's position on the screen in pixels
UIPoint getScreenPosition();
std::vector<UIElement *> getChildren();
void addChild(UIElement *child);
void removeChild(UIElement *child);
@ -200,4 +195,14 @@ public:
};
namespace UI {
void addElement(UIElement *element);
void removeElement(UIElement *element);
std::vector<UIElement *> getElements();
};
}

View File

@ -4,6 +4,18 @@
namespace kek {
class UIWindow: public UIElement {
UIWindow(UIValue x, UIValue y);
virtual ~UIWindow();
virtual UIElementType getType();
virtual void draw(UIPoint screenPos, glm::mat4 projection);
};
class TextElement: public UIElement {
protected: