mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29:44 +00:00
A review of the branch turned up a number of defects, gaps and leftovers; this commit addresses all of them. Defects: - the shadow uniform array held seven entries while eight locations were written, so the last one landed outside the object; - CursorShape::LAST stayed in the middle of the enum after the new corner shapes were appended, which made the GLFW cursor table read out of bounds; the extra shapes are now mapped onto the ones GLFW knows, and the table is only filled for those; - GlStateGuard asked glIsEnabled for an attribute pname, so it always got GL_INVALID_ENUM and never restored the vertex attribute arrays; - the early returns in wayland_window_initialize left the global window and input pointers dangling when falling back to GLFW; - the icon textures were listed nowhere, so none of them ever loaded; - a shader failure disabled the shadow after the surface had already been sized for it, and VOXEL_DECORATIONS outlived the switch cleanup. Behavior: - the keyboard now releases every pressed key when it loses focus, and a repeat rate of zero means no repeat; - the fourth UTF-8 lead byte is decoded with the right mask, control characters no longer reach text fields, and a vanished clipboard offer clears the cached text; - compositor side decorations disable ours again instead of leaving two titles, the window destructor releases the rest of the state, and the content inset is computed before the GUI acts so the HUD and the container never disagree for a frame; the HUD clamps it; - the window controls fall back to minimize, maximize and close, and the icons are drawn from textures only. Cleanups: one monotonic timer instead of three, one vertex shader shared by both renderers with a link check, one button layout parser, the inset rule moved into Screen, the scissor stack moved into the base Window (about 110 duplicated lines), named shadow and evdev constants, trimmed includes, a README note about the Wayland dependencies, and the long lines introduced by this branch wrapped.
210 lines
7.1 KiB
C++
210 lines
7.1 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);
|
|
|
|
/// @brief Offset of the main container from the top of the window,
|
|
/// e.g. the height of the engine drawn window bar
|
|
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;
|
|
}
|
|
|
|
int getContentInset() const {
|
|
return viewportOffset;
|
|
}
|
|
|
|
/// @brief Batch used to draw the window bar on top of the GUI
|
|
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();
|
|
};
|
|
}
|