Skip to content

Commit

Permalink
Refactor lots of the screen code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightkingale committed May 16, 2024
1 parent dff40b2 commit 66dea21
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 141 deletions.
9 changes: 9 additions & 0 deletions include/start_screen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef START_SCREEN_HPP
#define START_SCREEN_HPP


void draw_start_screen(int selected_menu_item);
void process_start_screen();


#endif
5 changes: 2 additions & 3 deletions include/screens.hpp → include/sub_screens.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#ifndef SCREENS_HPP
#define SCREENS_HPP
#ifndef SUB_SCREENS_HPP
#define SUB_SCREENS_HPP


void draw_menu_screen(int selected_menu_item);
void draw_unlink_menu();
void draw_backup_menu();
void draw_overwrite_menu(const char* backup_path);
Expand Down
5 changes: 3 additions & 2 deletions source/backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

#include "input.hpp"
#include "main.hpp"
#include "screens.hpp"
#include "start_screen.hpp"
#include "sub_screens.hpp"


bool backup_confirm = false;
Expand Down Expand Up @@ -45,7 +46,7 @@ handle_cleanup(FILE* account, FILE* backup, char* buffer, bool is_error = false)
}

if (is_error) {
draw_menu_screen(2);
draw_start_screen(2);
}
}

Expand Down
67 changes: 3 additions & 64 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#include "easter_egg.hpp"
#include "input.hpp"
#include "main.hpp"
#include "screens.hpp"
#include "start_screen.hpp"
#include "sub_screens.hpp"
#include "swap.hpp"
#include "unlink.hpp"
#include "utils.hpp"
Expand Down Expand Up @@ -169,71 +170,9 @@ main()
error_occurred = check_initialization(initialize_program(), "Error initializing program!") ||
check_initialization(initialize_graphics(), "Error initializing graphics!");

int selected_option = 0;
const int NUM_OPTIONS = 4;

while (WHBProcIsRunning()) {
if (!error_occurred)
draw_menu_screen(selected_option);

int button = read_input(); // Watch the controllers for input.
OSEnableHomeButtonMenu(1);

if (button & VPAD_BUTTON_UP) {
selected_option--;
if (selected_option < 0)
selected_option = NUM_OPTIONS - 1;
} else if (button & VPAD_BUTTON_DOWN) {
selected_option++;
if (selected_option >= NUM_OPTIONS)
selected_option = 0;
} else if (button & VPAD_BUTTON_A) {
switch (selected_option) {
case 0:
if (swap_account(NNID_BACKUP.c_str(), "Nintendo")) {
OSForceFullRelaunch();
SYSLaunchMenu();
}
break;
case 1:
if (swap_account(PNID_BACKUP.c_str(), "Pretendo")) {
OSForceFullRelaunch();
SYSLaunchMenu();
}
break;
case 2:
while (WHBProcIsRunning()) {
OSEnableHomeButtonMenu(0);
draw_backup_menu();
button = read_input();

if (button & VPAD_BUTTON_A) {
backup_account();
break;
} else if (button & VPAD_BUTTON_B)
break;
}
break;
case 3:
OSEnableHomeButtonMenu(0);
while (WHBProcIsRunning()) {
draw_unlink_menu();
button = read_input();

if (button & VPAD_BUTTON_A) {
if (unlink_account()) {
OSForceFullRelaunch();
SYSLaunchMenu();
}
break;
} else if (button & VPAD_BUTTON_B)
break;
}
break;
}
} else if (button & VPAD_BUTTON_SYNC) {
play_easter_egg();
}
process_start_screen();
}

deinitialize();
Expand Down
165 changes: 165 additions & 0 deletions source/start_screen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include <string>

#include <coreinit/launch.h>
#include <coreinit/screen.h>
#include <coreinit/thread.h>
#include <coreinit/time.h>
#include <mocha/mocha.h>
#include <nn/act.h>
#include <padscore/kpad.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <sndcore2/core.h>
#include <sysapp/launch.h>
#include <vpad/input.h>
#include <whb/log.h>
#include <whb/log_console.h>
#include <whb/proc.h>

