voxelcore/src/graphics/ui/GUI.hpp
alexei-zebra 6c2192b5d8
Draw the window title bar by the engine
The native Wayland backend had no window controls: no title, no close
button, no way to move or resize the window, and the fullscreen toggle
could throw the window to another monitor. GNOME does not implement
zxdg_decoration_manager_v1, so there is nobody to draw a frame for us:
the backend now falls back to its own decorations when the compositor
offers no server side ones, and the engine draws the visible part.

The bar itself is drawn by the engine (WindowBar.cpp) through the usual
batch and font, so it uses the same assets as the rest of the UI and the
backend only owns the behaviour: hit zones, cursor shapes, click
interception and the xdg-shell requests. Its geometry follows the
desktop's button layout, read and subscribed through
xdg-desktop-portal over sd-bus (libsystemd is an optional dependency,
gsettings is only a startup fallback).

Controls behave like they do elsewhere:
- pressing anywhere on the bar starts a move once the pointer leaves a
  5 px radius, so dragging works from the buttons as well;
- a button acts on release, only if the pointer stayed on it;
- a double click on the bar toggles maximize;
- the top edge and the top corners resize, the top 8 px of the sides,
  the sides and the bottom are 8 px wide resize strips;
- right and middle clicks and the scroll wheel over the bar never reach
  the game.

The in-game user interface is offset below the bar while menus keep the
whole window, so the translucent bar shows the menu background through
itself and never covers the HUD. The HUD lays its elements out against
the content height and the cursor is translated once, which fixes items
following the mouse one row above the pointer in game.

The window size is now written to the settings when the window is
resized in windowed mode and applied on the first configure, so the
window opens with the size it was left with, and leaving fullscreen or
borderless mode restores it instead of overwriting the settings with the
fullscreen size (an unconditional unmaximize made the compositor restore
the maximized geometry).

Measured: menu layout unchanged (706 bright pixels in the same place),
gui viewport 1123x751 in menus against 1123x721 in game, 1123x751 ->
2560x1080 -> 1123x751 round trip keeps the settings, a saved 1000x640
opens as 1000x640, no errors in the log.
2026-10-10 12:23:41 +03:00

203 lines
6.8 KiB
C++

#pragma once
#include "delegates.hpp"
#include <set>
#include <queue>
#include <memory>
#include <vector>
#include <string>
#include <glm/glm.hpp>
#include <unordered_map>
class DrawContext;
class Assets;
class Camera;
class Batch2D;
struct CursorState;
class Engine;
class Input;
class Window;
struct FontStylesScheme;
namespace devtools {
class Editor;
}
class UiDocument;
/*
Some info about padding and margin.
Padding is element inner space, margin is outer
glm::vec4 usage:
x - left
y - top
z - right
w - bottom
Outer element
+======================================================================+
| . . . . |
| .padding.y . . . |
| padding.x . . . . padding.z |
|- - - - - - + - - - - - + - - - - - - - - - -+- - - - - + - - - - - - |
| . . . . |
| . .margin.y . . |
| .margin.x . . margin.z. |
|- - - - - - + - - - - - +====================+- - - - - + - - - - - - |
| . | Inner element | . |
|- - - - - - + - - - - - +====================+- - - - - + - - - - - - |
| . . . . |
| . .margin.w . . |
| . . . . |
|- - - - - - + - - - - - + - - - - - - - - - -+- - - - - + - - - - - - |
| . . . . |
| .padding.w . . . |
| . . . . |
+======================================================================+
*/
namespace gui {
class UINode;
class Container;
class Menu;
class Frame;
/// @brief The main UI controller
class GUI {
Engine& engine;
Input& input;
std::unique_ptr<Batch2D> batch2D;
std::shared_ptr<Frame> container;
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;
std::unordered_map<std::string, std::shared_ptr<UINode>> storage;
std::shared_ptr<Frame> activeFrame;
std::unique_ptr<Camera> uicamera;
std::shared_ptr<Menu> menu;
std::queue<runnable> postRunnables;
std::vector<std::weak_ptr<UINode>> mouseOver;
std::unordered_map<std::string, std::shared_ptr<Frame>> frames;
vec2supplier cursorLocator;
float tooltipTimer = 0.0f;
float doubleClickTimer = 0.0f;
float doubleClickDelay = 0.5f;
bool doubleClicked = false;
bool debug = false;
void actMouse(Frame& frame, float delta, const CursorState& cursor);
void actFocused();
void updateTooltip(float delta);
void resetTooltip();
public:
static inline std::string CORE_MAIN = "core:main";
static constexpr int CONTEXT_MENU_ZINDEX = 999;
GUI(Engine& engine);
~GUI();
/// @brief Get the main menu (Menu) node
std::shared_ptr<Menu> getMenu();
/// @brief Get current focused node
/// @return focused node or nullptr
std::shared_ptr<UINode> getFocused() const;
/// @brief Check if all user input is caught by some element like TextBox
bool isFocusCaught() const;
/// @brief Main input handling and logic update method
/// @param delta delta time
/// @param viewport window size
void act(float delta, const glm::uvec2& viewport);
/// @brief Draw all visible elements on main container
/// @param pctx parent graphics context
/// @param assets active assets storage
void draw(const DrawContext& pctx, Assets& assets);
int viewportOffset = 0;
/// @brief Get the cursor position in the main container coordinates
glm::vec2 getCursorPosition() const;
/// @brief Shift the main container down by the given number of pixels
void setContentInset(int inset) {
viewportOffset = inset;
}
Batch2D* getBatch2D() {
return batch2D.get();
}
void postAct();
/// @brief Add element to the main container
/// @param node UI element
void add(std::shared_ptr<UINode> node);
void addFrame(std::shared_ptr<Frame> frame);
void setActiveFrame(
const std::string& id, vec2supplier cursorLocator = nullptr
);
std::shared_ptr<Frame> getActiveFrame() const;
std::shared_ptr<Frame> getFrame(const std::string& id);
/// @brief Remove node from the main container
void remove(UINode* node) noexcept;
void remove(const std::shared_ptr<UINode>& node) noexcept {
return remove(node.get());
}
/// @brief Store node in the GUI nodes dictionary
/// (does not add node to the main container)
/// @param name node key
/// @param node target node
void store(const std::string& name, std::shared_ptr<UINode> node);
/// @brief Get node from the GUI nodes dictionary
/// @param name node key
/// @return stored node or nullptr
std::shared_ptr<UINode> get(const std::string& name) noexcept;
/// @brief Remove node from the GUI nodes dictionary
/// @param name node key
void remove(const std::string& name) noexcept;
/// @brief Set node as focused
/// @param node new focused node or nullptr to remove focus
void setFocus(std::shared_ptr<UINode> node);
/// @brief Get the main container
/// @deprecated
std::shared_ptr<Container> getContainer() const;
void setSyntaxColorScheme(std::unique_ptr<FontStylesScheme> scheme);
FontStylesScheme* getSyntaxColorScheme() const;
void onAssetsLoad(Assets* assets);
void postRunnable(const runnable& callback);
void setDoubleClickDelay(float delay);
float getDoubleClickDelay() const;
void toggleDebug();
const Input& getInput() const;
Input& getInput();
Window& getWindow();
devtools::Editor& getEditor();
Engine& getEngine();
};
}