Skip to content

Commit

Permalink
xrUICore: optimize CUICursor memory layout, reorganize functions
Browse files Browse the repository at this point in the history
No functional changes, just code blocks movement
  • Loading branch information
Xottab-DUTY committed Dec 20, 2024
1 parent 947749c commit a44a542
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 62 deletions.
98 changes: 50 additions & 48 deletions src/xrUICore/Cursor/UICursor.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
#include "pch.hpp"

#include "UICursor.h"

#include "Static/UIStatic.h"
#include "Buttons/UIBtnHint.h"
#include "xrEngine/xr_input.h"

#define C_DEFAULT color_xrgb(0xff, 0xff, 0xff)

CUICursor::CUICursor()
{
InitInternal();
Device.seqRender.Add(this, -3 /*2*/);
}
//--------------------------------------------------------------------
CUICursor::~CUICursor()
void CUICursor::InitInternal()
{
xr_delete(m_static);
Device.seqRender.Remove(this);
m_static = xr_new<CUIStatic>("ui_ani_cursor");
m_static->InitTextureEx("ui" DELIMITER "ui_ani_cursor", "hud" DELIMITER "cursor");
Frect rect;
rect.set(0.0f, 0.0f, 40.0f, 40.0f);
m_static->SetTextureRect(rect);
Fvector2 sz;
sz.set(rect.rb);
sz.x *= UICore::get_current_kx();

m_static->SetWndSize(sz);
m_static->SetStretchTexture(true);

OnDeviceReset();
}

void CUICursor::OnDeviceReset()
{
correction.x = UI_BASE_WIDTH / (float)Device.m_rcWindowClient.w;
correction.x = UI_BASE_WIDTH / (float)Device.m_rcWindowClient.w;
correction.y = UI_BASE_HEIGHT / (float)Device.m_rcWindowClient.h;

SDL_Rect display;
Expand All @@ -41,6 +45,18 @@ void CUICursor::OnUIReset()
InitInternal();
}

CUICursor::CUICursor()
{
InitInternal();
Device.seqRender.Add(this, -3 /*2*/);
}
//--------------------------------------------------------------------
CUICursor::~CUICursor()
{
xr_delete(m_static);
Device.seqRender.Remove(this);
}

void CUICursor::Show()
{
bVisible = true;
Expand All @@ -54,23 +70,6 @@ void CUICursor::Hide()
m_pause_autohide = false;
}

void CUICursor::InitInternal()
{
m_static = xr_new<CUIStatic>("ui_ani_cursor");
m_static->InitTextureEx("ui" DELIMITER "ui_ani_cursor", "hud" DELIMITER "cursor");
Frect rect;
rect.set(0.0f, 0.0f, 40.0f, 40.0f);
m_static->SetTextureRect(rect);
Fvector2 sz;
sz.set(rect.rb);
sz.x *= UICore::get_current_kx();

m_static->SetWndSize(sz);
m_static->SetStretchTexture(true);

OnDeviceReset();
}

//--------------------------------------------------------------------
u32 last_render_frame = 0;
void CUICursor::OnRender()
Expand Down Expand Up @@ -124,15 +123,13 @@ void CUICursor::PauseAutohiding(bool pause)
m_become_visible_time = Device.dwTimeContinual;
}

Fvector2 CUICursor::GetCursorPosition() { return vPos; }

Fvector2 CUICursor::GetCursorPositionDelta()
void CUICursor::SetUICursorPosition(Fvector2 pos)
{
Fvector2 res_delta;

res_delta.x = vPos.x - vPrevPos.x;
res_delta.y = vPos.y - vPrevPos.y;
return res_delta;
vPos = pos;
Ivector2 p;
p.x = iFloor(vPos.x / correction.x);
p.y = iFloor(vPos.y / correction.y);
std::ignore = pInput->iSetMousePos(p);
}

void CUICursor::UpdateCursorPosition(int _dx, int _dy)
Expand All @@ -141,8 +138,8 @@ void CUICursor::UpdateCursorPosition(int _dx, int _dy)
if (pInput->IsExclusiveMode() || !m_bound_to_system_cursor)
{
float sens = 1.0f;
vPos.x += _dx * sens * correction.x;
vPos.y += _dy * sens * correction.y;
vPos.x += (float)_dx * sens * correction.x;
vPos.y += (float)_dy * sens * correction.y;
}
else
{
Expand All @@ -155,15 +152,6 @@ void CUICursor::UpdateCursorPosition(int _dx, int _dy)
clamp(vPos.y, 0.f, UI_BASE_HEIGHT);
}

void CUICursor::SetUICursorPosition(Fvector2 pos)
{
vPos = pos;
Ivector2 p;
p.x = iFloor(vPos.x / correction.x);
p.y = iFloor(vPos.y / correction.y);
pInput->iSetMousePos(p);
}

void CUICursor::WarpToWindow(CUIWindow* wnd, bool change_visibility /*= true*/)
{
// When change_visibility is true, call Show/Hide anyway
Expand All @@ -185,3 +173,17 @@ void CUICursor::WarpToWindow(CUIWindow* wnd, bool change_visibility /*= true*/)
pos.add(size).sub(sizeOfThird);
SetUICursorPosition(pos);
}

Fvector2 CUICursor::GetCursorPosition() const
{
return vPos;
}

Fvector2 CUICursor::GetCursorPositionDelta() const
{
return
{
vPos.x - vPrevPos.x,
vPos.y - vPrevPos.y
};
}
34 changes: 20 additions & 14 deletions src/xrUICore/Cursor/UICursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,42 @@ class CUIStatic;

class XRUICORE_API CUICursor : public pureRender, public CDeviceResetNotifier, public CUIResetNotifier
{
bool bVisible{};
Fvector2 vPos{};
CUIStatic* m_static{};
Fvector2 vPrevPos{};
Fvector2 correction;
bool bVisible{};
bool m_bound_to_system_cursor{};
CUIStatic* m_static{};
u32 m_become_visible_time{};
bool m_pause_autohide{};
u32 m_become_visible_time{};

void InitInternal();

public:
CUICursor();
virtual ~CUICursor();
virtual void OnRender();
~CUICursor() override;

void Show();
void Hide();

[[nodiscard]]
bool IsVisible() const { return bVisible; }

void OnRender() override;
void OnDeviceReset() override;
void OnUIReset() override;

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

Fvector2 GetCursorPositionDelta();

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

void OnDeviceReset() override;
void OnUIReset() override;
void SetUICursorPosition(Fvector2 pos);

bool IsVisible() const { return bVisible; }
void Show();
void Hide();
[[nodiscard]]
Fvector2 GetCursorPosition() const;

[[nodiscard]]
Fvector2 GetCursorPositionDelta() const;
};

0 comments on commit a44a542

Please sign in to comment.