Skip to content

Commit

Permalink
shortcut validation
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaGW2 committed Dec 11, 2024
1 parent 6ee85fb commit ef5da3f
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/Inputs/InputBinds/InputBindHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,19 @@ std::string CInputBindApi::IsInUse(InputBind aInputBind)
return "";
}

bool CInputBindApi::HasHandler(const std::string& aIdentifier)
{
const std::lock_guard<std::mutex> lock(this->Mutex);

auto it = this->Registry.find(aIdentifier);
if (it != this->Registry.end())
{
return it->second.Handler != nullptr;
}

return false;
}

const InputBind& CInputBindApi::Get(const std::string& aIdentifier)
{
const std::lock_guard<std::mutex> lock(this->Mutex);
Expand Down
6 changes: 6 additions & 0 deletions src/Inputs/InputBinds/InputBindHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class CInputBindApi
///----------------------------------------------------------------------------------------------------
std::string IsInUse(InputBind aInputBind);

///----------------------------------------------------------------------------------------------------
/// HasHandler:
/// Returns true if the InputBind with the passed identifier has a handler registered.
///----------------------------------------------------------------------------------------------------
bool HasHandler(const std::string& aIdentifier);

///----------------------------------------------------------------------------------------------------
/// Get:
/// Gets a InputBind.
Expand Down
2 changes: 1 addition & 1 deletion src/UI/UiContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ CUiContext::CUiContext(CLogHandler* aLogger, CLocalization* aLocalization, CText

this->Alerts = new CAlerts(aDataLink);
this->MainWindow = new CMainWindow();
this->QuickAccess = new CQuickAccess(aDataLink, aLogger, aInputBindApi, aTextureService, aLocalization);
this->QuickAccess = new CQuickAccess(aDataLink, aLogger, aInputBindApi, aTextureService, aLocalization, aEventApi);

this->FontManager = new CFontManager(aLocalization);
this->EscapeClose = new CEscapeClosing();
Expand Down
1 change: 1 addition & 0 deletions src/UI/Widgets/MainWindow/Options/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ void COptionsWindow::TabGeneral()
if (ImGui::Checkbox(langApi->Translate("((000111))"), &qactx->ShowArcDPSShortcut))
{
settingsctx->Set(OPT_QASHOWARCDPS, qactx->ShowArcDPSShortcut);
qactx->Validate(true);
}

/* prefetch currently selected position string */
Expand Down
79 changes: 71 additions & 8 deletions src/UI/Widgets/QuickAccess/QuickAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,24 @@ std::string CQuickAccess::EQAPositionToString(EQAPosition aQAPosition)
}
}

CQuickAccess::CQuickAccess(CDataLink* aDataLink, CLogHandler* aLogger, CInputBindApi* aInputBindApi, CTextureLoader* aTextureService, CLocalization* aLocalization)
void CQuickAccess::OnAddonLoaded(void* aEventData)
{
CContext* ctx = CContext::GetContext();
CUiContext* uictx = ctx->GetUIContext();
CQuickAccess* qactx = uictx->GetQuickAccess();

qactx->Validate(true);
}

