Change cheat implementation to show cheats in database order, fix some bugs

- AdvancedPaletteManager incorrectly handled negative y positions
- FocusManager still had a pointer to a view that was destroyed in the cheats panel. After changing focus, memory got corrupted.
This commit is contained in:
Gericom
2026-03-15 13:28:59 +01:00
parent 601fd6371e
commit b7d7f9f352
16 changed files with 313 additions and 424 deletions

View File

@@ -33,39 +33,40 @@ void CheatsViewModel::ActivateSelectedItem()
}
auto cheatCategory = GetCurrentCheatCategory();
u32 numberOfCategories = 0;
auto categories = cheatCategory->GetCategories(numberOfCategories);
u32 numberOfCheats = 0;
auto cheats = cheatCategory->GetCheats(numberOfCheats);
u32 numberOfSubEntries = 0;
auto subEntries = cheatCategory->GetSubEntries(numberOfSubEntries);
if (numberOfCategories + numberOfCheats == 0)
if (numberOfSubEntries == 0)
{
// There is nothing to activate
return;
}
if (_selectedItem < (int)numberOfCategories)
auto& entry = subEntries[_selectedItem];
if (entry.IsCheatCategory())
{
// Category activated
if (_categoryStackLevel + 1 != _categoryStack.size())
{
_categoryStack[++_categoryStackLevel] = { &categories[_selectedItem], (u32)_selectedItem };
_categoryStack[++_categoryStackLevel] = { &entry, (u32)_selectedItem };
_selectedItem = 0;
}
}
else
{
// Toggle cheat on/off
auto& cheat = cheats[_selectedItem - numberOfCategories];
bool isEnabled = !cheat.GetIsCheatActive();
bool isEnabled = !entry.GetIsCheatActive();
if (isEnabled && cheatCategory->GetIsMaxOneCheatActive())
{
for (u32 i = 0; i < numberOfCheats; i++)
for (u32 i = 0; i < numberOfSubEntries; i++)
{
cheats[i].SetIsCheatActive(false);
if (!subEntries[i].IsCheatCategory())
{
subEntries[i].SetIsCheatActive(false);
}
}
}
cheat.SetIsCheatActive(isEnabled);
entry.SetIsCheatActive(isEnabled);
_changed = true;
}
}
@@ -111,23 +112,24 @@ void CheatsViewModel::DisableAllCheats()
}
}
void CheatsViewModel::DisableAllCheats(const ICheatCategory* cheatCategory)
void CheatsViewModel::DisableAllCheats(const CheatEntry* cheatCategory)
{
u32 numberOfCategories = 0;
auto categories = cheatCategory->GetCategories(numberOfCategories);
for (u32 i = 0; i < numberOfCategories; i++)
u32 numberOfSubEntries = 0;
auto subEntries = cheatCategory->GetSubEntries(numberOfSubEntries);
for (u32 i = 0; i < numberOfSubEntries; i++)
{
DisableAllCheats(&categories[i]);
}
u32 numberOfCheats = 0;
auto cheats = cheatCategory->GetCheats(numberOfCheats);
for (u32 i = 0; i < numberOfCheats; i++)
{
if (cheats[i].GetIsCheatActive())
auto& entry = subEntries[i];
if (entry.IsCheatCategory())
{
cheats[i].SetIsCheatActive(false);
_changed = true;
DisableAllCheats(&entry);
}
else
{
if (entry.GetIsCheatActive())
{
entry.SetIsCheatActive(false);
_changed = true;
}
}
}
}

View File

@@ -44,7 +44,7 @@ public:
/// @brief Gets the current cheat category.
/// @return The current cheat category.
const ICheatCategory* GetCurrentCheatCategory() const { return _categoryStack[_categoryStackLevel].cheatCategory; }
const CheatEntry* GetCurrentCheatCategory() const { return _categoryStack[_categoryStackLevel].cheatCategory; }
/// @brief Gets the index of the selected item.
/// @return The index of the selected item.
@@ -64,7 +64,7 @@ public:
private:
struct CategoryStackEntry
{
const ICheatCategory* cheatCategory;
const CheatEntry* cheatCategory;
u32 index;
};
@@ -78,5 +78,5 @@ private:
u32 _categoryStackLevel = 0;
std::array<CategoryStackEntry, 8> _categoryStack;
void DisableAllCheats(const ICheatCategory* cheatCategory);
void DisableAllCheats(const CheatEntry* cheatCategory);
};

View File

