Functioning UI code

This commit is contained in:
MrLetsplay 2022-10-20 21:09:04 +02:00
parent 299b1b5773
commit 84b871e859
8 changed files with 246 additions and 48 deletions

View File

@ -65,7 +65,7 @@ target_link_libraries(kekengine PUBLIC microtar_static)
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_check_modules(FREETYPE REQUIRED freetype2) pkg_check_modules(FREETYPE REQUIRED freetype2)
target_link_libraries(kekengine PUBLIC ${FREETYPE_LIBRARIES}) target_link_libraries(kekengine PUBLIC ${FREETYPE_LIBRARIES})
target_include_directories(kekengine PRIVATE ${FREETYPE_INCLUDE_DIRS}) target_include_directories(kekengine PUBLIC ${FREETYPE_INCLUDE_DIRS})
if(UNIX) if(UNIX)
target_link_libraries(kekengine PUBLIC glfw GLEW GL) target_link_libraries(kekengine PUBLIC glfw GLEW GL)

View File

@ -22,12 +22,14 @@
#include "gameobject.h" #include "gameobject.h"
#include "scene.h" #include "scene.h"
#include "defaults.h" #include "defaults.h"
#include "ui.h"
#include "uielements.h"
kek::KekData kek::kekData; kek::KekData kek::kekData;
namespace kek::Engine { namespace kek::Engine {
static TextObject *fpsText; static TextElement *fpsText;
static void framebufferSizeCallback(GLFWwindow *window, int w, int h) { static void framebufferSizeCallback(GLFWwindow *window, int w, int h) {
glViewport(0, 0, w, h); glViewport(0, 0, w, h);
@ -170,8 +172,11 @@ int init() {
return KEK_ERROR; return KEK_ERROR;
} }
UIElement::init();
Defaults::init(); Defaults::init();
fpsText = new TextObject(new Font("font/MaredivRegular-yeg3.ttf"), "HELLO WORLD!"); fpsText = new TextElement(px(0), px(0));
//fpsText = new TextObject(new Font(KEK_DEFAULT_FONT), "HELLO WORLD!");
return KEK_SUCCESS; return KEK_SUCCESS;
} }
@ -280,10 +285,12 @@ int start() {
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glm::mat4 uiProjection = glm::ortho(0.0f, (float) kekData.screenWidth, (float) kekData.screenHeight, 0.0f);
int time = (int) (glfwGetTime() * 10); int time = (int) (glfwGetTime() * 10);
if(time != prevTime) fpsText->setText(std::to_string((int) floor(1.0f / kekData.lastFrameTime)) + " (" + std::to_string(kekData.lastFrameTime * 1000) + ")"); 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->getFont()->drawText(fpsText, 0, fpsText->getMetrics(24).height, 24, Colors::RED); fpsText->draw(UIPoint(0, 0), uiProjection);
glDisable(GL_BLEND); glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
@ -295,8 +302,6 @@ int start() {
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> secsTaken = end - start; std::chrono::duration<float> secsTaken = end - start;
kekData.lastFrameTime = secsTaken.count(); kekData.lastFrameTime = secsTaken.count();
//std::cout << "FT: " << kekData.lastFrameTime << '\n';
//std::cout << "FR: " << (1.0f / kekData.lastFrameTime) << '\n';
} }
return KEK_SUCCESS; return KEK_SUCCESS;

View File

@ -6,6 +6,8 @@
namespace kek { namespace kek {
Font *UIElement::defaultFont = nullptr;
UIElement::UIElement(UIValue x, UIValue y): x(x), y(y) { UIElement::UIElement(UIValue x, UIValue y): x(x), y(y) {
} }
@ -50,30 +52,30 @@ inline int UIElement::uiToScreen(UIValue val) {
return px; return px;
} }
/*void UIElement::drawAll(int x, int y, glm::mat4 projection) { void UIElement::drawAll(UIPoint screenPos, glm::mat4 projection) {
if(!isVisible()) return; if(!visible) return;
draw(x, y, projection); draw(screenPos, projection);
for(UIElement *child : children) { for(UIElement *child : children) {
UIPoint pos = child->getPosition(); UIPoint pos = child->getPosition();
child->drawAll(x + pos.x, y + pos.y, projection); child->drawAll(UIPoint(screenPos.x + pos.x, screenPos.y + pos.y), projection);
} }
} }
void UIElement::hoverAll(int x, int y, int absX, int absY) { void UIElement::hoverAll(UIPoint pos, UIPoint screenPos) {
UIBounds bounds = getBounds(); UIBounds bounds = getBounds();
if(x < bounds.x || y < bounds.y || x >= bounds.x + bounds.w || y >= bounds.y + bounds.h) { // Only used by topmost parent UIElement if(!bounds.contains(pos)) { // Only used by topmost parent UIElement
if(hovering) hoverExitAll(); if(hovering) hoverExitAll();
return; return;
} }
UIElement *hoveredChild = NULL; UIElement *hoveredChild = NULL;
for(UIElement *child : children) { for(UIElement *child : children) {
UIPoint pos = child->getPosition(); UIPoint childPos = child->getPosition();
int relX = x - pos.x; int relX = pos.x - childPos.x;
int relY = y - pos.y; int relY = pos.y - childPos.y;
UIBounds b = child->getBounds(); UIBounds b = child->getBounds();
if(relX < b.x || relY < b.y || relX > b.x + b.w || relY > b.y + b.h) continue; if(!b.contains(UIPoint(relX, relY))) continue;
hoveredChild = child; hoveredChild = child;
} }
@ -82,13 +84,13 @@ void UIElement::hoverAll(int x, int y, int absX, int absY) {
} }
if(hoveredChild) { if(hoveredChild) {
UIPoint pos = hoveredChild->getPosition(); UIPoint childPos = hoveredChild->getPosition();
hoveredChild->hoverAll(x - pos.x, y - pos.y, absX, absY); hoveredChild->hoverAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos);
} }
if(!hovering) hoverEnter(); if(!hovering) hoverEnter(pos, screenPos);
hovering = true; hovering = true;
hover(x, y, absX, absY); hover(pos, screenPos);
} }
void UIElement::hoverExitAll() { void UIElement::hoverExitAll() {
@ -100,32 +102,32 @@ void UIElement::hoverExitAll() {
hoverExit(); hoverExit();
} }
void UIElement::clickAll(int x, int y, int absX, int absY, MouseButton button) { void UIElement::clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button) {
UIBounds bounds = getBounds(); UIBounds bounds = getBounds();
if(x < bounds.x || y < bounds.y || x >= bounds.x + bounds.w || y >= bounds.y + bounds.h) return; // Only used by topmost parent UIElement if(!bounds.contains(pos)) return; // Only used by topmost parent UIElement
UIElement *clickedChild = NULL; UIElement *clickedChild = NULL;
for(UIElement *child : children) { for(UIElement *child : children) {
if(!child->clickable) continue; if(!child->clickable) continue;
UIPoint pos = child->getPosition(); UIPoint childPos = child->getPosition();
int relX = x - pos.x; int relX = pos.x - childPos.x;
int relY = y - pos.y; int relY = pos.y - childPos.y;
UIBounds b = child->getBounds(); UIBounds b = child->getBounds();
if(relX < b.x || relY < b.y || relX > b.x + b.w || relY > b.y + b.h) continue; if(!b.contains(UIPoint(relX, relY))) continue;
clickedChild = child; clickedChild = child;
} }
if(clickedChild != NULL) { if(clickedChild != NULL) {
UIPoint pos = clickedChild->getPosition(); UIPoint childPos = clickedChild->getPosition();
clickedChild->clickAll(x - pos.x, y - pos.y, absX, absY, button); clickedChild->clickAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos, button);
}else { }else {
click(x, y, absX, absY, button); click(pos, screenPos, button);
} }
} }
bool UIElement::focusEnterAll(UIPoint pos, UIPoint screenPos) { bool UIElement::focusEnterAll(UIPoint pos, UIPoint screenPos) {
UIBounds bounds = getBounds(); UIBounds bounds = getBounds();
if(pos.x < bounds.x || pos.y < bounds.y || pos.x >= bounds.x + bounds.w || pos.y >= bounds.y + bounds.h) return false; if(!bounds.contains(pos)) return false;
UIElement *focusedChild = nullptr; UIElement *focusedChild = nullptr;
for(UIElement *child : children) { for(UIElement *child : children) {
@ -133,13 +135,13 @@ bool UIElement::focusEnterAll(UIPoint pos, UIPoint screenPos) {
int relX = pos.x - childPos.x; int relX = pos.x - childPos.x;
int relY = pos.y - childPos.y; int relY = pos.y - childPos.y;
UIBounds b = child->getBounds(); UIBounds b = child->getBounds();
if(relX < b.x || relY < b.y || relX > b.x + b.w || relY > b.y + b.h) continue; if(!b.contains(UIPoint(relX, relY))) continue;
focusedChild = child; focusedChild = child;
} }
if(focusedChild) { if(focusedChild) {
UIPoint childPos = focusedChild->getPosition(); UIPoint childPos = focusedChild->getPosition();
bool focusable = focusedChild->focusEnterAll(pos.x - childPos.x, pos.y - childPos.y); bool focusable = focusedChild->focusEnterAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos);
if(focusable) return true; if(focusable) return true;
} }
@ -153,21 +155,21 @@ bool UIElement::focusEnterAll(UIPoint pos, UIPoint screenPos) {
UIElement *UIElement::dragEnterAll(UIPoint pos, UIPoint screenPos) { UIElement *UIElement::dragEnterAll(UIPoint pos, UIPoint screenPos) {
UIBounds bounds = getBounds(); UIBounds bounds = getBounds();
if(x < bounds.x || y < bounds.y || x >= bounds.x + bounds.w || y >= bounds.y + bounds.h) return nullptr; // Only used by topmost parent UIElement if(!bounds.contains(pos)) return nullptr; // Only used by topmost parent UIElement
UIElement *draggedChild = nullptr; UIElement *draggedChild = nullptr;
for(UIElement *child : children) { for(UIElement *child : children) {
UIPoint pos = child->getPosition(); UIPoint childPos = child->getPosition();
int relX = x - pos.x; int relX = pos.x - childPos.x;
int relY = y - pos.y; int relY = pos.y - childPos.y;
UIBounds b = child->getBounds(); UIBounds b = child->getBounds();
if(relX < b.x || relY < b.y || relX > b.x + b.w || relY > b.y + b.h) continue; if(!b.contains(UIPoint(relX, relY))) continue;
draggedChild = child; draggedChild = child;
} }
if(draggedChild) { if(draggedChild) {
UIPoint pos = draggedChild->getPosition(); UIPoint childPos = draggedChild->getPosition();
UIElement *dragged = draggedChild->dragEnterAll(x - pos.x, y - pos.y, absX, absY); UIElement *dragged = draggedChild->dragEnterAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos);
if(dragged) return dragged; if(dragged) return dragged;
} }
@ -177,8 +179,7 @@ UIElement *UIElement::dragEnterAll(UIPoint pos, UIPoint screenPos) {
} }
void UIElement::init() { void UIElement::init() {
defaultFont = new Font("fonts/DejaVuSans.ttf"); defaultFont = new Font(KEK_DEFAULT_FONT);
} }
*/
} }

