mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
fix textbox scrolling
This commit is contained in:
parent
1d4abd7758
commit
bebb5049ef
3 changed files with 28 additions and 15 deletions
|
|
@ -43,20 +43,28 @@ std::shared_ptr<UINode> Container::getAt(const glm::vec2& pos) {
|
||||||
return UINode::getAt(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) {
|
void Container::mouseMove(int x, int y) {
|
||||||
UINode::mouseMove(x, y);
|
UINode::mouseMove(x, y);
|
||||||
if (!scrollable) {
|
if (!scrollable || !scrollbarTriggered) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto pos = calcPos();
|
auto pos = calcPos();
|
||||||
x -= pos.x;
|
x -= pos.x;
|
||||||
y -= pos.y;
|
y -= pos.y;
|
||||||
if (prevScrollY == -1) {
|
|
||||||
if (x >= size.x - scrollBarWidth) {
|
|
||||||
prevScrollY = y;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int diff = (actualLength-size.y);
|
int diff = (actualLength-size.y);
|
||||||
if (diff > 0) {
|
if (diff > 0) {
|
||||||
scroll -= (y - prevScrollY) / static_cast<float>(size.y) * actualLength;
|
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) {
|
void Container::mouseRelease(int x, int y) {
|
||||||
UINode::mouseRelease(x, y);
|
UINode::mouseRelease(x, y);
|
||||||
prevScrollY = -1;
|
scrollbarTriggered = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Container::act(float delta) {
|
void Container::act(float delta) {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
namespace gui {
|
namespace gui {
|
||||||
class Container : public UINode, public ::util::ObjectsKeeper {
|
class Container : public UINode, public ::util::ObjectsKeeper {
|
||||||
int prevScrollY = -1;
|
int prevScrollY = -1;
|
||||||
|
bool scrollbarTriggered = false;
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::shared_ptr<UINode>> nodes;
|
std::vector<std::shared_ptr<UINode>> nodes;
|
||||||
std::vector<IntervalEvent> intervalEvents;
|
std::vector<IntervalEvent> intervalEvents;
|
||||||
|
|
@ -18,13 +19,14 @@ namespace gui {
|
||||||
int actualLength = 0;
|
int actualLength = 0;
|
||||||
bool scrollable = true;
|
bool scrollable = true;
|
||||||
|
|
||||||
bool isScrolling() {
|
bool isScrolling() const {
|
||||||
return prevScrollY != -1;
|
return scrollbarTriggered;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
Container(GUI& gui, glm::vec2 size);
|
Container(GUI& gui, glm::vec2 size);
|
||||||
virtual ~Container();
|
virtual ~Container();
|
||||||
|
|
||||||
|
virtual void click(int, int) override;
|
||||||
virtual void act(float delta) override;
|
virtual void act(float delta) override;
|
||||||
virtual void drawBackground(const DrawContext& pctx, const Assets& assets);
|
virtual void drawBackground(const DrawContext& pctx, const Assets& assets);
|
||||||
virtual void draw(const DrawContext& pctx, const Assets& assets) override;
|
virtual void draw(const DrawContext& pctx, const Assets& assets) override;
|
||||||
|
|
|
||||||
|
|
@ -648,7 +648,7 @@ void TextBox::refresh() {
|
||||||
label->setSize(
|
label->setSize(
|
||||||
size - glm::vec2(
|
size - glm::vec2(
|
||||||
padding.z + padding.x +
|
padding.z + padding.x +
|
||||||
LINE_NUMBERS_PANE_WIDTH * showLineNumbers + textOffset,
|
LINE_NUMBERS_PANE_WIDTH * showLineNumbers - textOffset,
|
||||||
padding.w + padding.y
|
padding.w + padding.y
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
@ -671,7 +671,9 @@ size_t TextBox::normalizeIndex(int index) {
|
||||||
/// @param y screen Y position
|
/// @param y screen Y position
|
||||||
/// @return non-normalized character index
|
/// @return non-normalized character index
|
||||||
int TextBox::calcIndexAt(int x, int y) const {
|
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();
|
const auto& labelText = label->getText();
|
||||||
glm::vec2 lcoord = label->calcPos();
|
glm::vec2 lcoord = label->calcPos();
|
||||||
uint line = label->getLineByYOffset(y - lcoord.y);
|
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) {
|
void TextBox::click(int x, int y) {
|
||||||
|
Container::click(x, y);
|
||||||
int index = normalizeIndex(calcIndexAt(x, y));
|
int index = normalizeIndex(calcIndexAt(x, y));
|
||||||
selectionStart = index;
|
selectionStart = index;
|
||||||
selectionEnd = index;
|
selectionEnd = index;
|
||||||
|
|
@ -1200,7 +1203,7 @@ void TextBox::setCaret(size_t position) {
|
||||||
if (font == nullptr) {
|
if (font == nullptr) {
|
||||||
return;
|
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.prepare(font, rawTextCache.metrics, width);
|
||||||
rawTextCache.update(input, multiline, label->isTextWrapping());
|
rawTextCache.update(input, multiline, label->isTextWrapping());
|
||||||
|
|
@ -1221,12 +1224,12 @@ void TextBox::setCaret(size_t position) {
|
||||||
}
|
}
|
||||||
int lcaret = caret - rawTextCache.getTextLineOffset(line);
|
int lcaret = caret - rawTextCache.getTextLineOffset(line);
|
||||||
int realoffset = rawTextCache.metrics.calcWidth(labelText, 0, lcaret) -
|
int realoffset = rawTextCache.metrics.calcWidth(labelText, 0, lcaret) -
|
||||||
static_cast<int>(textOffset) + 2;
|
static_cast<int>(textOffset);
|
||||||
|
|
||||||
if (realoffset - width > 0) {
|
if (realoffset - width > 0) {
|
||||||
setTextOffset(textOffset + realoffset - width);
|
setTextOffset(textOffset + realoffset - width);
|
||||||
} else if (realoffset < 0) {
|
} 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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue