Skip to content

Commit

Permalink
Working UI focus system (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Jan 6, 2025
1 parent 83ecf3f commit 3d614e4
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 194 deletions.
52 changes: 33 additions & 19 deletions src/xrGame/UIDialogHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void CDialogHolder::StartMenu(CUIDialogWnd* pDialog, bool bDoHideIndicators)
CurrentGameUI()->ShowGameIndicators(false);
}
}
SetFocused(nullptr);
UI().Focus().SetFocused(nullptr);
pDialog->SetHolder(this);

if (pDialog->NeedCursor())
Expand Down Expand Up @@ -248,6 +248,9 @@ void CDialogHolder::OnFrame()
(*it).wnd->Update();
}

if (m_is_foremost)
UI().Focus().Update(wnd);

m_b_in_update = false;
if (!m_dialogsToRender_new.empty())
{
Expand Down Expand Up @@ -316,6 +319,33 @@ bool CDialogHolder::IR_UIOnKeyboardPress(int dik)
if (TIR->OnKeyboardAction(dik, WINDOW_KEY_PRESSED))
return true;

if (dik > XR_CONTROLLER_BUTTON_INVALID && dik < XR_CONTROLLER_BUTTON_MAX)
{
FocusDirection direction = FocusDirection::Same;
switch (GetBindedAction(dik, EKeyContext::UI))
{
case kUI_MOVE_LEFT: direction = FocusDirection::Left; break;
case kUI_MOVE_RIGHT: direction = FocusDirection::Right; break;
case kUI_MOVE_UP: direction = FocusDirection::Up; break;
case kUI_MOVE_DOWN: direction = FocusDirection::Down; break;
}

if (direction != FocusDirection::Same)
{
auto& focus = UI().Focus();
const auto focused = focus.GetFocused();
const Fvector2 vec = focused ? focused->GetWndPos() : UI().GetUICursor().GetCursorPosition();
const auto [target, direct] = focus.FindClosestFocusable(vec, direction);

if (target)
{
focus.SetFocused(target);
GetUICursor().WarpToWindow(target, true);
return true;
}
}
}

if (!TIR->StopAnyMove() && g_pGameLevel)
{
IGameObject* O = Level().CurrentEntity();
Expand All @@ -334,24 +364,6 @@ bool CDialogHolder::IR_UIOnKeyboardPress(int dik)
}
}

/*if (const auto focused = GetFocused())
{
CUIWindow* target{};
switch (GetBindedAction(dik, EKeyContext::UI))
{
case kUI_MOVE_LEFT: target = FindClosestFocusable(focused, FocusDirection::Left); break;
case kUI_MOVE_RIGHT: target = FindClosestFocusable(focused, FocusDirection::Right); break;
case kUI_MOVE_UP: target = FindClosestFocusable(focused, FocusDirection::Up); break;
case kUI_MOVE_DOWN: target = FindClosestFocusable(focused, FocusDirection::Down); break;
}
if (target)
{
SetFocused(target);
GetUICursor().WarpToWindow(target, true);
}
}*/

return true;
}

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

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

Expand All @@ -600,6 +613,7 @@ bool CDialogHolder::FillDebugTree(const CUIDebugState& debugState)
ImGui::TreePop();
}
}
#endif
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions src/xrGame/UIDialogHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "xrCore/_flags.h"
#include "xrEngine/pure.h"
#include "xrUICore/ui_debug.h"
#include "xrUICore/ui_focus.h"

#include <SDL.h>

Expand Down Expand Up @@ -33,7 +32,7 @@ class recvItem
Flags8 m_flags;
};

