Files
sphaira/sphaira/source/ui/menus/image_viewer.cpp
ITotalJustice f0bdc01156 huge changes to everything (see below).
Changelog:
- re-enable use in release build.
- remove ftpsrv and untitled from builtin ghdl options, as both packages are available in the appstore.
- add image viewer (png, jpg, bmp)
- add music player (bfstm, bfwav, mp3, wav, ogg)
- add idv3 tag parsing support for mp3.
- add "decyption" of GTA Vice City mp3.
- add usbdvd support for music playback and file browsing.
- add nsz export support (solid, block, ldm).
- add xcz export support (same as above).
- add nro fs proper mount support (romfs, nacp, icon).
- add program nca fs support.
- add bfsar fs support.
- re-write the usb protocol, still wip. replaces tinfoil protocol.
- all threads are now create with pre-emptive support with the proper affinity mask set.
- fix oob crash in libpulsar when a bfwav was opened that had more than 2 channels.
- bump yyjson version.
- bump usbhsfs version.
- disable nvjpg.
- add support for theme music of any supported playback type (bfstm, bfwav, mp3, wav, ogg).
- add support for setting background music.
- add async exit to blocking threads (download, nxlink, ftpsrv) to reduce exit time.
- add support for dumping to pc via usb.
- add null, deflate, zstd hash options, mainly used for benchmarking.
- add sidebar slider (currently unused).
- file_viwer can now be used with any filesystem.
- filebrowser will only ever stat file once. previously it would keep stat'ing until it succeeded.
- disabled themezer due to the api breaking and i am not willing to keep maintaining it.
- disable zlt handling in usbds as it's not needed for my api's because the size is always known.
- remove usbds enums and GetSpeed() as i pr'd it to libnx.
- added support for mounting nca's from any source, including files, memory, nsps, xcis etc.
- split the lru cache into it's own header as it's now used in multiple places (nsz, all mounted options).
- add support for fetching and decrypting es personalised tickets.
- fix es common ticket converting where i forgot to also convert the cert chain as well.
- remove the download default music option.
- improve performance of libpulsar when opening a bfsar by remove the large setvbuf option. instead, use the default 1k buffer and handle large buffers manually in sphaira by using a lru cache (todo: just write my own bfsar parser).
- during app init and exit, load times have been halved as i now load/exit async. timestamps have also been added to measure how long everything takes.
- download now async loads / exits the etag json file to improve init times.
- add custom zip io to dumper to support writing a zip to any dest (such as usb).
- dumper now returns a proper error if the transfer was cancelled by the user.
- fatfs mount now sets the timestamp for files.
- fatfs mount handles folders with the archive bit by reporting them as a file.
- ftpsrv config is async loaded to speed up load times.
- nxlink now tries attempt to connect/accept by handling blocking rather than just bailing out.
- added support for minini floats.
- thread_file_transfer now spawns 3 threads rather than 2, to have the middle thread be a optional processor (mainly used for compressing/decompressing).
- added spinner to progress box, taken from nvg demo.
- progress box disables sleep mode on init.
- add gamecard detection to game menu to detect a refresh.
- handle xci that have the key area prepended.
- change gamecard mount fs to use the xci mount code instead of native fs, that way we can see all the partitions rather than just secure.
- reformat the ghdl entries to show the timestamp first.
- support for exporting saves to pc via usb.
- zip fs now uses lru cache.
2025-08-28 23:12:34 +01:00

135 lines
3.5 KiB
C++

#include "ui/menus/image_viewer.hpp"
#include "ui/nvg_util.hpp"
#include "app.hpp"
#include "i18n.hpp"
#include "image.hpp"
namespace sphaira::ui::menu::imageview {
namespace {
} // namespace
Menu::Menu(fs::Fs* fs, const fs::FsPath& path) : m_path{path} {
SetAction(Button::B, Action{[this](){
SetPop();
}});
std::vector<u8> m_image_buf;
const auto rc = fs->read_entire_file(path, m_image_buf);
if (R_FAILED(rc)) {
App::PushErrorBox(rc, "Failed to load image"_i18n);
SetPop();
return;
}
// try and load using nvjpg if possible.
u32 flags = ImageFlag_None;
if (path.ends_with(".jpg") || path.ends_with(".jpeg")) {
flags = ImageFlag_JPEG;
}
const auto result = ImageLoadFromMemory(m_image_buf, flags);
if (result.data.empty()) {
SetPop();
return;
}
m_image = nvgCreateImageRGBA(App::GetVg(), result.w, result.h, 0, result.data.data());
if (m_image <= 0) {
SetPop();
return;
}
m_image_width = result.w;
m_image_height = result.h;
// scale to fit.
const auto ws = SCREEN_WIDTH / m_image_width;
const auto hs = SCREEN_HEIGHT / m_image_height;
m_zoom = std::min(ws, hs);
UpdateSize();
}
Menu::~Menu() {
nvgDeleteImage(App::GetVg(), m_image);
}
void Menu::Update(Controller* controller, TouchInfo* touch) {
Widget::Update(controller, touch);
const auto kdown = controller->m_kdown | controller->m_kheld;
// pan support.
constexpr auto max_pan = 10.f;
constexpr auto max_panx = max_pan;// * (SCREEN_WIDTH / SCREEN_HEIGHT);
constexpr auto max_pany = max_pan;
if (controller->Got(kdown, Button::LS_LEFT)) {
m_xoff += max_panx;
}
if (controller->Got(kdown, Button::LS_RIGHT)) {
m_xoff -= max_panx;
}
if (controller->Got(kdown, Button::LS_UP)) {
m_yoff += max_pany;
}
if (controller->Got(kdown, Button::LS_DOWN)) {
m_yoff -= max_pany;
}
// zoom support, by 1% increments.
constexpr auto max_zoom = 0.01f;
if (controller->Got(kdown, Button::RS_UP)) {
m_zoom += max_zoom;
}
if (controller->Got(kdown, Button::RS_DOWN)) {
m_zoom -= max_zoom;
}
if (controller->Got(kdown, Button::LS_ANY) || controller->Got(kdown, Button::RS_ANY)) {
UpdateSize();
}
}
void Menu::Draw(NVGcontext* vg, Theme* theme) {
gfx::drawRect(vg, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, nvgRGB(0, 0, 0));
gfx::drawImage(vg, m_xoff + GetX(), m_yoff + GetY(), GetW(), GetH(), m_image);
// todo: when pan/zoom, show image info to the screen.
// todo: maybe show image info by default and option to hide it.
}
void Menu::UpdateSize() {
m_zoom = std::clamp(m_zoom, 0.1f, 4.0f);
// center pos.
const auto cx = SCREEN_WIDTH / 2;
const auto cy = SCREEN_HEIGHT / 2;
// calc position and size.
const auto w = m_image_width * m_zoom;
const auto h = m_image_height * m_zoom;
const auto x = cx - (w / 2);
const auto y = cy - (h / 2);
SetPos(x, y, w, h);
// clip edges.
if (SCREEN_HEIGHT >= h) {
m_yoff = 0;
// m_yoff = std::clamp(m_yoff, -y, +y);
} else {
m_yoff = std::clamp(m_yoff, (SCREEN_HEIGHT - h) - y, (h - SCREEN_HEIGHT) + y);
}
if (SCREEN_WIDTH >= w) {
m_xoff = 0;
// m_xoff = std::clamp(m_xoff, -x, +x);
} else {
m_xoff = std::clamp(m_xoff, (SCREEN_WIDTH - w) - x, (w - SCREEN_WIDTH) + x);
}
}
} // namespace sphaira::ui::menu::imageview