Skip to content

Commit 3d614e4

Browse files
committed
Working UI focus system (#943)
1 parent 83ecf3f commit 3d614e4

File tree

16 files changed

+245
-194
lines changed

16 files changed

+245
-194
lines changed

src/xrGame/UIDialogHolder.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void CDialogHolder::StartMenu(CUIDialogWnd* pDialog, bool bDoHideIndicators)
5555
CurrentGameUI()->ShowGameIndicators(false);
5656
}
5757
}
58-
SetFocused(nullptr);
58+
UI().Focus().SetFocused(nullptr);
5959
pDialog->SetHolder(this);
6060

6161
if (pDialog->NeedCursor())
@@ -248,6 +248,9 @@ void CDialogHolder::OnFrame()
248248
(*it).wnd->Update();
249249
}
250250

251+
if (m_is_foremost)
252+
UI().Focus().Update(wnd);
253+
251254
m_b_in_update = false;
252255
if (!m_dialogsToRender_new.empty())
253256
{
@@ -316,6 +319,33 @@ bool CDialogHolder::IR_UIOnKeyboardPress(int dik)
316319
if (TIR->OnKeyboardAction(dik, WINDOW_KEY_PRESSED))
317320
return true;
318321

322+
if (dik > XR_CONTROLLER_BUTTON_INVALID && dik < XR_CONTROLLER_BUTTON_MAX)
323+
{
324+
FocusDirection direction = FocusDirection::Same;
325+
switch (GetBindedAction(dik, EKeyContext::UI))
326+
{
327+
case kUI_MOVE_LEFT: direction = FocusDirection::Left; break;
328+
case kUI_MOVE_RIGHT: direction = FocusDirection::Right; break;
329+
case kUI_MOVE_UP: direction = FocusDirection::Up; break;
330+
case kUI_MOVE_DOWN: direction = FocusDirection::Down; break;
331+
}
332+
333+
if (direction != FocusDirection::Same)
334+
{
335+
auto& focus = UI().Focus();
336+
const auto focused = focus.GetFocused();
337+
const Fvector2 vec = focused ? focused->GetWndPos() : UI().GetUICursor().GetCursorPosition();
338+
const auto [target, direct] = focus.FindClosestFocusable(vec, direction);
339+
340+
if (target)
341+
{
342+
focus.SetFocused(target);
343+
GetUICursor().WarpToWindow(target, true);
344+
return true;
345+
}
346+
}
347+
}
348+
319349
if (!TIR->StopAnyMove() && g_pGameLevel)
320350
{
321351
IGameObject* O = Level().CurrentEntity();
@@ -334,24 +364,6 @@ bool CDialogHolder::IR_UIOnKeyboardPress(int dik)
334364
}
335365
}
336366

337-
/*if (const auto focused = GetFocused())
338-
{
339-
CUIWindow* target{};
340-
switch (GetBindedAction(dik, EKeyContext::UI))
341-
{
342-
case kUI_MOVE_LEFT: target = FindClosestFocusable(focused, FocusDirection::Left); break;
343-
case kUI_MOVE_RIGHT: target = FindClosestFocusable(focused, FocusDirection::Right); break;
344-
case kUI_MOVE_UP: target = FindClosestFocusable(focused, FocusDirection::Up); break;
345-
case kUI_MOVE_DOWN: target = FindClosestFocusable(focused, FocusDirection::Down); break;
346-
}
347-
348-
if (target)
349-
{
350-
SetFocused(target);
351-
GetUICursor().WarpToWindow(target, true);
352-
}
353-
}*/
354-
355367
return true;
356368
}
357369

@@ -574,6 +586,7 @@ bool CDialogHolder::IR_UIOnControllerHold(int dik, float x, float y)
574586

575587
bool CDialogHolder::FillDebugTree(const CUIDebugState& debugState)
576588
{
589+
#ifndef MASTER_GOLD
577590
// XXX: Was this meant to be used somewhere here? Because currently its unused and could also be constexpr
578591
//ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_OpenOnArrow;
579592

@@ -600,6 +613,7 @@ bool CDialogHolder::FillDebugTree(const CUIDebugState& debugState)
600613
ImGui::TreePop();
601614
}
602615
}
616+
#endif
603617
return true;
604618
}
605619

src/xrGame/UIDialogHolder.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "xrCore/_flags.h"
55
#include "xrEngine/pure.h"
66
#include "xrUICore/ui_debug.h"
7-
#include "xrUICore/ui_focus.h"
87

98
#include <SDL.h>
109

@@ -33,7 +32,7 @@ class recvItem
3332
Flags8 m_flags;
3433
};
3534

