Add XCUI input focus shortcut MVP
This commit is contained in:
65
engine/include/XCEngine/UI/Input/UIFocusController.h
Normal file
65
engine/include/XCEngine/UI/Input/UIFocusController.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include "UIInputPath.h"
|
||||
|
||||
namespace XCEngine {
|
||||
namespace UI {
|
||||
|
||||
struct UIFocusChange {
|
||||
UIInputPath previousPath = {};
|
||||
UIInputPath currentPath = {};
|
||||
UIInputPath lostPath = {};
|
||||
UIInputPath gainedPath = {};
|
||||
|
||||
bool Changed() const {
|
||||
return previousPath != currentPath;
|
||||
}
|
||||
};
|
||||
|
||||
class UIFocusController {
|
||||
public:
|
||||
const UIInputPath& GetFocusedPath() const {
|
||||
return m_focusedPath;
|
||||
}
|
||||
|
||||
const UIInputPath& GetActivePath() const {
|
||||
return m_activePath;
|
||||
}
|
||||
|
||||
const UIInputPath& GetPointerCapturePath() const {
|
||||
return m_pointerCapturePath;
|
||||
}
|
||||
|
||||
bool HasFocus() const {
|
||||
return !m_focusedPath.Empty();
|
||||
}
|
||||
|
||||
bool HasActivePath() const {
|
||||
return !m_activePath.Empty();
|
||||
}
|
||||
|
||||
bool HasPointerCapture() const {
|
||||
return !m_pointerCapturePath.Empty();
|
||||
}
|
||||
|
||||
UIFocusChange SetFocusedPath(const UIInputPath& path);
|
||||
UIFocusChange ClearFocus();
|
||||
|
||||
void SetActivePath(const UIInputPath& path);
|
||||
void ClearActivePath();
|
||||
|
||||
void SetPointerCapturePath(const UIInputPath& path);
|
||||
void ClearPointerCapturePath();
|
||||
|
||||
bool IsFocused(UIElementId elementId) const;
|
||||
bool IsActive(UIElementId elementId) const;
|
||||
bool IsCapturingPointer(UIElementId elementId) const;
|
||||
|
||||
private:
|
||||
UIInputPath m_focusedPath = {};
|
||||
UIInputPath m_activePath = {};
|
||||
UIInputPath m_pointerCapturePath = {};
|
||||
};
|
||||
|
||||
} // namespace UI
|
||||
} // namespace XCEngine
|
||||
110
engine/include/XCEngine/UI/Input/UIInputDispatcher.h
Normal file
110
engine/include/XCEngine/UI/Input/UIInputDispatcher.h
Normal file
@@ -0,0 +1,110 @@
|
||||
#pragma once
|
||||
|
||||
#include "UIFocusController.h"
|
||||
#include "UIInputRouter.h"
|
||||
#include "UIShortcutRegistry.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace UI {
|
||||
|
||||
struct UIInputDispatcherOptions {
|
||||
bool pointerDownChangesFocus = true;
|
||||
bool pointerDownStartsActivePath = true;
|
||||
UIPointerButton focusTransferButton = UIPointerButton::Left;
|
||||
};
|
||||
|
||||
struct UIInputDispatchSummary {
|
||||
UIFocusChange focusChange = {};
|
||||
UIInputDispatchResult routing = {};
|
||||
bool shortcutHandled = false;
|
||||
std::string commandId = {};
|
||||
|
||||
bool Handled() const {
|
||||
return shortcutHandled || routing.handled;
|
||||
}
|
||||
};
|
||||
|
||||
class UIInputDispatcher {
|
||||
public:
|
||||
explicit UIInputDispatcher(
|
||||
const UIInputDispatcherOptions& options = UIInputDispatcherOptions())
|
||||
: m_options(options) {
|
||||
}
|
||||
|
||||
UIFocusController& GetFocusController() {
|
||||
return m_focusController;
|
||||
}
|
||||
|
||||
const UIFocusController& GetFocusController() const {
|
||||
return m_focusController;
|
||||
}
|
||||
|
||||
UIShortcutRegistry& GetShortcutRegistry() {
|
||||
return m_shortcutRegistry;
|
||||
}
|
||||
|
||||
const UIShortcutRegistry& GetShortcutRegistry() const {
|
||||
return m_shortcutRegistry;
|
||||
}
|
||||
|
||||
template <typename HandlerFn>
|
||||
UIInputDispatchSummary Dispatch(
|
||||
const UIInputEvent& event,
|
||||
const UIInputPath& hoveredPath,
|
||||
HandlerFn&& handler) {
|
||||
UIInputDispatchSummary summary = {};
|
||||
|
||||
if (ShouldTransferFocusOnPointerDown(event, hoveredPath)) {
|
||||
summary.focusChange = m_focusController.SetFocusedPath(hoveredPath);
|
||||
}
|
||||
|
||||
if (ShouldStartActivePathOnPointerDown(event, hoveredPath)) {
|
||||
const UIInputPath& capturePath = m_focusController.GetPointerCapturePath();
|
||||
m_focusController.SetActivePath(capturePath.Empty() ? hoveredPath : capturePath);
|
||||
}
|
||||
|
||||
const UIShortcutContext shortcutContext = {
|
||||
m_focusController.GetFocusedPath(),
|
||||
m_focusController.GetActivePath(),
|
||||
hoveredPath };
|
||||
const UIShortcutMatch shortcutMatch = m_shortcutRegistry.Match(event, shortcutContext);
|
||||
if (shortcutMatch.matched) {
|
||||
summary.shortcutHandled = true;
|
||||
summary.commandId = shortcutMatch.binding.commandId;
|
||||
return FinalizeDispatch(event, std::move(summary));
|
||||
}
|
||||
|
||||
UIInputRouteContext routeContext = {};
|
||||
routeContext.hoveredPath = hoveredPath;
|
||||
routeContext.focusedPath = m_focusController.GetFocusedPath();
|
||||
routeContext.capturePath = m_focusController.GetPointerCapturePath();
|
||||
summary.routing = UIInputRouter::Dispatch(
|
||||
event,
|
||||
routeContext,
|
||||
std::forward<HandlerFn>(handler));
|
||||
return FinalizeDispatch(event, std::move(summary));
|
||||
}
|
||||
|
||||
private:
|
||||
bool ShouldTransferFocusOnPointerDown(
|
||||
const UIInputEvent& event,
|
||||
const UIInputPath& hoveredPath) const;
|
||||
|
||||
bool ShouldStartActivePathOnPointerDown(
|
||||
const UIInputEvent& event,
|
||||
const UIInputPath& hoveredPath) const;
|
||||
|
||||
UIInputDispatchSummary FinalizeDispatch(
|
||||
const UIInputEvent& event,
|
||||
UIInputDispatchSummary&& summary);
|
||||
|
||||
UIInputDispatcherOptions m_options = {};
|
||||
UIFocusController m_focusController = {};
|
||||
UIShortcutRegistry m_shortcutRegistry = {};
|
||||
};
|
||||
|
||||
} // namespace UI
|
||||
} // namespace XCEngine
|
||||
56
engine/include/XCEngine/UI/Input/UIInputPath.h
Normal file
56
engine/include/XCEngine/UI/Input/UIInputPath.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <initializer_list>
|
||||
#include <vector>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace UI {
|
||||
|
||||
using UIElementId = std::uint64_t;
|
||||
|
||||
struct UIInputPath {
|
||||
std::vector<UIElementId> elements = {};
|
||||
|
||||
UIInputPath() = default;
|
||||
UIInputPath(std::initializer_list<UIElementId> values)
|
||||
: elements(values) {
|
||||
}
|
||||
|
||||
bool Empty() const {
|
||||
return elements.empty();
|
||||
}
|
||||
|
||||
std::size_t Size() const {
|
||||
return elements.size();
|
||||
}
|
||||
|
||||
UIElementId Root() const {
|
||||
return elements.empty() ? 0u : elements.front();
|
||||
}
|
||||
|
||||
UIElementId Target() const {
|
||||
return elements.empty() ? 0u : elements.back();
|
||||
}
|
||||
|
||||
bool Contains(UIElementId elementId) const;
|
||||
|
||||
void Clear() {
|
||||
elements.clear();
|
||||
}
|
||||
};
|
||||
|
||||
bool operator==(const UIInputPath& lhs, const UIInputPath& rhs);
|
||||
bool operator!=(const UIInputPath& lhs, const UIInputPath& rhs);
|
||||
|
||||
std::size_t GetUIInputPathCommonPrefixLength(
|
||||
const UIInputPath& lhs,
|
||||
const UIInputPath& rhs);
|
||||
|
||||
UIInputPath BuildUIInputPathSuffix(
|
||||
const UIInputPath& path,
|
||||
std::size_t startIndex);
|
||||
|
||||
} // namespace UI
|
||||
} // namespace XCEngine
|
||||
104
engine/include/XCEngine/UI/Input/UIInputRouter.h
Normal file
104
engine/include/XCEngine/UI/Input/UIInputRouter.h
Normal file
@@ -0,0 +1,104 @@
|
||||
#pragma once
|
||||
|
||||
#include "UIInputPath.h"
|
||||
|
||||
#include <XCEngine/UI/Types.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace UI {
|
||||
|
||||
enum class UIInputRoutingPhase : std::uint8_t {
|
||||
Capture = 0,
|
||||
Target,
|
||||
Bubble
|
||||
};
|
||||
|
||||
enum class UIInputTargetKind : std::uint8_t {
|
||||
None = 0,
|
||||
Hovered,
|
||||
Focused,
|
||||
Captured
|
||||
};
|
||||
|
||||
struct UIInputRouteContext {
|
||||
UIInputPath hoveredPath = {};
|
||||
UIInputPath focusedPath = {};
|
||||
UIInputPath capturePath = {};
|
||||
};
|
||||
|
||||
struct UIInputRoutingStep {
|
||||
UIElementId elementId = 0;
|
||||
UIInputRoutingPhase phase = UIInputRoutingPhase::Target;
|
||||
bool isTargetElement = false;
|
||||
};
|
||||
|
||||
struct UIInputRoutingPlan {
|
||||
UIInputTargetKind targetKind = UIInputTargetKind::None;
|
||||
UIInputPath targetPath = {};
|
||||
std::vector<UIInputRoutingStep> steps = {};
|
||||
|
||||
bool HasTargetPath() const {
|
||||
return !targetPath.Empty();
|
||||
}
|
||||
};
|
||||
|
||||
struct UIInputDispatchRequest {
|
||||
const UIInputEvent* event = nullptr;
|
||||
UIElementId elementId = 0;
|
||||
UIInputRoutingPhase phase = UIInputRoutingPhase::Target;
|
||||
UIInputTargetKind targetKind = UIInputTargetKind::None;
|
||||
bool isTargetElement = false;
|
||||
};
|
||||
|
||||
struct UIInputDispatchDecision {
|
||||
bool handled = false;
|
||||
bool stopPropagation = false;
|
||||
};
|
||||
|
||||
struct UIInputDispatchResult {
|
||||
UIInputRoutingPlan plan = {};
|
||||
bool handled = false;
|
||||
};
|
||||
|
||||
class UIInputRouter {
|
||||
public:
|
||||
static bool IsPointerEvent(UIInputEventType type);
|
||||
static bool IsKeyboardEvent(UIInputEventType type);
|
||||
|
||||
static UIInputRoutingPlan BuildRoutingPlan(
|
||||
const UIInputEvent& event,
|
||||
const UIInputRouteContext& context);
|
||||
|
||||
template <typename HandlerFn>
|
||||
static UIInputDispatchResult Dispatch(
|
||||
const UIInputEvent& event,
|
||||
const UIInputRouteContext& context,
|
||||
HandlerFn&& handler) {
|
||||
UIInputDispatchResult result = {};
|
||||
result.plan = BuildRoutingPlan(event, context);
|
||||
|
||||
for (const UIInputRoutingStep& step : result.plan.steps) {
|
||||
UIInputDispatchRequest request = {};
|
||||
request.event = &event;
|
||||
request.elementId = step.elementId;
|
||||
request.phase = step.phase;
|
||||
request.targetKind = result.plan.targetKind;
|
||||
request.isTargetElement = step.isTargetElement;
|
||||
|
||||
const UIInputDispatchDecision decision = handler(request);
|
||||
result.handled = result.handled || decision.handled;
|
||||
if (decision.stopPropagation) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace UI
|
||||
} // namespace XCEngine
|
||||
67
engine/include/XCEngine/UI/Input/UIShortcutRegistry.h
Normal file
67
engine/include/XCEngine/UI/Input/UIShortcutRegistry.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include "UIInputPath.h"
|
||||
|
||||
#include <XCEngine/UI/Types.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace UI {
|
||||
|
||||
enum class UIShortcutScope : std::uint8_t {
|
||||
Global = 0,
|
||||
Window,
|
||||
Panel,
|
||||
Widget
|
||||
};
|
||||
|
||||
struct UIShortcutChord {
|
||||
std::int32_t keyCode = 0;
|
||||
UIInputModifiers modifiers = {};
|
||||
bool allowRepeat = false;
|
||||
};
|
||||
|
||||
struct UIShortcutBinding {
|
||||
std::uint64_t bindingId = 0;
|
||||
UIShortcutScope scope = UIShortcutScope::Global;
|
||||
UIElementId ownerId = 0;
|
||||
UIInputEventType triggerEventType = UIInputEventType::KeyDown;
|
||||
UIShortcutChord chord = {};
|
||||
std::string commandId = {};
|
||||
};
|
||||
|
||||
struct UIShortcutContext {
|
||||
UIInputPath focusedPath = {};
|
||||
UIInputPath activePath = {};
|
||||
UIInputPath hoveredPath = {};
|
||||
};
|
||||
|
||||
struct UIShortcutMatch {
|
||||
bool matched = false;
|
||||
UIShortcutBinding binding = {};
|
||||
};
|
||||
|
||||
class UIShortcutRegistry {
|
||||
public:
|
||||
std::uint64_t RegisterBinding(const UIShortcutBinding& binding);
|
||||
bool UnregisterBinding(std::uint64_t bindingId);
|
||||
void Clear();
|
||||
|
||||
const std::vector<UIShortcutBinding>& GetBindings() const {
|
||||
return m_bindings;
|
||||
}
|
||||
|
||||
UIShortcutMatch Match(
|
||||
const UIInputEvent& event,
|
||||
const UIShortcutContext& context) const;
|
||||
|
||||
private:
|
||||
std::vector<UIShortcutBinding> m_bindings = {};
|
||||
std::uint64_t m_nextBindingId = 1;
|
||||
};
|
||||
|
||||
} // namespace UI
|
||||
} // namespace XCEngine
|
||||
102
engine/include/XCEngine/UI/Types.h
Normal file
102
engine/include/XCEngine/UI/Types.h
Normal file
@@ -0,0 +1,102 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace UI {
|
||||
|
||||
struct UIPoint {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
|
||||
constexpr UIPoint() = default;
|
||||
constexpr UIPoint(float xValue, float yValue)
|
||||
: x(xValue)
|
||||
, y(yValue) {
|
||||
}
|
||||
};
|
||||
|
||||
struct UISize {
|
||||
float width = 0.0f;
|
||||
float height = 0.0f;
|
||||
|
||||
constexpr UISize() = default;
|
||||
constexpr UISize(float widthValue, float heightValue)
|
||||
: width(widthValue)
|
||||
, height(heightValue) {
|
||||
}
|
||||
};
|
||||
|
||||
struct UIRect {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float width = 0.0f;
|
||||
float height = 0.0f;
|
||||
|
||||
constexpr UIRect() = default;
|
||||
constexpr UIRect(float xValue, float yValue, float widthValue, float heightValue)
|
||||
: x(xValue)
|
||||
, y(yValue)
|
||||
, width(widthValue)
|
||||
, height(heightValue) {
|
||||
}
|
||||
};
|
||||
|
||||
struct UITextureHandle {
|
||||
std::uintptr_t nativeHandle = 0;
|
||||
std::uint32_t width = 0;
|
||||
std::uint32_t height = 0;
|
||||
|
||||
constexpr bool IsValid() const {
|
||||
return nativeHandle != 0 && width > 0 && height > 0;
|
||||
}
|
||||
};
|
||||
|
||||
enum class UIPointerButton : std::uint8_t {
|
||||
None = 0,
|
||||
Left,
|
||||
Right,
|
||||
Middle,
|
||||
X1,
|
||||
X2
|
||||
};
|
||||
|
||||
enum class UIInputEventType : std::uint8_t {
|
||||
None = 0,
|
||||
PointerMove,
|
||||
PointerEnter,
|
||||
PointerLeave,
|
||||
PointerButtonDown,
|
||||
PointerButtonUp,
|
||||
PointerWheel,
|
||||
KeyDown,
|
||||
KeyUp,
|
||||
Character,
|
||||
FocusGained,
|
||||
FocusLost
|
||||
};
|
||||
|
||||
struct UIInputModifiers {
|
||||
bool shift = false;
|
||||
bool control = false;
|
||||
bool alt = false;
|
||||
bool super = false;
|
||||
};
|
||||
|
||||
struct UIInputEvent {
|
||||
UIInputEventType type = UIInputEventType::None;
|
||||
UIPoint position = {};
|
||||
UIPoint delta = {};
|
||||
float wheelDelta = 0.0f;
|
||||
std::int32_t keyCode = 0;
|
||||
std::uint32_t character = 0;
|
||||
std::uint32_t pointerId = 0;
|
||||
std::uint64_t timestampNanoseconds = 0;
|
||||
UIPointerButton pointerButton = UIPointerButton::None;
|
||||
UIInputModifiers modifiers = {};
|
||||
bool repeat = false;
|
||||
bool synthetic = false;
|
||||
};
|
||||
|
||||
} // namespace UI
|
||||
} // namespace XCEngine
|
||||
Reference in New Issue
Block a user