View File

@ -0,0 +1,140 @@
#include "uielements.h"
namespace kek {
static inline int offsetX(int w, Origin origin) {
switch(origin) {
case Origin::TOP_LEFT:
case Origin::BOTTOM_LEFT:
case Origin::LEFT_CENTER:
return 0;
case Origin::TOP_RIGHT:
case Origin::BOTTOM_RIGHT:
case Origin::RIGHT_CENTER:
return -w;
case Origin::TOP_CENTER:
case Origin::BOTTOM_CENTER:
case Origin::CENTER:
return -w / 2;
}
return 0;
}
static inline int offsetY(int h, Origin origin) {
switch(origin) {
case Origin::TOP_LEFT:
case Origin::TOP_CENTER:
case Origin::TOP_RIGHT:
return 0;
case Origin::BOTTOM_LEFT:
case Origin::BOTTOM_CENTER:
case Origin::BOTTOM_RIGHT:
return -h;
case Origin::LEFT_CENTER:
case Origin::RIGHT_CENTER:
case Origin::CENTER:
return -h / 2;
}
return 0;
}
static inline UIBounds offsetUIBounds(int w, int h, Origin origin) {
return UIBounds(offsetX(w, origin), offsetY(h, origin), w, h);
}
TextElement::TextElement(UIValue x, UIValue y, Font *font): UIElement(x, y) {
this->text = new TextObject(font, "Text");
this->sizePixels = KEK_DEFAULT_FONT_SIZE_PIXELS;
this->color = Colors::WHITE;
this->textMode = TextMode::ORIGIN;
this->textBounds = TextBounds::SMALLEST;
}
TextElement::TextElement(UIValue x, UIValue y): TextElement(x, y, UIElement::defaultFont) {}
TextElement::~TextElement() {
delete text;
}
UIElementType TextElement::getType() {
return UIElementType::TEXT;
}
UIBounds TextElement::getBounds() {
TextMetrics metrics = text->getMetrics(sizePixels);
int w = metrics.width, h, offsetY = 0;
switch(textBounds) {
case TextBounds::SMALLEST:
default:
h = metrics.height;
offsetY = metrics.offsetY;
break;
case TextBounds::LINE:
FontMetrics metrics = text->getFont()->getMetrics(sizePixels);
h = metrics.lineHeight;
offsetY = metrics.ascender;
break;
}
switch(textMode) {
case TextMode::BASELINE:
return UIBounds(offsetX(w, origin), -offsetY, w, h);
case TextMode::ORIGIN:
default:
return offsetUIBounds(w, h, origin);
}
}
void TextElement::setText(std::string text) {
this->text->setText(text);
}
void TextElement::setSizePixels(int sizePixels) {
this->sizePixels = sizePixels;
}
void TextElement::setColor(Color color) {
this->color = color;
}
void TextElement::setTextMode(TextMode textMode) {
this->textMode = textMode;
}
void TextElement::setTextBounds(TextBounds textBounds) {
this->textBounds = textBounds;
}
void TextElement::draw(UIPoint screenPos, glm::mat4 projection) {
UIBounds offset = getBounds();
switch(textMode) {
case TextMode::BASELINE:
text->getFont()->drawText(text, projection, offset.x + screenPos.x, screenPos.y, sizePixels, color);
break;
case TextMode::ORIGIN:
switch(textBounds) {
case TextBounds::SMALLEST:
default:
text->getFont()->drawTextFromOrigin(text, projection, offset.x + screenPos.x, offset.y + screenPos.y, sizePixels, color);
break;
case TextBounds::LINE:
FontMetrics metrics = text->getFont()->getMetrics(sizePixels);
int offsetY = metrics.ascender;
text->getFont()->drawText(text, projection, offset.x + screenPos.x, offset.y + screenPos.y + offsetY, sizePixels, color);
break;
}
break;
}
}
}