36-
class CDialogHolder : public pureFrame, public CUIDebuggable, public CUIFocusSystem
35+
class CDialogHolder : public pureFrame, public CUIDebuggable
3736
{
3837
// dialogs
3938
xr_vector<recvItem> m_input_receivers;

src/xrGame/ui/UIDialogWnd.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,7 @@ class CUIDialogWnd : public CUIWindow
2222
bool OnControllerAction(int axis, float x, float y, EUIMessages controller_action) override;
2323

2424
CDialogHolder* GetHolder() const { return m_pParentHolder; }
25-
26-
void SetHolder(CDialogHolder* h)
27-
{
28-
if (m_pParentHolder)
29-
m_pParentHolder->UnregisterFocusable(this);
30-
m_pParentHolder = h;
31-
}
32-
33-
CUIFocusSystem* GetCurrentFocusSystem() const override { return GetHolder(); }
25+
void SetHolder(CDialogHolder* h) { m_pParentHolder = h; }
3426

3527
virtual bool StopAnyMove() { return true; }
3628
virtual bool NeedCursor() const { return pInput->IsCurrentInputTypeKeyboardMouse(); }

src/xrUICore/Buttons/UIButton.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
CUIButton::CUIButton() : CUIStatic("CUIButton")
1212
{
13-
m_bFocusValuable = true;
1413
m_eButtonState = BUTTON_NORMAL;
1514
m_bIsSwitch = false;
1615

@@ -22,6 +21,13 @@ CUIButton::CUIButton() : CUIStatic("CUIButton")
2221
TextItemControl()->SetTextComplexMode(false);
2322
TextItemControl()->SetTextAlignment(CGameFont::alCenter); // this will create class instance for m_pLines
2423
TextItemControl()->SetVTextAlignment(valCenter);
24+
25+
UI().Focus().RegisterFocusable(this);
26+
}
27+
28+
CUIButton::~CUIButton()
29+
{
30+
UI().Focus().UnregisterFocusable(this);
2531
}
2632

2733
void CUIButton::Reset()

src/xrUICore/Buttons/UIButton.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class XRUICORE_API CUIButton : public CUIStatic
88

99
public:
1010
CUIButton();
11+
~CUIButton() override;
1112

1213
virtual bool OnMouseAction(float x, float y, EUIMessages mouse_action);
1314
virtual void OnClick();

src/xrUICore/ComboBox/UIComboBox.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
CUIComboBox::CUIComboBox() : CUIWindow("CUIComboBox")
1111
{
12-
m_bFocusValuable = true;
13-
1412
AttachChild(&m_frameLine);
1513
AttachChild(&m_text);
1614

@@ -21,6 +19,13 @@ CUIComboBox::CUIComboBox() : CUIWindow("CUIComboBox")
2119
m_bInited = false;
2220
m_eState = LIST_FONDED;
2321
m_textColor[0] = 0xff00ff00;
22+
23+
UI().Focus().RegisterFocusable(this);
24+
}
25+
26+
CUIComboBox::~CUIComboBox()
27+
{
28+
UI().Focus().UnregisterFocusable(this);
2429
}
2530

2631
void CUIComboBox::SetListLength(int length)

src/xrUICore/ComboBox/UIComboBox.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class XRUICORE_API CUIComboBox final : public CUIWindow, public CUIOptionsItem,
1616

1717
public:
1818
CUIComboBox();
19+
~CUIComboBox() override;
1920

2021
// CUIOptionsItem
2122
virtual void SetCurrentOptValue(); // opt->current

src/xrUICore/EditBox/UICustomEdit.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
CUICustomEdit::CUICustomEdit() : CUIStatic("CUICustomEdit")
1010
{
11-
m_bFocusValuable = true;
12-
1311
m_editor_control = xr_new<text_editor::line_edit_control>(EDIT_BUF_SIZE);
1412
Init(EDIT_BUF_SIZE);
1513

@@ -25,9 +23,15 @@ CUICustomEdit::CUICustomEdit() : CUIStatic("CUICustomEdit")
2523
m_force_update = true;
2624
m_last_key_state_time = 0;
2725
m_next_focus_capturer = NULL;
26+
27+
UI().Focus().RegisterFocusable(this);
2828
}
2929

30-
CUICustomEdit::~CUICustomEdit() { xr_delete(m_editor_control); }
30+
CUICustomEdit::~CUICustomEdit()
31+
{
32+
xr_delete(m_editor_control);
33+
UI().Focus().UnregisterFocusable(this);
34+
}
3135

3236
text_editor::line_edit_control& CUICustomEdit::ec()
3337
{

src/xrUICore/TrackBar/UITrackBar.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ CUITrackBar::CUITrackBar()
1919
: m_b_invert(false), m_b_is_float(true), m_b_bound_already_set(false), m_f_val(0), m_f_max(1), m_f_min(0),
2020
m_f_step(0.01f), m_f_opt_backup_value(0)
2121
{
22-
m_bFocusValuable = true;
23-
2422
m_pSlider = xr_new<CUI3tButton>();
2523
AttachChild(m_pSlider);
2624
m_pSlider->SetAutoDelete(true);
@@ -31,6 +29,13 @@ CUITrackBar::CUITrackBar()
3129
m_static->SetAutoDelete(true);
3230

3331
m_b_mouse_capturer = false;
32+
33+
//UI().Focus().RegisterFocusable(this);
34+
}
35+
36+
CUITrackBar::~CUITrackBar()
37+
{
38+
//UI().Focus().UnregisterFocusable(this);
3439
}
3540

3641
bool CUITrackBar::OnMouseAction(float x, float y, EUIMessages mouse_action)

src/xrUICore/TrackBar/UITrackBar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class XRUICORE_API CUITrackBar final : public CUI_IB_FrameLineWnd, public CUIOpt
88
{
99
public:
1010
CUITrackBar();
11+
~CUITrackBar() override;
12+
1113
// CUIOptionsItem
1214
virtual void SetCurrentOptValue(); // opt->current
1315
virtual void SaveBackUpOptValue(); // current->backup

0 commit comments

Comments
 (0)