#include "backup.hpp"
#include "easter_egg.hpp"
#include "input.hpp"
#include "main.hpp"
#include "nintendo_glyphs.hpp"
#include "start_screen.hpp"
#include "sub_screens.hpp"
#include "swap.hpp"
#include "unlink.hpp"
#include "utils.hpp"


int selected_option = 0;
const int NUM_OPTIONS = 4;


bool
swap_account_action(const char* account_backup, const char* account_type)
{
if (swap_account(account_backup, account_type)) {
OSForceFullRelaunch();
SYSLaunchMenu();
return true;
}
return false;
}


bool
backup_account_action()
{
while (WHBProcIsRunning()) {
OSEnableHomeButtonMenu(0);
draw_backup_menu();
int button = read_input();

if (button & VPAD_BUTTON_A) {
backup_account();
return true;
} else if (button & VPAD_BUTTON_B)
return true;
}
return false;
}


bool
unlink_account_action()
{
OSEnableHomeButtonMenu(0);
while (WHBProcIsRunning()) {
draw_unlink_menu();
int button = read_input();

if (button & VPAD_BUTTON_A) {
if (unlink_account()) {
OSForceFullRelaunch();
SYSLaunchMenu();
return true;
}
} else if (button & VPAD_BUTTON_B)
return true;
}
return false;
}


void
draw_start_screen(int selected_menu_item)
{
draw_background(16, 16, 16, 255);
draw_screen_bars(); // This gives us the top and bottom bars.

struct menu_item {
const char* icon;
const char* option;
};

const menu_item choices[] = {
{"\uF0AC", "Swap to Nintendo Network ID"}, // Network icon
{"\uF233", "Swap to Pretendo Network ID"}, // Server icon
{"\uF0C7", "Backup the account.dat File"}, // Save icon
{"\uF12D", "Unlink the account.dat File"} // Unlink icon
};

const int NUM_MENU_ITEMS = sizeof(choices) / sizeof(choices[0]);

for (int item = 0; item < NUM_MENU_ITEMS; item++) {
bool is_selected = item == selected_menu_item;
draw_rectangle(0, 90 + item * 120, SCREEN_WIDTH, 120, \
is_selected ? 100 : 22, is_selected ? 100 : 22, is_selected ? 255 : 22, 255);
draw_rectangle(5, 95 + item * 120, 1910, 110, \
is_selected ? 0 : 25, is_selected ? 0 : 25, is_selected ? 0 : 25, 255);
draw_text(choices[item].option, 160, 120 + item * 120, 50);

SDL_Color icon_color = {255, 255, 255, 255};
if (item == 0) {
icon_color = {255, 150, 0, 255}; // Orange icon.
} else if (item == 1) {
icon_color = {255, 50, 255, 255}; // Purple icon.
}
draw_icon(choices[item].icon, 64, 125 + item * 120, 50, icon_color);

if (is_selected) {
draw_text(NIN_GLYPH_BTN_A, SCREEN_WIDTH - 64 - get_text_size(NIN_GLYPH_BTN_A, 50),
115 + item * 120, 50, {100, 100, 255, 255});
}
}

SDL_RenderPresent(renderer);
}


void
process_start_screen()
{
draw_start_screen(selected_option); // Draw the start screen.
int button = read_input(); // Watch the controllers for input.
OSEnableHomeButtonMenu(1);

if (button & VPAD_BUTTON_UP) {
selected_option--;
if (selected_option < 0)
selected_option = NUM_OPTIONS - 1;
} else if (button & VPAD_BUTTON_DOWN) {
selected_option++;
if (selected_option >= NUM_OPTIONS)
selected_option = 0;
} else if (button & VPAD_BUTTON_A) {
switch (selected_option) {
case 0:
swap_account_action(NNID_BACKUP.c_str(), "Nintendo");
break;
case 1:
swap_account_action(PNID_BACKUP.c_str(), "Pretendo");
break;
case 2:
backup_account_action();
break;
case 3:
unlink_account_action();
break;
}
} else if (button & VPAD_BUTTON_SYNC) {
play_easter_egg();
}
}
68 changes: 0 additions & 68 deletions source/screens.cpp → source/sub_screens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,74 +9,6 @@
#include "utils.hpp"


