Commit d7fafbd1 authored by Jean-Baptiste Demonte's avatar Jean-Baptiste Demonte

feat(favorite): Add "Delete favorite" from favorite view feature

parent bd744aae
......@@ -96,6 +96,7 @@ class FileData
* Booleans
*/
inline bool isEmpty() const { return mType == ItemType::Empty; }
inline bool isGame() const { return mType == ItemType::Game; }
inline bool isFolder() const { return mType == ItemType::Folder; }
......
......@@ -182,6 +182,16 @@ int FolderData::countItemsRecursively(Filter includes, bool includefolders) cons
return result;
}
bool FolderData::hasGame() const
{
for (FileData* fd : mChildren)
{
if (fd->isGame() || (fd->isFolder() && CastFolder(fd)->hasGame()))
return true;
}
return false;
}
int FolderData::getItems(FileData::List& to, Filter includes, bool includefolders) const
{
int gameCount = 0;
......
......@@ -126,6 +126,11 @@ class FolderData : public FileData
*/
FileData* LookupGame(const std::string& item, SearchAttributes attributes) { return LookupGame(item, attributes, std::string()); }
/*!
* Return true if contain at least one game
*/
bool hasGame() const;
/*!
* Get total games in all folders, incuding hidden
* @return Game count
......
......@@ -791,3 +791,12 @@ int SystemData::getSystemIndex(const std::string &name)
return i;
return -1;
}
SystemData* SystemData::getFirstSystemWithGame()
{
for (auto &system : sSystemVector)
if (system->hasGame())
return system;
return nullptr;
}
\ No newline at end of file
......@@ -108,7 +108,8 @@ class SystemData
std::string getGamelistPath(bool forWrite) const;
//bool hasGamelist() const;
std::string getThemePath() const;
bool hasGame() const { return mRootFolder.hasGame(); }
int getGameCount() const { return mRootFolder.countAll(false); };
int getFavoritesCount() const{ return mRootFolder.countAllFavorites(false); };
int getHiddenCount() const{ return mRootFolder.countAllHidden(false); };
......@@ -122,6 +123,7 @@ class SystemData
static std::vector<SystemData*> sSystemVector;
static SystemData* getFavoriteSystem();
static SystemData* getSystem(std::string& name);
static SystemData* getFirstSystemWithGame();
static int getSystemIndex(const std::string& name);
static const std::vector<SystemData*>& getAllSystems() { return sSystemVector; }
......
......@@ -156,9 +156,8 @@ void SystemView::populate()
void SystemView::goToSystem(SystemData* system, bool animate)
{
setCursor(system);
if (!setCursor(system)) // When deleting last favorite from favorite view, favorite system is no longer available
setCursor(SystemData::getFirstSystemWithGame());
if(!animate)
finishAnimation(0);
......
......@@ -52,11 +52,23 @@ void ViewController::goToStart()
/* mState.viewing = START_SCREEN;
mCurrentView.reset();
playViewTransition(); */
int firstSystemIndex = getFirstSystemIndex();
if (RecalboxConf::getInstance()->get("emulationstation.hidesystemview") == "1" || RecalboxConf::getInstance()->get("emulationstation.bootongamelist") == "1")
goToGameList(SystemData::sSystemVector.at(firstSystemIndex));
else
goToSystemView(SystemData::sSystemVector.at(firstSystemIndex));
std::string systemName = RecalboxConf::getInstance()->get("emulationstation.selectedsystem");
int index = systemName.empty() ? -1 : SystemData::getSystemIndex(systemName);
SystemData* selectedSystem = index < 0 ? nullptr : SystemData::sSystemVector.at(index);
if (!selectedSystem || !selectedSystem->hasGame())
selectedSystem = SystemData::getFirstSystemWithGame();
if (RecalboxConf::getInstance()->get("emulationstation.hidesystemview") == "1")
goToGameList(selectedSystem);
else
{
if (RecalboxConf::getInstance()->get("emulationstation.bootongamelist") == "1")
goToGameList(selectedSystem);
else
goToSystemView(selectedSystem);
}
}
int ViewController::getSystemId(SystemData* system)
......@@ -67,11 +79,16 @@ int ViewController::getSystemId(SystemData* system)
void ViewController::goToSystemView(SystemData* system)
{
auto systemList = getSystemListView();
systemList->setPosition(getSystemId(system) * (float)Renderer::getScreenWidth(), systemList->getPosition().y());
if (!system->hasGame()) {
system = SystemData::getFirstSystemWithGame();
}
mState.viewing = SYSTEM_SELECT;
mState.system = system;
auto systemList = getSystemListView();
systemList->setPosition(getSystemId(system) * (float)Renderer::getScreenWidth(), systemList->getPosition().y());
systemList->goToSystem(system, false);
mCurrentView = systemList;
mCurrentView->onShow();
......@@ -119,7 +136,7 @@ bool ViewController::goToGameList(std::string& systemName) {
void ViewController::goToGameList(SystemData* system)
{
if(mState.viewing == SYSTEM_SELECT)
if (mState.viewing == SYSTEM_SELECT)
{
// move system list
auto sysList = getSystemListView();
......@@ -132,16 +149,19 @@ void ViewController::goToGameList(SystemData* system)
if (mInvalidGameList[system])
{
if(!system->isFavorite()) {
mInvalidGameList[system] = false;
if (!reloadGameListView(system))
{
// if listview has been reload due to last game has been deleted,
// we have to stop the previous goToGameList process because current
// system will no longer exists in the available list
return;
}
if (!system->isFavorite())
{
updateFavorite(system, getGameListView(system).get()->getCursor());
if (mFavoritesOnly != Settings::getInstance()->getBool("FavoritesOnly")) {
reloadGameListView(system);
mFavoritesOnly = Settings::getInstance()->getBool("FavoritesOnly");
}
}else {
reloadGameListView(system);
mFavoritesOnly = Settings::getInstance()->getBool("FavoritesOnly");
}
mInvalidGameList[system] = false;
}
mState.viewing = GAME_LIST;
......@@ -402,9 +422,9 @@ void ViewController::preload()
}
}
void ViewController::reloadGameListView(IGameListView* view, bool reloadTheme)
bool ViewController::reloadGameListView(IGameListView* view, bool reloadTheme)
{
if(view->getRoot()->countAll(false) > 0)
if (view->getRoot()->countAll(false) > 0)
{
for (auto it = mGameListViews.begin(); it != mGameListViews.end(); it++)
{
......@@ -412,30 +432,27 @@ void ViewController::reloadGameListView(IGameListView* view, bool reloadTheme)
{
bool isCurrent = (mCurrentView == it->second);
SystemData *system = it->first;
int gameCount = system->getGameCount();
FileData *cursor = gameCount != 0 ? view->getCursor() : nullptr;
bool hasGame = system->hasGame();
FileData *cursor = hasGame ? view->getCursor() : nullptr;
mGameListViews.erase(it);
if (reloadTheme)
system->loadTheme();
std::shared_ptr<IGameListView> newView = getGameListView(system);
if (gameCount != 0)
if (hasGame)
newView->setCursor(cursor);
/*if (system->getGameCount() > 1) {
newView->setCursor(cursor);
} else if (system->getGameCount() == 1) {
newView->setCursor(system->getRootFolder()->getChildren().at(0));
}*/
if (isCurrent)
mCurrentView = newView;
break;
}
}
return true;
}
else
{
deleteAndReloadAll();
return false;
}
}
......@@ -528,18 +545,3 @@ HelpStyle ViewController::getHelpStyle()
return mCurrentView->getHelpStyle();
}
int ViewController::getFirstSystemIndex() {
std::string systemName = RecalboxConf::getInstance()->get("emulationstation.selectedsystem");
if(!systemName.empty()){
int index = SystemData::getSystemIndex(systemName);
if (index != -1){
LOG(LogInfo) << "emulationstation.selectedsystem variable set to " << systemName.c_str() << " system found !";
return index;
}else {
LOG(LogWarning) << "emulationstation.selectedsystem variable set to " << systemName.c_str() << " but unable to find such a system.";
return 0;
}
}
return 0;
}
......@@ -21,8 +21,8 @@ public:
// If a basic view detected a metadata change, it can request to recreate
// the current gamelist view (as it may change to be detailed).
void reloadGameListView(IGameListView* gamelist, bool reloadTheme = false);
inline void reloadGameListView(SystemData* system, bool reloadTheme = false) { reloadGameListView(getGameListView(system).get(), reloadTheme); }
bool reloadGameListView(IGameListView* gamelist, bool reloadTheme = false);
inline bool reloadGameListView(SystemData* system, bool reloadTheme = false) { return reloadGameListView(getGameListView(system).get(), reloadTheme); }
void deleteAndReloadAll();
void reloadAll(); // Reload everything with a theme. Used when the "ThemeSet" setting changes.
void setInvalidGamesList(SystemData* system);
......@@ -83,7 +83,7 @@ private:
void playViewTransition();
int getSystemId(SystemData* system);
std::shared_ptr<GuiComponent> mCurrentView;
std::map< SystemData*, std::shared_ptr<IGameListView> > mGameListViews;
std::shared_ptr<SystemView> mSystemListView;
......@@ -96,8 +96,5 @@ private:
bool mFavoritesOnly;
State mState;
int getFirstSystemIndex();
Window* mWindow;
Window* mWindow;
};
......@@ -18,8 +18,7 @@ ISimpleGameListView::ISimpleGameListView(Window* window, FolderData* root)
mHeaderText(window),
mHeaderImage(window),
mBackground(window),
mThemeExtras(window),
mFavoriteChange(false)
mThemeExtras(window)
{
mFavoritesCount = getRoot()->getSystem()->getFavoritesCount();
mFavoritesOnly = mFavoritesCount > 0 && Settings::getInstance()->getBool("FavoritesOnly");
......@@ -38,6 +37,8 @@ ISimpleGameListView::ISimpleGameListView(Window* window, FolderData* root)
mBackground.setResize(mSize.x(), mSize.y());
mBackground.setDefaultZIndex(0);
mIsFavoriteSystem = getRoot()->getSystem() == SystemData::getFavoriteSystem();
addChild(&mHeaderText);
addChild(&mBackground);
}
......@@ -176,13 +177,6 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) {
else if (!hideSystemView)
{
onFocusLost();
if (mFavoriteChange)
{
ViewController::get()->setInvalidGamesList(getRoot()->getSystem());
mFavoriteChange = false;
}
ViewController::get()->goToSystemView(getRoot()->getSystem());
}
return true;
......@@ -191,38 +185,35 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) {
// TOGGLE FAVORITES
if (config->isMappedTo("y", input)) {
FileData* cursor = getCursor();
if (!ViewController::get()->getState().getSystem()->isFavorite() && cursor->getSystem()->getHasFavoritesInTheme()) {
if (cursor->isGame()) {
mFavoriteChange = true;
MetadataDescriptor& md = cursor->Metadata();
SystemData *favoriteSystem = SystemData::getFavoriteSystem();
if (md.Favorite()) {
md.SetFavorite(false);
if (favoriteSystem != nullptr) {
favoriteSystem->getRootFolder()->removeChild(cursor);
}
} else {
md.SetFavorite(true);
if (favoriteSystem != nullptr) {
favoriteSystem->getRootFolder()->addChild(cursor, false);
}
}
if (favoriteSystem != nullptr) {
ViewController::get()->setInvalidGamesList(favoriteSystem);
ViewController::get()->getSystemListView()->manageFavorite();
}
// Reload to refresh the favorite icon
int cursorPlace = getCursorIndex();
refreshList();
setCursorIndex(cursorPlace);
mFavoritesCount = mFavoritesCount + (md.Favorite() ? 1 : -1);
if (!mFavoritesCount) { mFavoritesOnly = false; }
updateHelpPrompts();
}
if (cursor->isGame() && cursor->getSystem()->getHasFavoritesInTheme()) {
MetadataDescriptor& md = cursor->Metadata();
SystemData *favoriteSystem = SystemData::getFavoriteSystem();
md.SetFavorite(!md.Favorite());
if (favoriteSystem != nullptr) {
if (md.Favorite()) {
favoriteSystem->getRootFolder()->addChild(cursor, false);
} else {
favoriteSystem->getRootFolder()->removeChild(cursor);
}
if (mIsFavoriteSystem) {
ViewController::get()->setInvalidGamesList(cursor->getSystem());
} else {
ViewController::get()->setInvalidGamesList(favoriteSystem);
}
ViewController::get()->getSystemListView()->manageFavorite();
}
// Reload to refresh the favorite icon
int cursorPlace = getCursorIndex();
refreshList();
setCursorIndex(cursorPlace);
mFavoritesCount = mFavoritesCount + (md.Favorite() ? 1 : -1);
if (!mFavoritesCount) { mFavoritesOnly = false; }
updateHelpPrompts();
}
RecalboxSystem::getInstance()->NotifyGame(*getCursor(), false, false);
return true;
......@@ -232,10 +223,6 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) {
if (config->isMappedTo("right", input)) {
if (Settings::getInstance()->getBool("QuickSystemSelect") && !hideSystemView) {
onFocusLost();
if (mFavoriteChange) {
ViewController::get()->setInvalidGamesList(getCursor()->getSystem());
mFavoriteChange = false;
}
ViewController::get()->goToNextGameList();
RecalboxSystem::getInstance()->NotifyGame(*getCursor(), false, false);
return true;
......@@ -246,10 +233,6 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) {
if (config->isMappedTo("left", input)) {
if (Settings::getInstance()->getBool("QuickSystemSelect") && !hideSystemView) {
onFocusLost();
if (mFavoriteChange) {
ViewController::get()->setInvalidGamesList(getCursor()->getSystem());
mFavoriteChange = false;
}
ViewController::get()->goToPrevGameList();
RecalboxSystem::getInstance()->NotifyGame(*getCursor(), false, false);
return true;
......@@ -314,7 +297,7 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) {
}
if(config->isMappedTo("select", input) && input.value && getRoot()->getSystem() != SystemData::getFavoriteSystem())
if(config->isMappedTo("select", input) && input.value && !mIsFavoriteSystem)
{
if (mFavoritesCount) {
mFavoritesOnly = !mFavoritesOnly;
......@@ -342,7 +325,6 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) {
std::vector<HelpPrompt> ISimpleGameListView::getHelpPrompts() {
bool hideSystemView = RecalboxConf::getInstance()->get("emulationstation.hidesystemview") == "1";
bool isFavoriteSystem = getRoot()->getSystem() == SystemData::getFavoriteSystem();
std::vector<HelpPrompt> prompts;
prompts.push_back(HelpPrompt("b", _("LAUNCH")));
......@@ -350,7 +332,9 @@ std::vector<HelpPrompt> ISimpleGameListView::getHelpPrompts() {
if ((RecalboxConf::getInstance()->get("global.netplay") == "1") && (RecalboxConf::getInstance()->isInList("global.netplay.systems", getCursor()->getSystem()->getName())))
prompts.push_back(HelpPrompt("x", _("NETPLAY")));
if (!isFavoriteSystem)
if (mIsFavoriteSystem)
prompts.push_back(HelpPrompt("y", _("Remove from favorite")));
else
prompts.push_back(HelpPrompt("y", _("Favorite")));
if (!hideSystemView)
......@@ -361,7 +345,7 @@ std::vector<HelpPrompt> ISimpleGameListView::getHelpPrompts() {
if (Settings::getInstance()->getBool("QuickSystemSelect") && !hideSystemView)
prompts.push_back(HelpPrompt("left/right", _("SYSTEM")));
if (!isFavoriteSystem)
if (!mIsFavoriteSystem)
{
prompts.push_back(HelpPrompt("start", _("OPTIONS")));
......
......@@ -51,6 +51,6 @@ protected:
bool mFavoritesOnly;
private:
bool mFavoriteChange;
int mFavoritesCount;
bool mIsFavoriteSystem;
int mFavoritesCount;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment