Added game-select and system-select custom events and a corresponding 'Browsing custom events' menu option

This commit is contained in:
Leon Styhre 2025-02-10 19:18:16 +01:00
parent fe10e14b80
commit 78a0fcdff7
5 changed files with 83 additions and 1 deletions

View file

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

View file

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

View file

@ -118,6 +118,8 @@ private:
Renderer* mRenderer;
bool mStaticVideoAudio;
bool mTriggerEvent;
bool mTriggeredEventFastScroll;
std::shared_ptr<ThemeData> mTheme;
std::vector<GuiComponent*> mThemeExtras;

View file

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

View file

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