diff --git a/es-app/src/guis/GuiMenu.cpp b/es-app/src/guis/GuiMenu.cpp index d53854337..6b488fc82 100644 --- a/es-app/src/guis/GuiMenu.cpp +++ b/es-app/src/guis/GuiMenu.cpp @@ -1946,6 +1946,29 @@ void GuiMenu::openOtherOptions() s->setNeedsSaving(); } }); + + // Custom event scripts when browsing games and systems, fired using Scripting::fireEvent(). + auto customEventScriptsBrowsing = std::make_shared(); + customEventScriptsBrowsing->setState( + Settings::getInstance()->getBool("CustomEventScriptsBrowsing")); + s->addWithLabel(_("BROWSING CUSTOM EVENTS"), customEventScriptsBrowsing); + s->addSaveFunc([customEventScriptsBrowsing, s] { + if (customEventScriptsBrowsing->getState() != + Settings::getInstance()->getBool("CustomEventScriptsBrowsing")) { + Settings::getInstance()->setBool("CustomEventScriptsBrowsing", + customEventScriptsBrowsing->getState()); + s->setNeedsSaving(); + } + }); + + // If custom event scripts are disabled, then gray out this option. + if (!Settings::getInstance()->getBool("CustomEventScripts")) { + customEventScriptsBrowsing->setEnabled(false); + customEventScriptsBrowsing->setOpacity(DISABLED_OPACITY); + customEventScriptsBrowsing->getParent() + ->getChild(customEventScriptsBrowsing->getChildIndex() - 1) + ->setOpacity(DISABLED_OPACITY); + } #endif // Only show games included in the gamelist.xml files. @@ -2114,6 +2137,25 @@ void GuiMenu::openOtherOptions() applicationUpdaterFrequency->setCallback(applicationUpdaterFrequencyFunc); #endif + auto browsingEventsToggleFunc = [customEventScriptsBrowsing]() { + if (customEventScriptsBrowsing->getEnabled()) { + customEventScriptsBrowsing->setEnabled(false); + customEventScriptsBrowsing->setOpacity(DISABLED_OPACITY); + customEventScriptsBrowsing->getParent() + ->getChild(customEventScriptsBrowsing->getChildIndex() - 1) + ->setOpacity(DISABLED_OPACITY); + } + else { + customEventScriptsBrowsing->setEnabled(true); + customEventScriptsBrowsing->setOpacity(1.0f); + customEventScriptsBrowsing->getParent() + ->getChild(customEventScriptsBrowsing->getChildIndex() - 1) + ->setOpacity(1.0f); + } + }; + + customEventScripts->setCallback(browsingEventsToggleFunc); + s->setSize(mSize); mWindow->pushGui(s); } diff --git a/es-app/src/views/GamelistView.cpp b/es-app/src/views/GamelistView.cpp index c26ea44a6..896130c51 100644 --- a/es-app/src/views/GamelistView.cpp +++ b/es-app/src/views/GamelistView.cpp @@ -9,6 +9,7 @@ #include "views/GamelistView.h" #include "CollectionSystemsManager.h" +#include "Scripting.h" #include "UIModeController.h" #include "animations/LambdaAnimation.h" #include "utils/LocalizationUtil.h" @@ -20,6 +21,8 @@ GamelistView::GamelistView(FileData* root) : GamelistBase {root} , mRenderer {Renderer::getInstance()} , mStaticVideoAudio {false} + , mTriggerEvent {false} + , mTriggeredEventFastScroll {false} { } @@ -424,6 +427,17 @@ void GamelistView::update(int deltaTime) anim->advanceAnimation(0, deltaTime); } + if (mTriggerEvent) { + mTriggerEvent = false; + FileData* file {mPrimary->size() > 0 ? mPrimary->getSelected() : nullptr}; + if (file) { + Scripting::fireEvent("game-select", file->getPath(), + file->getSourceFileData()->metadata.get("name"), + file->getSourceFileData()->getSystem()->getName(), + file->getSourceFileData()->getSystem()->getFullName()); + } + } + updateChildren(deltaTime); } @@ -595,6 +609,7 @@ std::vector GamelistView::getHelpPrompts() void GamelistView::updateView(const CursorState& state) { bool loadedTexture {false}; + mTriggerEvent = false; if (mPrimary->isScrolling()) { onDemandTextureLoad(); @@ -607,8 +622,21 @@ void GamelistView::updateView(const CursorState& state) // If the game data has already been rendered to the view, then skip it this time. // This also happens when fast-scrolling. - if (file == mLastUpdated) + if (file == mLastUpdated) { + if (!mTriggeredEventFastScroll && state == CursorState::CURSOR_SCROLLING && + Settings::getInstance()->getBool("CustomEventScripts") && + Settings::getInstance()->getBool("CustomEventScriptsBrowsing")) { + mTriggeredEventFastScroll = true; + Scripting::fireEvent("game-select"); + } return; + } + + if (Settings::getInstance()->getBool("CustomEventScripts") && + Settings::getInstance()->getBool("CustomEventScriptsBrowsing")) { + mTriggerEvent = true; + mTriggeredEventFastScroll = false; + } if (!loadedTexture) onDemandTextureLoad(); diff --git a/es-app/src/views/GamelistView.h b/es-app/src/views/GamelistView.h index d05e223c3..67f9cc32a 100644 --- a/es-app/src/views/GamelistView.h +++ b/es-app/src/views/GamelistView.h @@ -118,6 +118,8 @@ private: Renderer* mRenderer; bool mStaticVideoAudio; + bool mTriggerEvent; + bool mTriggeredEventFastScroll; std::shared_ptr mTheme; std::vector mThemeExtras; diff --git a/es-app/src/views/SystemView.cpp b/es-app/src/views/SystemView.cpp index 20c8c0271..7afc19bb4 100644 --- a/es-app/src/views/SystemView.cpp +++ b/es-app/src/views/SystemView.cpp @@ -9,6 +9,7 @@ #include "views/SystemView.h" #include "Log.h" +#include "Scripting.h" #include "Settings.h" #include "Sound.h" #include "UIModeController.h" @@ -248,6 +249,14 @@ void SystemView::onCursorChanged(const CursorState& state) mWindow->passHelpComponents(nullptr); mWindow->passClockComponents(&mSystemElements[mPrimary->getCursor()].clockComponents); + if (Settings::getInstance()->getBool("CustomEventScripts") && + Settings::getInstance()->getBool("CustomEventScriptsBrowsing")) { + Scripting::fireEvent( + "system-select", mSystemElements[mPrimary->getCursor()].system->getName(), + mSystemElements[mPrimary->getCursor()].system->getFullName(), + mSystemElements[mPrimary->getCursor()].system->getRootFolder()->getFullPath()); + } + for (auto& clock : mSystemElements[mPrimary->getCursor()].clockComponents) clock->update(1000); diff --git a/es-core/src/Settings.cpp b/es-core/src/Settings.cpp index 142688cda..f167b9d21 100644 --- a/es-core/src/Settings.cpp +++ b/es-core/src/Settings.cpp @@ -311,6 +311,7 @@ void Settings::setDefaults() mBoolMap["ShowHiddenGames"] = {true, true}; #if !defined(__IOS__) mBoolMap["CustomEventScripts"] = {false, false}; + mBoolMap["CustomEventScriptsBrowsing"] = {false, false}; #endif mBoolMap["ParseGamelistOnly"] = {false, false}; mBoolMap["MAMENameStripExtraInfo"] = {true, true};