fix textbox scrolling

This commit is contained in:
MihailRis 2026-05-03 21:14:56 +03:00
parent 1d4abd7758
commit bebb5049ef
3 changed files with 28 additions and 15 deletions

View file

@ -43,20 +43,28 @@ std::shared_ptr<UINode> Container::getAt(const glm::vec2& pos) {
return UINode::getAt(pos);
}
void Container::click(int x, int y) {
UINode::click(x, y);
if (scrollable) {
auto pos = calcPos();
x -= pos.x;
y -= pos.y;
if (x >= size.x - scrollBarWidth) {
scrollbarTriggered = true;
prevScrollY = y;
}
}
}
void Container::mouseMove(int x, int y) {
UINode::mouseMove(x, y);
if (!scrollable) {
if (!scrollable || !scrollbarTriggered) {
return;
}
auto pos = calcPos();
x -= pos.x;
y -= pos.y;
if (prevScrollY == -1) {
if (x >= size.x - scrollBarWidth) {
prevScrollY = y;
}
return;
}
int diff = (actualLength-size.y);
if (diff > 0) {
scroll -= (y - prevScrollY) / static_cast<float>(size.y) * actualLength;
@ -69,7 +77,7 @@ void Container::mouseMove(int x, int y) {
void Container::mouseRelease(int x, int y) {
UINode::mouseRelease(x, y);
prevScrollY = -1;
scrollbarTriggered = false;
}
void Container::act(float delta) {

View file

@ -9,6 +9,7 @@
namespace gui {
class Container : public UINode, public ::util::ObjectsKeeper {
int prevScrollY = -1;
bool scrollbarTriggered = false;
protected:
std::vector<std::shared_ptr<UINode>> nodes;
std::vector<IntervalEvent> intervalEvents;
@ -18,13 +19,14 @@ namespace gui {
int actualLength = 0;
bool scrollable = true;
bool isScrolling() {
return prevScrollY != -1;
bool isScrolling() const {
return scrollbarTriggered;
}
public:
Container(GUI& gui, glm::vec2 size);
virtual ~Container();
virtual void click(int, int) override;
virtual void act(float delta) override;
virtual void drawBackground(const DrawContext& pctx, const Assets& assets);
virtual void draw(const DrawContext& pctx, const Assets& assets) override;

View file

@ -648,7 +648,7 @@ void TextBox::refresh() {
label->setSize(
size - glm::vec2(
padding.z + padding.x +
LINE_NUMBERS_PANE_WIDTH * showLineNumbers + textOffset,
LINE_NUMBERS_PANE_WIDTH * showLineNumbers - textOffset,
padding.w + padding.y
)
);
@ -671,7 +671,9 @@ size_t TextBox::normalizeIndex(int index) {
/// @param y screen Y position
/// @return non-normalized character index
int TextBox::calcIndexAt(int x, int y) const {
if (!rawTextCache.metrics.font.has_value()) return 0;
if (!rawTextCache.metrics.font.has_value()) {
return 0;
}
const auto& labelText = label->getText();
glm::vec2 lcoord = label->calcPos();
uint line = label->getLineByYOffset(y - lcoord.y);
@ -733,6 +735,7 @@ void TextBox::doubleClick(int x, int y) {
}
void TextBox::click(int x, int y) {
Container::click(x, y);
int index = normalizeIndex(calcIndexAt(x, y));
selectionStart = index;
selectionEnd = index;
@ -1200,7 +1203,7 @@ void TextBox::setCaret(size_t position) {
if (font == nullptr) {
return;
}
int width = label->getSize().x;
int width = size.x - padding.x - padding.z - LINE_NUMBERS_PANE_WIDTH * showLineNumbers;
rawTextCache.prepare(font, rawTextCache.metrics, width);
rawTextCache.update(input, multiline, label->isTextWrapping());
@ -1221,12 +1224,12 @@ void TextBox::setCaret(size_t position) {
}
int lcaret = caret - rawTextCache.getTextLineOffset(line);
int realoffset = rawTextCache.metrics.calcWidth(labelText, 0, lcaret) -
static_cast<int>(textOffset) + 2;
static_cast<int>(textOffset);
if (realoffset - width > 0) {
setTextOffset(textOffset + realoffset - width);
} else if (realoffset < 0) {
setTextOffset(std::max(textOffset + realoffset, static_cast<size_t>(0)));
setTextOffset(std::max<int>(textOffset + realoffset - static_cast<int>(padding.x + padding.w), 0));
}
}