Merge pull request #922 from MihailRis/new-ui-properties

add selectbox 'button' mode
This commit is contained in:
MihailRis 2026-08-09 22:04:53 +03:00 • committed by GitHub
commit e00038327f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 57 additions and 9 deletions

View file

@ -192,6 +192,9 @@ Example of list description:
- `width` - minimum content width. Default: 100.
- `selected` - initially selected value. Default: "".
- `onselect` - function to which the user-selected value is passed
- `mode` - behaviour / display mode:
- `select` - default mode.
- `button` - button mode: the text is not replaced with the selected option; the element rendered as a regular button.
## *bindbox*

View file

@ -193,6 +193,9 @@
- `width` - минимальная ширина содержимого. По-умолчанию: 100.
- `selected` - изначально выбранное значение. По-умолчанию: "".
- `onselect` - функция, в которую передаётся выбранное пользователем значение
- `mode` - режим работы:
- `select` - режим по-умолчанию.
- `button` - режим кнопки: текст не заменяется на выбранную опцию, а элемент выглядит как обычная кнопка.
## Привязка ввода - *bindbox*

View file

@ -260,7 +260,7 @@ void GUI::act(float delta, const glm::uvec2& vp) {
hover = nullptr;
}
}
if (focus) {
if (focus && focusedOnStart == focus.get()) {
actFocused();
}
if (focus && !focus->isFocused()) {
@ -274,6 +274,7 @@ void GUI::postAct() {
postRunnables.pop();
callback();
}
focusedOnStart = focus.get();
}
void GUI::draw(const DrawContext& pctx, Assets& assets) {

View file

@ -72,6 +72,7 @@ namespace gui {
std::shared_ptr<UINode> hover;
std::shared_ptr<UINode> pressed;
std::shared_ptr<UINode> focus;
const UINode* focusedOnStart = nullptr;
std::shared_ptr<UINode> tooltip;
std::shared_ptr<UiDocument> rootDocument;
std::unique_ptr<FontStylesScheme> syntaxColorScheme;

View file

@ -13,14 +13,21 @@ SelectBox::SelectBox(
GUI& gui,
std::vector<Option>&& options,
Option selected,
Mode mode,
int contentWidth,
const glm::vec4& padding
)
: Button(gui, selected.text, padding, nullptr, glm::vec2(contentWidth, -1)),
options(std::move(options)) {
listenAction(UIAction::CLICK, [this](GUI& gui) {
auto panel = std::make_shared<Panel>(gui, getSize());
: Button(gui, selected.text, padding, nullptr, glm::vec2(-1, -1)),
options(std::move(options)), mode(mode) {
listenAction(UIAction::CLICK, [this, contentWidth](GUI& gui) {
auto panel = std::make_shared<Panel>(
gui,
getMode() == Mode::BUTTON ? glm::vec2 {contentWidth, contentWidth}
: getSize(),
glm::vec4 {2.0f},
0.0f
);
panel->setColor({});
panel->setPadding(glm::vec4(0, size.y, 0, 0));
panel->setPos(calcPos() + glm::vec2(0, 0));
for (const auto& option : this->options) {
@ -51,7 +58,9 @@ void SelectBox::listenChange(OnStringChange&& callback) {
void SelectBox::setSelected(const Option& selected) {
this->selected = selected;
this->label->setText(selected.text);
if (mode == Mode::SELECT) {
this->label->setText(selected.text);
}
}
const SelectBox::Option& SelectBox::getSelected() const {
@ -66,6 +75,14 @@ void SelectBox::setOptions(std::vector<Option>&& options) {
this->options = std::move(options);
}
SelectBox::Mode SelectBox::getMode() const {
return mode;
}
void SelectBox::setMode(SelectBox::Mode mode) {
this->mode = mode;
}
void SelectBox::drawBackground(const DrawContext& pctx, const Assets&) {
glm::vec2 pos = calcPos();
auto batch = pctx.getBatch2D();
@ -73,6 +90,10 @@ void SelectBox::drawBackground(const DrawContext& pctx, const Assets&) {
batch->setColor(calcColor());
batch->rect(pos.x, pos.y, size.x, size.y);
batch->setColor({1.0f, 1.0f, 1.0f, 0.333f});
if (mode != Mode::SELECT) {
return;
}
int paddingRight = padding.w;
int widthHalf = 8;

View file

@ -11,15 +11,21 @@ namespace gui {
std::string value;
std::wstring text;
};
enum class Mode {
SELECT,
BUTTON,
};
private:
std::vector<Option> options;
Option selected {};
StringCallbacksSet changeCallbacks;
Mode mode;
public:
SelectBox(
GUI& gui,
std::vector<Option>&& elements,
Option selected,
Mode mode,
int contentWidth,
const glm::vec4& padding
);
@ -34,8 +40,13 @@ namespace gui {
void setOptions(std::vector<Option>&& options);
Mode getMode() const;
void setMode(Mode mode);
void drawBackground(const DrawContext& pctx, const Assets&) override;
std::shared_ptr<Label> getLabel() const;
};
}

View file

@ -458,8 +458,7 @@ static std::shared_ptr<UINode> read_select(
}
auto value = elem->attr("value").getText();
auto text = parse_inner_text(*elem, reader.getContext());
options.push_back(SelectBox::Option {std::move(value), std::move(text)}
);
options.push_back(SelectBox::Option {std::move(value), std::move(text)});
}
if (element.has("selected")) {
@ -481,10 +480,19 @@ static std::shared_ptr<UINode> read_select(
selected.text = innerText;
}
SelectBox::Mode mode = SelectBox::Mode::SELECT;
if (element.has("mode")) {
auto modeName = element.attr("mode").getText();
if (modeName == "button") {
mode = SelectBox::Mode::BUTTON;
}
}
auto selectBox = std::make_shared<SelectBox>(
gui,
std::move(options),
std::move(selected),
mode,
contentWidth,
std::move(padding)
);