Skip to content

Commit

Permalink
Added self updater and improved UI
Browse files Browse the repository at this point in the history
  • Loading branch information
CompSciOrBust committed Oct 29, 2019
1 parent a457475 commit 0efdafc
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ BUILD := build
SOURCES := source
DATA := data
INCLUDES := include
ROMFS := romfs
#ROMFS := romfs
APP_TITLE := Amiigo
APP_AUTHOR := CompSciOrBust
APP_VERSION := 1.3.1
APP_VERSION := 1.4.0

#---------------------------------------------------------------------------------
# options for code generation
Expand Down
2 changes: 2 additions & 0 deletions include/Networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <string>

std::size_t CurlStrWrite(const char* in, std::size_t size, std::size_t num, std::string* out);
std::size_t CurlFileWrite(const char* in, std::size_t size, std::size_t num, FILE* out);
std::string RetrieveContent(std::string URL, std::string MIMEType);
void RetrieveToFile(std::string, std::string);
std::string FormatURL(std::string TextToFormat);
bool HasConnection();
5 changes: 4 additions & 1 deletion include/UI.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <SDL2/SDL_ttf.h>
#include <string>
#include <vector>
#include <switch.h>
using namespace std;

class ScrollList
Expand All @@ -26,4 +27,6 @@ class ScrollList
bool CenterText = false;
};

bool CheckButtonPressed(SDL_Rect*, int, int);
bool CheckButtonPressed(SDL_Rect*, int, int);
TTF_Font *GetSharedFont(int FontSize);
void DrawButtonBorders(SDL_Renderer*, ScrollList*, ScrollList*, int, int, int, int, bool);
31 changes: 31 additions & 0 deletions include/UpdaterUI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//A lot of code for this was modified from Goldleaf. Thank you Xor (Again).
#include <SDL.h>
#include <SDL2/SDL_ttf.h>
//#include <UI.h>
#include "networking.h"
#include "nlohmann/json.hpp"
#include <fstream>
using namespace std;
using json = nlohmann::json;