void
draw_menu_screen(int selected_menu_item)
{
draw_background(16, 16, 16, 255);
draw_screen_bars(); // This gives us the top and bottom bars.

const char* menu_options[] = {
"Swap to Nintendo Network ID",
"Swap to Pretendo Network ID",
"Backup the account.dat File",
"Unlink the account.dat File"
}; // Menu options.

const char* menu_icons[] = {
"\uF0AC", // Network icon
"\uF233", // Server icon
"\uF0C7", // Save icon
"\uF12D" // Unlink icon
}; // Font Awesome icons.

const int NUM_MENU_ITEMS = sizeof(menu_options) / sizeof(menu_options[0]); // Number of menu items.

// Draw the unselected menu items.
for (int item = 0; item < NUM_MENU_ITEMS; item++) {
if (item != selected_menu_item) {
draw_rectangle(0, 90 + item * 120, SCREEN_WIDTH, 120, 22, 22, 22, 255); // Gray border.
draw_rectangle(5, 95 + item * 120, 1910, 110, 25, 25, 25, 255); // Gray rectangle.
}
draw_text(menu_options[item], 160, 120 + item * 120, 50);

if (item == 0) {
// This is the Swap to Nintendo Network ID option.
draw_icon(menu_icons[item], 64, 125 + item * 120, 50, {255, 150, 0, 255}); // Orange icon.
} else if (item == 1) {
// This is the Swap to Pretendo Network ID option.
draw_icon(menu_icons[item], 64, 125 + item * 120, 50, {255, 50, 255, 255}); // Purple icon.
} else {
// Any other option will draw the icon in white.
draw_icon(menu_icons[item], 64, 125 + item * 120, 50);
}
}

// Draw the selected menu item.
for (int item = 0; item < NUM_MENU_ITEMS; item++) {
if (item == selected_menu_item) {
draw_rectangle(0, 90 + item * 120, SCREEN_WIDTH, 120, 100, 100, 255, 255); // Blue border.
draw_rectangle(5, 95 + item * 120, 1910, 110, 0, 0, 0, 255); // Black rectangle.
draw_text(NIN_GLYPH_BTN_A, SCREEN_WIDTH - 64 - get_text_size(NIN_GLYPH_BTN_A, 50),
115 + item * 120, 50, {100, 100, 255, 255});
}
draw_text(menu_options[item], 160, 120 + item * 120, 50);

if (item == 0) {
// This is the Swap to Nintendo Network ID option.
draw_icon(menu_icons[item], 64, 125 + item * 120, 50, {255, 150, 0, 255}); // Orange icon.
} else if (item == 1) {
// This is the Swap to Pretendo Network ID option.
draw_icon(menu_icons[item], 64, 125 + item * 120, 50, {255, 50, 255, 255}); // Purple icon.
} else {
// Any other option will draw the icon in white.
draw_icon(menu_icons[item], 64, 125 + item * 120, 50);
}
}

SDL_RenderPresent(renderer);
}


void
draw_unlink_menu()
{
Expand Down
7 changes: 4 additions & 3 deletions source/swap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#include <whb/proc.h>

#include "main.hpp"
#include "screens.hpp"
#include "start_screen.hpp"
#include "sub_screens.hpp"


void
Expand All @@ -36,9 +37,9 @@ handle_cleanup(FILE* backup, const char* account_type, char* buffer, bool is_err
if (is_error) {
// Print the main menu.
if (strcmp(account_type, "Nintendo"))
draw_menu_screen(0);
draw_start_screen(0);
else if (strcmp(account_type, "Pretendo"))
draw_menu_screen(1);
draw_start_screen(1);
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/unlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <whb/proc.h>

#include "main.hpp"
#include "screens.hpp"
#include "sub_screens.hpp"


bool
Expand Down

0 comments on commit 66dea21

Please sign in to comment.