View File

@ -35,3 +35,6 @@
#define KEK_FONT_BITMAP_CHAR_BITS 8 // = log2(KEK_FONT_BITMAP_WIDTH_BLOCKS * KEK_FONT_BITMAP_HEIGHT_BLOCKS) #define KEK_FONT_BITMAP_CHAR_BITS 8 // = log2(KEK_FONT_BITMAP_WIDTH_BLOCKS * KEK_FONT_BITMAP_HEIGHT_BLOCKS)
#define KEK_FONT_BITMAP_CHAR_MASK 0xFF // = KEK_FONT_BITMAP_CHAR_BITS 1s in binary #define KEK_FONT_BITMAP_CHAR_MASK 0xFF // = KEK_FONT_BITMAP_CHAR_BITS 1s in binary
#define KEK_TEXT_BLOCK_SIZE 8 #define KEK_TEXT_BLOCK_SIZE 8
#define KEK_DEFAULT_FONT "font/MaredivRegular-yeg3.ttf"
#define KEK_DEFAULT_FONT_SIZE_PIXELS 24

View File

@ -4,6 +4,7 @@
#include "constants.h" #include "constants.h"
#include "engine.h" #include "engine.h"
#include "errordialog.h" #include "errordialog.h"
#include "fonts.h"
#include "gameobject.h" #include "gameobject.h"
#include "mesh.h" #include "mesh.h"
#include "object.h" #include "object.h"
@ -13,3 +14,5 @@
#include "shader.h" #include "shader.h"
#include "types.h" #include "types.h"
#include "utils.h" #include "utils.h"
#include "ui.h"
#include "uielements.h"

View File

@ -14,21 +14,26 @@
namespace kek { namespace kek {
struct UIPoint {
int x, y;
UIPoint(int x, int 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) {
this->x = x; this->x = x;
this->y = y; this->y = y;
this->w = w; this->w = w;
this->h = h; this->h = h;
} }
};
struct UIPoint { inline bool contains(UIPoint pos) {
int x, y; return pos.x < x || pos.y < y || pos.x >= x + w || pos.y >= y + h;
UIPoint(int x, int y) {
this->x = x;
this->y = y;
} }
}; };

View File

@ -0,0 +1,41 @@
#pragma once
#include "ui.h"
namespace kek {
class TextElement: public UIElement {
protected:
TextObject *text;
int sizePixels;
Color color;
TextMode textMode;
TextBounds textBounds;
public:
TextElement(UIValue x, UIValue y, Font *font);
TextElement(UIValue x, UIValue y);
virtual ~TextElement();
virtual UIElementType getType();
virtual UIBounds getBounds();
void setText(std::string text);
void setSizePixels(int sizePixels);
void setColor(Color color);
void setTextMode(TextMode textMode);
void setTextBounds(TextBounds textBounds);
virtual void draw(UIPoint screenPos, glm::mat4 projection);
};
}