From d525032d212a29809fe541ead766400af2b1c438 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Sat, 6 Feb 2021 12:17:27 +0000 Subject: [PATCH] Port to DUH --- .github/workflows/build.yml | 1 + .gitmodules | 3 + CMakeLists.txt | 6 +- DUH | 1 + assets.yml | 1 - assets/buttons.png | Bin 1028 -> 0 bytes control-icons.cpp | 43 ---------- control-icons.hpp | 40 ---------- file-browser.cpp | 154 ------------------------------------ file-browser.hpp | 40 ---------- mjpeg-player.cpp | 2 +- 11 files changed, 10 insertions(+), 281 deletions(-) create mode 100644 .gitmodules create mode 160000 DUH delete mode 100644 assets/buttons.png delete mode 100644 control-icons.cpp delete mode 100644 control-icons.hpp delete mode 100644 file-browser.cpp delete mode 100644 file-browser.hpp diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9e79d7e..0703168 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -56,6 +56,7 @@ jobs: uses: actions/checkout@v2 with: path: main + submodules: true # Check out the 32Blit API we build against - name: Checkout 32Blit API diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..825a8c1 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "DUH"] + path = DUH + url = https://github.com/Daft-Freak/DUH diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a54847..20cff97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,8 +5,6 @@ project(mjpeg-player) set(32BLIT_PATH "../" CACHE PATH "Path to 32blit.cmake") set(PROJECT_SOURCE avi-file.cpp - control-icons.cpp - file-browser.cpp mjpeg-player.cpp ) @@ -24,7 +22,11 @@ if(NOT EXISTS ${32BLIT_PATH}/32blit.cmake) message(FATAL_ERROR "Define location of 32Blit API with -D32BLIT_PATH=") endif() include (${32BLIT_PATH}/32blit.cmake) +add_subdirectory(DUH) + blit_executable (${PROJECT_NAME} ${PROJECT_SOURCE}) blit_assets_yaml (${PROJECT_NAME} assets.yml) blit_metadata (${PROJECT_NAME} metadata.yml) +target_link_libraries (${PROJECT_NAME} DUH) + add_custom_target (flash DEPENDS ${PROJECT_NAME}.flash) diff --git a/DUH b/DUH new file mode 160000 index 0000000..1fe5043 --- /dev/null +++ b/DUH @@ -0,0 +1 @@ +Subproject commit 1fe50432d9ce5ca0a179893590cc829dbb267a87 diff --git a/assets.yml b/assets.yml index 8bde96f..d1f99f7 100644 --- a/assets.yml +++ b/assets.yml @@ -1,6 +1,5 @@ assets.cpp: prefix: asset_ - assets/buttons.png: buttons assets/12x8font.png: name: tall_font type: font/image diff --git a/assets/buttons.png b/assets/buttons.png deleted file mode 100644 index 827ef311f42303d77c49759a3b65b75d0ea940da..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1028 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H3?#oinD`S&F%}28J29*~C-V}>Ne=J{aRt&! z3MQ@^HfffI{bAPk3)OBeNPBm9*6&YdGoJ=z?K3c+lINYNALOlKWMgS>Ut*BZ6rJ}l zdG`PKvrkUn{`3F;|JiG|x74*~W@SZ&1(i8K-dRPB+udaLpp!q*5Zyo|t ziX}mQ!3;nRc!Ap&za8+>%UPZt z?h>A)FrA4(=}?>g)*a1J%QWsr^S`Nob8PS3x{%v%j;vn%rF^H(kKciB4oS1iF?gKY zJ5B95XN~=O#m7IU_c|~o#P4uhK4Hh9zDtVC%gG->59wPwYej27|KrXN z8w`4%S2{hEX{=i7y)r!^zf-xG;qtrJn!7)2xIdS1^F%F%Rrgnli5<^h8y&&DxG^B> z^V)517AzB(p0_i2;X~1NRdNpmZY+Jys5d_+zt?kt+xv#%S(pAT{-r-H zi+zOLbLOM~*c&qwyezTU@Re^7@h?!tEE0s#|2gWD4}YM->yu9@!aJw5Bl zsxbmInRv$T7m~S3qG&Sn}_3ZS1k(aCu+UF%1Pt4pK zfBxdlz~d{D?a%Iv>eQIIFZ%9@ouRu=*gY+pI%VDFuk*hj`ucAc!-OIkmjt$lkL8^{d?!`d*V^c`J--c)wi#|-oD-ZyJnWSUC4cRreikq*@fyEO}Fnk zrn=SUgyN?96$j=$Tx(cnxTZKlYJF>iWJFAT;8m_&cYbfbQ1)l;-rD-B^S$@HWzl_A zWunn>#nFM&a=+q>g3=j3Y84jmxP1Pf&VnDZA`jCJemf&j^gN%X(JlNy7RxavJ(mQf z2MQUk4VKLAjFZH&j&$wj?y^35^j=xko=@S+ZN8Y7Rp-b*^4)OS<$>@$2i1B>?s7hu rAN6a)DwQUBs0ekspi@7N|6r6=n)2ZAsqcEgti|Bz>gTe~DWM4f(VYF! diff --git a/control-icons.cpp b/control-icons.cpp deleted file mode 100644 index 85da3f8..0000000 --- a/control-icons.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "control-icons.hpp" - -#include "assets.hpp" - -#include "engine/engine.hpp" - -ControlIcons controlIcons; - -ControlIcons::ControlIcons() -{ - sprites = blit::SpriteSheet::load(asset_buttons); -} - -ControlIcons::~ControlIcons() -{ - delete sprites; -} - -void ControlIcons::render(Icon icon, blit::Point pos, blit::Pen colour, int size) -{ - int spriteIndex = static_cast(icon); - - blit::Rect spriteRect; - if(size <= 8) - spriteRect = blit::Rect(spriteIndex, 0, 1, 1); - else - spriteRect = blit::Rect((spriteIndex % 8) * 2, (spriteIndex / 8) * 2 + 1, 2, 2); - - if(size >= 16) - spriteRect.y += 4; - - blit::Pen shadowCol; - if((colour.r + colour.g + colour.b) / 3 > 63) - shadowCol = blit::Pen(colour.r / 2, colour.g / 2, colour.b / 2, colour.a); - else - shadowCol = blit::Pen(std::min(0xFF, colour.r * 2), std::min(0xFF, colour.g * 2), std::min(0xFF, colour.b * 2), colour.a); - - blit::screen.sprites = sprites; - sprites->palette[1] = colour; - sprites->palette[2] = shadowCol; - - blit::screen.sprite(spriteRect, pos); -} \ No newline at end of file diff --git a/control-icons.hpp b/control-icons.hpp deleted file mode 100644 index 8b97c20..0000000 --- a/control-icons.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include "engine/input.hpp" -#include "graphics/sprite.hpp" - -class ControlIcons final -{ -public: - ControlIcons(); - ~ControlIcons(); - - enum class Icon - { - A = 0, - B, - X, - Y, - Home, - Menu, - - StickLR, - StickUD, - StickL, - StickU, - StickD, - StickR, - - DPadL, - DPadU, - DPadD, - DPadR - }; - - void render(Icon icon, blit::Point pos, blit::Pen colour = blit::Pen(0xFF, 0xFF, 0xFF), int size = 12); -private: - - blit::SpriteSheet *sprites; -}; - -extern ControlIcons controlIcons; \ No newline at end of file diff --git a/file-browser.cpp b/file-browser.cpp deleted file mode 100644 index baff751..0000000 --- a/file-browser.cpp +++ /dev/null @@ -1,154 +0,0 @@ -#include "file-browser.hpp" - -#include "control-icons.hpp" - -#include "engine/engine.hpp" - -FileBrowser::FileBrowser(const blit::Font &font) : Menu("", nullptr, 0, font) { - // too early - //files = blit::list_files(""); - - item_h = font.char_h + 2; - item_adjust_y = 0; - - header_h = item_h; - footer_h = 0; - margin_y = 0; - - background_colour = blit::Pen(0x11, 0x11, 0x11); - foreground_colour = blit::Pen(0xF7, 0xF7, 0xF7); - selected_item_background = blit::Pen(0x22, 0x22, 0x22); -} - -void FileBrowser::init() { - update_list(); -} - -void FileBrowser::render() -{ - blit::Menu::render(); - - const int iconSize = font.char_h > 8 ? 12 : 8; - - const int32_t backTextWidth = blit::screen.measure_text("Back", font).w; - - blit::Rect r(display_rect.tl(), blit::Size(display_rect.w, header_h)); - - blit::screen.pen = header_foreground; - - r.x += item_padding_x; - r.w -= item_padding_x * 2; - - // back icon - if(!cur_dir.empty()) { - blit::Point iconOffset(-(backTextWidth + iconSize + 2), 1); // from the top-right - - blit::screen.text("Back", font, r, true, blit::TextAlign::center_right); - controlIcons.render(ControlIcons::Icon::B, r.tr() + iconOffset, header_foreground, iconSize); - } -} - -void FileBrowser::set_extensions(std::set exts) { - file_exts = exts; -} - -void FileBrowser::set_on_file_open(void (*func)(std::string)) { - on_file_open = func; -} - -void FileBrowser::set_current_dir(const std::string &dir) { - if(dir[0] != '/') - cur_dir = "/" + dir; - else - cur_dir = dir; - - if(cur_dir.back() != '/') - cur_dir += "/"; - - update_list(); -} - -void FileBrowser::update_list() { - title = cur_dir; - - files = blit::list_files(cur_dir.substr(0, cur_dir.length() - 1)); - - std::sort(files.begin(), files.end(), [](blit::FileInfo &a, blit::FileInfo & b){return a.name < b.name;}); - - if(file_exts.empty()) - return; - - // filter by extensions - files.erase(std::remove_if(files.begin(), files.end(), [this](const blit::FileInfo &f) { - if(f.name[0] == '.') - return true; - - if(!(f.flags & blit::FileFlags::directory)) { - std::string ext; - auto dotPos = f.name.find_last_of('.'); - if(dotPos != std::string::npos) - ext = f.name.substr(dotPos); - - // convert to lower case - std::for_each(ext.begin(), ext.end(), [](char & c) {c = tolower(c);}); - - if(file_exts.find(ext) == file_exts.end()) - return true; - } - - return false; - }), files.end()); - - // update menu items - file_items.resize(files.size()); - - unsigned int i = 0; - for(auto &file : files) { - if(file.flags & blit::FileFlags::directory) - file.name += "/"; - - file_items[i].id = i; - file_items[i++].label = file.name.c_str(); - } - - set_items(file_items.data(), file_items.size()); -} - -void FileBrowser::render_item(const Item &item, int y, int index) const { - blit::Menu::render_item(item, y, index); - - if(index == current_item) { - const int iconSize = font.char_h > 8 ? 12 : 8; - - blit::Rect r(display_rect.x + item_padding_x, y, display_rect.w - item_padding_x * 2 - iconSize - 2, item_h); - blit::Point iconPos = blit::Point(display_rect.x + display_rect.w - item_padding_x -iconSize, y + 1); // from the top-right - controlIcons.render(ControlIcons::Icon::A, iconPos, foreground_colour, iconSize); - } -} - -void FileBrowser::update_item(const Item &item) { - if(blit::buttons.released & blit::Button::B) { - if(cur_dir != "/") { - // go up - auto pos = cur_dir.find_last_of('/', cur_dir.length() - 2); - if(pos == std::string::npos) - cur_dir = ""; - else - cur_dir = cur_dir.substr(0, pos + 1); - - update_list(); - } - } -} - -void FileBrowser::item_activated(const Item &item){ - if(!num_items) - return; - - if(files[current_item].flags & blit::FileFlags::directory) { - cur_dir += files[current_item].name; - update_list(); - } - else if(on_file_open) - on_file_open(cur_dir + files[current_item].name); -} \ No newline at end of file diff --git a/file-browser.hpp b/file-browser.hpp deleted file mode 100644 index 4379968..0000000 --- a/file-browser.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "engine/file.hpp" -#include "engine/menu.hpp" - -class FileBrowser final : public blit::Menu { -public: - FileBrowser(const blit::Font &font = blit::minimal_font); - - void init(); - - void render(); - - void set_extensions(std::set exts); - - void set_on_file_open(void (*func)(std::string)); - - void set_current_dir(const std::string &dir); - -private: - void update_list(); - - void render_item(const Item &item, int y, int index) const override; - - void update_item(const Item &item) override; - - void item_activated(const Item &item) override; - - std::vector files; - std::vector file_items; - std::string cur_dir = "/"; - - std::set file_exts; - void (*on_file_open)(std::string) = nullptr; -}; \ No newline at end of file diff --git a/mjpeg-player.cpp b/mjpeg-player.cpp index 813c6b3..21e1572 100644 --- a/mjpeg-player.cpp +++ b/mjpeg-player.cpp @@ -31,7 +31,7 @@ ffmpeg -i 2020-02-19\ 12-14-08.mkv -vcodec mjpeg -q:v 2 -pix_fmt yuvj420p -vf sc */ const blit::Font tallFont(asset_tall_font); -FileBrowser fileBrowser(tallFont); +duh::FileBrowser fileBrowser(tallFont); std::string fileToLoad; bool renderedLoadMessage = false;