CQuickAccess::CQuickAccess(CDataLink* aDataLink, CLogHandler* aLogger, CInputBindApi* aInputBindApi, CTextureLoader* aTextureService, CLocalization* aLocalization, CEventApi* aEventApi)
{
this->NexusLink = (NexusLinkData*)aDataLink->GetResource(DL_NEXUS_LINK);
this->MumbleLink = (Mumble::Data*)aDataLink->GetResource(DL_MUMBLE_LINK);
this->Logger = aLogger;
this->InputBindApi = aInputBindApi;
this->TextureService = aTextureService;
this->Language = aLocalization;
this->EventApi = aEventApi;

CContext* ctx = CContext::GetContext();
CSettings* settingsCtx = ctx->GetSettingsCtx();
Expand All @@ -80,16 +90,21 @@ CQuickAccess::CQuickAccess(CDataLink* aDataLink, CLogHandler* aLogger, CInputBin
this->Offset.x = settingsCtx->Get<float>(OPT_QAOFFSETX, 0.0f);
this->Offset.y = settingsCtx->Get<float>(OPT_QAOFFSETY, 0.0f);
this->Visibility = settingsCtx->Get<EQAVisibility>(OPT_QAVISIBILITY, EQAVisibility::AlwaysShow);

this->EventApi->Subscribe(EV_ADDON_LOADED, CQuickAccess::OnAddonLoaded);
}

CQuickAccess::~CQuickAccess()
{
this->EventApi->Unsubscribe(EV_ADDON_LOADED, CQuickAccess::OnAddonLoaded);

this->NexusLink = nullptr;
this->MumbleLink = nullptr;
this->Logger = nullptr;
this->InputBindApi = nullptr;
this->TextureService = nullptr;
this->Language = nullptr;
this->EventApi = nullptr;
}

void CQuickAccess::Render()
Expand All @@ -113,7 +128,6 @@ void CQuickAccess::Render()
this->IsInvalid = false;
}

this->NexusLink->QuickAccessIconsCount = static_cast<int>(this->Registry.size());
this->NexusLink->QuickAccessMode = (int)this->Location;
this->NexusLink->QuickAccessIsVertical = this->VerticalLayout;

Expand Down Expand Up @@ -205,7 +219,7 @@ void CQuickAccess::Render()
const std::lock_guard<std::mutex> lock(this->Mutex);
for (auto& [identifier, shortcut] : this->Registry)
{
if (this->ShowArcDPSShortcut == false && identifier == QA_ARCDPS) { continue; }
if (!shortcut.IsValid) { continue; }

//Logger->Debug(CH_QUICKACCESS, "size: %f | c: %d | scale: %f", size, c, Renderer::Scaling);

Expand Down Expand Up @@ -415,10 +429,17 @@ void CQuickAccess::AddShortcut(const char* aIdentifier, const char* aTextureIden
int amt = 0;
if (sh.TextureNormal != nullptr) { amt++; }
if (sh.TextureHover != nullptr) { amt++; }

if (str == QA_ARCDPS)
{
this->HasArcDPSShortcut = true;
}
}
}

this->WhereAreMyParents();

this->Validate(true);
}

void CQuickAccess::RemoveShortcut(const char* aIdentifier)
Expand All @@ -431,13 +452,20 @@ void CQuickAccess::RemoveShortcut(const char* aIdentifier)

if (it != this->Registry.end())
{
if (strcmp(aIdentifier, QA_ARCDPS) == 0)
{
this->HasArcDPSShortcut = false;
}

for (auto& orphan : it->second.ContextItems)
{
this->OrphanedCallbacks[orphan.first] = orphan.second;
}
}

this->Registry.erase(str);

this->Validate(false);
}

void CQuickAccess::NotifyShortcut(const char* aIdentifier)
Expand Down Expand Up @@ -494,6 +522,8 @@ void CQuickAccess::AddContextItem(const char* aIdentifier, const char* aTargetSh
{
this->OrphanedCallbacks[str] = shortcut;
}

this->Validate(false);
}

void CQuickAccess::RemoveContextItem(const char* aIdentifier)
Expand All @@ -508,6 +538,8 @@ void CQuickAccess::RemoveContextItem(const char* aIdentifier)
}

this->OrphanedCallbacks.erase(aIdentifier);

this->Validate(false);
}

std::map<std::string, Shortcut> CQuickAccess::GetRegistry() const
Expand Down Expand Up @@ -543,11 +575,6 @@ int CQuickAccess::Verify(void* aStartAddress, void* aEndAddress)
refCounter++;
}
}

for (std::string remidentifier : remove)
{
shortcut.ContextItems.erase(remidentifier);
}
}

/* remove bastard children */
Expand All @@ -567,9 +594,40 @@ int CQuickAccess::Verify(void* aStartAddress, void* aEndAddress)
this->OrphanedCallbacks.erase(remidentifier);
}

this->Validate(false);

return refCounter;
}