class CDialogHolder : public pureFrame, public CUIDebuggable, public CUIFocusSystem
class CDialogHolder : public pureFrame, public CUIDebuggable
{
// dialogs
xr_vector<recvItem> m_input_receivers;
Expand Down
10 changes: 1 addition & 9 deletions src/xrGame/ui/UIDialogWnd.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ class CUIDialogWnd : public CUIWindow
bool OnControllerAction(int axis, float x, float y, EUIMessages controller_action) override;

CDialogHolder* GetHolder() const { return m_pParentHolder; }

void SetHolder(CDialogHolder* h)
{
if (m_pParentHolder)
m_pParentHolder->UnregisterFocusable(this);
m_pParentHolder = h;
}

CUIFocusSystem* GetCurrentFocusSystem() const override { return GetHolder(); }
void SetHolder(CDialogHolder* h) { m_pParentHolder = h; }

virtual bool StopAnyMove() { return true; }
virtual bool NeedCursor() const { return pInput->IsCurrentInputTypeKeyboardMouse(); }
Expand Down
8 changes: 7 additions & 1 deletion src/xrUICore/Buttons/UIButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

CUIButton::CUIButton() : CUIStatic("CUIButton")
{
m_bFocusValuable = true;
m_eButtonState = BUTTON_NORMAL;
m_bIsSwitch = false;

Expand All @@ -22,6 +21,13 @@ CUIButton::CUIButton() : CUIStatic("CUIButton")
TextItemControl()->SetTextComplexMode(false);
TextItemControl()->SetTextAlignment(CGameFont::alCenter); // this will create class instance for m_pLines
TextItemControl()->SetVTextAlignment(valCenter);

UI().Focus().RegisterFocusable(this);
}

CUIButton::~CUIButton()
{
UI().Focus().UnregisterFocusable(this);
}

void CUIButton::Reset()
Expand Down
1 change: 1 addition & 0 deletions src/xrUICore/Buttons/UIButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class XRUICORE_API CUIButton : public CUIStatic

public:
CUIButton();
~CUIButton() override;

virtual bool OnMouseAction(float x, float y, EUIMessages mouse_action);
virtual void OnClick();
Expand Down
9 changes: 7 additions & 2 deletions src/xrUICore/ComboBox/UIComboBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

CUIComboBox::CUIComboBox() : CUIWindow("CUIComboBox")
{
m_bFocusValuable = true;

AttachChild(&m_frameLine);
AttachChild(&m_text);

Expand All @@ -21,6 +19,13 @@ CUIComboBox::CUIComboBox() : CUIWindow("CUIComboBox")
m_bInited = false;
m_eState = LIST_FONDED;
m_textColor[0] = 0xff00ff00;

UI().Focus().RegisterFocusable(this);
}

CUIComboBox::~CUIComboBox()
{
UI().Focus().UnregisterFocusable(this);
}

void CUIComboBox::SetListLength(int length)
Expand Down
1 change: 1 addition & 0 deletions src/xrUICore/ComboBox/UIComboBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class XRUICORE_API CUIComboBox final : public CUIWindow, public CUIOptionsItem,

public:
CUIComboBox();
~CUIComboBox() override;

// CUIOptionsItem
virtual void SetCurrentOptValue(); // opt->current
Expand Down
10 changes: 7 additions & 3 deletions src/xrUICore/EditBox/UICustomEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

CUICustomEdit::CUICustomEdit() : CUIStatic("CUICustomEdit")
{
m_bFocusValuable = true;

m_editor_control = xr_new<text_editor::line_edit_control>(EDIT_BUF_SIZE);
Init(EDIT_BUF_SIZE);

Expand All @@ -25,9 +23,15 @@ CUICustomEdit::CUICustomEdit() : CUIStatic("CUICustomEdit")
m_force_update = true;
m_last_key_state_time = 0;
m_next_focus_capturer = NULL;

UI().Focus().RegisterFocusable(this);
}

CUICustomEdit::~CUICustomEdit() { xr_delete(m_editor_control); }
CUICustomEdit::~CUICustomEdit()
{
xr_delete(m_editor_control);
UI().Focus().UnregisterFocusable(this);
}

text_editor::line_edit_control& CUICustomEdit::ec()
{
Expand Down
9 changes: 7 additions & 2 deletions src/xrUICore/TrackBar/UITrackBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ CUITrackBar::CUITrackBar()
: 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),
m_f_step(0.01f), m_f_opt_backup_value(0)
{
m_bFocusValuable = true;

m_pSlider = xr_new<CUI3tButton>();
AttachChild(m_pSlider);
m_pSlider->SetAutoDelete(true);
Expand All @@ -31,6 +29,13 @@ CUITrackBar::CUITrackBar()
m_static->SetAutoDelete(true);

m_b_mouse_capturer = false;

//UI().Focus().RegisterFocusable(this);
}

CUITrackBar::~CUITrackBar()
{
//UI().Focus().UnregisterFocusable(this);
}

bool CUITrackBar::OnMouseAction(float x, float y, EUIMessages mouse_action)
Expand Down
2 changes: 2 additions & 0 deletions src/xrUICore/TrackBar/UITrackBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class XRUICORE_API CUITrackBar final : public CUI_IB_FrameLineWnd, public CUIOpt
{
public:
CUITrackBar();
~CUITrackBar() override;

// CUIOptionsItem
virtual void SetCurrentOptValue(); // opt->current
virtual void SaveBackUpOptValue(); // current->backup
Expand Down
31 changes: 7 additions & 24 deletions src/xrUICore/Windows/UIWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "UIWindow.h"

#include "ui_focus.h"
#include "Cursor/UICursor.h"

CUIWindow::CUIWindow(pcstr window_name) : m_windowName(window_name)
Expand Down Expand Up @@ -43,25 +42,6 @@ void CUIWindow::Draw(float x, float y)

void CUIWindow::Update()
{
/*if (auto* focusSystem = GetCurrentFocusSystem())
{
const bool valuable = IsFocusValuable();
const bool registered = focusSystem->IsRegistered(this);
if (valuable)
{
if (!registered)
focusSystem->RegisterFocusable(this);
if (!focusSystem->GetFocused())
focusSystem->SetFocused(this);
}
else if (!valuable && registered)
{
if (focusSystem->GetFocused() == this)
focusSystem->SetFocused(nullptr);
focusSystem->UnregisterFocusable(this);
}
}*/

bool cursor_on_window = false;
if (GetUICursor().IsVisible())
{
Expand Down Expand Up @@ -572,10 +552,13 @@ bool CUIWindow::FillDebugTree(const CUIDebugState& debugState)
rnd.seed((s32)(intptr_t)this);
color = color_rgba(rnd.randI(255), rnd.randI(255), rnd.randI(255), 255);
}
else if (GetCurrentFocusSystem() && GetCurrentFocusSystem()->GetFocused() == this)
color = color_rgba(200, 150, 200, 255);
else if (IsFocusValuable())
color = color_rgba(255, 0, 255, 255);
else if (IsFocusValuable(nullptr))
{
if (CursorOverWindow())
color = color_rgba(200, 150, 200, 255);
else
color = color_rgba(255, 0, 255, 255);
}

const auto draw_list = hovered ? ImGui::GetForegroundDrawList() : ImGui::GetBackgroundDrawList();
draw_list->AddRect((const ImVec2&)rect.lt, (const ImVec2&)rect.rb, color);
Expand Down
26 changes: 8 additions & 18 deletions src/xrUICore/Windows/UIWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "xrUICore/uiabstract.h"
#include "xrUICore/ui_debug.h"

class CUIFocusSystem;

class XRUICORE_API CUIWindow : public CUISimpleWindow, public CUIDebuggable
{
public:
Expand Down Expand Up @@ -83,24 +81,22 @@ class XRUICORE_API CUIWindow : public CUISimpleWindow, public CUIDebuggable

virtual void Enable(bool status) { m_bIsEnabled = status; }

void SetFocusValuable(bool valuable) { m_bFocusValuable = valuable; }

[[nodiscard]]
bool IsEnabled() const { return m_bIsEnabled; }

[[nodiscard]]
bool IsFocusValuable() const
bool IsFocusValuable(const CUIWindow* parent) const
{
if (!m_bFocusValuable)
return false;

bool ok;
for (auto it = this; ; it = it->GetParent())
const CUIWindow* it = this;
for (;; it = it->GetParent())
{
ok = it->IsShown() && it->IsEnabled();
if (!ok || !it->GetParent())
break;
}
if (parent && parent != it)
return false;
return ok;
}

Expand All @@ -111,13 +107,6 @@ class XRUICORE_API CUIWindow : public CUISimpleWindow, public CUIDebuggable
Enable(status);
}

virtual CUIFocusSystem* GetCurrentFocusSystem() const
{
if (m_pParentWnd)
return m_pParentWnd->GetCurrentFocusSystem();
return nullptr;
}

[[nodiscard]]
virtual bool IsShown() const { return GetVisible(); }

Expand Down Expand Up @@ -167,7 +156,10 @@ class XRUICORE_API CUIWindow : public CUISimpleWindow, public CUIDebuggable

using WINDOW_LIST = ui_list<CUIWindow*>;

[[nodiscard]]
WINDOW_LIST& GetChildWndList() { return m_ChildWndList; }
[[nodiscard]]
const WINDOW_LIST& GetChildWndList() const { return m_ChildWndList; }

[[nodiscard]]
IC bool IsAutoDelete() const { return m_bAutoDelete; }
Expand Down Expand Up @@ -237,8 +229,6 @@ class XRUICORE_API CUIWindow : public CUISimpleWindow, public CUIDebuggable
// Если курсор над окном
bool m_bCursorOverWindow{};
bool m_bCustomDraw{};

bool m_bFocusValuable{};
};

XRUICORE_API bool fit_in_rect(CUIWindow* w, Frect const& vis_rect, float border = 0.0f, float dx16pos = 0.0f);
Loading

0 comments on commit 3d614e4

Please sign in to comment.