Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions romsel_dsimenutheme/arm9/source/fileBrowse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "graphics/fontHandler.h"
#include "graphics/graphics.h"
#include "graphics/iconHandler.h"
#include "gift.h"
#include "iconTitle.h"
#include "ndsheaderbanner.h"
#include "perGameSettings.h"
Expand Down Expand Up @@ -699,6 +700,12 @@ void moveCursor(bool right, const std::vector<DirEntry> dirContents, int maxEntr
}

void updateBoxArt(void) {
// 3DS theme: a gift that is still wrapped spins on the top screen instead of box art
if (giftSpinUpdate(boxArtFound && movingApp == -1 && ms().theme == TWLSettings::ETheme3DS && ms().showBoxArt && !ms().macroMode &&
!isDirectory[CURPOS] && giftActive(CURPOS))) {
showSTARTborder = true;
return;
}
if (!boxArtFound) return;
showSTARTborder = true;
if (ms().theme == TWLSettings::EThemeHBL || ms().macroMode || !ms().showBoxArt || boxArtLoaded) return;
Expand Down Expand Up @@ -2859,11 +2866,13 @@ void getFileInfo(SwitchState scrn, vector<vector<DirEntry>> dirContents, bool re
}
if (reSpawnBoxes)
spawnedtitleboxes = 0;
giftClearSlots();
for (int i = 0; i < 40; i++) {
if (i + PAGENUM * 40 < file_count) {
isDirectory[i] = dirContents[scrn][i + PAGENUM * 40].isDirectory;
const std::string &std_romsel_filename = dirContents[scrn][i + PAGENUM * 40].name;
getGameInfo(isDirectory[i], std_romsel_filename.c_str(), i);
giftUpdateSlot(i, isDirectory[i], std_romsel_filename);

if (isDirectory[i]) {
bnrWirelessIcon[i] = 0;
Expand Down Expand Up @@ -2979,6 +2988,7 @@ static MoveModeState beginMoveMode(SwitchState scrn, const std::vector<std::vect
clearText();
updateText(false);
mkdir(sys().isRunFromSD() ? "sd:/_nds/TWiLightMenu/extras" : "fat:/_nds/TWiLightMenu/extras", 0777);
giftMovingApp = giftActive(CURPOS); // Keep the gift wrap on the lifted app
movingApp = (PAGENUM * 40) + (CURPOS);
titleboxXspacing = MOVE_SPACING;
titleboxXdest[ms().secondaryDevice] = titleboxXpos[ms().secondaryDevice] = CURPOS * titleboxXspacing;
Expand Down Expand Up @@ -3120,6 +3130,7 @@ static void endMoveMode(SwitchState scrn, std::vector<std::vector<DirEntry>> &di
}

movingApp = -1;
giftMovingApp = false;
titleboxXspacing = 58;
titleboxXdest[ms().secondaryDevice] = titleboxXpos[ms().secondaryDevice] = CURPOS * titleboxXspacing;
}
Expand Down Expand Up @@ -3993,6 +4004,19 @@ std::string browseForFile(const std::vector<std::string_view> extensionList) {

// Startup...
if ((((pressed & KEY_A) || (pressed & KEY_START) || (ms().theme == TWLSettings::ETheme3DS && (pressed & KEY_TOUCH) && touch.py > 171)) && bannerTextShown && showSTARTborder) || gameTapped) {
if (CURPOS + PAGENUM * 40 < ((int)dirContents[scrn].size()) && giftActive(CURPOS)) {
// New software still in its gift wrap: unwrap it first.
// The next press launches it as usual.
snd().playSelect();
for (int f = 1; f <= GIFT_ANIM_LENGTH; f++) {
giftAnimFrame[CURPOS] = f;
giftSpinUpdate(true); // Keep the gift spinning on the top screen
bgOperations(true);
}
giftFinishUnwrap(CURPOS, dirContents[scrn].at(CURPOS + PAGENUM * 40).name);
gameTapped = false;
continue;
}
bannerTextShown = false; // Redraw title when done
DirEntry *entry = &dirContents[scrn].at(CURPOS + PAGENUM * 40);
if (entry->isDirectory) {
Expand Down
208 changes: 208 additions & 0 deletions romsel_dsimenutheme/arm9/source/gift.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
#include "gift.h"

#include <limits.h>
#include <set>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <nds.h>

#include "common/systemdetails.h"
#include "common/twlmenusettings.h"
#include "graphics/color.h"
#include "graphics/graphics.h"
#include "graphics/ThemeTextures.h"

// Defined in ndsheaderbanner.cpp
extern bool isDirectory[40];
extern bool bnrSysSettings[41];

// Defined in fileBrowse.cpp and ThemeTextures.cpp
extern bool boxArtLoaded;
extern unsigned int boxArtWidth, boxArtHeight;

bool giftWrapped[40] = {false};
int giftAnimFrame[40] = {0};
bool giftMovingApp = false;

// Full paths of every app that has been unwrapped
static std::set<std::string> seenApps;
static bool seenListLoaded = false;

static const char *seenListPath(void) {
return sys().isRunFromSD() ? "sd:/_nds/TWiLightMenu/giftseen.txt" : "fat:/_nds/TWiLightMenu/giftseen.txt";
}

// Apps are remembered by folder + file name, so the same file name in two
// different folders is two different apps.
static std::string entryKey(const std::string &name) {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) == NULL) {
return name;
}
std::string key(cwd);
if (key.empty() || key[key.size() - 1] != '/') {
key += '/';
}
return key + name;
}

void giftLoadSeenList() {
if (seenListLoaded) {
return;
}
seenListLoaded = true;

FILE *file = fopen(seenListPath(), "r");
if (!file) {
return; // No list yet: every app is new
}
char line[PATH_MAX + 2];
while (fgets(line, sizeof(line), file)) {
size_t len = strlen(line);
while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) {
line[--len] = '\0';
}
if (len > 0) {
seenApps.insert(std::string(line));
}
}
fclose(file);
}

void giftClearSlots() {
for (int i = 0; i < 40; i++) {
giftWrapped[i] = false;
giftAnimFrame[i] = 0;
}
}

void giftUpdateSlot(int slot, bool isDir, const std::string &name) {
giftAnimFrame[slot] = 0;
if (ms().theme != TWLSettings::ETheme3DS || isDir) {
giftWrapped[slot] = false;
return;
}
giftLoadSeenList();
giftWrapped[slot] = (seenApps.find(entryKey(name)) == seenApps.end());
}

bool giftActive(int slot) {
return giftWrapped[slot] && !isDirectory[slot] && !bnrSysSettings[slot];
}

int giftSpriteFrame(int slot) {
const int t = giftAnimFrame[slot];
if (t < 24) {
return 0; // Closed (shaking)
} else if (t < 30) {
return 1; // Lid starts to lift
} else if (t < 36) {
return 2; // Lid up, light shining out
}
return 3; // Burst of light
}

int giftShakeX(int slot) {
const int t = giftAnimFrame[slot];
if (t <= 0 || t >= 24) {
return 0;
}
const int amplitude = (t < 12) ? 2 : 3;
return ((t / 2) % 2) ? amplitude : -amplitude;
}

void giftFinishUnwrap(int slot, const std::string &name) {
const std::string key = entryKey(name);
if (seenApps.insert(key).second) {
FILE *file = fopen(seenListPath(), "a");
if (file) {
fprintf(file, "%s\n", key.c_str());
fclose(file);
}
}
giftWrapped[slot] = false;
giftAnimFrame[slot] = 0;
}

// ---------------------------------------------------------------------------
// Spinning gift on the top screen
// ---------------------------------------------------------------------------

void giftComposite(unsigned short *dst, const unsigned short *bg, const unsigned char *frame,
int w, int h, const unsigned short *colorTable) {
const unsigned short *colors = (const unsigned short *)frame;
const unsigned char *alphas = frame + (w * h * 2);
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
const int i = (y * w) + x;
const int o = (y * 256) + x;
const unsigned char a = alphas[i];
if (a == 0) {
dst[o] = bg[o];
continue;
}
unsigned short color = colors[i] | BIT(15);
if (colorTable) {
color = colorTable[color % 0x8000] | BIT(15);
}
dst[o] = (a == 255) ? color : alphablend(color, bg[o], a);
}
}
}

static FILE *spinFile = NULL;
static int spinFrame = 0;
static int spinTick = 0;
static unsigned char spinBuffer[GIFT_SPIN_FRAME_BYTES] __attribute__((aligned(4)));

static void spinClose(void) {
if (spinFile) {
fclose(spinFile);
spinFile = NULL;
}
}

bool giftSpinUpdate(bool wanted) {
if (!wanted) {
if (spinFile) {
spinClose();
// If nothing else has cleared the top screen yet, put the background back
if (boxArtWidth == GIFT_SPIN_W && boxArtHeight == GIFT_SPIN_H) {
clearBoxArt();
resumeRotatingCubesVideo();
}
boxArtLoaded = false;
}
return false;
}

// (Re)start when we have no file open, or the top screen was cleared under us
// (for example the cursor moved away and back)
const bool restart = (spinFile == NULL) || !boxArtLoaded || (boxArtWidth != GIFT_SPIN_W);
if (restart) {
spinClose();
spinFile = fopen(GIFT_SPIN_PATH, "rb");
if (!spinFile) {
return false; // No animation data: fall back to normal box art
}
spinFrame = 0;
spinTick = 0;
} else {
if (++spinTick < GIFT_SPIN_TICKS) {
return true;
}
spinTick = 0;
spinFrame = (spinFrame + 1) % GIFT_SPIN_FRAMES;
}

if (fseek(spinFile, (long)spinFrame * GIFT_SPIN_FRAME_BYTES, SEEK_SET) != 0 ||
fread(spinBuffer, 1, GIFT_SPIN_FRAME_BYTES, spinFile) != (size_t)GIFT_SPIN_FRAME_BYTES) {
spinClose();
return false;
}
tex().drawGiftSpinFrame(spinBuffer, restart);
boxArtLoaded = true;
return true;
}
69 changes: 69 additions & 0 deletions romsel_dsimenutheme/arm9/source/gift.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef TWLMENU_GIFT_H
#define TWLMENU_GIFT_H

#include <string>

// "New software" gifts for the 3DS theme.
//
// An app that has never been opened is drawn as a wrapped gift. Selecting it
// and tapping it (or pressing A) plays an unwrap animation instead of
// launching. The app is then remembered in a list on the card, so it only
// ever shows as a gift once. With no list yet, every app starts out wrapped.

// Length of the unwrap animation, in frames (60 per second)
#define GIFT_ANIM_LENGTH 44

// Per box slot of the current page
extern bool giftWrapped[40];
// 0 = idle, 1..GIFT_ANIM_LENGTH = unwrapping
extern int giftAnimFrame[40];

// Reads the list of opened apps from the card (only done once per run)
void giftLoadSeenList();

// Call when a slot is filled with an entry; name is relative to the current folder
void giftUpdateSlot(int slot, bool isDirectory, const std::string &name);

// Clears the gift state of every slot (call before refilling a page)
void giftClearSlots();

// True if the slot should currently be drawn as a gift. This does not depend on the
// platform: NDS, GBA, GB, GBC, NES, SNES and every other launchable file are all
// wrapped until opened. Only folders and the system Settings entry are skipped.
bool giftActive(int slot);

// True while the app being carried in move mode is still wrapped. Remembered when the
// app is picked up, because the slot it came from can be refilled by a page flip.
extern bool giftMovingApp;

// Sprite sheet frame (0 = closed .. 3 = burst) and horizontal shake for a slot
int giftSpriteFrame(int slot);
int giftShakeX(int slot);

// Remembers the app as opened and saves the list. Call once the animation is done.
void giftFinishUnwrap(int slot, const std::string &name);

// ---- Spinning 3D gift on the top screen (3DS theme) ----
// While a wrapped gift is selected, a short turntable animation of a gift box plays where
// the box art normally goes. Frames are stored in nitro:/graphics/giftspin.bin, one after
// the other: per frame W*H RGB555 pixels (little endian) followed by W*H alpha bytes.
#define GIFT_SPIN_W 112
#define GIFT_SPIN_H 112
#define GIFT_SPIN_FRAMES 24 // 7.5 degrees apart, covering a half turn (the gift looks the same after that)
#define GIFT_SPIN_TICKS 4 // Screen frames per animation frame
#define GIFT_SPIN_FRAME_BYTES (GIFT_SPIN_W * GIFT_SPIN_H * 3)
#ifndef GIFT_SPIN_PATH
#define GIFT_SPIN_PATH "nitro:/graphics/giftspin.bin"
#endif

// Call once per screen frame. wanted = a wrapped gift is selected right now.
// Starts, advances or stops the animation. Returns true while it is on screen,
// in which case the caller should skip drawing normal box art.
bool giftSpinUpdate(bool wanted);

// Draws one frame (w x h) over bg into dst. dst and bg are 256 pixels wide.
// Transparent pixels show bg, partly transparent ones are blended with it.
void giftComposite(unsigned short *dst, const unsigned short *bg, const unsigned char *frame,
int w, int h, const unsigned short *colorTable);

#endif
Loading