1 #include "views/gamelist/BasicGameListView.h"
2 
3 #include "utils/FileSystemUtil.h"
4 #include "views/UIModeController.h"
5 #include "views/ViewController.h"
6 #include "CollectionSystemManager.h"
7 #include "Settings.h"
8 #include "SystemData.h"
9 
BasicGameListView(Window * window,FileData * root)10 BasicGameListView::BasicGameListView(Window* window, FileData* root)
11 	: ISimpleGameListView(window, root), mList(window)
12 {
13 	mList.setSize(mSize.x(), mSize.y() * 0.8f);
14 	mList.setPosition(0, mSize.y() * 0.2f);
15 	mList.setDefaultZIndex(20);
16 	addChild(&mList);
17 
18 	populateList(root->getChildrenListToDisplay());
19 }
20 
onThemeChanged(const std::shared_ptr<ThemeData> & theme)21 void BasicGameListView::onThemeChanged(const std::shared_ptr<ThemeData>& theme)
22 {
23 	ISimpleGameListView::onThemeChanged(theme);
24 	using namespace ThemeFlags;
25 	mList.applyTheme(theme, getName(), "gamelist", ALL);
26 
27 	sortChildren();
28 }
29 
onFileChanged(FileData * file,FileChangeType change)30 void BasicGameListView::onFileChanged(FileData* file, FileChangeType change)
31 {
32 	if(change == FILE_METADATA_CHANGED)
33 	{
34 		// might switch to a detailed view
35 		ViewController::get()->reloadGameListView(this);
36 		return;
37 	}
38 
39 	ISimpleGameListView::onFileChanged(file, change);
40 }
41 
populateList(const std::vector<FileData * > & files)42 void BasicGameListView::populateList(const std::vector<FileData*>& files)
43 {
44 	mList.clear();
45 	mHeaderText.setText(mRoot->getSystem()->getFullName());
46 	if (files.size() > 0)
47 	{
48 		for(auto it = files.cbegin(); it != files.cend(); it++)
49 		{
50 			mList.add((*it)->getName(), *it, ((*it)->getType() == FOLDER));
51 		}
52 	}
53 	else
54 	{
55 		addPlaceholder();
56 	}
57 }
58 
getCursor()59 FileData* BasicGameListView::getCursor()
60 {
61 	return mList.getSelected();
62 }
63 
setCursor(FileData * cursor)64 void BasicGameListView::setCursor(FileData* cursor)
65 {
66 	if(!mList.setCursor(cursor) && (!cursor->isPlaceHolder()))
67 	{
68 		populateList(cursor->getParent()->getChildrenListToDisplay());
69 		mList.setCursor(cursor);
70 
71 		// update our cursor stack in case our cursor just got set to some folder we weren't in before
72 		if(mCursorStack.empty() || mCursorStack.top() != cursor->getParent())
73 		{
74 			std::stack<FileData*> tmp;
75 			FileData* ptr = cursor->getParent();
76 			while(ptr && ptr != mRoot)
77 			{
78 				tmp.push(ptr);
79 				ptr = ptr->getParent();
80 			}
81 
82 			// flip the stack and put it in mCursorStack
83 			mCursorStack = std::stack<FileData*>();
84 			while(!tmp.empty())
85 			{
86 				mCursorStack.push(tmp.top());
87 				tmp.pop();
88 			}
89 		}
90 	}
91 }
92 
addPlaceholder()93 void BasicGameListView::addPlaceholder()
94 {
95 	// empty list - add a placeholder
96 	FileData* placeholder = new FileData(PLACEHOLDER, "<No Entries Found>", this->mRoot->getSystem()->getSystemEnvData(), this->mRoot->getSystem());
97 	mList.add(placeholder->getName(), placeholder, (placeholder->getType() == PLACEHOLDER));
98 }
99 
getQuickSystemSelectRightButton()100 std::string BasicGameListView::getQuickSystemSelectRightButton()
101 {
102 	return "right";
103 }
104 
getQuickSystemSelectLeftButton()105 std::string BasicGameListView::getQuickSystemSelectLeftButton()
106 {
107 	return "left";
108 }
109 
launch(FileData * game)110 void BasicGameListView::launch(FileData* game)
111 {
112 	ViewController::get()->launch(game);
113 }
114 
remove(FileData * game,bool deleteFile)115 void BasicGameListView::remove(FileData *game, bool deleteFile)
116 {
117 	if (deleteFile)
118 		Utils::FileSystem::removeFile(game->getPath());  // actually delete the file on the filesystem
119 	FileData* parent = game->getParent();
120 	if (getCursor() == game)                     // Select next element in list, or prev if none
121 	{
122 		std::vector<FileData*> siblings = parent->getChildrenListToDisplay();
123 		auto gameIter = std::find(siblings.cbegin(), siblings.cend(), game);
124 		unsigned int gamePos = (int)std::distance(siblings.cbegin(), gameIter);
125 		if (gameIter != siblings.cend())
126 		{
127 			if ((gamePos + 1) < siblings.size())
128 			{
129 				setCursor(siblings.at(gamePos + 1));
130 			} else if (gamePos > 1) {
131 				setCursor(siblings.at(gamePos - 1));
132 			}
133 		}
134 	}
135 	mList.remove(game);
136 	if(mList.size() == 0)
137 	{
138 		addPlaceholder();
139 	}
140 	delete game;                                 // remove before repopulating (removes from parent)
141 	onFileChanged(parent, FILE_REMOVED);           // update the view, with game removed
142 }
143 
getHelpPrompts()144 std::vector<HelpPrompt> BasicGameListView::getHelpPrompts()
145 {
146 	std::vector<HelpPrompt> prompts;
147 
148 	if(Settings::getInstance()->getBool("QuickSystemSelect"))
149 		prompts.push_back(HelpPrompt("left/right", "system"));
150 	prompts.push_back(HelpPrompt("up/down", "choose"));
151 	prompts.push_back(HelpPrompt("a", "launch"));
152 	prompts.push_back(HelpPrompt("b", "back"));
153 	if(!UIModeController::getInstance()->isUIModeKid())
154 		prompts.push_back(HelpPrompt("select", "options"));
155 	if(mRoot->getSystem()->isGameSystem())
156 		prompts.push_back(HelpPrompt("x", "random"));
157 	if(mRoot->getSystem()->isGameSystem() && !UIModeController::getInstance()->isUIModeKid())
158 	{
159 		std::string prompt = CollectionSystemManager::get()->getEditingCollection();
160 		prompts.push_back(HelpPrompt("y", prompt));
161 	}
162 	return prompts;
163 }
164