Share settings page helpers, add GUI cursor/scale API

- move create_trackbar_setting/update_trackbar_label/create_checkbox from
  settings_audio/display/graphics pages to core:settings_common module
- GUI scale trackbar shows the really applied scale, e.g. '3 (effective 2)'
- add gui.get_scale(), gui.get_max_scale(), gui.get_cursor_pos() (UI units);
  document that input.get_mouse_pos() returns window pixels
- InventoryView default color is defined once (InventoryView::DEFAULT_COLOR)
  instead of duplicated literals; 'color' attribute still overrides it
This commit is contained in:
loki5512344 2026-10-03 13:12:30 +00:00
parent 851c1a0e27
commit 33e132a231
15 changed files with 173 additions and 123 deletions

View file

@ -93,13 +93,18 @@ void GUI::onAssetsLoad(Assets* assets) {
static constexpr glm::uvec2 MIN_UI_SIZE {640, 480};
static constexpr int MAX_SCALE = 4;
int GUI::calcScale(const glm::uvec2& viewport) const {
int GUI::calcMaxScale(const glm::uvec2& viewport) {
int maxFit = 1;
while (maxFit < MAX_SCALE &&
viewport.x / (maxFit + 1) >= MIN_UI_SIZE.x &&
viewport.y / (maxFit + 1) >= MIN_UI_SIZE.y) {
maxFit++;
}
return maxFit;
}
int GUI::calcScale(const glm::uvec2& viewport) const {
int maxFit = calcMaxScale(viewport);
int value = engine.getSettings().display.guiScale.get();
if (value <= 0) {
return maxFit; // auto
@ -111,6 +116,10 @@ int GUI::getScale() const {
return scale;
}
int GUI::getMaxScale() const {
return calcMaxScale(engine.getWindow().getSize());
}
CursorState GUI::getCursor() const {
auto cursor = input.getCursor();
cursor.pos /= static_cast<float>(scale);

View file

@ -95,6 +95,7 @@ namespace gui {
bool doubleClicked = false;
bool debug = false;
static int calcMaxScale(const glm::uvec2& viewport);
int calcScale(const glm::uvec2& viewport) const;
CursorState getScaledCursor() const;
@ -134,6 +135,9 @@ namespace gui {
/// @brief Get current GUI scale: window pixels per one UI unit
int getScale() const;
/// @brief Get maximal GUI scale that fits the current window size
int getMaxScale() const;
/// @brief Get cursor state in UI coordinates
CursorState getCursor() const;

View file

@ -78,7 +78,7 @@ void InventoryBuilder::addGrid(
if (addpanel) {
auto panel =
std::make_shared<gui::Container>(gui, glm::vec2(width, height));
view->setColor(glm::vec4(0.122f, 0.122f, 0.122f, 0.878f));
view->setColor(InventoryView::DEFAULT_COLOR);
view->add(panel, pos);
}

View file

@ -160,6 +160,8 @@ namespace gui {
static const int SLOT_INTERVAL = 4;
static const int SLOT_SIZE = ITEM_ICON_SIZE;
/// @brief Default background color (can be overridden by 'color' attribute)
static inline const glm::vec4 DEFAULT_COLOR {0.122f, 0.122f, 0.122f, 0.878f};
};
class InventoryBuilder {

View file

@ -860,7 +860,7 @@ static std::shared_ptr<UINode> read_inventory(
UiXmlReader& reader, const xml::xmlelement& element
) {
auto view = std::make_shared<InventoryView>(reader.getGUI());
view->setColor(glm::vec4(0.122f, 0.122f, 0.122f, 0.878f)); // TODO: fixme
view->setColor(InventoryView::DEFAULT_COLOR);
reader.addIgnore("slot");
reader.addIgnore("slots-grid");
reader.readUINode(reader, element, *view);

View file

@ -1067,6 +1067,20 @@ static int l_gui_getviewport(lua::State* L) {
return lua::pushvec2(L, engine->getGUI().getContainer()->getSize());
}
static int l_gui_get_scale(lua::State* L) {
return lua::pushinteger(L, engine->getGUI().getScale());
}
static int l_gui_get_max_scale(lua::State* L) {
return lua::pushinteger(L, engine->getGUI().getMaxScale());
}
static int l_gui_get_cursor_pos(lua::State* L) {
if (engine->isHeadless())
return 0;
return lua::pushvec2(L, engine->getGUI().getCursor().pos);
}
static int l_gui_clear_markup(lua::State* L) {
auto lang = lua::require_string(L, 1);
std::string text = lua::require_string(L, 2);
@ -1247,6 +1261,9 @@ static int l_screenshot(lua::State* L) {
const luaL_Reg guilib[] = {
{"screenshot", lua::wrap<l_screenshot>},
{"get_viewport", lua::wrap<l_gui_getviewport>},
{"get_scale", lua::wrap<l_gui_get_scale>},
{"get_max_scale", lua::wrap<l_gui_get_max_scale>},
{"get_cursor_pos", lua::wrap<l_gui_get_cursor_pos>},
{"getattr", lua::wrap<l_gui_getattr>},
{"setattr", lua::wrap<l_gui_setattr>},
{"get_env", lua::wrap<l_gui_get_env>},