adding folder picker

This commit is contained in:
NotImplementedLife 2023-06-16 11:40:36 +03:00
parent 5528710e70
commit 35a8a98de3
15 changed files with 323 additions and 38 deletions

View File

@ -0,0 +1 @@
-tiles -8bpp -mw4 -mh4

BIN
arm9/assets/folder_icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 724 B

View File

@ -37,6 +37,7 @@ namespace DSC
void clear(BgPaletteType pal_type); void clear(BgPaletteType pal_type);
void set_cursor(int row, int col_px); void set_cursor(int row, int col_px);
int get_cursor_x() const;
inline const FontData& get_font() const { return font; } inline const FontData& get_font() const { return font; }

View File

@ -0,0 +1,17 @@
#pragma once
#include <fat.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
class DirStream
{
private:
DIR* root = nullptr;
public:
bool open(const char* dir);
bool next(char* dest_name);
void close();
};

View File

@ -2,4 +2,17 @@
#include "ppm_reader.hpp" #include "ppm_reader.hpp"
extern PPMReader* ppm_reader; extern PPMReader* ppm_reader;
void set_fat_inited(bool value);
bool is_fat_inited();
class Config
{
public:
static bool is_configured();
static void initialize();
};

View File

@ -13,4 +13,5 @@ extern PPMReader* ppm_reader;
Scene* get_player_scene(); Scene* get_player_scene();
Scene* get_simple_scene(); Scene* get_simple_scene();
Scene* get_credits_scene(); Scene* get_credits_scene();
Scene* get_folder_picker_scene();

View File

