add 'scroll-step' property to container

This commit is contained in:
MihailRis 2024-11-19 06:46:54 +03:00
parent 5c005b4f9a
commit 2412699b89
6 changed files with 21 additions and 3 deletions

View File

@ -56,7 +56,8 @@ Buttons and panels are also containers.
- `padding` - element padding. Type: 4D vector.
*left, top, right, bottom*
`scrollable` - element scrollability. Works on panels only. Type: boolean
- `scrollable` - element scrollability. Type: boolean.
- `scroll-step` - scrolling step. Type: integer.
# Common *panel* attributes

View File

@ -59,7 +59,8 @@
В число контейнеров также входят панели и кнопки.
- `padding` - внутренний отступ элемента. Тип: 4D вектор.
Порядок: `"left,top,right,bottom"`
- `scrollable` - возможность скроллинга. Работает только у Panel. Тип: логический.
- `scrollable` - возможность скроллинга. Тип: логический.
- `scroll-step` - шаг скроллинга. Тип: целочисленный.
# Общие атрибуты панелей

View File

@ -165,6 +165,14 @@ void Container::setSize(glm::vec2 size) {
}
}
int Container::getScrollStep() const {
return scrollStep;
}
void Container::setScrollStep(int step) {
scrollStep = step;
}
void Container::refresh() {
std::stable_sort(nodes.begin(), nodes.end(), [](const auto& a, const auto& b) {
return a->getZIndex() < b->getZIndex();

View File

@ -32,6 +32,8 @@ namespace gui {
void listenInterval(float interval, ontimeout callback, int repeat=-1);
virtual glm::vec2 getContentOffset() override {return glm::vec2(0.0f, scroll);};
virtual void setSize(glm::vec2 size) override;
virtual int getScrollStep() const;
virtual void setScrollStep(int step);
virtual void refresh() override;
const std::vector<std::shared_ptr<UINode>>& getNodes() const;

View File

@ -43,6 +43,7 @@ TextBox::TextBox(std::wstring placeholder, glm::vec4 padding)
textInitX = label->getPos().x;
scrollable = true;
scrollStep = 0;
}
void TextBox::draw(const DrawContext* pctx, Assets* assets) {
@ -746,7 +747,9 @@ void TextBox::setCaret(size_t position) {
uint line = label->getLineByTextIndex(caret);
int offset = label->getLineYOffset(line) + getContentOffset().y;
uint lineHeight = font->getLineHeight()*label->getLineInterval();
scrollStep = lineHeight;
if (scrollStep == 0) {
scrollStep = lineHeight;
}
if (offset < 0) {
scrolled(-glm::floor(offset/static_cast<double>(scrollStep)+0.5f));
} else if (offset >= getSize().y) {

View File

@ -172,6 +172,9 @@ static void _readContainer(UiXmlReader& reader, const xml::xmlelement& element,
if (element->has("scrollable")) {
container.setScrollable(element->attr("scrollable").asBool());
}
if (element->has("scroll-step")) {
container.setScrollStep(element->attr("scroll-step").asInt());
}
for (auto& sub : element->getElements()) {
if (sub->isText())
continue;