@@ -25,9 +25,9 @@ CheatListItemView::CheatListItemView(const VramOffsets& vramOffsets,
void CheatListItemView::Update()
{
_nameLabel.SetPosition(_position.x + NAME_LABEL_X, _position.y + NAME_LABEL_Y);
if (_cheat != nullptr)
if (_cheatEntry != nullptr && !_cheatEntry->IsCheatCategory())
{
_iconVramOffset = _cheat->GetIsCheatActive()
_iconVramOffset = _cheatEntry->GetIsCheatActive()
? _vramOffsets.checkboxCheckedIconVramOffset
: _vramOffsets.checkboxUncheckedIconVramOffset;
}

View File

@@ -1,8 +1,7 @@
#pragma once
#include "gui/views/ViewContainer.h"
#include "gui/views/Label2DView.h"
#include "cheats/CheatCategory.h"
#include "cheats/Cheat.h"
#include "cheats/CheatEntry.h"
class MaterialColorScheme;
class IFontRepository;
@@ -34,17 +33,14 @@ public:
_nameLabel.SetText(name);
}
void SetCategory(const CheatCategory* cheatCategory)
void SetEntry(const CheatEntry* cheatEntry)
{
_cheat = nullptr;
_nameLabel.SetText(cheatCategory->GetName());
_iconVramOffset = _vramOffsets.folderIconVramOffset;
}
void SetCheat(const Cheat* cheat)
{
_cheat = cheat;
_nameLabel.SetText(_cheat->GetName());
_cheatEntry = cheatEntry;
_nameLabel.SetText(cheatEntry->GetName());
if (cheatEntry->IsCheatCategory())
{
_iconVramOffset = _vramOffsets.folderIconVramOffset;
}
}
private:
@@ -52,5 +48,5 @@ private:
VramOffsets _vramOffsets;
const MaterialColorScheme* _materialColorScheme;
u32 _iconVramOffset = 0;
const Cheat* _cheat = nullptr;
const CheatEntry* _cheatEntry = nullptr;
};

View File

@@ -1,25 +1,22 @@
#pragma once
#include "gui/views/RecyclerAdapter.h"
#include "cheats/CheatCategory.h"
#include "cheats/Cheat.h"
#include "cheats/CheatEntry.h"
#include "CheatListItemView.h"
/// @brief Recycler adapter for cheats.
class CheatsAdapter : public RecyclerAdapter
{
public:
CheatsAdapter(const ICheatCategory* cheatCategory, const MaterialColorScheme* materialColorScheme,
CheatsAdapter(const CheatEntry* cheatCategory, const MaterialColorScheme* materialColorScheme,
const IFontRepository* fontRepository, const CheatListItemView::VramOffsets& vramOffsets)
: _cheatCategory(cheatCategory), _materialColorScheme(materialColorScheme)
, _fontRepository(fontRepository), _vramOffsets(vramOffsets) { }
u32 GetItemCount() const override
{
u32 numberOfCategories = 0;
_cheatCategory->GetCategories(numberOfCategories);
u32 numberOfCheats = 0;
_cheatCategory->GetCheats(numberOfCheats);
return numberOfCategories + numberOfCheats;
u32 numberOfSubEntries = 0;
_cheatCategory->GetSubEntries(numberOfSubEntries);
return numberOfSubEntries;
}
void GetViewSize(int& width, int& height) const override
@@ -41,19 +38,9 @@ public:
void BindView(View* view, int index) const override
{
auto listItemView = static_cast<CheatListItemView*>(view);
u32 numberOfCategories = 0;
auto categories = _cheatCategory->GetCategories(numberOfCategories);
if ((u32)index < numberOfCategories)
{
listItemView->SetCategory(&categories[index]);
}
else
{
index -= numberOfCategories;
u32 numberOfCheats = 0;
auto cheats = _cheatCategory->GetCheats(numberOfCheats);
listItemView->SetCheat(&cheats[index]);
}
u32 numberOfSubEntries = 0;
auto subEntries = _cheatCategory->GetSubEntries(numberOfSubEntries);
listItemView->SetEntry(&subEntries[index]);
}
void ReleaseView(View* view, int index) const override
@@ -62,7 +49,7 @@ public:
}
private:
const ICheatCategory* _cheatCategory;
const CheatEntry* _cheatCategory;
const MaterialColorScheme* _materialColorScheme;
const IFontRepository* _fontRepository;
CheatListItemView::VramOffsets _vramOffsets;

View File

@@ -237,6 +237,9 @@ bool CheatsBottomSheetView::HandleInput(const InputProvider& inputProvider, Focu
void CheatsBottomSheetView::UpdateCheatList()
{
// Need to unfocus first, otherwise the focus manager still contains a pointer to a view that is going to be destroyed
_focusManager->Unfocus();
auto oldAdapter = _cheatsAdapter;
_cheatsAdapter = new CheatsAdapter(
_viewModel->GetCurrentCheatCategory(), _materialColorScheme, _fontRepository, _vramOffsets);
@@ -261,21 +264,8 @@ void CheatsBottomSheetView::UpdateDescriptionText()
else
{
auto cheatCategory = _viewModel->GetCurrentCheatCategory();
u32 numberOfCategories = 0;
auto categories = cheatCategory->GetCategories(numberOfCategories);
u32 numberOfCheats = 0;
auto cheats = cheatCategory->GetCheats(numberOfCheats);
if ((u32)selectedItem < numberOfCategories)
{
_descriptionLabel.SetText(categories[selectedItem].GetDescription());
}
else if ((u32)selectedItem < numberOfCategories + numberOfCheats)
{
_descriptionLabel.SetText(cheats[selectedItem - numberOfCategories].GetDescription());
}
else
{
_descriptionLabel.SetText("");
}
u32 numberOfSubEntries = 0;
auto subEntries = cheatCategory->GetSubEntries(numberOfSubEntries);
_descriptionLabel.SetText(subEntries[selectedItem].GetDescription());
}
}