@ -14,6 +14,8 @@ DSC::VwfEngine::VwfEngine(const FontData& font, ExtendedFontChart ext_chart)
} }
int DSC::VwfEngine::get_cursor_x() const { return crt_col*8+px_offset; }
void DSC::VwfEngine::set_cursor(int row, int col_px) void DSC::VwfEngine::set_cursor(int row, int col_px)
{ {
crt_row = row; crt_row = row;

View File

@ -25,7 +25,6 @@ class CreditsScene : public SimpleScene
} }
} }
__attribute__((noinline)) __attribute__((noinline))
void run() override void run() override
{ {

View File

@ -0,0 +1,30 @@
#include "filesystem.hpp"
bool DirStream::open(const char* dir)
{
if(root!=nullptr)
closedir(root);
return nullptr == (root = opendir(dir));
}
bool DirStream::next(char* dest_name)
{
if(root==nullptr) return false;
struct dirent *entry;
while((entry=readdir(root))!=NULL)
{
if(strcmp(".", entry->d_name)==0 || strcmp("..", entry->d_name)==0)
continue;
if(entry->d_type != DT_DIR)
continue;
strcpy(dest_name, entry->d_name);
return true;
}
return false;
}
void DirStream::close()
{
closedir(root);
root = nullptr;
}

View File

@ -0,0 +1,221 @@
#include <nds.h>
#include "scenes.hpp"
#include "simple_scene.hpp"
#include "folder_icon.h"
#include "filesystem.hpp"
class FolderPickerScene : public SimpleScene
{
ObjFrame* folder_normal_frame;
ObjFrame* folder_highlighted_frame;
ObjFrame* folder_back_frame;
DirStream dirstream;
Sprite* folder_icons[5];
Vector<char*> subfolders;
char* current_path;
int depth=0;
int selected_index=0;
int max_subfolders = 65536;
VwfEngine* vwf = new VwfEngine(Resources::Fonts::default_8x16);
void init() override
{
SimpleScene::init();
require_tiledmap_4bpp(MAIN_BG2, 256, 256, 32*24);
require_tiledmap_4bpp(SUB_BG2, 256, 256, 32*24);
begin_sprites_init();
folder_normal_frame = new ObjFrame(&ROA_folder_icon8,0,0);
folder_highlighted_frame = new ObjFrame(&ROA_folder_icon8,0,1);
folder_back_frame = new ObjFrame(&ROA_folder_icon8,0,2);
get_obj_allocator_sub()->allocate(folder_normal_frame);
get_obj_allocator_sub()->allocate(folder_highlighted_frame);
get_obj_allocator_sub()->allocate(folder_back_frame);
for(int i=0;i<5;i++)
{
folder_icons[i] = create_sprite(new Sprite(SIZE_32x32, Engine::Sub));
folder_icons[i]->set_default_allocator(nullptr);
folder_icons[i]->add_frame(i%2==0?folder_normal_frame : folder_highlighted_frame);
folder_icons[i]->set_position(8,8+32*i);
}
end_sprites_init();
key_down.add_event(&FolderPickerScene::on_key_down, this);
current_path = (char*)malloc(32768);
if(current_path==nullptr)
{
Debug::log("ALLOCATION FAILED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! _ CURRENT PATH");
}
current_path[0]=0;
dirstream.open("fat://");
clear_subfolders();
}
void on_key_down(void*, void* _k)
{
int keys = (int)_k;
if(keys & KEY_LEFT)
{
if(selected_index>=5)
{
selected_index-=5;
display_page(selected_index/5);
}
}
else if(keys & KEY_RIGHT)
{
if(selected_index<max_subfolders-5)
{
selected_index+=5;
display_page(selected_index/5);
}
}
else if(keys & KEY_UP)
{
if(selected_index>=1)
{
selected_index--;
display_page(selected_index/5);
}
}
else if(keys & KEY_DOWN)
{
if(selected_index<max_subfolders-1)
{
selected_index++;
display_page(selected_index/5);
}
}
/*if(keys & KEY_TOUCH)
{
close()->next(gen_title_scene());
}*/
}
__attribute__((noinline))
void run() override
{
solve_map_requirements();
load_assets();
BG_PALETTE[0x91]=BG_PALETTE_SUB[0x91]=Colors::Black;
BG_PALETTE[0x92]=BG_PALETTE_SUB[0x92]=Colors::Blue;
vwf->set_render_space(bgGetGfxPtr(2),24,32);
VwfEngine::prepare_map(*vwf, MAIN_BG2, 32, 0, 0, 0x9);
vwf->clear(Pal4bit);
vwf->set_cursor(6, 74);
vwf->put_text("Pick a directory", Pal4bit, SolidColorBrush(0x1));
vwf->set_render_space(bgGetGfxPtr(6),24,32);
VwfEngine::prepare_map(*vwf, SUB_BG2, 32, 0, 0, 0x9);
vwf->clear(Pal4bit);
vwf->set_cursor(10, 20);
vwf->put_text("Touch the screen to return to title", Pal4bit, SolidColorBrush(0x1));
Hardware::MainEngine::objEnable(128, true); // set to 128
Hardware::SubEngine::objEnable(128, true); // set to 128
display_page(0);
Scene::run();
}
void frame() override
{
GenericScene256::frame();
}
void display_page(int page)
{
int last_id = page*5+4;
char* name = new char[256];
while(last_id+1>=subfolders.size() && dirstream.next(name))
{
char* dirname = (char*)malloc(strlen(name)+1);
if(dirname==nullptr)
{
Debug::log("ALLOCATION FAILED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! _ DIRRRR");
}
strcpy(dirname, name);
subfolders.push_back(dirname);
Debug::log(dirname);
}
delete[] name;
if(last_id+1>=subfolders.size())
{
max_subfolders = subfolders.size();
}
vwf->clear(Pal4bit);
int k=page*5;
int i=0;
for(;i<5 && k<subfolders.size();i++, k++)
{
folder_icons[i]->show();
if(k==0 && depth>0)
{
folder_icons[i]->set_frame(0, folder_back_frame);
}
else
{
folder_icons[i]->set_frame(0, folder_normal_frame);
}
Debug::log(subfolders[k]);
vwf->set_cursor(1+2*i, 42);
vwf->put_text(subfolders[k], Pal4bit, SolidColorBrush(0x1 + (k==selected_index)));
}
for(;i<5;i++)
{
folder_icons[i]->hide();
}
}
static char* sptr(const char* val)
{
char* r = (char*)malloc(strlen(val)+1);
strcpy(r, val);
return r;
}
void clear_subfolders()
{
for(int i=0;i<subfolders.size();i++)
free(subfolders[i]);
subfolders.clear();
if(depth>0)
subfolders.push_back(sptr("Back"));
}
~FolderPickerScene()
{
key_down.remove_event(&FolderPickerScene::on_key_down, this);
delete vwf;
clear_subfolders();
subfolders.push_back(sptr(".."));
}
};
Scene* get_folder_picker_scene()
{
return new FolderPickerScene();
}

View File

View File

@ -1,3 +1,16 @@
#include "globals.hpp" #include "globals.hpp"
PPMReader* ppm_reader = nullptr; PPMReader* ppm_reader = nullptr;
unsigned int state_flags = 0;
constexpr int STATE_FAT_INITED_BIT = 0;
constexpr int STATE_FAT_INITED_MASK = 1<<STATE_FAT_INITED_BIT;
void set_fat_inited(bool value)
{
state_flags &= ~STATE_FAT_INITED_MASK;
state_flags |= value << STATE_FAT_INITED_BIT;
}
bool is_fat_inited() { return state_flags & STATE_FAT_INITED_MASK; }

View File

@ -1,24 +1,14 @@
#include "scenes.hpp" #include "scenes.hpp"
#include "player_bg.h" #include <fat.h>
#include "globals.hpp"
using namespace DSC; using namespace DSC;
class MainScene : public GenericScene256 void DSC::init_main_scene()
{ {
void init() override set_fat_inited(fatInitDefault());
{
GenericScene256::init(); DSC::__MAIN_SCENE__= gen_title_scene();
require_bitmap(MAIN_BG3, &ROA_player_bg8); }
require_bitmap(SUB_BG3, &ROA_player_bg8);
key_down.add_event(&MainScene::on_key_down, this);
}
void on_key_down(void*, void*)
{
close()->next(gen_title_scene());
}
};
Scene* gen_main_scene() { return new MainScene(); }

View File

@ -319,7 +319,7 @@ public:
for(int i=0;i<24*32;i++) for(int i=0;i<24*32;i++)
{ {
bg0map[i]=0xE000 | i; bg0map[i]=0xE000 | i;
} }
BG_PALETTE[0x00]=Colors::White; BG_PALETTE[0x00]=Colors::White;

View File

@ -1,5 +1,5 @@
#include <nds.h> #include <nds.h>
#include <fat.h> #include "globals.hpp"
#include "logo_front_bin.h" #include "logo_front_bin.h"
#include "logo_back_bin.h" #include "logo_back_bin.h"
@ -13,14 +13,18 @@
#include "scenes.hpp" #include "scenes.hpp"
extern "C" char fake_heap_start[];
extern "C" char fake_heap_end[];
static char BK_RESERVED[1024*512]; static char BK_RESERVED[1024*512];
using namespace DSC; using namespace DSC;
Scene* get_browse_scene()
{
//if(!is_fat_inited())
//return get_player_scene();
return get_folder_picker_scene();
}
class TitleScene : public GenericScene256 class TitleScene : public GenericScene256
{ {
int textureID[4]; int textureID[4];
@ -37,12 +41,7 @@ class TitleScene : public GenericScene256
VwfEngine* vwf = new VwfEngine(Resources::Fonts::default_8x16); VwfEngine* vwf = new VwfEngine(Resources::Fonts::default_8x16);
void init() override void init() override
{ {
int* ptt = (int*)fake_heap_start;
Debug::warn("Heap start = %X", *ptt);
ptt = (int*)fake_heap_end;
Debug::log("Heap end = %X", *ptt);
GenericScene256::init(); GenericScene256::init();
key_down.add_event(&TitleScene::on_key_down, this); key_down.add_event(&TitleScene::on_key_down, this);
@ -65,7 +64,7 @@ class TitleScene : public GenericScene256
title[0]->set_position(128-64, 116); title[0]->set_position(128-64, 116);
title[1]->set_position(128, 116); title[1]->set_position(128, 116);
if(!fatInitDefault()) if(!is_fat_inited())
{ {
fat_fail = create_sprite(new Sprite(SIZE_64x32, Engine::Sub)); fat_fail = create_sprite(new Sprite(SIZE_64x32, Engine::Sub));
fat_fail->add_frame(new ObjFrame(&ROA_fat_fail8, 0,0)); fat_fail->add_frame(new ObjFrame(&ROA_fat_fail8, 0,0));
@ -117,7 +116,7 @@ class TitleScene : public GenericScene256
typedef Scene*(*fgen)(); typedef Scene*(*fgen)();
inline static constexpr fgen scenegens[4] = { &get_player_scene, &get_simple_scene, &get_simple_scene, &get_credits_scene }; inline static constexpr fgen scenegens[4] = { &get_browse_scene, &get_simple_scene, &get_simple_scene, &get_credits_scene };
void on_key_down(void* sender, void* args) void on_key_down(void* sender, void* args)
{ {
@ -381,6 +380,4 @@ class TitleScene : public GenericScene256
Scene* gen_title_scene() Scene* gen_title_scene()
{ {
return new TitleScene(); return new TitleScene();
} }
dsc_launch(TitleScene)