Skip to content

Commit

Permalink
Remade cursor autohide mechanism (#943)
Browse files Browse the repository at this point in the history
Now CUIDialogHolder is responsible for cursor visibility, it's the best
place for this purpose. Previously, the cursor was tracking itself.

For now, until other UI windows are adapted to change, the cursor will
be visible like in vanilla.
  • Loading branch information
Xottab-DUTY committed Dec 20, 2024
1 parent a44a542 commit 9e4a07d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 53 deletions.
28 changes: 20 additions & 8 deletions src/xrGame/UIDialogHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ void CDialogHolder::StartMenu(CUIDialogWnd* pDialog, bool bDoHideIndicators)
SetFocused(nullptr);
pDialog->SetHolder(this);

if (pDialog->NeedCursor())
GetUICursor().Show();

if (g_pGameLevel)
{
CActor* A = smart_cast<CActor*>(Level().CurrentViewEntity());
Expand Down Expand Up @@ -97,9 +94,6 @@ void CDialogHolder::StopMenu(CUIDialogWnd* pDialog)

RemoveDialogToRender(pDialog);
pDialog->SetHolder(NULL);

if (!TopInputReceiver() || !TopInputReceiver()->NeedCursor())
GetUICursor().Hide();
}

void CDialogHolder::AddDialogToRender(CUIWindow* pDialog)
Expand Down Expand Up @@ -230,8 +224,26 @@ void CDialogHolder::OnFrame()

m_b_in_update = true;

if (!GEnv.isDedicatedServer && GetUICursor().IsVisible() && pInput->IsCurrentInputTypeController())
GetUICursor().UpdateAutohideTiming();
if (!GEnv.isDedicatedServer)
{
auto& cursor = GetUICursor();
const bool need_cursor = TopInputReceiver() && TopInputReceiver()->NeedCursor();

const u32 cur_time = Device.dwTimeContinual;

if (need_cursor)
{
if (!cursor.IsVisible())
{
cursor.Show();
m_become_visible_time = cur_time;
}
}
else if (float(cur_time - m_become_visible_time) > (psControllerCursorAutohideTime * 1000.f))
{
cursor.Hide();
}
}

CUIDialogWnd* wnd = TopInputReceiver();
if (wnd && wnd->IsEnabled())
Expand Down
1 change: 1 addition & 0 deletions src/xrGame/UIDialogHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class CDialogHolder : public pureFrame, public CUIDebuggable, public CUIFocusSys
xr_vector<recvItem> m_input_receivers;
xr_vector<dlgItem> m_dialogsToRender;
xr_vector<dlgItem> m_dialogsToRender_new;
u32 m_become_visible_time{};
bool m_b_in_update;

void StartMenu(CUIDialogWnd* pDialog, bool bDoHideIndicators);
Expand Down
2 changes: 1 addition & 1 deletion src/xrGame/ui/UIChatWnd.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CUIChatWnd final : public CUIDialogWnd, public CUIWndCallback
void SetEditBoxPrefix(LPCSTR prefix);
void ChatToAll(bool b) { sendNextMessageToAll = b; }
void PendingMode(bool const is_pending_mode);
virtual bool NeedCursor() const { return false; }
bool NeedCursor() const override { return false; }
virtual void SendMessage(CUIWindow* pWnd, s16 msg, void* pData = NULL);

pcstr GetDebugType() override { return "CUIChatWnd"; }
Expand Down
1 change: 0 additions & 1 deletion src/xrGame/ui/UIMapFilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ void CUIMapFilters::SelectFilter(bool select, bool next /*= true*/)
m_selected_filter = int(m_filters.size() - 1);
}
cursor.WarpToWindow(m_filters[m_selected_filter]);
cursor.PauseAutohiding(true);
}
}

Expand Down
36 changes: 0 additions & 36 deletions src/xrUICore/Cursor/UICursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@ CUICursor::~CUICursor()
Device.seqRender.Remove(this);
}

void CUICursor::Show()
{
bVisible = true;
m_become_visible_time = Device.dwTimeContinual;
}

void CUICursor::Hide()
{
bVisible = false;
m_become_visible_time = 0;
m_pause_autohide = false;
}

//--------------------------------------------------------------------
u32 last_render_frame = 0;
void CUICursor::OnRender()
Expand Down Expand Up @@ -100,29 +87,6 @@ void CUICursor::OnRender()
m_static->Draw();
}

void CUICursor::UpdateAutohideTiming()
{
if (m_pause_autohide)
return;

const u32 cur_time = Device.dwTimeContinual;

if (float(cur_time - m_become_visible_time) > (psControllerCursorAutohideTime * 1000.f))
{
Hide();
}
}

void CUICursor::PauseAutohiding(bool pause)
{
if (m_pause_autohide == pause)
return;

m_pause_autohide = pause;
if (!m_pause_autohide)
m_become_visible_time = Device.dwTimeContinual;
}

void CUICursor::SetUICursorPosition(Fvector2 pos)
{
vPos = pos;
Expand Down
9 changes: 2 additions & 7 deletions src/xrUICore/Cursor/UICursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ class XRUICORE_API CUICursor : public pureRender, public CDeviceResetNotifier, p
Fvector2 correction;
bool bVisible{};
bool m_bound_to_system_cursor{};
bool m_pause_autohide{};
u32 m_become_visible_time{};

void InitInternal();

public:
CUICursor();
~CUICursor() override;

void Show();
void Hide();
void Show() { bVisible = true; }
void Hide() { bVisible = false; }

[[nodiscard]]
bool IsVisible() const { return bVisible; }
Expand All @@ -30,9 +28,6 @@ class XRUICORE_API CUICursor : public pureRender, public CDeviceResetNotifier, p
void OnDeviceReset() override;
void OnUIReset() override;

void UpdateAutohideTiming();
void PauseAutohiding(bool pause);

void WarpToWindow(CUIWindow* wnd, bool change_visibility = true);
void UpdateCursorPosition(int _dx, int _dy);

Expand Down

0 comments on commit 9e4a07d

Please sign in to comment.