Better TextField cursor
This commit is contained in:
parent
81ea6e0dc5
commit
e619767b04
@ -1,6 +1,7 @@
|
||||
#include "uielements.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <algorithm>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <string>
|
||||
|
||||
@ -239,8 +240,24 @@ void TextFieldElement::updateText(std::u32string newText) {
|
||||
this->text = Unicode::convertU32ToStd(newText);
|
||||
this->textElement->setText(this->text);
|
||||
|
||||
UIBounds bounds = getBounds();
|
||||
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() {
|
||||
@ -310,6 +327,9 @@ void TextFieldElement::focusExit() {
|
||||
|
||||
Input::uncaptureKeyboardInput(capture);
|
||||
capture = KEK_INVALID_ID;
|
||||
|
||||
cursorPos = 0;
|
||||
updateText(Unicode::convertStdToU32(this->text));
|
||||
}
|
||||
|
||||
void TextFieldElement::draw(UIPoint screenPos, glm::mat4 projection) {
|
||||
|
@ -16,13 +16,13 @@ float clamp(float value, float min, float max) {
|
||||
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 < min) value += (max - min);
|
||||
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 < min) value = min;
|
||||
return value;
|
||||
|
@ -39,9 +39,23 @@ float clampCyclic(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 {
|
||||
FRONT,
|
||||
|
Loading…
Reference in New Issue
Block a user