void CQuickAccess::Validate(bool aLock)
{
if (aLock)
{
this->Mutex.lock();
}

int amtValid = 0;

for (auto& [identifier, shortcut] : this->Registry)
{
shortcut.IsValid = this->IsValid(shortcut);

if (this->ShowArcDPSShortcut == false && identifier == QA_ARCDPS)
{
shortcut.IsValid = false;
}

if (shortcut.IsValid) { amtValid++; }
}

if (aLock)
{
this->Mutex.unlock();
}

this->NexusLink->QuickAccessIconsCount = amtValid;
}

void CQuickAccess::WhereAreMyParents()
{
const std::lock_guard<std::mutex> lock(this->Mutex);
Expand All @@ -591,3 +649,8 @@ void CQuickAccess::WhereAreMyParents()
this->OrphanedCallbacks.erase(remidentifier);
}
}

bool CQuickAccess::IsValid(const Shortcut& aShortcut)
{
return aShortcut.ContextItems.size() > 0 || this->InputBindApi->HasHandler(aShortcut.IBIdentifier);
}
23 changes: 22 additions & 1 deletion src/UI/Widgets/QuickAccess/QuickAccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class CQuickAccess : public virtual IWindow
///----------------------------------------------------------------------------------------------------
static std::string EQAPositionToString(EQAPosition aQAPosition);

///----------------------------------------------------------------------------------------------------
/// OnAddonLoaded:
/// Rechecks invalid shortcuts on addon load.
///----------------------------------------------------------------------------------------------------
static void OnAddonLoaded(void* aEventData);

public:
bool VerticalLayout = false;
bool ShowArcDPSShortcut = true;
Expand All @@ -60,7 +66,7 @@ class CQuickAccess : public virtual IWindow
///----------------------------------------------------------------------------------------------------
/// ctor
///----------------------------------------------------------------------------------------------------
CQuickAccess(CDataLink* aDataLink, CLogHandler* aLogger, CInputBindApi* aInputBindApi, CTextureLoader* aTextureService, CLocalization* aLocalization);
CQuickAccess(CDataLink* aDataLink, CLogHandler* aLogger, CInputBindApi* aInputBindApi, CTextureLoader* aTextureService, CLocalization* aLocalization, CEventApi* aEventApi);

///----------------------------------------------------------------------------------------------------
/// dtor
Expand Down Expand Up @@ -139,13 +145,20 @@ class CQuickAccess : public virtual IWindow
///----------------------------------------------------------------------------------------------------
int Verify(void* aStartAddress, void* aEndAddress);

///----------------------------------------------------------------------------------------------------
/// Validate:
/// Validates all shortcuts.
///----------------------------------------------------------------------------------------------------
void Validate(bool aLock);

private:
NexusLinkData* NexusLink;
Mumble::Data* MumbleLink;
CLogHandler* Logger;
CInputBindApi* InputBindApi;
CTextureLoader* TextureService;
CLocalization* Language;
CEventApi* EventApi;

mutable std::mutex Mutex;
std::map<std::string, Shortcut> Registry;
Expand All @@ -155,11 +168,19 @@ class CQuickAccess : public virtual IWindow

Texture* IconNotification = nullptr;

bool HasArcDPSShortcut = false;

///----------------------------------------------------------------------------------------------------
/// WhereAreMyParents:
/// Returns orphaned context items to their parents.
///----------------------------------------------------------------------------------------------------
void WhereAreMyParents();

///----------------------------------------------------------------------------------------------------
/// IsValid:
/// Returns true if the passed shortcut is valid. Not threadsafe.
///----------------------------------------------------------------------------------------------------
bool IsValid(const Shortcut& aShortcut);
};

#endif
2 changes: 2 additions & 0 deletions src/UI/Widgets/QuickAccess/Shortcut.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ struct ContextItem

struct Shortcut
{
bool IsValid;

int TextureGetAttempts;
std::string TextureNormalIdentifier;
std::string TextureHoverIdentifier;
Expand Down

0 comments on commit ef5da3f

Please sign in to comment.