class UpdaterUI
{
private:
void DrawText(std::string);
int UpdateState = 0;
bool CheckForNewVersion();
std::string UpdateText = "null";
TTF_Font *TextFont;
SDL_Color TextColour = {0, 0, 0};
json GitAPIData;
string latestid;
public:
UpdaterUI();
void DrawUI();
SDL_Event *Event;
int *WindowState;
SDL_Renderer *renderer;
int *Width;
int *Height;
int *IsDone;
};
22 changes: 13 additions & 9 deletions source/AmiigoUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ class AmiigoUI
AmiigoUI::AmiigoUI()
{
//Load the header font
HeaderFont = TTF_OpenFont("romfs:/font.ttf", 48);
//HeaderFont = TTF_OpenFont("romfs:/font.ttf", 48);
HeaderFont = GetSharedFont(48);

//Create the lists
AmiiboList = new ScrollList();
MenuList = new ScrollList();
//Add items to the menu list
MenuList->ListingTextVec.push_back("Amiibo list");
MenuList->ListingTextVec.push_back("Amiigo Maker");
MenuList->ListingTextVec.push_back("Check for updates");
MenuList->ListingTextVec.push_back("Exit");
//Scan the Amiibo folder for Amiibos
ScanForAmiibos();
Expand Down Expand Up @@ -164,11 +167,6 @@ void AmiigoUI::DrawUI()
*WindowState = MenuList->SelectedIndex;
}
}
//B pressed so switch to Amiibo generator
else if(Event->jbutton.button == 1)
{
*WindowState = 1;
}
}
break;
}
Expand All @@ -184,6 +182,8 @@ void AmiigoUI::DrawUI()
DrawFooter();
AmiiboList->DrawList();
MenuList->DrawList();
DrawButtonBorders(renderer, AmiiboList, MenuList, HeaderHeight, FooterHeight, *Width, *Height, false);
//DrawButtonBorders();
//Check if list item selected via touch screen
if(AmiiboList->ItemSelected)
{
Expand Down Expand Up @@ -340,10 +340,14 @@ void AmiigoUI::InitList()
HeaderHeight = (*Height / 100) * 10;
FooterHeight = (*Height / 100) * 10;
AmiiboListWidth = (*Width / 100) * 80;
//for shared font
PlFontData standardFontData;
plGetSharedFontByType(&standardFontData, PlSharedFontType_Standard);

//Assign vars
AmiiboList->TouchListX = &TouchX;
AmiiboList->TouchListY = &TouchY;
AmiiboList->ListFont = TTF_OpenFont("romfs:/font.ttf", 32); //Load the list font
AmiiboList->ListFont = GetSharedFont(32); //Load the list font
AmiiboList->ListingsOnScreen = 10;
AmiiboList->ListHeight = *Height - HeaderHeight - FooterHeight;
AmiiboList->ListWidth = AmiiboListWidth;
Expand All @@ -358,8 +362,8 @@ void AmiigoUI::InitList()
//Menu list
MenuList->TouchListX = &TouchX;
MenuList->TouchListY = &TouchY;
MenuList->ListFont = TTF_OpenFont("romfs:/font.ttf", 32); //Load the list font
MenuList->ListingsOnScreen = 3;
MenuList->ListFont = GetSharedFont(32); //Load the list font
MenuList->ListingsOnScreen = MenuList->ListingTextVec.size();
MenuList->ListHeight = *Height - HeaderHeight - FooterHeight;
MenuList->ListWidth = *Width - AmiiboListWidth;
MenuList->ListYOffset = HeaderHeight;
Expand Down
9 changes: 6 additions & 3 deletions source/CreatorUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ class CreatorUI
CreatorUI::CreatorUI()
{
nifmInitialize(); //Init nifm for connection stuff
HeaderFont = TTF_OpenFont("romfs:/font.ttf", 48); //Load the header font
ListFont = TTF_OpenFont("romfs:/font.ttf", 32); //Load the list font
HeaderFont = GetSharedFont(48);
ListFont = GetSharedFont(32);
//HeaderFont = TTF_OpenFont("romfs:/font.ttf", 48); //Load the header font
//ListFont = TTF_OpenFont("romfs:/font.ttf", 32); //Load the list font
GetDataFromAPI(""); //Get data from the API

//Create the lists
Expand Down Expand Up @@ -104,7 +106,7 @@ void CreatorUI::InitList()
//Create the lists
SeriesList->TouchListX = &TouchX;
SeriesList->TouchListY = &TouchY;
SeriesList->ListFont = TTF_OpenFont("romfs:/font.ttf", 32); //Load the list font
SeriesList->ListFont = GetSharedFont(32); //Load the list font
SeriesList->ListingsOnScreen = 10;
SeriesList->ListWidth = SeriesListWidth;
SeriesList->renderer = renderer;
Expand Down Expand Up @@ -212,6 +214,7 @@ void CreatorUI::DrawUI()
SeriesList->DrawList();
MenuList->DrawList();
DrawFooter();
DrawButtonBorders(renderer, SeriesList, MenuList, HeaderHeight, FooterHeight, *Width, *Height, true);
//Check if list item selected via touch screen
if(SeriesList->ItemSelected)
{
Expand Down
30 changes: 30 additions & 0 deletions source/Networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ std::size_t CurlStrWrite(const char* in, std::size_t size, std::size_t num, std:
out->append(in, totalBytes);
return totalBytes;
}

std::size_t CurlFileWrite(const char* in, std::size_t size, std::size_t num, FILE* out)
{
fwrite(in, size, num, out);
return (size * num);
}

std::string RetrieveContent(std::string URL, std::string MIMEType)
{
socketInitializeDefault();
Expand All @@ -23,6 +30,7 @@ std::string RetrieveContent(std::string URL, std::string MIMEType)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerdata);
}
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Amiigo"); //Turns out this was important and I should not have deleted it
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
Expand All @@ -34,6 +42,28 @@ std::string RetrieveContent(std::string URL, std::string MIMEType)
return cnt;
}

void RetrieveToFile(std::string URL, std::string Path)
{
socketInitializeDefault();
FILE *f = fopen(Path.c_str(), "wb");
if(f)
{
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Amiigo");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFileWrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
fclose(f);
socketExit();
}

//I made this so even though it's only one two calls it's probably janky.
std::string FormatURL(std::string TextToFormat)
{
Expand Down
43 changes: 43 additions & 0 deletions source/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <SDL2/SDL_ttf.h>
#include <string>
#include <vector>
#include <switch.h>
using namespace std;

class ScrollList
Expand Down Expand Up @@ -131,4 +132,46 @@ void ScrollList::DrawList()
SDL_DestroyTexture(FileNameTexture);
SDL_FreeSurface(FileNameSurface);
}
}

//Thank you to Nichole Mattera for telling me how to do this
TTF_Font *GetSharedFont(int FontSize)
{
PlFontData standardFontData;
plGetSharedFontByType(&standardFontData, PlSharedFontType_Standard);
return TTF_OpenFontRW(SDL_RWFromMem(standardFontData.address, standardFontData.size), 1, FontSize);
}

void DrawButtonBorders(SDL_Renderer* renderer, ScrollList *LeftList, ScrollList *MenuList, int HeaderHeight, int FooterHeight, int Width, int Height, bool SplitFooter)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
int BorderSize = 3;
//Draw border for the two lists
SDL_Rect BorderRect = {MenuList->ListXOffset, MenuList->ListYOffset, BorderSize, MenuList->ListHeight};
SDL_RenderFillRect(renderer, &BorderRect);
//Draw border for the header
BorderRect = {0, HeaderHeight, Width, BorderSize};
SDL_RenderFillRect(renderer, &BorderRect);
//Draw the menu list border
for(int i = 0; i < MenuList->ListingsOnScreen; i++)
{
int MenuListButtonSize = MenuList->ListHeight / MenuList->ListingsOnScreen;
SDL_Rect BorderRect = {MenuList->ListXOffset, MenuList->ListYOffset + (i * MenuListButtonSize), MenuList->ListWidth, BorderSize};
SDL_RenderFillRect(renderer, &BorderRect);
}
//Draw the left list border
for(int i = 1; i < LeftList->ListingsOnScreen; i++)
{
int MenuListButtonSize = LeftList->ListHeight / LeftList->ListingsOnScreen;
SDL_Rect BorderRect = {0, LeftList->ListYOffset + (i * MenuListButtonSize) - 1, LeftList->ListWidth, BorderSize};
SDL_RenderFillRect(renderer, &BorderRect);
if(LeftList->ListingTextVec.size() == i) break;
}
//Draw the footer border
BorderRect = {0, Height - FooterHeight, Width, BorderSize};
SDL_RenderFillRect(renderer, &BorderRect);
if(!SplitFooter) return;
//Draw the footer button border
BorderRect = {Width/2, Height - FooterHeight, BorderSize, Height};
SDL_RenderFillRect(renderer, &BorderRect);
}
Loading

0 comments on commit 0efdafc

Please sign in to comment.