diff --git a/doc/en/scripting/ui.md b/doc/en/scripting/ui.md index 7a11b2f5e..efe0dc2ae 100644 --- a/doc/en/scripting/ui.md +++ b/doc/en/scripting/ui.md @@ -231,6 +231,25 @@ Properties: | value | string | yes | yes | Selected value | | options | table | yes | yes | List of options (tables `{value=..., text=...}`) | +## Progress bar (progressbar) + +Properties: + +| Name | Type | Read | Write | Description | +|--------------|--------|------|-------|---------------------------------------------------------------| +| value | float | yes | yes | current value | +| min | float | yes | yes | minimum value | +| max | float | yes | yes | maximum value | +| progress | float | yes | no | progress 0..1 | +| displayValue | float | yes | no | displayed (animated) value | +| color | rgba | yes | yes | color of the filled part | +| bgColor | rgba | yes | yes | background color (empty part) | +| textColor | rgba | yes | yes | text color | +| text | string | yes | yes | text centered in the bar | +| smooth | bool | yes | yes | smooth value change | +| smoothSpeed | number | yes | yes | smooth transition speed in units per second | +| orientation | string | yes | yes | fill direction: "horizontal" / "vertical" | + ## Inventory Properties: diff --git a/doc/en/xml-ui-layouts.md b/doc/en/xml-ui-layouts.md index 704a0b786..99a399d89 100644 --- a/doc/en/xml-ui-layouts.md +++ b/doc/en/xml-ui-layouts.md @@ -198,6 +198,21 @@ Example of list description: - `select` - default mode. - `button` - button mode: the text is not replaced with the selected option; the element rendered as a regular button. +## Progress bar - *progressbar* + +Non-interactive element for displaying progress. Draws a background and a filled part. + +- `min` - minimal value. Type: number. Default: 0 +- `max` - maximal value. Type: number. Default: 100 +- `value` - initial value. Type: number. Default: 0 +- `color` - color of the filled part. Type: RGBA color. Default: blue `#3399FFCC` +- `bg-color` - background color (empty part). Type: RGBA color. Default: semi-transparent black `#00000066` +- `orientation` - fill direction: `horizontal` / `vertical` +- `smooth` - smooth value change on updates. Type: boolean. Default: false +- `smooth-speed` - smooth transition speed in units per second. Type: number. Default: 120 +- `text-color` - text color. Type: RGBA color. Default: white `#FFFFFFFF` +- `font` - font name. Type: string. Default: "normal" + ## *bindbox* An element for displaying and editing key or mouse button bindings. diff --git a/doc/ru/scripting/ui.md b/doc/ru/scripting/ui.md index 09e296f9b..c5e24796e 100644 --- a/doc/ru/scripting/ui.md +++ b/doc/ru/scripting/ui.md @@ -231,6 +231,25 @@ Canvas.decode(data: Bytearray, format: str) -> Canvas | value | string | да | да | Выбранное значение | | options | table | да | да | Список опций (таблиц `{value=..., text=...}`) | +## Прогресс-бар (progressbar) + +Свойства: + +| Название | Тип | Чтение | Запись | Описание | +|--------------|--------|--------|--------|------------------------------------------------------------------------| +| value | float | да | да | текущее значение | +| min | float | да | да | минимальное значение | +| max | float | да | да | максимальное значение | +| progress | float | да | нет | прогресс 0..1 | +| displayValue | float | да | нет | отображаемое (анимированное) значение | +| color | rgba | да | да | цвет заполненной части | +| bgColor | rgba | да | да | цвет фона (пустой части) | +| textColor | rgba | да | да | цвет текста | +| text | string | да | да | текст по центру бара | +| smooth | bool | да | да | плавное изменение значения | +| smoothSpeed | number | да | да | скорость плавного перехода в единицах в секунду | +| orientation | string | да | да | направление заполнения: "horizontal" / "vertical" | + ## Инвентарь (inventory) Свойства: diff --git a/doc/ru/xml-ui-layouts.md b/doc/ru/xml-ui-layouts.md index 9be788e7f..2ab04e446 100644 --- a/doc/ru/xml-ui-layouts.md +++ b/doc/ru/xml-ui-layouts.md @@ -199,6 +199,21 @@ - `select` - режим по-умолчанию. - `button` - режим кнопки: текст не заменяется на выбранную опцию, а элемент выглядит как обычная кнопка. +## Прогресс-бар - *progressbar* + +Неинтерактивный элемент для отображения прогресса. Рисует фон и заполненную часть. + +- `min` — минимальное значение. Тип: число. По-умолчанию: 0 +- `max` — максимальное значение. Тип: число. По-умолчанию: 100 +- `value` — начальное значение. Тип: число. По-умолчанию: 0 +- `color` — цвет заполненной части. Тип: RGBA цвет. По-умолчанию: синий `#3399FFCC` +- `bg-color` — цвет фона (пустой части). Тип: RGBA цвет. По-умолчанию: чёрный полупрозрачный `#00000066` +- `orientation` — направление заполнения: `horizontal` / `vertical` +- `smooth` — плавное изменение значения при обновлениях. Тип: логический. По-умолчанию: false +- `smooth-speed` — скорость плавного перехода в единицах в секунду. Тип: число. По-умолчанию: 120 +- `text-color` — цвет текста. Тип: RGBA цвет. По-умолчанию: белый `#FFFFFFFF` +- `font` — имя шрифта. Тип: строка. По-умолчанию: "normal" + ## Привязка ввода - *bindbox* Элемент для отображения и изменения привязки клавиш или кнопок мыши. diff --git a/src/graphics/ui/elements/ProgressBar.cpp b/src/graphics/ui/elements/ProgressBar.cpp new file mode 100644 index 000000000..a1c698863 --- /dev/null +++ b/src/graphics/ui/elements/ProgressBar.cpp @@ -0,0 +1,172 @@ +#include "ProgressBar.hpp" + +#include +#include + +#include "graphics/core/Batch2D.hpp" +#include "graphics/core/DrawContext.hpp" +#include "graphics/core/Font.hpp" +#include "assets/Assets.hpp" + +using namespace gui; + +ProgressBar::ProgressBar( + GUI& gui, + double min, + double max, + double value +) : UINode(gui, glm::vec2(100, 16)), + min(min), + max(max), + value(value), + displayValue(value) +{ + setColor(glm::vec4(0.2f, 0.6f, 1.0f, 0.8f)); + setInteractive(false); +} + +void ProgressBar::act(float delta) { + UINode::act(delta); + if (smoothTransition) { + double diff = value - displayValue; + if (std::fabs(diff) < 2.0) { + displayValue = value; + } else { + double step = smoothSpeed * delta; + if (std::fabs(diff) < step) { + displayValue = value; + } else { + displayValue += std::copysign(step, diff); + } + } + } else { + displayValue = value; + } +} + +void ProgressBar::draw(const DrawContext& pctx, const Assets& assets) { + glm::vec2 pos = calcPos(); + auto batch = pctx.getBatch2D(); + batch->texture(nullptr); + + batch->setColor(bgColor); + batch->rect(pos.x, pos.y, size.x, size.y); + + float t = 0.0f; + if (max > min) { + t = glm::clamp( + static_cast((displayValue - min) / (max - min)), + 0.0f, + 1.0f + ); + } + + if (t > 0.0f) { + batch->setColor(color); + if (orientation == Orientation::HORIZONTAL) { + batch->rect(pos.x, pos.y, size.x * t, size.y); + } else { + batch->rect(pos.x, pos.y + size.y * (1.0f - t), size.x, size.y * t); + } + } + + if (!text.empty()) { + auto font = assets.getShared(fontName); + if (font) { + int textWidth = font->calcWidth(text); + int lineHeight = font->getLineHeight(); + int tx = static_cast(pos.x + (size.x - textWidth) * 0.5f); + int ty = static_cast(pos.y + (size.y - lineHeight) * 0.5f); + batch->setColor(textColor); + font->draw(*batch, text, tx, ty, nullptr, 0); + } + } +} + +double ProgressBar::getValue() const { + return value; +} + +double ProgressBar::getMin() const { + return min; +} + +double ProgressBar::getMax() const { + return max; +} + +double ProgressBar::getProgress() const { + return (max - min != 0.0) ? (value - min) / (max - min) : 0.0; +} + +double ProgressBar::getDisplayValue() const { + return displayValue; +} + +glm::vec4 ProgressBar::getBgColor() const { + return bgColor; +} + +glm::vec4 ProgressBar::getTextColor() const { + return textColor; +} + +const std::wstring& ProgressBar::getText() const { + return text; +} + +bool ProgressBar::isSmooth() const { + return smoothTransition; +} + +float ProgressBar::getSmoothSpeed() const { + return smoothSpeed; +} + +Orientation ProgressBar::getOrientation() const { + return orientation; +} + +void ProgressBar::setValue(double x) { + value = x; +} + +void ProgressBar::setMin(double x) { + min = x; +} + +void ProgressBar::setMax(double x) { + max = x; +} + +void ProgressBar::setBgColor(glm::vec4 color) { + bgColor = color; +} + +void ProgressBar::setTextColor(glm::vec4 color) { + textColor = color; +} + +void ProgressBar::setText(std::wstring text) { + this->text = std::move(text); +} + +void ProgressBar::setFontName(std::string name) { + fontName = std::move(name); +} + +const std::string& ProgressBar::getFontName() const { + return fontName; +} + +void ProgressBar::setSmooth(bool flag) { + smoothTransition = flag; +} + +void ProgressBar::setSmoothSpeed(float speed) { + smoothSpeed = speed; +} + +void ProgressBar::setOrientation(Orientation ori) { + orientation = ori; +} diff --git a/src/graphics/ui/elements/ProgressBar.hpp b/src/graphics/ui/elements/ProgressBar.hpp new file mode 100644 index 000000000..aa0b1b7a4 --- /dev/null +++ b/src/graphics/ui/elements/ProgressBar.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include + +#include "UINode.hpp" +#include "commons.hpp" + +namespace gui { + class ProgressBar final : public UINode { + protected: + glm::vec4 bgColor {0.0f, 0.0f, 0.0f, 0.4f}; + glm::vec4 textColor {1.0f, 1.0f, 1.0f, 1.0f}; + std::wstring text; + double min; + double max; + double value; + double displayValue; + float smoothSpeed = 100.0f; + Orientation orientation = Orientation::HORIZONTAL; + bool smoothTransition = false; + std::string fontName = "normal"; + public: + ProgressBar( + GUI& gui, + double min = 0.0, + double max = 100.0, + double value = 0.0 + ); + void act(float delta) override; + void draw(const DrawContext& pctx, const Assets& assets) override; + + double getValue() const; + double getMin() const; + double getMax() const; + double getProgress() const; + double getDisplayValue() const; + glm::vec4 getBgColor() const; + glm::vec4 getTextColor() const; + Orientation getOrientation() const; + const std::wstring& getText() const; + const std::string& getFontName() const; + bool isSmooth() const; + float getSmoothSpeed() const; + + void setValue(double); + void setMin(double); + void setMax(double); + void setBgColor(glm::vec4); + void setTextColor(glm::vec4); + void setText(std::wstring text); + void setFontName(std::string name); + void setSmooth(bool flag); + void setSmoothSpeed(float speed); + void setOrientation(Orientation); + }; +} diff --git a/src/graphics/ui/gui_xml.cpp b/src/graphics/ui/gui_xml.cpp index 41a603cea..ae6897677 100644 --- a/src/graphics/ui/gui_xml.cpp +++ b/src/graphics/ui/gui_xml.cpp @@ -15,6 +15,7 @@ #include "elements/Menu.hpp" #include "elements/ModelViewer.hpp" #include "elements/Panel.hpp" +#include "elements/ProgressBar.hpp" #include "elements/SelectBox.hpp" #include "elements/SplitBox.hpp" #include "elements/TextBox.hpp" @@ -725,6 +726,39 @@ static std::shared_ptr read_track_bar( return bar; } +static std::shared_ptr read_progress_bar( + const UiXmlReader& reader, + const xml::xmlelement& element +) { + double minv = element.attr("min", "0.0").asFloat(); + double maxv = element.attr("max", "100.0").asFloat(); + double def = element.attr("value", "0.0").asFloat(); + auto bar = std::make_shared(reader.getGUI(), minv, maxv, def); + read_uinode(reader, element, *bar); + if (element.has("font")) { + bar->setFontName(element.attr("font").getText()); + } + if (element.has("bg-color")) { + bar->setBgColor(element.attr("bg-color").asColor()); + } + if (element.has("text-color")) { + bar->setTextColor(element.attr("text-color").asColor()); + } + if (element.has("smooth")) { + bar->setSmooth(element.attr("smooth").asBool()); + } + if (element.has("smooth-speed")) { + bar->setSmoothSpeed(element.attr("smooth-speed").asFloat()); + } + if (element.has("orientation")) { + auto& oname = element.attr("orientation").getText(); + if (oname == "vertical") { + bar->setOrientation(Orientation::VERTICAL); + } + } + return bar; +} + static std::shared_ptr read_input_bind_box( UiXmlReader& reader, const xml::xmlelement& element ) { @@ -910,6 +944,7 @@ UiXmlReader::UiXmlReader(gui::GUI& gui, const scriptenv& env) add("select", read_select); add("textbox", read_text_box); add("pagebox", read_page_box); + add("progressbar", read_progress_bar); add("splitbox", read_split_box); add("checkbox", read_check_box); add("trackbar", read_track_bar); diff --git a/src/logic/scripting/lua/libs/libgui.cpp b/src/logic/scripting/lua/libs/libgui.cpp index dc4806d00..d9616d4e4 100644 --- a/src/logic/scripting/lua/libs/libgui.cpp +++ b/src/logic/scripting/lua/libs/libgui.cpp @@ -12,6 +12,7 @@ #include "graphics/ui/elements/InventoryView.hpp" #include "graphics/ui/elements/Menu.hpp" #include "graphics/ui/elements/Panel.hpp" +#include "graphics/ui/elements/ProgressBar.hpp" #include "graphics/ui/elements/TextBox.hpp" #include "graphics/ui/elements/TrackBar.hpp" #include "graphics/ui/elements/InlineFrame.hpp" @@ -271,15 +272,54 @@ static int p_is_checked(UINode* node, lua::State* L) { static int p_get_value(UINode* node, lua::State* L) { if (auto bar = dynamic_cast(node)) { return lua::pushnumber(L, bar->getValue()); + } else if (auto bar = dynamic_cast(node)) { + return lua::pushnumber(L, bar->getValue()); } else if (auto box = dynamic_cast(node)) { return lua::pushstring(L, box->getSelected().value); } return 0; } +static int p_get_progress(UINode* node, lua::State* L) { + if (auto bar = dynamic_cast(node)) { + return lua::pushnumber(L, bar->getProgress()); + } + return 0; +} + +static int p_get_display_value(UINode* node, lua::State* L) { + if (auto bar = dynamic_cast(node)) { + return lua::pushnumber(L, bar->getDisplayValue()); + } + return 0; +} + +static int p_get_bg_color(UINode* node, lua::State* L) { + if (auto bar = dynamic_cast(node)) { + return lua::pushcolor(L, bar->getBgColor()); + } + return 0; +} + +static int p_is_smooth(UINode* node, lua::State* L) { + if (auto bar = dynamic_cast(node)) { + return lua::pushboolean(L, bar->isSmooth()); + } + return 0; +} + +static int p_get_smooth_speed(UINode* node, lua::State* L) { + if (auto bar = dynamic_cast(node)) { + return lua::pushnumber(L, bar->getSmoothSpeed()); + } + return 0; +} + static int p_get_min(UINode* node, lua::State* L) { if (auto bar = dynamic_cast(node)) { return lua::pushnumber(L, bar->getMin()); + } else if (auto bar = dynamic_cast(node)) { + return lua::pushnumber(L, bar->getMin()); } return 0; } @@ -287,6 +327,8 @@ static int p_get_min(UINode* node, lua::State* L) { static int p_get_max(UINode* node, lua::State* L) { if (auto bar = dynamic_cast(node)) { return lua::pushnumber(L, bar->getMax()); + } else if (auto bar = dynamic_cast(node)) { + return lua::pushnumber(L, bar->getMax()); } return 0; } @@ -315,10 +357,33 @@ static int p_get_track_color(UINode* node, lua::State* L) { static int p_get_text_color(UINode* node, lua::State* L) { if (auto box = dynamic_cast(node)) { return lua::pushcolor(L, box->getTextColor()); + } else if (auto bar = dynamic_cast(node)) { + return lua::pushcolor(L, bar->getTextColor()); } return 0; } +static int p_get_orientation(UINode* node, lua::State* L) { + if (auto bar = dynamic_cast(node)) { + if (bar->getOrientation() == Orientation::VERTICAL) { + return lua::pushstring(L, "vertical"); + } + return lua::pushstring(L, "horizontal"); + } + return 0; +} + +static void p_set_orientation(UINode* node, lua::State* L, int idx) { + if (auto bar = dynamic_cast(node)) { + std::string_view name = lua::require_string(L, idx); + if (name == "vertical") { + bar->setOrientation(Orientation::VERTICAL); + } else if (name == "horizontal") { + bar->setOrientation(Orientation::HORIZONTAL); + } + } +} + static int p_is_valid(UINode* node, lua::State* L) { if (auto box = dynamic_cast(node)) { return lua::pushboolean(L, box->validate()); @@ -354,6 +419,8 @@ static int p_get_text(UINode* node, lua::State* L) { return lua::pushwstring(L, label->getText()); } else if (auto box = dynamic_cast(node)) { return lua::pushwstring(L, box->getText()); + } else if (auto bar = dynamic_cast(node)) { + return lua::pushwstring(L, bar->getText()); } return 0; } @@ -684,13 +751,20 @@ static int l_gui_getattr(lua::State* L) { {"src", p_get_src}, {"fallback", p_get_fallback}, {"value", p_get_value}, + {"progress", p_get_progress}, + {"displayValue", p_get_display_value}, {"min", p_get_min}, {"max", p_get_max}, {"step", p_get_step}, {"scroll", p_get_scroll}, {"trackWidth", p_get_track_width}, {"trackColor", p_get_track_color}, + {"bgColor", p_get_bg_color}, + {"textColor", p_get_text_color}, + {"smooth", p_is_smooth}, + {"smoothSpeed", p_get_smooth_speed}, + {"orientation", p_get_orientation}, {"checked", p_is_checked}, {"page", p_get_page}, {"back", p_get_back}, @@ -765,6 +839,8 @@ static void p_set_text(UINode* node, lua::State* L, int idx) { button->setText(lua::require_wstring(L, idx)); } else if (auto box = dynamic_cast(node)) { box->setText(lua::require_wstring(L, idx)); + } else if (auto bar = dynamic_cast(node)) { + bar->setText(lua::require_wstring(L, idx)); } } static void p_set_caret(UINode* node, lua::State* L, int idx) { @@ -859,6 +935,8 @@ static void p_set_zindex(UINode* node, lua::State* L, int idx) { static void p_set_value(UINode* node, lua::State* L, int idx) { if (auto bar = dynamic_cast(node)) { bar->setValue(lua::tonumber(L, idx)); + } else if (auto bar = dynamic_cast(node)) { + bar->setValue(lua::tonumber(L, idx)); } else if (auto selectbox = dynamic_cast(node)) { auto value = lua::require_lstring(L, idx); const auto& options = selectbox->getOptions(); @@ -875,11 +953,32 @@ static void p_set_value(UINode* node, lua::State* L, int idx) { static void p_set_min(UINode* node, lua::State* L, int idx) { if (auto bar = dynamic_cast(node)) { bar->setMin(lua::tonumber(L, idx)); + } else if (auto bar = dynamic_cast(node)) { + bar->setMin(lua::tonumber(L, idx)); } } static void p_set_max(UINode* node, lua::State* L, int idx) { if (auto bar = dynamic_cast(node)) { bar->setMax(lua::tonumber(L, idx)); + } else if (auto bar = dynamic_cast(node)) { + bar->setMax(lua::tonumber(L, idx)); + } +} + +static void p_set_bg_color(UINode* node, lua::State* L, int idx) { + if (auto bar = dynamic_cast(node)) { + bar->setBgColor(lua::tocolor(L, idx)); + } +} + +static void p_set_smooth(UINode* node, lua::State* L, int idx) { + if (auto bar = dynamic_cast(node)) { + bar->setSmooth(lua::toboolean(L, idx)); + } +} +static void p_set_smooth_speed(UINode* node, lua::State* L, int idx) { + if (auto bar = dynamic_cast(node)) { + bar->setSmoothSpeed(lua::tonumber(L, idx)); } } static void p_set_step(UINode* node, lua::State* L, int idx) { @@ -900,6 +999,8 @@ static void p_set_track_color(UINode* node, lua::State* L, int idx) { static void p_set_text_color(UINode* node, lua::State* L, int idx) { if (auto box = dynamic_cast(node)) { box->setTextColor(lua::tocolor(L, idx)); + } else if (auto bar = dynamic_cast(node)) { + bar->setTextColor(lua::tocolor(L, idx)); } } static void p_set_checked(UINode* node, lua::State* L, int idx) { @@ -997,7 +1098,11 @@ static int l_gui_setattr(lua::State* L) { {"scroll", p_set_scroll}, {"trackWidth", p_set_track_width}, {"trackColor", p_set_track_color}, + {"bgColor", p_set_bg_color}, {"textColor", p_set_text_color}, + {"smooth", p_set_smooth}, + {"smoothSpeed", p_set_smooth_speed}, + {"orientation", p_set_orientation}, {"checked", p_set_checked}, {"page", p_set_page}, {"inventory", p_set_inventory},