Skip to content

Commit

Permalink
Code clean up and partition support
Browse files Browse the repository at this point in the history
  • Loading branch information
CompSciOrBust committed Jan 27, 2020
1 parent 6312392 commit 8b26b02
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DATA := data
INCLUDES := include
APP_AUTHOR := CompSciOrBust
#ROMFS := romfs
APP_VERSION := 0.6.2 Beta
APP_VERSION := 0.6.3 Beta

#---------------------------------------------------------------------------------
# options for code generation
Expand Down
5 changes: 5 additions & 0 deletions include/UI.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class ScrollList
bool CenterText = false;
ScrollList();
void Enslave(ScrollList*);
void MoveUp();
void MoveDown();
void JumpUp();
void JumpDown();
void ResetPos();
};

bool CheckButtonPressed(SDL_Rect*, int, int);
Expand Down
130 changes: 41 additions & 89 deletions source/ExplorerUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ExplorerUI : public UIWindow
vector <dirent> Files = vector <dirent>(0);
string HighlightedPath = "";
string *ChosenFile;
string DirPath = "sdmc://";
string DirPath = "mount:/";
//functions
ExplorerUI();
void GetInput();
Expand Down Expand Up @@ -86,7 +86,7 @@ void ExplorerUI::GetInput()
if (Event->jbutton.which == 0)
{
//Y pressed
if(Event->jbutton.button == 3)
if(Event->jbutton.button == 3 && DirPath != "mount:/")
{
//Update highlighted file if one exists
if(!FileNameList->ListingTextVec.empty())
Expand All @@ -107,101 +107,43 @@ void ExplorerUI::GetInput()
//Up pressed
else if(Event->jbutton.button == 13)
{
FileNameList->CursorIndex--;
FileNameList->SelectedIndex--;
FileNameList->MoveUp();
}
//Down pressed
else if(Event->jbutton.button == 15)
{
FileNameList->CursorIndex++;
FileNameList->SelectedIndex++;
FileNameList->MoveDown();
}
//Left pressed
else if(Event->jbutton.button == 12)
{
//If less items than there are spaces then move to the first item
if(FileNameList->ListingsOnScreen > FileNameList->ListingTextVec.size())
{
FileNameList->SelectedIndex = 0;
FileNameList->CursorIndex = 0;
}
//If the cursor is on the first item on the screen then move the cursor by the number of items on screen
else if(FileNameList->CursorIndex == 0)
{
//If we are on the first item jump to the last
if(FileNameList->SelectedIndex == 0)
{
FileNameList->SelectedIndex = FileNameList->ListingTextVec.size() -1;
FileNameList->CursorIndex = FileNameList->ListingsOnScreen -1;
FileNameList->ListRenderOffset = FileNameList->ListingTextVec.size() -1 - FileNameList->CursorIndex;
}
else
{
FileNameList->ListRenderOffset -= FileNameList->ListingsOnScreen;
FileNameList->SelectedIndex -= FileNameList->ListingsOnScreen;
FileNameList->CursorIndex = 0;
}
//Stop on the first item if we go past it
if(FileNameList->SelectedIndex < 0)
{
FileNameList->ListRenderOffset = 0;
FileNameList->SelectedIndex = 0;
FileNameList->CursorIndex = 0;
}
}
//If cursor isn't on the first item on screen then jump to it
else
{
FileNameList->SelectedIndex -= FileNameList->CursorIndex;
FileNameList->CursorIndex = 0;
}
FileNameList->JumpUp();
}
//Right pressed
else if(Event->jbutton.button == 14)
{
//If less items than there are spaces then move to the last item
if(FileNameList->ListingsOnScreen > FileNameList->ListingTextVec.size())
{
FileNameList->SelectedIndex = FileNameList->ListingTextVec.size()-1;
FileNameList->CursorIndex = FileNameList->ListingTextVec.size()-1;
}
//If the cursor is on the last item on the screen then move the cursor by the number of items on screen
else if(FileNameList->CursorIndex == FileNameList->ListingsOnScreen -1)
{
//If we're on the last item jump to the first
if(FileNameList->SelectedIndex == FileNameList->ListingTextVec.size()-1)
{
FileNameList->ListRenderOffset = 0;
FileNameList->SelectedIndex = 0;
FileNameList->CursorIndex = 0;
}
else
{
FileNameList->ListRenderOffset += FileNameList->ListingsOnScreen - 1;
FileNameList->SelectedIndex += FileNameList->ListingsOnScreen;
FileNameList->CursorIndex = FileNameList->ListingsOnScreen;
}
//If we go past the last item jump back to it
if(FileNameList->SelectedIndex > FileNameList->ListingTextVec.size())
{
FileNameList->SelectedIndex = FileNameList->ListingTextVec.size() -1;
FileNameList->CursorIndex = FileNameList->ListingsOnScreen;
FileNameList->ListRenderOffset = FileNameList->ListingTextVec.size() -1 - FileNameList->CursorIndex;
}
}
//If cursor isn't on the last item on screen then jump to it
else
{
FileNameList->SelectedIndex += FileNameList->ListingsOnScreen - FileNameList->CursorIndex -1;
FileNameList->CursorIndex = FileNameList->ListingsOnScreen -1;
}
FileNameList->JumpDown();
}
//A pressed
else if(Event->jbutton.button == 0)
{
//If not in a partition show partition list
if(DirPath == "mount:/")
{
DirPath = FileNameList->ListingTextVec.at(FileNameList->SelectedIndex);
LoadListDirs(DirPath);
//Store cursor position
ListOffsets.push_back(FileNameList->ListRenderOffset);
ListOffsets.push_back(FileNameList->CursorIndex);
ListOffsets.push_back(FileNameList->SelectedIndex);
//Reset the cursor
FileNameList->ResetPos();
break;
}
//If there are no files don't do anything
if(Files.empty()) break;
//Check if directory. If not open file.
string FilePath = DirPath + Files.at(FileNameList->SelectedIndex).d_name;
//Check if directory. If not open file.
if(CheckIsDir(FilePath))
{
DirPath = FilePath + "/";
Expand All @@ -211,9 +153,7 @@ void ExplorerUI::GetInput()
ListOffsets.push_back(FileNameList->CursorIndex);
ListOffsets.push_back(FileNameList->SelectedIndex);
//Reset the cursor
FileNameList->SelectedIndex = 0;
FileNameList->CursorIndex = 0;
FileNameList->ListRenderOffset = 0; //Reminder to future self. Reset this var or fatals will occur of which crash report won't help debug.
FileNameList->ResetPos();
}
else
{
Expand All @@ -228,9 +168,7 @@ void ExplorerUI::GetInput()
//Reset the cursor
if(ListOffsets.empty())
{
FileNameList->SelectedIndex = 0;
FileNameList->CursorIndex = 0;
FileNameList->ListRenderOffset = 0;
FileNameList->ResetPos();
}
else
{
Expand Down Expand Up @@ -355,14 +293,28 @@ void ExplorerUI::OpenFile(string Path)

void ExplorerUI::LoadListDirs(string DirPath)
{
Files = LoadDirs(DirPath);
ListAccesMutex.lock();
//clear the lists
FileNameList->ListingTextVec.clear();
FileSizeList->ListingTextVec.clear();
for(int i = 0; i < Files.size(); i++)
//If no drive selected show drives
if(DirPath == "mount:/")
{
FileNameList->ListingTextVec.push_back("sdmc:/");
FileSizeList->ListingTextVec.push_back("SD card");
FileNameList->ListingTextVec.push_back("user:/");
FileSizeList->ListingTextVec.push_back("NAND");
FileNameList->ListingTextVec.push_back("sys:/");
FileSizeList->ListingTextVec.push_back("NAND");
}
else
{
FileNameList->ListingTextVec.push_back(Files.at(i).d_name);
FileSizeList->ListingTextVec.push_back(GetFileSize(DirPath + "/" + Files.at(i).d_name));
Files = LoadDirs(DirPath);
for(int i = 0; i < Files.size(); i++)
{
FileNameList->ListingTextVec.push_back(Files.at(i).d_name);
FileSizeList->ListingTextVec.push_back(GetFileSize(DirPath + "/" + Files.at(i).d_name));
}
}
ListAccesMutex.unlock();
}
Expand Down
104 changes: 104 additions & 0 deletions source/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class ScrollList
bool CenterText = false;
ScrollList();
void Enslave(ScrollList*);
void MoveUp();
void MoveDown();
void JumpUp();
void JumpDown();
void ResetPos();
};

ScrollList::ScrollList()
Expand Down Expand Up @@ -171,6 +176,105 @@ void ScrollList::DrawList()
}
}

void ScrollList::JumpDown()
{
//If less items than there are spaces then move to the last item
if(ListingsOnScreen > ListingTextVec.size())
{
SelectedIndex = ListingTextVec.size()-1;
CursorIndex = ListingTextVec.size()-1;
}
//If the cursor is on the last item on the screen then move the cursor by the number of items on screen
else if(CursorIndex == ListingsOnScreen -1)
{
//If we're on the last item jump to the first
if(SelectedIndex == ListingTextVec.size()-1)
{
ListRenderOffset = 0;
SelectedIndex = 0;
CursorIndex = 0;
}
else
{
ListRenderOffset += ListingsOnScreen - 1;
SelectedIndex += ListingsOnScreen;
CursorIndex = ListingsOnScreen;
}
//If we go past the last item jump back to it
if(SelectedIndex > ListingTextVec.size())
{
SelectedIndex = ListingTextVec.size() -1;
CursorIndex = ListingsOnScreen;
ListRenderOffset = ListingTextVec.size() -1 - CursorIndex;
}
}
//If cursor isn't on the last item on screen then jump to it
else
{
SelectedIndex += ListingsOnScreen - CursorIndex -1;
CursorIndex = ListingsOnScreen -1;
}
}

void ScrollList::JumpUp()
{
//If less items than there are spaces then move to the first item
if(ListingsOnScreen > ListingTextVec.size())
{
SelectedIndex = 0;
CursorIndex = 0;
}
//If the cursor is on the first item on the screen then move the cursor by the number of items on screen
else if(CursorIndex == 0)
{
//If we are on the first item jump to the last
if(SelectedIndex == 0)
{
SelectedIndex = ListingTextVec.size() -1;
CursorIndex = ListingsOnScreen -1;
ListRenderOffset = ListingTextVec.size() -1 - CursorIndex;
}
else
{
ListRenderOffset -= ListingsOnScreen;
SelectedIndex -= ListingsOnScreen;
CursorIndex = 0;
}
//Stop on the first item if we go past it
if(SelectedIndex < 0)
{
ListRenderOffset = 0;
SelectedIndex = 0;
CursorIndex = 0;
}
}
//If cursor isn't on the first item on screen then jump to it
else
{
SelectedIndex -= CursorIndex;
CursorIndex = 0;
}
}

void ScrollList::MoveUp()
{
CursorIndex--;
SelectedIndex--;
}

void ScrollList::MoveDown()
{
CursorIndex++;
SelectedIndex++;
}

void ScrollList::ResetPos()
{
SelectedIndex = 0;
CursorIndex = 0;
ListRenderOffset = 0;
}

//Thank you to Nichole Mattera for telling me how to do this
TTF_Font *GetSharedFont(int FontSize)
{
Expand Down
15 changes: 3 additions & 12 deletions source/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ std::string GoUpDir(std::string Path)
CurrentPath[Path.length()-1] = 0x00;
Path = CurrentPath;
int LastSlash = Path.find_last_of('/');
CurrentPath[LastSlash] = 0x00;
if(strlen(CurrentPath) < 8)
if(LastSlash == -1)
{
return "sdmc:/";
return "mount:";
}
CurrentPath[LastSlash] = 0x00;
return CurrentPath;
}

Expand Down Expand Up @@ -80,15 +80,6 @@ std::string GetFileSize(std::string Path)
//Mostly taken from before the rewrite
void RecursiveFileCopy(std::string SourcePath, std::string DestPath, std::string FileName)
{
//Don't crash
if(SourcePath == "sdmc://")
{
SourcePath = "sdmc:/";
}
if(DestPath == "sdmc://")
{
DestPath = "sdmc:/";
}
//If dir
if(CheckIsDir(SourcePath))
{
Expand Down
15 changes: 15 additions & 0 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ int main(int argc, char* argv[])
SDL_Event Event;
string ChosenFile = "";

//Init nx link for printf
socketInitializeDefault();
nxlinkStdio();

//init
// mandatory at least on switch, else gfx is not properly closed
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
Expand Down Expand Up @@ -67,6 +71,16 @@ int main(int argc, char* argv[])
TTF_Init(); //Init the font
plInitialize(); //Init needed for shared font

//Mount the file systems
//user
FsFileSystem UserFS;
fsOpenBisFileSystem(&UserFS, FsBisPartitionId_User, "");
fsdevMountDevice("user", UserFS);
//System
FsFileSystem SysFS;
fsOpenBisFileSystem(&SysFS, FsBisPartitionId_System, "");
fsdevMountDevice("sys", UserFS);

//Init explorer UI
ExplorerUI *Explorer = new ExplorerUI();
Explorer->Renderer = Renderer;
Expand Down Expand Up @@ -219,6 +233,7 @@ int main(int argc, char* argv[])
InputLoopThread.join();

//Clean up
socketExit();
plExit();
SDL_DestroyRenderer(Renderer);
SDL_DestroyWindow(Window);
Expand Down

0 comments on commit 8b26b02

Please sign in to comment.