Add XCUI input focus shortcut MVP
This commit is contained in:
63
engine/src/UI/Input/UIFocusController.cpp
Normal file
63
engine/src/UI/Input/UIFocusController.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <XCEngine/UI/Input/UIFocusController.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace UI {
|
||||
|
||||
namespace {
|
||||
|
||||
UIFocusChange BuildFocusChange(
|
||||
const UIInputPath& previousPath,
|
||||
const UIInputPath& currentPath) {
|
||||
UIFocusChange change = {};
|
||||
change.previousPath = previousPath;
|
||||
change.currentPath = currentPath;
|
||||
|
||||
const std::size_t commonPrefixLength =
|
||||
GetUIInputPathCommonPrefixLength(previousPath, currentPath);
|
||||
change.lostPath = BuildUIInputPathSuffix(previousPath, commonPrefixLength);
|
||||
change.gainedPath = BuildUIInputPathSuffix(currentPath, commonPrefixLength);
|
||||
return change;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
UIFocusChange UIFocusController::SetFocusedPath(const UIInputPath& path) {
|
||||
const UIInputPath previousPath = m_focusedPath;
|
||||
m_focusedPath = path;
|
||||
return BuildFocusChange(previousPath, m_focusedPath);
|
||||
}
|
||||
|
||||
UIFocusChange UIFocusController::ClearFocus() {
|
||||
return SetFocusedPath({});
|
||||
}
|
||||
|
||||
void UIFocusController::SetActivePath(const UIInputPath& path) {
|
||||
m_activePath = path;
|
||||
}
|
||||
|
||||
void UIFocusController::ClearActivePath() {
|
||||
m_activePath.Clear();
|
||||
}
|
||||
|
||||
void UIFocusController::SetPointerCapturePath(const UIInputPath& path) {
|
||||
m_pointerCapturePath = path;
|
||||
}
|
||||
|
||||
void UIFocusController::ClearPointerCapturePath() {
|
||||
m_pointerCapturePath.Clear();
|
||||
}
|
||||
|
||||
bool UIFocusController::IsFocused(UIElementId elementId) const {
|
||||
return m_focusedPath.Contains(elementId);
|
||||
}
|
||||
|
||||
bool UIFocusController::IsActive(UIElementId elementId) const {
|
||||
return m_activePath.Contains(elementId);
|
||||
}
|
||||
|
||||
bool UIFocusController::IsCapturingPointer(UIElementId elementId) const {
|
||||
return m_pointerCapturePath.Contains(elementId);
|
||||
}
|
||||
|
||||
} // namespace UI
|
||||
} // namespace XCEngine
|
||||
34
engine/src/UI/Input/UIInputDispatcher.cpp
Normal file
34
engine/src/UI/Input/UIInputDispatcher.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <XCEngine/UI/Input/UIInputDispatcher.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace UI {
|
||||
|
||||
bool UIInputDispatcher::ShouldTransferFocusOnPointerDown(
|
||||
const UIInputEvent& event,
|
||||
const UIInputPath& hoveredPath) const {
|
||||
return m_options.pointerDownChangesFocus &&
|
||||
event.type == UIInputEventType::PointerButtonDown &&
|
||||
event.pointerButton == m_options.focusTransferButton &&
|
||||
!hoveredPath.Empty();
|
||||
}
|
||||
|
||||
bool UIInputDispatcher::ShouldStartActivePathOnPointerDown(
|
||||
const UIInputEvent& event,
|
||||
const UIInputPath& hoveredPath) const {
|
||||
return m_options.pointerDownStartsActivePath &&
|
||||
event.type == UIInputEventType::PointerButtonDown &&
|
||||
!hoveredPath.Empty();
|
||||
}
|
||||
|
||||
UIInputDispatchSummary UIInputDispatcher::FinalizeDispatch(
|
||||
const UIInputEvent& event,
|
||||
UIInputDispatchSummary&& summary) {
|
||||
if (event.type == UIInputEventType::PointerButtonUp) {
|
||||
m_focusController.ClearActivePath();
|
||||
}
|
||||
|
||||
return std::move(summary);
|
||||
}
|
||||
|
||||
} // namespace UI
|
||||
} // namespace XCEngine
|
||||
45
engine/src/UI/Input/UIInputPath.cpp
Normal file
45
engine/src/UI/Input/UIInputPath.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <XCEngine/UI/Input/UIInputPath.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace UI {
|
||||
|
||||
bool UIInputPath::Contains(UIElementId elementId) const {
|
||||
return std::find(elements.begin(), elements.end(), elementId) != elements.end();
|
||||
}
|
||||
|
||||
bool operator==(const UIInputPath& lhs, const UIInputPath& rhs) {
|
||||
return lhs.elements == rhs.elements;
|
||||
}
|
||||
|
||||
bool operator!=(const UIInputPath& lhs, const UIInputPath& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
std::size_t GetUIInputPathCommonPrefixLength(
|
||||
const UIInputPath& lhs,
|
||||
const UIInputPath& rhs) {
|
||||
const std::size_t limit = (std::min)(lhs.elements.size(), rhs.elements.size());
|
||||
std::size_t index = 0;
|
||||
while (index < limit && lhs.elements[index] == rhs.elements[index]) {
|
||||
++index;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
UIInputPath BuildUIInputPathSuffix(
|
||||
const UIInputPath& path,
|
||||
std::size_t startIndex) {
|
||||
UIInputPath suffix = {};
|
||||
if (startIndex >= path.elements.size()) {
|
||||
return suffix;
|
||||
}
|
||||
|
||||
suffix.elements.assign(path.elements.begin() + static_cast<std::ptrdiff_t>(startIndex), path.elements.end());
|
||||
return suffix;
|
||||
}
|
||||
|
||||
} // namespace UI
|
||||
} // namespace XCEngine
|
||||
86
engine/src/UI/Input/UIInputRouter.cpp
Normal file
86
engine/src/UI/Input/UIInputRouter.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include <XCEngine/UI/Input/UIInputRouter.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace UI {
|
||||
|
||||
namespace {
|
||||
|
||||
UIInputRoutingPlan BuildPlanForTargetPath(
|
||||
UIInputTargetKind targetKind,
|
||||
const UIInputPath& path) {
|
||||
UIInputRoutingPlan plan = {};
|
||||
plan.targetKind = targetKind;
|
||||
plan.targetPath = path;
|
||||
|
||||
if (path.Empty()) {
|
||||
return plan;
|
||||
}
|
||||
|
||||
const std::size_t size = path.elements.size();
|
||||
for (std::size_t index = 0; index + 1 < size; ++index) {
|
||||
UIInputRoutingStep step = {};
|
||||
step.elementId = path.elements[index];
|
||||
step.phase = UIInputRoutingPhase::Capture;
|
||||
plan.steps.push_back(step);
|
||||
}
|
||||
|
||||
UIInputRoutingStep targetStep = {};
|
||||
targetStep.elementId = path.Target();
|
||||
targetStep.phase = UIInputRoutingPhase::Target;
|
||||
targetStep.isTargetElement = true;
|
||||
plan.steps.push_back(targetStep);
|
||||
|
||||
for (std::size_t index = size; index > 1; --index) {
|
||||
UIInputRoutingStep bubbleStep = {};
|
||||
bubbleStep.elementId = path.elements[index - 2];
|
||||
bubbleStep.phase = UIInputRoutingPhase::Bubble;
|
||||
plan.steps.push_back(bubbleStep);
|
||||
}
|
||||
|
||||
return plan;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool UIInputRouter::IsPointerEvent(UIInputEventType type) {
|
||||
switch (type) {
|
||||
case UIInputEventType::PointerMove:
|
||||
case UIInputEventType::PointerEnter:
|
||||
case UIInputEventType::PointerLeave:
|
||||
case UIInputEventType::PointerButtonDown:
|
||||
case UIInputEventType::PointerButtonUp:
|
||||
case UIInputEventType::PointerWheel:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool UIInputRouter::IsKeyboardEvent(UIInputEventType type) {
|
||||
return type == UIInputEventType::KeyDown ||
|
||||
type == UIInputEventType::KeyUp ||
|
||||
type == UIInputEventType::Character;
|
||||
}
|
||||
|
||||
UIInputRoutingPlan UIInputRouter::BuildRoutingPlan(
|
||||
const UIInputEvent& event,
|
||||
const UIInputRouteContext& context) {
|
||||
if (IsPointerEvent(event.type)) {
|
||||
if (!context.capturePath.Empty()) {
|
||||
return BuildPlanForTargetPath(UIInputTargetKind::Captured, context.capturePath);
|
||||
}
|
||||
|
||||
return BuildPlanForTargetPath(UIInputTargetKind::Hovered, context.hoveredPath);
|
||||
}
|
||||
|
||||
if (IsKeyboardEvent(event.type) ||
|
||||
event.type == UIInputEventType::FocusGained ||
|
||||
event.type == UIInputEventType::FocusLost) {
|
||||
return BuildPlanForTargetPath(UIInputTargetKind::Focused, context.focusedPath);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace UI
|
||||
} // namespace XCEngine
|
||||
121
engine/src/UI/Input/UIShortcutRegistry.cpp
Normal file
121
engine/src/UI/Input/UIShortcutRegistry.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
#include <XCEngine/UI/Input/UIShortcutRegistry.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace UI {
|
||||
|
||||
namespace {
|
||||
|
||||
const UIInputPath& ResolvePrimaryShortcutPath(const UIShortcutContext& context) {
|
||||
if (!context.activePath.Empty()) {
|
||||
return context.activePath;
|
||||
}
|
||||
|
||||
if (!context.focusedPath.Empty()) {
|
||||
return context.focusedPath;
|
||||
}
|
||||
|
||||
return context.hoveredPath;
|
||||
}
|
||||
|
||||
bool ModifiersEqual(
|
||||
const UIInputModifiers& lhs,
|
||||
const UIInputModifiers& rhs) {
|
||||
return lhs.shift == rhs.shift &&
|
||||
lhs.control == rhs.control &&
|
||||
lhs.alt == rhs.alt &&
|
||||
lhs.super == rhs.super;
|
||||
}
|
||||
|
||||
bool IsBindingActive(
|
||||
const UIShortcutBinding& binding,
|
||||
const UIShortcutContext& context) {
|
||||
if (binding.scope == UIShortcutScope::Global) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (binding.ownerId == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ResolvePrimaryShortcutPath(context).Contains(binding.ownerId);
|
||||
}
|
||||
|
||||
bool DoesBindingMatchEvent(
|
||||
const UIShortcutBinding& binding,
|
||||
const UIInputEvent& event) {
|
||||
return binding.triggerEventType == event.type &&
|
||||
binding.chord.keyCode == event.keyCode &&
|
||||
ModifiersEqual(binding.chord.modifiers, event.modifiers) &&
|
||||
(binding.chord.allowRepeat || !event.repeat);
|
||||
}
|
||||
|
||||
int GetShortcutScopePriority(UIShortcutScope scope) {
|
||||
switch (scope) {
|
||||
case UIShortcutScope::Widget:
|
||||
return 3;
|
||||
case UIShortcutScope::Panel:
|
||||
return 2;
|
||||
case UIShortcutScope::Window:
|
||||
return 1;
|
||||
case UIShortcutScope::Global:
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::uint64_t UIShortcutRegistry::RegisterBinding(const UIShortcutBinding& binding) {
|
||||
UIShortcutBinding storedBinding = binding;
|
||||
storedBinding.bindingId = storedBinding.bindingId != 0 ? storedBinding.bindingId : m_nextBindingId++;
|
||||
if (storedBinding.bindingId >= m_nextBindingId) {
|
||||
m_nextBindingId = storedBinding.bindingId + 1;
|
||||
}
|
||||
|
||||
m_bindings.push_back(storedBinding);
|
||||
return m_bindings.back().bindingId;
|
||||
}
|
||||
|
||||
bool UIShortcutRegistry::UnregisterBinding(std::uint64_t bindingId) {
|
||||
for (auto it = m_bindings.begin(); it != m_bindings.end(); ++it) {
|
||||
if (it->bindingId != bindingId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
m_bindings.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void UIShortcutRegistry::Clear() {
|
||||
m_bindings.clear();
|
||||
}
|
||||
|
||||
UIShortcutMatch UIShortcutRegistry::Match(
|
||||
const UIInputEvent& event,
|
||||
const UIShortcutContext& context) const {
|
||||
UIShortcutMatch bestMatch = {};
|
||||
int bestPriority = -1;
|
||||
|
||||
for (const UIShortcutBinding& binding : m_bindings) {
|
||||
if (!DoesBindingMatchEvent(binding, event) || !IsBindingActive(binding, context)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const int priority = GetShortcutScopePriority(binding.scope);
|
||||
if (!bestMatch.matched ||
|
||||
priority > bestPriority ||
|
||||
(priority == bestPriority && binding.bindingId > bestMatch.binding.bindingId)) {
|
||||
bestMatch.matched = true;
|
||||
bestMatch.binding = binding;
|
||||
bestPriority = priority;
|
||||
}
|
||||
}
|
||||
|
||||
return bestMatch;
|
||||
}
|
||||
|
||||
} // namespace UI
|
||||
} // namespace XCEngine
|
||||
Reference in New Issue
Block a user