From ac66deeb25e03e562f17d475c59d9fad2f336e8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 16 Feb 2023 19:01:13 +0100 Subject: [PATCH] Improve cheats UX --- Common/UI/View.h | 8 +++--- UI/CwCheatScreen.cpp | 66 ++++++++++++++++++++++++++++++++++---------- UI/CwCheatScreen.h | 8 +++++- 3 files changed, 63 insertions(+), 19 deletions(-) diff --git a/Common/UI/View.h b/Common/UI/View.h index c50a335151..f857d837ae 100644 --- a/Common/UI/View.h +++ b/Common/UI/View.h @@ -741,10 +741,10 @@ protected: std::string smallText_; ImageID image_; // Centered if no text, on the left if text. ImageID rightIconImage_ = ImageID::invalid(); // Shows in the right. - float rightIconScale_; - float rightIconRot_; - bool rightIconFlipH_; - bool rightIconKeepColor_; + float rightIconScale_ = 0.0f; + float rightIconRot_ = 0.0f; + bool rightIconFlipH_ = false; + bool rightIconKeepColor_ = false; Padding textPadding_; bool centered_ = false; float imgScale_ = 1.0f; diff --git a/UI/CwCheatScreen.cpp b/UI/CwCheatScreen.cpp index a8f0c9a70f..184a7233a0 100644 --- a/UI/CwCheatScreen.cpp +++ b/UI/CwCheatScreen.cpp @@ -34,6 +34,10 @@ static const int FILE_CHECK_FRAME_INTERVAL = 53; +static Path GetGlobalCheatFile() { + return GetSysDirectory(DIRECTORY_CHEATS) / "cheat.db"; +} + CwCheatScreen::CwCheatScreen(const Path &gamePath) : UIDialogScreenWithGameBackground(gamePath) { } @@ -53,7 +57,7 @@ void CwCheatScreen::LoadCheatInfo() { gameID = g_paramSFO.GenerateFakeID(gamePath_.ToString()); } - if (engine_ == nullptr || gameID != gameID_) { + if (!engine_ || gameID != gameID_) { gameID_ = gameID; delete engine_; engine_ = new CWCheatEngine(gameID_); @@ -77,6 +81,7 @@ void CwCheatScreen::CreateViews() { using namespace UI; auto cw = GetI18NCategory("CwCheats"); auto di = GetI18NCategory("Dialog"); + auto mm = GetI18NCategory("MainMenu"); root_ = new AnchorLayout(new LayoutParams(FILL_PARENT, FILL_PARENT)); @@ -84,9 +89,15 @@ void CwCheatScreen::CreateViews() { Margins actionMenuMargins(50, -15, 15, 0); LinearLayout *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(400, FILL_PARENT)); - leftColumn->Add(new ItemHeader(cw->T("Options"))); //leftColumn->Add(new Choice(cw->T("Add Cheat")))->OnClick.Handle(this, &CwCheatScreen::OnAddCheat); - leftColumn->Add(new Choice(cw->T("Import Cheats")))->OnClick.Handle(this, &CwCheatScreen::OnImportCheat); + leftColumn->Add(new ItemHeader(cw->T("Import Cheats"))); + + Path cheatPath = GetGlobalCheatFile(); + + leftColumn->Add(new Choice(cheatPath.ToVisualString()))->OnClick.Handle(this, &CwCheatScreen::OnImportCheat); + leftColumn->Add(new Choice(mm->T("Browse"), ImageID("I_FOLDER_OPEN")))->OnClick.Handle(this, &CwCheatScreen::OnImportBrowse); + + leftColumn->Add(new ItemHeader(cw->T("Options"))); #if !defined(MOBILE_DEVICE) leftColumn->Add(new Choice(cw->T("Edit Cheat File")))->OnClick.Handle(this, &CwCheatScreen::OnEditCheatFile); #endif @@ -141,6 +152,21 @@ void CwCheatScreen::onFinish(DialogResult result) { } } +void CwCheatScreen::sendMessage(const char *message, const char *value) { + // Always call the base class method first to handle the most common messages. + UIDialogScreenWithGameBackground::sendMessage(message, value); + if (!strcmp(message, "browse_fileSelect")) { + Path path(value); + INFO_LOG(SYSTEM, "Attempting to load cheats from: '%s'", path.ToVisualString().c_str()); + if (ImportCheats(path)) { + g_Config.bReloadCheats = true; + } else { + // Show an error message? + } + RecreateViews(); + } +} + UI::EventReturn CwCheatScreen::OnEnableAll(UI::EventParams ¶ms) { enableAllFlag_ = !enableAllFlag_; @@ -193,24 +219,39 @@ static char *GetLineNoNewline(char *temp, int sz, FILE *fp) { return line; } +UI::EventReturn CwCheatScreen::OnImportBrowse(UI::EventParams ¶ms) { + System_SendMessage("browse_file", ""); + return UI::EVENT_DONE; +} + UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams ¶ms) { - if (gameID_.length() != 9 || !engine_) { - WARN_LOG(COMMON, "CWCHEAT: Incorrect ID(%s) - can't import cheats.", gameID_.c_str()); + if (!ImportCheats(GetGlobalCheatFile())) { + // Show an error message? return UI::EVENT_DONE; } - std::vector title; - std::vector newList; - Path cheatFile = GetSysDirectory(DIRECTORY_CHEATS) / "cheat.db"; + g_Config.bReloadCheats = true; + RecreateViews(); + return UI::EVENT_DONE; +} + +bool CwCheatScreen::ImportCheats(const Path & cheatFile) { + if (gameID_.length() != 9 || !engine_) { + WARN_LOG(COMMON, "CWCHEAT: Incorrect ID(%s) - can't import cheats.", gameID_.c_str()); + return false; + } + std::string gameID = StringFromFormat("_S %s-%s", gameID_.substr(0, 4).c_str(), gameID_.substr(4).c_str()); FILE *in = File::OpenCFile(cheatFile, "rt"); - if (!in) { WARN_LOG(COMMON, "Unable to open %s\n", cheatFile.c_str()); - return UI::EVENT_SKIPPED; + return false; } + std::vector title; + std::vector newList; + char linebuf[2048]{}; bool parseGameEntry = false; bool parseCheatEntry = false; @@ -281,10 +322,7 @@ UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams ¶ms) { } } fclose(append); - - g_Config.bReloadCheats = true; - RecreateViews(); - return UI::EVENT_DONE; + return true; } UI::EventReturn CwCheatScreen::OnCheckBox(int index) { diff --git a/UI/CwCheatScreen.h b/UI/CwCheatScreen.h index b343cbd818..d29961a2e6 100644 --- a/UI/CwCheatScreen.h +++ b/UI/CwCheatScreen.h @@ -35,9 +35,12 @@ public: UI::EventReturn OnAddCheat(UI::EventParams ¶ms); UI::EventReturn OnImportCheat(UI::EventParams ¶ms); + UI::EventReturn OnImportBrowse(UI::EventParams ¶ms); UI::EventReturn OnEditCheatFile(UI::EventParams ¶ms); UI::EventReturn OnEnableAll(UI::EventParams ¶ms); + void sendMessage(const char *message, const char *value) override; + void update() override; void onFinish(DialogResult result) override; @@ -48,16 +51,19 @@ protected: private: UI::EventReturn OnCheckBox(int index); + bool ImportCheats(const Path &cheatFile); enum { INDEX_ALL = -1 }; bool HasCheatWithName(const std::string &name); bool RebuildCheatFile(int index); UI::ScrollView *rightScroll_ = nullptr; + UI::TextView *errorMessageView_ = nullptr; + CWCheatEngine *engine_ = nullptr; std::vector fileInfo_; std::string gameID_; int fileCheckCounter_ = 0; - uint64_t fileCheckHash_; + uint64_t fileCheckHash_ = 0; bool enableAllFlag_ = false; };