Skip to content

Commit

Permalink
Support gamepad navigation in the properties box (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Jan 17, 2025
1 parent e789aea commit 84f4c66
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/xrUICore/ListBox/UIListBoxItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ void CUIListBoxItem::Draw()
CUIWindow::Draw();
}

bool CUIListBoxItem::OnKeyboardAction(int dik, EUIMessages keyboard_action)
{
if (WINDOW_KEY_PRESSED == keyboard_action && CursorOverWindow() && GetSelected())
{
if (IsBinded(kUI_ACCEPT, dik, EKeyContext::UI))
{
GetMessageTarget()->SendMessage(this, LIST_ITEM_CLICKED, &tag);
return true;
}
}
return inherited::OnKeyboardAction(dik, keyboard_action);
}

void CUIListBoxItem::OnFocusReceive()
{
inherited::OnFocusReceive();
Expand Down
1 change: 1 addition & 0 deletions src/xrUICore/ListBox/UIListBoxItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class XRUICORE_API CUIListBoxItem : public CUIFrameLineWnd, public CUISelectable
~CUIListBoxItem() override;

virtual void Draw();
bool OnKeyboardAction(int dik, EUIMessages keyboard_action) override;
virtual bool OnMouseDown(int mouse_btn);
virtual void OnFocusReceive();
void InitDefault();
Expand Down
40 changes: 38 additions & 2 deletions src/xrUICore/PropertiesBox/UIPropertiesBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ void CUIPropertiesBox::Show(const Frect& parent_rect, const Fvector2& point)
ResetAll();

GetParent()->SetCapture(this, true);
GetParent()->SetKeyboardCapture(this, true);
UI().Focus().LockToWindow(this);
m_UIListWnd.Reset();
if (pInput->IsCurrentInputTypeController())
m_UIListWnd.SelectFirst();
}

void CUIPropertiesBox::Hide()
Expand All @@ -165,6 +169,12 @@ void CUIPropertiesBox::Hide()
if (GetParent()->GetMouseCapturer() == this)
GetParent()->SetCapture(this, false);

if (GetParent()->GetKeyboardCapturer() == this)
GetParent()->SetKeyboardCapture(this, false);

if (UI().Focus().GetLocker())
UI().Focus().Unlock();

if (m_sub_property_box)
m_sub_property_box->Hide();
}
Expand Down Expand Up @@ -208,7 +218,33 @@ void CUIPropertiesBox::Update() { inherited::Update(); }
void CUIPropertiesBox::Draw() { inherited::Draw(); }
bool CUIPropertiesBox::OnKeyboardAction(int dik, EUIMessages keyboard_action)
{
if (keyboard_action == WINDOW_KEY_HOLD)
return false; // allow player to walk
if (inherited::OnKeyboardAction(dik, keyboard_action))
return true;

EGameActions action = GetBindedAction(dik, EKeyContext::UI);
if (action == kNOTBINDED)
action = GetBindedAction(dik);

switch (action)
{
case kFWD:
case kBACK:
case kL_STRAFE:
case kR_STRAFE:
// Allow player to walk
return false;

case kUI_MOVE_LEFT:
case kUI_MOVE_RIGHT:
case kUI_MOVE_UP:
case kUI_MOVE_DOWN:
// Allow focus system to work
return false;

case kUI_BACK:
case kQUIT:
Hide();
return true;
}
return true;
}
15 changes: 15 additions & 0 deletions src/xrUICore/ScrollView/UIScrollView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,21 @@ void CUIScrollView::SetSelected(CUIWindow* w)
}
}

bool CUIScrollView::SelectFirst()
{
ScrollToBegin();

if (Empty() || !m_flags.test(eItemsSelectabe))
return false;

const auto first = Items()[0];
ScrollToWindow(first);
SetSelected(first);
if (UI().Focus().IsRegistered(first))
UI().Focus().SetFocused(first);
return true;
}

bool CUIScrollView::SelectNext(bool next, bool loop)
{
if (Empty())
Expand Down
1 change: 1 addition & 0 deletions src/xrUICore/ScrollView/UIScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class XRUICORE_API CUIScrollView : public CUIWindow, public CUIWndCallback
void SetFixedScrollBar(bool b);
float GetDesiredChildWidth() const;
virtual void SetSelected(CUIWindow*);
bool SelectFirst();
bool SelectNext(bool next, bool loop);
CUIWindow* GetSelected();
Fvector2 GetPadSize();
Expand Down
3 changes: 2 additions & 1 deletion src/xrUICore/Windows/UIWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ class XRUICORE_API CUIWindow : public CUISimpleWindow, public CUIDebuggable
//захватить/освободить мышь окном
//сообщение посылается дочерним окном родительскому
void SetCapture(CUIWindow* pChildWindow, bool capture_status);
CUIWindow* GetMouseCapturer() { return m_pMouseCapturer; }
CUIWindow* GetMouseCapturer() const { return m_pMouseCapturer; }

//окошко, которому пересылаются сообщения,
//если NULL, то шлем на GetParent()
void SetMessageTarget(CUIWindow* pWindow) { m_pMessageTarget = pWindow; }
CUIWindow* GetMessageTarget();

void SetKeyboardCapture(CUIWindow* pChildWindow, bool capture_status);
CUIWindow* GetKeyboardCapturer() const { return m_pKeyboardCapturer; }

//обработка сообщений не предусмотреных стандартными обработчиками
//ф-ция должна переопределяться
Expand Down

0 comments on commit 84f4c66

Please sign in to comment.