add 'cursor' ui property

This commit is contained in:
MihailRis
2024-12-30 21:41:05 +03:00
parent 10f0bd0290
commit cb9690ebc7
14 changed files with 144 additions and 1 deletions
+39
View File
@@ -0,0 +1,39 @@
#include "commons.hpp"
#include <map>
std::optional<CursorShape> CursorShape_from(std::string_view name) {
static std::map<std::string_view, CursorShape> shapes = {
{"arrow", CursorShape::ARROW},
{"text", CursorShape::TEXT},
{"crosshair", CursorShape::CROSSHAIR},
{"pointer", CursorShape::POINTER},
{"ew-resize", CursorShape::EW_RESIZE},
{"ns-resize", CursorShape::NS_RESIZE},
{"nwse-resize", CursorShape::NWSE_RESIZE},
{"nesw-resize", CursorShape::NESW_RESIZE},
{"all-resize", CursorShape::ALL_RESIZE},
{"not-allowed", CursorShape::NOT_ALLOWED}
};
const auto& found = shapes.find(name);
if (found == shapes.end()) {
return std::nullopt;
}
return found->second;
}
std::string to_string(CursorShape shape) {
static std::string names[] = {
"arrow",
"text",
"crosshair",
"pointer",
"ew-resize",
"ns-resize",
"nwse-resize",
"nesw-resize",
"all-resize",
"not-allowed"
};
return names[static_cast<int>(shape)];
}
+42 -1
View File
@@ -1,5 +1,8 @@
#pragma once
#include <string>
#include <optional>
enum class DrawPrimitive {
point = 0,
line,
@@ -7,9 +10,47 @@ enum class DrawPrimitive {
};
enum class BlendMode {
normal, addition, inversion
/// @brief Normal blending mode.
normal,
/// @brief Additive blending mode.
addition,
/// @brief Subtractive blending mode.
inversion
};
/// @brief Standard GLFW 3.4 cursor shapes (same order and count as in GLFW).
/// It also works in GLFW 3.3 (unsupported shapes will be replaced with the arrow).
enum class CursorShape {
/// @brief Regular arrow
ARROW,
/// @brief Text input I-beam
TEXT,
/// @brief Crosshair
CROSSHAIR,
/// @brief Pointing hand
POINTER,
/// @brief Horizontal resize arrow
EW_RESIZE,
/// @brief Vertical resize arrow
NS_RESIZE,
// GLFW 3.4+ cursor shapes
/// @brief Diagonal resize arrow (top-left to bottom-right)
NWSE_RESIZE,
/// @brief Diagonal resize arrow (top-right to bottom-left)
NESW_RESIZE,
/// @brief All-direction resize arrow
ALL_RESIZE,
/// @brief Operation not allowed
NOT_ALLOWED,
LAST=NOT_ALLOWED
};
std::optional<CursorShape> CursorShape_from(std::string_view name);
std::string to_string(CursorShape shape);
class Flushable {
public:
virtual ~Flushable() = default;
+4
View File
@@ -212,6 +212,10 @@ void GUI::draw(const DrawContext& pctx, const Assets& assets) {
batch2D->begin();
container->draw(ctx, assets);
if (hover) {
Window::setCursor(hover->getCursor());
}
}
std::shared_ptr<UINode> GUI::getFocused() const {
+1
View File
@@ -25,6 +25,7 @@ TextBox::TextBox(std::wstring placeholder, glm::vec4 padding)
input(L""),
placeholder(std::move(placeholder))
{
setCursor(CursorShape::TEXT);
setOnUpPressed(nullptr);
setOnDownPressed(nullptr);
setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.75f));
+8
View File
@@ -150,6 +150,14 @@ float UINode::getTooltipDelay() const {
return tooltipDelay;
}
void UINode::setCursor(CursorShape shape) {
cursor = shape;
}
CursorShape UINode::getCursor() const {
return cursor;
}
glm::vec2 UINode::calcPos() const {
if (parent) {
return pos + parent->calcPos() + parent->getContentOffset();
+6
View File
@@ -1,6 +1,7 @@
#pragma once
#include "delegates.hpp"
#include "graphics/core/commons.hpp"
#include "window/input.hpp"
#include <glm/glm.hpp>
@@ -112,6 +113,8 @@ namespace gui {
std::wstring tooltip;
/// @brief element tooltip delay
float tooltipDelay = 0.5f;
/// @brief cursor shape when mouse is over the element
CursorShape cursor = CursorShape::ARROW;
UINode(glm::vec2 size);
public:
@@ -206,6 +209,9 @@ namespace gui {
virtual void setTooltipDelay(float delay);
virtual float getTooltipDelay() const;
virtual void setCursor(CursorShape shape);
virtual CursorShape getCursor() const;
virtual glm::vec4 calcColor() const;
/// @brief Get inner content offset. Used for scroll
+5
View File
@@ -156,6 +156,11 @@ static void _readUINode(
if (element.has("tooltip-delay")) {
node.setTooltipDelay(element.attr("tooltip-delay").asFloat());
}
if (element.has("cursor")) {
if (auto cursor = CursorShape_from(element.attr("cursor").getText())) {
node.setCursor(*cursor);
}
}
if (auto onclick = create_action(reader, element, "onclick")) {
node.listenAction(onclick);