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() { void init() {
keyForward = Input::createKeyMapping("Forward", GLFW_KEY_W); keyForward = Input::createKeyBinding("Forward", GLFW_KEY_W);
keyBackward = Input::createKeyMapping("Backward", GLFW_KEY_S); keyBackward = Input::createKeyBinding("Backward", GLFW_KEY_S);
keyLeft = Input::createKeyMapping("Left", GLFW_KEY_A); keyLeft = Input::createKeyBinding("Left", GLFW_KEY_A);
keyRight = Input::createKeyMapping("Right", GLFW_KEY_D); keyRight = Input::createKeyBinding("Right", GLFW_KEY_D);
keyUp = Input::createKeyMapping("Up", GLFW_KEY_SPACE); keyUp = Input::createKeyBinding("Up", GLFW_KEY_SPACE);
keyDown = Input::createKeyMapping("Down", GLFW_KEY_LEFT_CONTROL); keyDown = Input::createKeyBinding("Down", GLFW_KEY_LEFT_CONTROL);
Input::addPeriodicCallback(PeriodicCallback(defaultInput, nullptr)); Input::addPeriodicCallback(PeriodicCallback(defaultInput, nullptr));
} }

View File

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

View File

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

View File

@ -6,6 +6,32 @@
namespace kek { 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; Font *UIElement::defaultFont = nullptr;
UIElement::UIElement(UIValue x, UIValue y): x(x), y(y) { UIElement::UIElement(UIValue x, UIValue y): x(x), y(y) {
@ -32,6 +58,10 @@ UIPoint UIElement::getScreenPosition() {
return pos; return pos;
} }
std::vector<UIElement *> UIElement::getChildren() {
return children;
}
void UIElement::addChild(UIElement *child) { void UIElement::addChild(UIElement *child) {
children.push_back(child); children.push_back(child);
} }
@ -182,4 +212,22 @@ void UIElement::init() {
defaultFont = new Font(KEK_DEFAULT_FONT); 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_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 #define KEK_NOCLIP_SPEED 10.0f
@ -24,8 +24,8 @@
#define KEK_LIGHT_MAX_DISTANCE 30 #define KEK_LIGHT_MAX_DISTANCE 30
#define KEK_LIGHT_MAX_DISTANCE_SQUARED (KEK_LIGHT_MAX_DISTANCE * KEK_LIGHT_MAX_DISTANCE) #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_BINDING_NAME "INVALID"
#define KEK_INVALID_KEY_MAPPING -1 #define KEK_INVALID_KEY_BINDING -1
#define KEK_FONT_RESOLUTION 64 #define KEK_FONT_RESOLUTION 64
#define KEK_FONT_BITMAP_WIDTH_BLOCKS 16 #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 InputListener;
typedef unsigned int GLFWKey; typedef unsigned int GLFWKey;
typedef unsigned int GLFWKeyState; typedef unsigned int GLFWKeyState;
typedef unsigned int KeyMapping; typedef unsigned int KeyBinding;
typedef unsigned int GLFWMouseButton; typedef unsigned int GLFWMouseButton;
struct KeyMappingData { struct KeyBindingData {
std::string name; std::string name;
GLFWKey key; GLFWKey key;
}; };
@ -38,12 +38,12 @@ InputListener addMouseListener(MouseCallback callback);
void removeMouseListener(InputListener listener); 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 "scene.h"
#include "texture.h" #include "texture.h"
#include "fonts.h" #include "fonts.h"
#include "ui.h"
namespace kek { namespace kek {
@ -16,7 +17,7 @@ struct KekData {
std::map<InputListener, PeriodicCallback> periodicCallbacks; std::map<InputListener, PeriodicCallback> periodicCallbacks;
std::map<InputListener, KeyCallback> keyCallbacks; std::map<InputListener, KeyCallback> keyCallbacks;
std::map<InputListener, MouseCallback> mouseCallbacks; std::map<InputListener, MouseCallback> mouseCallbacks;
std::map<KeyMapping, KeyMappingData> keyMappings; std::map<KeyBinding, KeyBindingData> keyBindings;
GLFWwindow *window; GLFWwindow *window;
Shader *shader; Shader *shader;
@ -32,6 +33,8 @@ struct KekData {
std::map<std::string, std::weak_ptr<Texture>> loadedTextures; std::map<std::string, std::weak_ptr<Texture>> loadedTextures;
FT_Library freetype; FT_Library freetype;
std::vector<UIElement *> uiElements;
}; };
extern KekData kekData; extern KekData kekData;

View File

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