Better TextField cursor

This commit is contained in:
MrLetsplay 2023-10-15 19:46:28 +02:00
parent 81ea6e0dc5
commit e619767b04
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
3 changed files with 39 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#include "uielements.h" #include "uielements.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <algorithm>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <string> #include <string>
@ -239,8 +240,24 @@ void TextFieldElement::updateText(std::u32string newText) {
this->text = Unicode::convertU32ToStd(newText); this->text = Unicode::convertU32ToStd(newText);
this->textElement->setText(this->text); this->textElement->setText(this->text);
UIBounds bounds = getBounds();
TextMetrics m = textElement->text->getMetrics(0, newText.length() - cursorPos, textElement->sizePixels); TextMetrics m = textElement->text->getMetrics(0, newText.length() - cursorPos, textElement->sizePixels);
this->cursor->x = uiPx(m.width); TextMetrics fullM = textElement->text->getMetrics(0, newText.length(), textElement->sizePixels);
// Set offset to keep cursor visible
float offsetX = -this->textElement->x.pixels;
float minOffsetX = std::max(m.width - bounds.w + 1, 0);
float maxOffsetX = m.width;
// Adjust offset to make sure the text field is always fully utilized (if possible)
float adjust = bounds.w - (fullM.width - offsetX);
std::cout << adjust << std::endl;
if(adjust > 0) offsetX -= adjust;
offsetX = clamp(offsetX, minOffsetX, maxOffsetX);
this->cursor->x = uiPx(m.width - offsetX);
this->textElement->x = uiPx(-offsetX);
} }
UIElementType TextFieldElement::getType() { UIElementType TextFieldElement::getType() {
@ -310,6 +327,9 @@ void TextFieldElement::focusExit() {
Input::uncaptureKeyboardInput(capture); Input::uncaptureKeyboardInput(capture);
capture = KEK_INVALID_ID; capture = KEK_INVALID_ID;
cursorPos = 0;
updateText(Unicode::convertStdToU32(this->text));
} }
void TextFieldElement::draw(UIPoint screenPos, glm::mat4 projection) { void TextFieldElement::draw(UIPoint screenPos, glm::mat4 projection) {

View File

@ -16,13 +16,13 @@ float clamp(float value, float min, float max) {
return value; return value;
} }
float clampCyclic(int value, int min, int max) { int clampCyclic(int value, int min, int max) {
while(value > max) value -= (max - min); while(value > max) value -= (max - min);
while(value < min) value += (max - min); while(value < min) value += (max - min);
return value; return value;
} }
float clamp(int value, int min, int max) { int clamp(int value, int min, int max) {
if(value > max) value = max; if(value > max) value = max;
if(value < min) value = min; if(value < min) value = min;
return value; return value;

View File

@ -39,9 +39,23 @@ float clampCyclic(float value, float min, float max);
float clamp(float value, float min, float max); float clamp(float value, float min, float max);
float clampCyclic(int value, int min, int max); int clampCyclic(int value, int min, int max);
float clamp(int value, int min, int max); int clamp(int value, int min, int max);
template <typename T>
T clamp(T value, T min, T max) {
if(value > max) value = max;
if(value < min) value = min;
return value;
}
template <typename T>
T clampCyclic(T value, T min, T max) {
while(value > max) value -= (max - min);
while(value < min) value += (max - min);
return value;
}
enum class CubeFace { enum class CubeFace {
FRONT, FRONT,