Skip to content

Commit

Permalink
scripts menu enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
rennancockles committed Oct 2, 2024
1 parent 10f801c commit 44170d3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
21 changes: 17 additions & 4 deletions src/core/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,16 @@ void drawSubmenu(int index,std::vector<Option>& options, String system) {
tft.setTextColor(FGCOLOR-0x2000);
tft.drawCentreString(options[menuSize-1].label.c_str(),WIDTH/2, 42+(HEIGHT-134)/2,SMOOTH_FONT);
}
tft.setTextSize(FG);
tft.setTextColor(FGCOLOR);
tft.drawCentreString(options[index].label.c_str(),WIDTH/2, 67+(HEIGHT-134)/2,SMOOTH_FONT);

int selectedTextSize = options[index].label.length() <= WIDTH/(LW*FG)-1 ? FG : FM;
tft.setTextSize(selectedTextSize);
tft.setTextColor(FGCOLOR);
tft.drawCentreString(
options[index].label.c_str(),
WIDTH/2,
67+(HEIGHT-134)/2+((selectedTextSize-1)%2)*LH/2,
SMOOTH_FONT
);

if (index+1<menuSize) {
tft.setTextSize(FM);
Expand All @@ -409,7 +416,13 @@ void drawSubmenu(int index,std::vector<Option>& options, String system) {
tft.setTextColor(FGCOLOR-0x2000);
tft.drawCentreString(options[0].label.c_str(),WIDTH/2, 102+(HEIGHT-134)/2,SMOOTH_FONT);
}
tft.drawFastHLine(WIDTH/2 - options[index].label.size()*FG*LW/2, 67+FG*LH+(HEIGHT-134)/2,options[index].label.size()*FG*LW,FGCOLOR);

tft.drawFastHLine(
WIDTH/2 - options[index].label.size()*selectedTextSize*LW/2,
67+(HEIGHT-134)/2+((selectedTextSize-1)%2)*LH/2+selectedTextSize*LH,
options[index].label.size()*selectedTextSize*LW,
FGCOLOR
);
tft.fillRect(WIDTH-5,0,5,HEIGHT,BGCOLOR);
tft.fillRect(WIDTH-5,index*HEIGHT/menuSize,5,HEIGHT/menuSize,FGCOLOR);

Expand Down
6 changes: 3 additions & 3 deletions src/core/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ MainMenu::MainMenu() {
&rfidMenu,
&irMenu,
&fmMenu,
&othersMenu,
&clockMenu,
&configMenu,
#if !defined(CORE) && !defined(CORE2)
&scriptsMenu,
#endif
&othersMenu,
&clockMenu,
&configMenu,
};

_totalItems = _menuItems.size();
Expand Down
57 changes: 41 additions & 16 deletions src/core/menu_items/ScriptsMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,62 @@
#include "modules/bjs_interpreter/interpreter.h" // for JavaScript interpreter


void ScriptsMenu::optionsMenu() {
String getScriptsFolder(FS *&fs) {
String folder;
String possibleFolders[] = {"/scripts", "/BruceScripts", "/BruceJS"};
int listSize = sizeof(possibleFolders) / sizeof(possibleFolders[0]);

for (int i = 0; i < listSize; i++) {
if(SD.exists(possibleFolders[i])) {
fs = &SD;
return possibleFolders[i];
}
if(LittleFS.exists(possibleFolders[i])) {
fs = &LittleFS;
return possibleFolders[i];
}
}
return "";
}

String Folder = "/scripts";
FS* fs = NULL;
if(SD.exists(Folder)) fs = &SD;
if(LittleFS.exists(Folder)) fs = &LittleFS;
if(!fs) return; // dir not found

//String fileList[MAXFILES][3];
//readFs(fs, Folder, fileList, "bjs");
std::vector<Option> getScriptsOptionsList() {
std::vector<Option> opt = {};
FS* fs;
String folder = getScriptsFolder(fs);
if(folder == "") return opt; // did not find

options = { };

File root = fs->open(Folder);
if (!root || !root.isDirectory()) return; // not a dir
File root = fs->open(folder);
if (!root || !root.isDirectory()) return opt; // not a dir
File file2 = root.openNextFile();

while (file2) {
if (file2.isDirectory()) continue;

String fileName = String(file2.name());
if( ! fileName.endsWith(".js") && ! fileName.endsWith(".bjs")) continue;
// else append to the choices
String entry_title = String(file2.name()); entry_title = entry_title.substring(0, entry_title.lastIndexOf(".")); // remove the extension
options.push_back({entry_title.c_str(), [=]() { run_bjs_script_headless(*fs, file2.path()); }});

String entry_title = String(file2.name());
entry_title = entry_title.substring(0, entry_title.lastIndexOf(".")); // remove the extension
opt.push_back(
{entry_title.c_str(), [=]() { run_bjs_script_headless(*fs, file2.path()); }}
);

file2 = root.openNextFile();
}
file2.close();
root.close();

options.push_back({"Load...", [=]() { run_bjs_script(); }});
options.push_back({"Main Menu", [=]() { backToMenu(); }});
return opt;
}


void ScriptsMenu::optionsMenu() {
options = getScriptsOptionsList();

options.push_back({"Load...", [=]() { run_bjs_script(); }});
options.push_back({"Main Menu", [=]() { backToMenu(); }});

delay(200);
loopOptions(options,false,true,"Scripts");
Expand Down

0 comments on commit 44170d3

Please sign in to comment.