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

@@ -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());
}
}