Batch update libstorm-nvgt modules

This commit is contained in:
Storm Dragon
2026-02-22 19:18:47 -05:00
parent 44f13b1aeb
commit aa1ab8f533
16 changed files with 1197 additions and 1046 deletions

View File

@@ -3,23 +3,23 @@
// Resolves an audio path with optional extension fallback. // Resolves an audio path with optional extension fallback.
// Checks exact path first, then ".ogg", then ".wav". // Checks exact path first, then ".ogg", then ".wav".
string resolve_audio_path(const string audioPath) { string resolve_audio_path(const string audioPath) {
if (audioPath == "") { if (audioPath == "") {
return ""; return "";
} }
if (file_exists(audioPath)) { if (file_exists(audioPath)) {
return audioPath; return audioPath;
} }
string oggPath = audioPath + ".ogg"; string oggPath = audioPath + ".ogg";
if (file_exists(oggPath)) { if (file_exists(oggPath)) {
return oggPath; return oggPath;
} }
string wavPath = audioPath + ".wav"; string wavPath = audioPath + ".wav";
if (file_exists(wavPath)) { if (file_exists(wavPath)) {
return wavPath; return wavPath;
} }
return ""; return "";
} }

View File

@@ -1,58 +1,64 @@
// Crash and log helpers. // Crash and log helpers.
string log_format_timestamp() { string log_format_timestamp() {
datetime dt; datetime dt;
string stamp = dt.format(DATE_TIME_FORMAT_RFC1123); string stamp = dt.format(DATE_TIME_FORMAT_RFC1123);
return "[" + stamp + "]"; return "[" + stamp + "]";
} }
string log_flatten_multiline(const string&in text) { string log_flatten_multiline(const string& in text) {
string result = ""; string result = "";
bool lastWasSeparator = false; bool lastWasSeparator = false;
for (uint charIndex = 0; charIndex < text.length(); charIndex++) { for (uint charIndex = 0; charIndex < text.length(); charIndex++) {
string ch = text.substr(charIndex, 1); string ch = text.substr(charIndex, 1);
if (ch == "\r" || ch == "\n") { if (ch == "\r" || ch == "\n") {
if (!lastWasSeparator) { if (!lastWasSeparator) {
result += " | "; result += " | ";
lastWasSeparator = true; lastWasSeparator = true;
} }
continue; continue;
} }
result += ch; result += ch;
lastWasSeparator = false; lastWasSeparator = false;
} }
return result; return result;
} }
bool log_append_line(const string&in logPath, const string&in message) { bool log_append_line(const string& in logPath, const string& in message) {
file logFile; file logFile;
if (!logFile.open(logPath, "ab")) { if (!logFile.open(logPath, "ab")) {
return false; return false;
} }
// Keep log format as message then timestamp. // Keep log format as message then timestamp.
logFile.write(message + " " + log_format_timestamp() + "\r\n"); logFile.write(message + " " + log_format_timestamp() + "\r\n");
logFile.close(); logFile.close();
return true; return true;
} }
void log_unhandled_exception_to_file(const string&in logPath = "crash.log", const string&in context = "") { void log_unhandled_exception_to_file(const string& in logPath = "crash.log", const string& in context = "") {
string info = get_exception_info(); string info = get_exception_info();
string filePath = get_exception_file(); string filePath = get_exception_file();
int line = get_exception_line(); int line = get_exception_line();
string func = get_exception_function(); string func = get_exception_function();
string stack = log_flatten_multiline(last_exception_call_stack); string stack = log_flatten_multiline(last_exception_call_stack);
string message = "Unhandled exception"; string message = "Unhandled exception";
if (context != "") message += " (" + context + ")"; if (context != "")
if (info != "") message += ": " + info; message += " (" + context + ")";
if (filePath != "") message += " at " + filePath; if (info != "")
if (line > 0) message += ":" + line; message += ": " + info;
if (func != "") message += " in " + func; if (filePath != "")
if (stack != "") message += " | stack: " + stack; message += " at " + filePath;
if (line > 0)
message += ":" + line;
if (func != "")
message += " in " + func;
if (stack != "")
message += " | stack: " + stack;
log_append_line(logPath, message); log_append_line(logPath, message);
} }

View File

@@ -1,58 +1,72 @@
// Dictionary utility helpers. // Dictionary utility helpers.
double dict_get_number(dictionary@ data, const string&in key, double defaultValue) { double dict_get_number(dictionary @data, const string& in key, double defaultValue) {
double value = 0.0; double value = 0.0;
if (@data is null) return defaultValue; if (@data is null)
if (data.get(key, value)) return value; return defaultValue;
if (data.get(key, value))
return value;
int valueInt = 0; int valueInt = 0;
if (data.get(key, valueInt)) return valueInt; if (data.get(key, valueInt))
return valueInt;
string valueString = ""; string valueString = "";
if (data.get(key, valueString)) { if (data.get(key, valueString)) {
return parse_int(valueString); return parse_int(valueString);
} }
return defaultValue; return defaultValue;
} }
bool dict_get_bool(dictionary@ data, const string&in key, bool defaultValue) { bool dict_get_bool(dictionary @data, const string& in key, bool defaultValue) {
bool value = false; bool value = false;
if (@data is null) return defaultValue; if (@data is null)
if (data.get(key, value)) return value; return defaultValue;
if (data.get(key, value))
return value;
int valueInt = 0; int valueInt = 0;
if (data.get(key, valueInt)) return valueInt != 0; if (data.get(key, valueInt))
return valueInt != 0;
string valueString = ""; string valueString = "";
if (data.get(key, valueString)) return valueString == "1" || valueString == "true"; if (data.get(key, valueString))
return valueString == "1" || valueString == "true";
return defaultValue; return defaultValue;
} }
bool dict_has_keys(dictionary@ data) { bool dict_has_keys(dictionary @data) {
if (@data is null) return false; if (@data is null)
string[]@ keys = data.get_keys(); return false;
return @keys !is null && keys.length() > 0; string[] @keys = data.get_keys();
return @keys !is null && keys.length() > 0;
} }
bool dict_has_number_key(dictionary@ data, const string&in key) { bool dict_has_number_key(dictionary @data, const string& in key) {
double value = 0.0; double value = 0.0;
if (@data is null) return false; if (@data is null)
if (data.get(key, value)) return true; return false;
if (data.get(key, value))
return true;
int valueInt = 0; int valueInt = 0;
if (data.get(key, valueInt)) return true; if (data.get(key, valueInt))
return true;
string valueString = ""; string valueString = "";
if (data.get(key, valueString)) return valueString.length() > 0; if (data.get(key, valueString))
return valueString.length() > 0;
return false; return false;
} }
string[] dict_get_string_list(dictionary@ data, const string&in key) { string[] dict_get_string_list(dictionary @data, const string& in key) {
string[] result; string[] result;
if (@data is null) return result; if (@data is null)
if (!data.get(key, result)) return result; return result;
return result; if (!data.get(key, result))
return result;
return result;
} }

View File

@@ -1,11 +1,11 @@
#include "audio_paths.nvgt" #include "audio_paths.nvgt"
#include "file_viewer.nvgt" #include "file_viewer.nvgt"
funcdef void docs_browser_speak_callback(const string &in message, bool interrupt); funcdef void docs_browser_speak_callback(const string& in message, bool interrupt);
funcdef void docs_browser_tick_callback(); funcdef void docs_browser_tick_callback();
docs_browser_speak_callback@ docsBrowserSpeakCallback = null; docs_browser_speak_callback @docsBrowserSpeakCallback = null;
docs_browser_tick_callback@ docsBrowserTickCallback = null; docs_browser_tick_callback @docsBrowserTickCallback = null;
string docsBrowserDirectory = "files"; string docsBrowserDirectory = "files";
string docsBrowserMenuSoundDir = "sounds/menu"; string docsBrowserMenuSoundDir = "sounds/menu";
@@ -16,189 +16,198 @@ string[] docsBrowserExtensions = {"md", "MD", "txt", "TXT"};
sound docsBrowserMoveSound; sound docsBrowserMoveSound;
sound docsBrowserSelectSound; sound docsBrowserSelectSound;
void docs_browser_set_speak_callback(docs_browser_speak_callback@ callback) { void docs_browser_set_speak_callback(docs_browser_speak_callback @callback) {
@docsBrowserSpeakCallback = @callback; @docsBrowserSpeakCallback = @callback;
} }
void docs_browser_set_tick_callback(docs_browser_tick_callback@ callback) { void docs_browser_set_tick_callback(docs_browser_tick_callback @callback) {
@docsBrowserTickCallback = @callback; @docsBrowserTickCallback = @callback;
} }
void docs_browser_set_docs_dir(const string docsDir) { void docs_browser_set_docs_dir(const string docsDir) {
docsBrowserDirectory = docsDir; docsBrowserDirectory = docsDir;
} }
void docs_browser_set_menu_sound_dir(const string menuSoundDir) { void docs_browser_set_menu_sound_dir(const string menuSoundDir) {
docsBrowserMenuSoundDir = menuSoundDir; docsBrowserMenuSoundDir = menuSoundDir;
} }
void docs_browser_set_wrap(bool wrap) { void docs_browser_set_wrap(bool wrap) {
docsBrowserWrap = wrap; docsBrowserWrap = wrap;
} }
void docs_browser_set_extensions(string[]@ extensions) { void docs_browser_set_extensions(string[] @extensions) {
docsBrowserExtensions.resize(0); docsBrowserExtensions.resize(0);
if (@extensions is null) return; if (@extensions is null)
for (uint extIndex = 0; extIndex < extensions.length(); extIndex++) { return;
docsBrowserExtensions.insert_last(extensions[extIndex]); for (uint extIndex = 0; extIndex < extensions.length(); extIndex++) {
} docsBrowserExtensions.insert_last(extensions[extIndex]);
}
} }
void docs_browser_reset_default_extensions() { void docs_browser_reset_default_extensions() {
string[] defaults = {"md", "MD", "txt", "TXT"}; string[] defaults = {"md", "MD", "txt", "TXT"};
docs_browser_set_extensions(defaults); docs_browser_set_extensions(defaults);
} }
void docs_browser_speak(const string &in message, bool interrupt) { void docs_browser_speak(const string& in message, bool interrupt) {
if (@docsBrowserSpeakCallback !is null) { if (@docsBrowserSpeakCallback !is null) {
docsBrowserSpeakCallback(message, interrupt); docsBrowserSpeakCallback(message, interrupt);
return; return;
} }
screen_reader_speak(message, interrupt); screen_reader_speak(message, interrupt);
} }
void docs_browser_tick() { void docs_browser_tick() {
if (@docsBrowserTickCallback !is null) { if (@docsBrowserTickCallback !is null) {
docsBrowserTickCallback(); docsBrowserTickCallback();
} }
} }
bool docs_browser_sort_case_insensitive(const string &in a, const string &in b) { bool docs_browser_sort_case_insensitive(const string& in a, const string& in b) {
return a.lower() < b.lower(); return a.lower() < b.lower();
} }
void docs_browser_append_unique_case_insensitive(string[]@ items, const string&in value) { void docs_browser_append_unique_case_insensitive(string[] @items, const string& in value) {
string lowerValue = value.lower(); string lowerValue = value.lower();
for (uint itemIndex = 0; itemIndex < items.length(); itemIndex++) { for (uint itemIndex = 0; itemIndex < items.length(); itemIndex++) {
if (items[itemIndex].lower() == lowerValue) return; if (items[itemIndex].lower() == lowerValue)
} return;
items.insert_last(value); }
items.insert_last(value);
} }
string docs_browser_collapse_spaces(const string&in text) { string docs_browser_collapse_spaces(const string& in text) {
string result = text; string result = text;
while (result.find_first(" ") > -1) { while (result.find_first(" ") > -1) {
result = result.replace(" ", " ", true); result = result.replace(" ", " ", true);
} }
return result; return result;
} }
string docs_browser_format_label(const string&in filename) { string docs_browser_format_label(const string& in filename) {
string name = filename; string name = filename;
int dotPos = name.find_last_of("."); int dotPos = name.find_last_of(".");
if (dotPos > 0) { if (dotPos > 0) {
name = name.substr(0, dotPos); name = name.substr(0, dotPos);
} }
name = name.replace("_", " ", true); name = name.replace("_", " ", true);
name = name.replace("-", " ", true); name = name.replace("-", " ", true);
name = docs_browser_collapse_spaces(name); name = docs_browser_collapse_spaces(name);
name = name.lower(); name = name.lower();
name.trim_whitespace_this(); name.trim_whitespace_this();
if (name.length() == 0) return "Document"; if (name.length() == 0)
string first = name.substr(0, 1).upper(); return "Document";
if (name.length() == 1) return first; string first = name.substr(0, 1).upper();
return first + name.substr(1); if (name.length() == 1)
return first;
return first + name.substr(1);
} }
void docs_browser_collect_entries(string[]@ labels, string[]@ paths) { void docs_browser_collect_entries(string[] @labels, string[] @paths) {
labels.resize(0); labels.resize(0);
paths.resize(0); paths.resize(0);
if (!directory_exists(docsBrowserDirectory)) return; if (!directory_exists(docsBrowserDirectory))
return;
string[] docFiles; string[] docFiles;
for (uint extIndex = 0; extIndex < docsBrowserExtensions.length(); extIndex++) { for (uint extIndex = 0; extIndex < docsBrowserExtensions.length(); extIndex++) {
string ext = docsBrowserExtensions[extIndex]; string ext = docsBrowserExtensions[extIndex];
if (ext == "") continue; if (ext == "")
if (ext.substr(0, 1) == ".") { continue;
ext = ext.substr(1); if (ext.substr(0, 1) == ".") {
} ext = ext.substr(1);
}
string[]@ found = find_files(docsBrowserDirectory + "/*." + ext); string[] @found = find_files(docsBrowserDirectory + "/*." + ext);
if (@found is null) continue; if (@found is null)
for (uint fileIndex = 0; fileIndex < found.length(); fileIndex++) { continue;
docs_browser_append_unique_case_insensitive(docFiles, found[fileIndex]); for (uint fileIndex = 0; fileIndex < found.length(); fileIndex++) {
} docs_browser_append_unique_case_insensitive(docFiles, found[fileIndex]);
} }
}
if (docFiles.length() > 1) { if (docFiles.length() > 1) {
docFiles.sort(docs_browser_sort_case_insensitive); docFiles.sort(docs_browser_sort_case_insensitive);
} }
for (uint fileIndex = 0; fileIndex < docFiles.length(); fileIndex++) { for (uint fileIndex = 0; fileIndex < docFiles.length(); fileIndex++) {
labels.insert_last(docs_browser_format_label(docFiles[fileIndex])); labels.insert_last(docs_browser_format_label(docFiles[fileIndex]));
paths.insert_last(docsBrowserDirectory + "/" + docFiles[fileIndex]); paths.insert_last(docsBrowserDirectory + "/" + docFiles[fileIndex]);
} }
} }
void docs_browser_add_entries(string[]@ labels, string[]@ paths, int[]@ types, const int typeValue = 1) { void docs_browser_add_entries(string[] @labels, string[] @paths, int[] @types, const int typeValue = 1) {
string[] docLabels; string[] docLabels;
string[] docPaths; string[] docPaths;
docs_browser_collect_entries(docLabels, docPaths); docs_browser_collect_entries(docLabels, docPaths);
for (uint entryIndex = 0; entryIndex < docLabels.length(); entryIndex++) { for (uint entryIndex = 0; entryIndex < docLabels.length(); entryIndex++) {
labels.insert_last(docLabels[entryIndex]); labels.insert_last(docLabels[entryIndex]);
paths.insert_last(docPaths[entryIndex]); paths.insert_last(docPaths[entryIndex]);
types.insert_last(typeValue); types.insert_last(typeValue);
} }
} }
void docs_browser_play_ui_sound(sound &inout soundObj, const string basePath) { void docs_browser_play_ui_sound(sound& inout soundObj, const string basePath) {
string soundPath = resolve_audio_path(basePath); string soundPath = resolve_audio_path(basePath);
if (soundPath == "") return; if (soundPath == "")
return;
soundObj.close(); soundObj.close();
if (!soundObj.load(soundPath)) return; if (!soundObj.load(soundPath))
soundObj.play(); return;
soundObj.play();
} }
void docs_browser_run_menu(const string menuTitle = "Documents") { void docs_browser_run_menu(const string menuTitle = "Documents") {
string[] labels; string[] labels;
string[] paths; string[] paths;
docs_browser_collect_entries(labels, paths); docs_browser_collect_entries(labels, paths);
docs_browser_speak(menuTitle + ".", true); docs_browser_speak(menuTitle + ".", true);
if (labels.length() == 0) { if (labels.length() == 0) {
docs_browser_speak("No documents found.", true); docs_browser_speak("No documents found.", true);
return; return;
} }
int selection = 0; int selection = 0;
docs_browser_speak(labels[selection], true); docs_browser_speak(labels[selection], true);
while (true) { while (true) {
wait(5); wait(5);
docs_browser_tick(); docs_browser_tick();
if (key_pressed(KEY_ESCAPE)) { if (key_pressed(KEY_ESCAPE)) {
docs_browser_speak("Closed.", true); docs_browser_speak("Closed.", true);
return; return;
} }
if (key_pressed(KEY_DOWN)) { if (key_pressed(KEY_DOWN)) {
docs_browser_play_ui_sound(docsBrowserMoveSound, docsBrowserMenuSoundDir + "/menu_move"); docs_browser_play_ui_sound(docsBrowserMoveSound, docsBrowserMenuSoundDir + "/menu_move");
selection++; selection++;
if (selection >= int(labels.length())) { if (selection >= int(labels.length())) {
selection = docsBrowserWrap ? 0 : int(labels.length()) - 1; selection = docsBrowserWrap ? 0 : int(labels.length()) - 1;
} }
docs_browser_speak(labels[selection], true); docs_browser_speak(labels[selection], true);
} }
if (key_pressed(KEY_UP)) { if (key_pressed(KEY_UP)) {
docs_browser_play_ui_sound(docsBrowserMoveSound, docsBrowserMenuSoundDir + "/menu_move"); docs_browser_play_ui_sound(docsBrowserMoveSound, docsBrowserMenuSoundDir + "/menu_move");
selection--; selection--;
if (selection < 0) { if (selection < 0) {
selection = docsBrowserWrap ? int(labels.length()) - 1 : 0; selection = docsBrowserWrap ? int(labels.length()) - 1 : 0;
} }
docs_browser_speak(labels[selection], true); docs_browser_speak(labels[selection], true);
} }
if (key_pressed(KEY_RETURN)) { if (key_pressed(KEY_RETURN)) {
docs_browser_play_ui_sound(docsBrowserSelectSound, docsBrowserMenuSoundDir + "/menu_select"); docs_browser_play_ui_sound(docsBrowserSelectSound, docsBrowserMenuSoundDir + "/menu_select");
file_viewer_file(paths[selection], labels[selection], true); file_viewer_file(paths[selection], labels[selection], true);
docs_browser_speak(labels[selection], true); docs_browser_speak(labels[selection], true);
} }
} }
} }

View File

@@ -3,83 +3,76 @@
// Reusable text/file viewer based on NVGT audio_form. // Reusable text/file viewer based on NVGT audio_form.
// Returns edited text in edit mode when user confirms. // Returns edited text in edit mode when user confirms.
string file_viewer(string content, string title = "Text Reader", bool readOnly = true) { string file_viewer(string content, string title = "Text Reader", bool readOnly = true) {
audio_form f; audio_form f;
f.create_window(title, false, true); f.create_window(title, false, true);
int textControl = f.create_input_box( int textControl = f.create_input_box((readOnly ? "Document (read only)" : "Document (editable)"), content, "", 0,
(readOnly ? "Document (read only)" : "Document (editable)"), readOnly, true, true);
content,
"",
0,
readOnly,
true,
true
);
int okButton = -1; int okButton = -1;
int closeButton = -1; int closeButton = -1;
if (readOnly) { if (readOnly) {
closeButton = f.create_button("&Close", true, true); closeButton = f.create_button("&Close", true, true);
} else { } else {
okButton = f.create_button("&OK", true); okButton = f.create_button("&OK", true);
closeButton = f.create_button("&Cancel", false, true); closeButton = f.create_button("&Cancel", false, true);
} }
f.focus(textControl); f.focus(textControl);
while (true) { while (true) {
f.monitor(); f.monitor();
wait(5); wait(5);
if (!readOnly && okButton != -1 && f.is_pressed(okButton)) { if (!readOnly && okButton != -1 && f.is_pressed(okButton)) {
return f.get_text(textControl); return f.get_text(textControl);
} }
if (closeButton != -1 && f.is_pressed(closeButton)) { if (closeButton != -1 && f.is_pressed(closeButton)) {
return ""; return "";
} }
if (key_pressed(KEY_ESCAPE)) { if (key_pressed(KEY_ESCAPE)) {
return ""; return "";
} }
} }
return ""; return "";
} }
string file_viewer_lines(string[] lines, string title = "Text Reader", bool readOnly = true) { string file_viewer_lines(string[] lines, string title = "Text Reader", bool readOnly = true) {
string content = join(lines, "\n"); string content = join(lines, "\n");
return file_viewer(content, title, readOnly); return file_viewer(content, title, readOnly);
} }
// Opens a file in the viewer. In edit mode, saves on confirm. // Opens a file in the viewer. In edit mode, saves on confirm.
// Returns edited text when saved or empty string when canceled/failure. // Returns edited text when saved or empty string when canceled/failure.
string file_viewer_file(string filePath, string title = "", bool readOnly = true) { string file_viewer_file(string filePath, string title = "", bool readOnly = true) {
file f; file f;
if (!f.open(filePath, "rb")) { if (!f.open(filePath, "rb")) {
screen_reader_speak("Failed to open file: " + filePath, true); screen_reader_speak("Failed to open file: " + filePath, true);
return ""; return "";
} }
string content = f.read(); string content = f.read();
f.close(); f.close();
if (title == "") { if (title == "") {
title = filePath; title = filePath;
} }
string result = file_viewer(content, title, readOnly); string result = file_viewer(content, title, readOnly);
if (!readOnly && result != "") { if (!readOnly && result != "") {
if (f.open(filePath, "wb")) { if (f.open(filePath, "wb")) {
f.write(result); f.write(result);
f.close(); f.close();
screen_reader_speak("File saved successfully", true); screen_reader_speak("File saved successfully", true);
return result; return result;
} }
screen_reader_speak("Failed to save file", true); screen_reader_speak("Failed to save file", true);
} }
return result; return result;
} }

View File

@@ -1,12 +1,12 @@
#include "audio_paths.nvgt" #include "audio_paths.nvgt"
funcdef void learn_sounds_speak_callback(const string &in message, bool interrupt); funcdef void learn_sounds_speak_callback(const string& in message, bool interrupt);
funcdef void learn_sounds_tick_callback(); funcdef void learn_sounds_tick_callback();
funcdef void learn_sounds_setup_callback(); funcdef void learn_sounds_setup_callback();
learn_sounds_speak_callback@ learnSoundsSpeakCallback = null; learn_sounds_speak_callback @learnSoundsSpeakCallback = null;
learn_sounds_tick_callback@ learnSoundsTickCallback = null; learn_sounds_tick_callback @learnSoundsTickCallback = null;
learn_sounds_setup_callback@ learnSoundsSetupCallback = null; learn_sounds_setup_callback @learnSoundsSetupCallback = null;
string[] learnSoundsSkipList; string[] learnSoundsSkipList;
string[] learnSoundsDescriptionPaths; string[] learnSoundsDescriptionPaths;
@@ -22,319 +22,390 @@ sound learnSoundsPreviewSound;
sound learnSoundsMoveSound; sound learnSoundsMoveSound;
sound learnSoundsSelectSound; sound learnSoundsSelectSound;
void learn_sounds_set_speak_callback(learn_sounds_speak_callback@ callback) { void learn_sounds_set_speak_callback(learn_sounds_speak_callback @callback) {
@learnSoundsSpeakCallback = @callback; @learnSoundsSpeakCallback = @callback;
} }
void learn_sounds_set_tick_callback(learn_sounds_tick_callback@ callback) { void learn_sounds_set_tick_callback(learn_sounds_tick_callback @callback) {
@learnSoundsTickCallback = @callback; @learnSoundsTickCallback = @callback;
} }
void learn_sounds_set_setup_callback(learn_sounds_setup_callback@ callback) { void learn_sounds_set_setup_callback(learn_sounds_setup_callback @callback) {
@learnSoundsSetupCallback = @callback; @learnSoundsSetupCallback = @callback;
learnSoundsSetupApplied = false; learnSoundsSetupApplied = false;
} }
void learn_sounds_set_root_dir(const string rootDir) { void learn_sounds_set_root_dir(const string rootDir) {
learnSoundsRootDir = rootDir; learnSoundsRootDir = rootDir;
} }
void learn_sounds_set_menu_sound_dir(const string menuSoundDir) { void learn_sounds_set_menu_sound_dir(const string menuSoundDir) {
learnSoundsMenuSoundDir = menuSoundDir; learnSoundsMenuSoundDir = menuSoundDir;
} }
void learn_sounds_set_wrap(bool wrap) { void learn_sounds_set_wrap(bool wrap) {
learnSoundsWrap = wrap; learnSoundsWrap = wrap;
} }
void learn_sounds_set_play_select_sound(bool playSelectSound) { void learn_sounds_set_play_select_sound(bool playSelectSound) {
learnSoundsPlaySelectSound = playSelectSound; learnSoundsPlaySelectSound = playSelectSound;
} }
void learn_sounds_clear_skip_entries() { void learn_sounds_clear_skip_entries() {
learnSoundsSkipList.resize(0); learnSoundsSkipList.resize(0);
} }
void learn_sounds_add_skip_entry(const string entry) { void learn_sounds_add_skip_entry(const string entry) {
if (entry == "") { if (entry == "") {
return; return;
} }
learnSoundsSkipList.insert_last(entry); learnSoundsSkipList.insert_last(entry);
} }
void learn_sounds_clear_descriptions() { void learn_sounds_clear_descriptions() {
learnSoundsDescriptionPaths.resize(0); learnSoundsDescriptionPaths.resize(0);
learnSoundsDescriptionTexts.resize(0); learnSoundsDescriptionTexts.resize(0);
} }
void learn_sounds_add_description(const string soundPath, const string description) { void learn_sounds_add_description(const string soundPath, const string description) {
if (soundPath == "") { if (soundPath == "") {
return; return;
} }
learnSoundsDescriptionPaths.insert_last(soundPath); learnSoundsDescriptionPaths.insert_last(soundPath);
learnSoundsDescriptionTexts.insert_last(description); learnSoundsDescriptionTexts.insert_last(description);
} }
void learn_sounds_reset_configuration() { void learn_sounds_reset_configuration() {
learn_sounds_clear_skip_entries(); learn_sounds_clear_skip_entries();
learn_sounds_clear_descriptions(); learn_sounds_clear_descriptions();
learnSoundsSetupApplied = false; learnSoundsSetupApplied = false;
} }
void learn_sounds_speak(const string &in message, bool interrupt) { void learn_sounds_speak(const string& in message, bool interrupt) {
if (@learnSoundsSpeakCallback !is null) { if (@learnSoundsSpeakCallback !is null) {
learnSoundsSpeakCallback(message, interrupt); learnSoundsSpeakCallback(message, interrupt);
return; return;
} }
screen_reader_speak(message, interrupt); screen_reader_speak(message, interrupt);
} }
void learn_sounds_tick() { void learn_sounds_tick() {
if (@learnSoundsTickCallback !is null) { if (@learnSoundsTickCallback !is null) {
learnSoundsTickCallback(); learnSoundsTickCallback();
} }
} }
void learn_sounds_apply_setup_once() { void learn_sounds_apply_setup_once() {
if (learnSoundsSetupApplied) { if (learnSoundsSetupApplied) {
return; return;
} }
if (@learnSoundsSetupCallback !is null) { if (@learnSoundsSetupCallback !is null) {
learnSoundsSetupCallback(); learnSoundsSetupCallback();
} }
learnSoundsSetupApplied = true; learnSoundsSetupApplied = true;
} }
string learn_sounds_normalize_path(const string&in path) { string learn_sounds_normalize_path(const string& in path) {
return path.replace("\\", "/", true); return path.replace("\\", "/", true);
} }
bool learn_sounds_is_directory_skip_entry(const string&in entry) { bool learn_sounds_is_directory_skip_entry(const string& in entry) {
if (entry.length() == 0) return false; if (entry.length() == 0)
if (entry.substr(entry.length() - 1) == "/") return true; return false;
return directory_exists(entry); if (entry.substr(entry.length() - 1) == "/")
return true;
return directory_exists(entry);
} }
bool learn_sounds_should_skip_path(const string&in path) { bool learn_sounds_should_skip_path(const string& in path) {
string normalizedPath = learn_sounds_normalize_path(path); string normalizedPath = learn_sounds_normalize_path(path);
for (uint skipIndex = 0; skipIndex < learnSoundsSkipList.length(); skipIndex++) { for (uint skipIndex = 0; skipIndex < learnSoundsSkipList.length(); skipIndex++) {
string entry = learn_sounds_normalize_path(learnSoundsSkipList[skipIndex]); string entry = learn_sounds_normalize_path(learnSoundsSkipList[skipIndex]);
if (entry.length() == 0) continue; if (entry.length() == 0)
continue;
bool isDirectory = learn_sounds_is_directory_skip_entry(entry); bool isDirectory = learn_sounds_is_directory_skip_entry(entry);
if (isDirectory) { if (isDirectory) {
if (entry.substr(entry.length() - 1) != "/") entry += "/"; if (entry.substr(entry.length() - 1) != "/")
if (normalizedPath.length() >= entry.length() && entry += "/";
normalizedPath.substr(0, entry.length()) == entry) { if (normalizedPath.length() >= entry.length() && normalizedPath.substr(0, entry.length()) == entry) {
return true; return true;
} }
continue; continue;
} }
if (normalizedPath == entry) return true; if (normalizedPath == entry)
return true;
if (entry.find_first("/") < 0 && entry.find_first("\\") < 0) { if (entry.find_first("/") < 0 && entry.find_first("\\") < 0) {
if (normalizedPath.length() >= entry.length() + 1 && if (normalizedPath.length() >= entry.length() + 1 &&
normalizedPath.substr(normalizedPath.length() - entry.length()) == entry) { normalizedPath.substr(normalizedPath.length() - entry.length()) == entry) {
return true; return true;
} }
} }
} }
return false; return false;
} }
string learn_sounds_get_description(const string&in path) { string learn_sounds_get_description(const string& in path) {
string normalizedPath = learn_sounds_normalize_path(path); string normalizedPath = learn_sounds_normalize_path(path);
uint descriptionCount = learnSoundsDescriptionPaths.length(); uint descriptionCount = learnSoundsDescriptionPaths.length();
if (learnSoundsDescriptionTexts.length() < descriptionCount) { if (learnSoundsDescriptionTexts.length() < descriptionCount) {
descriptionCount = learnSoundsDescriptionTexts.length(); descriptionCount = learnSoundsDescriptionTexts.length();
} }
for (uint descriptionIndex = 0; descriptionIndex < descriptionCount; descriptionIndex++) { for (uint descriptionIndex = 0; descriptionIndex < descriptionCount; descriptionIndex++) {
if (learn_sounds_normalize_path(learnSoundsDescriptionPaths[descriptionIndex]) == normalizedPath) { if (learn_sounds_normalize_path(learnSoundsDescriptionPaths[descriptionIndex]) == normalizedPath) {
return learnSoundsDescriptionTexts[descriptionIndex]; return learnSoundsDescriptionTexts[descriptionIndex];
} }
} }
return ""; return "";
} }
void learn_sounds_gather_files_recursive(const string&in basePath, string[]@ outFiles) { bool learn_sounds_contains_path_ci(string[] @paths, const string& in path) {
string[]@ oggFiles = find_files(basePath + "/*.ogg"); string normalizedPath = learn_sounds_normalize_path(path).lower();
if (@oggFiles !is null) { for (uint pathIndex = 0; pathIndex < paths.length(); pathIndex++) {
for (uint fileIndex = 0; fileIndex < oggFiles.length(); fileIndex++) { if (learn_sounds_normalize_path(paths[pathIndex]).lower() == normalizedPath) {
outFiles.insert_last(basePath + "/" + oggFiles[fileIndex]); return true;
} }
} }
return false;
string[]@ oggUpperFiles = find_files(basePath + "/*.OGG");
if (@oggUpperFiles !is null) {
for (uint fileIndex = 0; fileIndex < oggUpperFiles.length(); fileIndex++) {
outFiles.insert_last(basePath + "/" + oggUpperFiles[fileIndex]);
}
}
string[]@ wavFiles = find_files(basePath + "/*.wav");
if (@wavFiles !is null) {
for (uint fileIndex = 0; fileIndex < wavFiles.length(); fileIndex++) {
outFiles.insert_last(basePath + "/" + wavFiles[fileIndex]);
}
}
string[]@ wavUpperFiles = find_files(basePath + "/*.WAV");
if (@wavUpperFiles !is null) {
for (uint fileIndex = 0; fileIndex < wavUpperFiles.length(); fileIndex++) {
outFiles.insert_last(basePath + "/" + wavUpperFiles[fileIndex]);
}
}
string[]@ folders = find_directories(basePath + "/*");
if (@folders !is null) {
for (uint folderIndex = 0; folderIndex < folders.length(); folderIndex++) {
learn_sounds_gather_files_recursive(basePath + "/" + folders[folderIndex], outFiles);
}
}
} }
bool learn_sounds_sort_case_insensitive(const string &in a, const string &in b) { void learn_sounds_add_path_if_missing(string[] @paths, const string& in path) {
return a.lower() < b.lower(); if (learn_sounds_contains_path_ci(paths, path)) {
return;
}
paths.insert_last(path);
} }
string learn_sounds_collapse_spaces(const string&in text) { bool learn_sounds_has_audio_extension(const string& in path) {
string result = text; string lowerPath = path.lower();
while (result.find_first(" ") > -1) { if (lowerPath.length() < 4)
result = result.replace(" ", " ", true); return false;
} string suffix = lowerPath.substr(lowerPath.length() - 4);
return result; return suffix == ".ogg" || suffix == ".wav";
} }
string learn_sounds_label_from_path(const string&in soundPath) { void learn_sounds_gather_files_from_pack(const string& in basePath, string[] @outFiles) {
string normalizedPath = learn_sounds_normalize_path(soundPath); pack @activePack = cast<pack @>(sound_default_pack);
int slashPos = normalizedPath.find_last_of("/"); if (@activePack is null)
string name = (slashPos >= 0) ? normalizedPath.substr(slashPos + 1) : normalizedPath; return;
string lowerName = name.lower(); string[] @packFiles = activePack.list_files();
if (lowerName.length() > 4 && lowerName.substr(lowerName.length() - 4) == ".ogg") { if (@packFiles is null)
name = name.substr(0, name.length() - 4); return;
} else if (lowerName.length() > 4 && lowerName.substr(lowerName.length() - 4) == ".wav") {
name = name.substr(0, name.length() - 4);
}
name = name.replace("_", " ", true); string normalizedBasePath = learn_sounds_normalize_path(basePath);
name = name.replace("-", " ", true); if (normalizedBasePath != "" && normalizedBasePath.substr(normalizedBasePath.length() - 1) != "/") {
name = learn_sounds_collapse_spaces(name); normalizedBasePath += "/";
name = name.lower(); }
name.trim_whitespace_this(); string normalizedBasePathLower = normalizedBasePath.lower();
if (name.length() == 0) return "Sound"; for (uint fileIndex = 0; fileIndex < packFiles.length(); fileIndex++) {
return name; string normalizedPath = learn_sounds_normalize_path(packFiles[fileIndex]);
if (normalizedPath.find("./") == 0) {
normalizedPath = normalizedPath.substr(2);
}
if (!learn_sounds_has_audio_extension(normalizedPath)) {
continue;
}
if (normalizedBasePathLower != "") {
if (normalizedPath.length() < normalizedBasePathLower.length()) {
continue;
}
if (normalizedPath.substr(0, normalizedBasePathLower.length()).lower() != normalizedBasePathLower) {
continue;
}
}
learn_sounds_add_path_if_missing(outFiles, normalizedPath);
}
} }
void learn_sounds_collect_entries(string[]@ labels, string[]@ soundPaths) { void learn_sounds_gather_files_recursive(const string& in basePath, string[] @outFiles) {
labels.resize(0); string[] @oggFiles = find_files(basePath + "/*.ogg");
soundPaths.resize(0); if (@oggFiles !is null) {
for (uint fileIndex = 0; fileIndex < oggFiles.length(); fileIndex++) {
learn_sounds_add_path_if_missing(outFiles, basePath + "/" + oggFiles[fileIndex]);
}
}
if (!directory_exists(learnSoundsRootDir)) { string[] @oggUpperFiles = find_files(basePath + "/*.OGG");
return; if (@oggUpperFiles !is null) {
} for (uint fileIndex = 0; fileIndex < oggUpperFiles.length(); fileIndex++) {
learn_sounds_add_path_if_missing(outFiles, basePath + "/" + oggUpperFiles[fileIndex]);
}
}
string[] discoveredFiles; string[] @wavFiles = find_files(basePath + "/*.wav");
learn_sounds_gather_files_recursive(learnSoundsRootDir, discoveredFiles); if (@wavFiles !is null) {
if (discoveredFiles.length() > 1) { for (uint fileIndex = 0; fileIndex < wavFiles.length(); fileIndex++) {
discoveredFiles.sort(learn_sounds_sort_case_insensitive); learn_sounds_add_path_if_missing(outFiles, basePath + "/" + wavFiles[fileIndex]);
} }
}
for (uint fileIndex = 0; fileIndex < discoveredFiles.length(); fileIndex++) { string[] @wavUpperFiles = find_files(basePath + "/*.WAV");
string soundPath = learn_sounds_normalize_path(discoveredFiles[fileIndex]); if (@wavUpperFiles !is null) {
if (learn_sounds_should_skip_path(soundPath)) { for (uint fileIndex = 0; fileIndex < wavUpperFiles.length(); fileIndex++) {
continue; learn_sounds_add_path_if_missing(outFiles, basePath + "/" + wavUpperFiles[fileIndex]);
} }
}
string label = learn_sounds_label_from_path(soundPath); string[] @folders = find_directories(basePath + "/*");
string description = learn_sounds_get_description(soundPath); if (@folders !is null) {
if (description.length() > 0) { for (uint folderIndex = 0; folderIndex < folders.length(); folderIndex++) {
label += " - " + description; learn_sounds_gather_files_recursive(basePath + "/" + folders[folderIndex], outFiles);
} }
}
labels.insert_last(label);
soundPaths.insert_last(soundPath);
}
} }
void learn_sounds_play_ui_sound(sound &inout soundObj, const string basePath) { bool learn_sounds_sort_case_insensitive(const string& in a, const string& in b) {
string soundPath = resolve_audio_path(basePath); return a.lower() < b.lower();
if (soundPath == "") { }
return;
}
soundObj.close(); string learn_sounds_collapse_spaces(const string& in text) {
if (!soundObj.load(soundPath)) { string result = text;
return; while (result.find_first(" ") > -1) {
} result = result.replace(" ", " ", true);
soundObj.play(); }
return result;
}
string learn_sounds_label_from_path(const string& in soundPath) {
string normalizedPath = learn_sounds_normalize_path(soundPath);
int slashPos = normalizedPath.find_last_of("/");
string name = (slashPos >= 0) ? normalizedPath.substr(slashPos + 1) : normalizedPath;
string lowerName = name.lower();
if (lowerName.length() > 4 && lowerName.substr(lowerName.length() - 4) == ".ogg") {
name = name.substr(0, name.length() - 4);
} else if (lowerName.length() > 4 && lowerName.substr(lowerName.length() - 4) == ".wav") {
name = name.substr(0, name.length() - 4);
}
name = name.replace("_", " ", true);
name = name.replace("-", " ", true);
name = learn_sounds_collapse_spaces(name);
name = name.lower();
name.trim_whitespace_this();
if (name.length() == 0)
return "Sound";
return name;
}
void learn_sounds_collect_entries(string[] @labels, string[] @soundPaths) {
labels.resize(0);
soundPaths.resize(0);
string[] discoveredFiles;
if (directory_exists(learnSoundsRootDir)) {
learn_sounds_gather_files_recursive(learnSoundsRootDir, discoveredFiles);
}
learn_sounds_gather_files_from_pack(learnSoundsRootDir, discoveredFiles);
if (discoveredFiles.length() > 1) {
discoveredFiles.sort(learn_sounds_sort_case_insensitive);
}
for (uint fileIndex = 0; fileIndex < discoveredFiles.length(); fileIndex++) {
string soundPath = learn_sounds_normalize_path(discoveredFiles[fileIndex]);
if (learn_sounds_should_skip_path(soundPath)) {
continue;
}
string label = learn_sounds_label_from_path(soundPath);
string description = learn_sounds_get_description(soundPath);
if (description.length() > 0) {
label += " - " + description;
}
labels.insert_last(label);
soundPaths.insert_last(soundPath);
}
}
void learn_sounds_play_ui_sound(sound& inout soundObj, const string basePath) {
soundObj.close();
// Avoid file_exists()-only path checks so packaged Android assets can still load.
if (soundObj.load(basePath)) {
soundObj.play();
return;
}
if (soundObj.load(basePath + ".ogg")) {
soundObj.play();
return;
}
if (!soundObj.load(basePath + ".wav")) {
return;
}
soundObj.play();
} }
void learn_sounds_run_menu() { void learn_sounds_run_menu() {
learn_sounds_apply_setup_once(); learn_sounds_apply_setup_once();
string[] labels; string[] labels;
string[] soundPaths; string[] soundPaths;
learn_sounds_collect_entries(labels, soundPaths); learn_sounds_collect_entries(labels, soundPaths);
learn_sounds_speak("Learn sounds.", true); learn_sounds_speak("Learn sounds.", true);
if (labels.length() == 0) { if (labels.length() == 0) {
learn_sounds_speak("No sounds available.", true); learn_sounds_speak("No sounds available.", true);
return; return;
} }
int selection = 0; int selection = 0;
learn_sounds_speak(labels[selection], true); learn_sounds_speak(labels[selection], true);
while (true) { while (true) {
wait(5); wait(5);
learn_sounds_tick(); learn_sounds_tick();
if (key_pressed(KEY_ESCAPE)) { if (key_pressed(KEY_ESCAPE)) {
learn_sounds_speak("Closed.", true); learn_sounds_speak("Closed.", true);
return; return;
} }
if (key_pressed(KEY_DOWN)) { if (key_pressed(KEY_DOWN)) {
learn_sounds_play_ui_sound(learnSoundsMoveSound, learnSoundsMenuSoundDir + "/menu_move"); learn_sounds_play_ui_sound(learnSoundsMoveSound, learnSoundsMenuSoundDir + "/menu_move");
selection++; selection++;
if (selection >= int(labels.length())) { if (selection >= int(labels.length())) {
selection = learnSoundsWrap ? 0 : int(labels.length()) - 1; selection = learnSoundsWrap ? 0 : int(labels.length()) - 1;
} }
learn_sounds_speak(labels[selection], true); learn_sounds_speak(labels[selection], true);
} }
if (key_pressed(KEY_UP)) { if (key_pressed(KEY_UP)) {
learn_sounds_play_ui_sound(learnSoundsMoveSound, learnSoundsMenuSoundDir + "/menu_move"); learn_sounds_play_ui_sound(learnSoundsMoveSound, learnSoundsMenuSoundDir + "/menu_move");
selection--; selection--;
if (selection < 0) { if (selection < 0) {
selection = learnSoundsWrap ? int(labels.length()) - 1 : 0; selection = learnSoundsWrap ? int(labels.length()) - 1 : 0;
} }
learn_sounds_speak(labels[selection], true); learn_sounds_speak(labels[selection], true);
} }
if (key_pressed(KEY_RETURN)) { if (key_pressed(KEY_RETURN)) {
if (learnSoundsPlaySelectSound) { if (learnSoundsPlaySelectSound) {
learn_sounds_play_ui_sound(learnSoundsSelectSound, learnSoundsMenuSoundDir + "/menu_select"); learn_sounds_play_ui_sound(learnSoundsSelectSound, learnSoundsMenuSoundDir + "/menu_select");
} }
string selectedPath = soundPaths[selection]; string selectedPath = soundPaths[selection];
if (!file_exists(selectedPath)) {
learn_sounds_speak("Sound not found.", true);
continue;
}
learnSoundsPreviewSound.close(); learnSoundsPreviewSound.close();
if (!learnSoundsPreviewSound.load(selectedPath)) { if (!learnSoundsPreviewSound.load(selectedPath)) {
learn_sounds_speak("Unable to load sound.", true); learn_sounds_speak("Unable to load sound.", true);
continue; continue;
} }
learnSoundsPreviewSound.play(); learnSoundsPreviewSound.play();
} }
} }
} }

View File

@@ -3,120 +3,150 @@
// Applies common menu sounds from a directory. // Applies common menu sounds from a directory.
// Looks for both .ogg and .wav files automatically. // Looks for both .ogg and .wav files automatically.
void menu_apply_default_sounds(menu@ menuRef, const string soundDir = "sounds/menu") { void menu_apply_default_sounds(menu @menuRef, const string soundDir = "sounds/menu") {
if (@menuRef is null) { if (@menuRef is null) {
return; return;
} }
menuRef.click_sound = resolve_audio_path(soundDir + "/menu_move"); menuRef.click_sound = resolve_audio_path(soundDir + "/menu_move");
menuRef.select_sound = resolve_audio_path(soundDir + "/menu_select"); menuRef.select_sound = resolve_audio_path(soundDir + "/menu_select");
menuRef.edge_sound = resolve_audio_path(soundDir + "/menu_edge"); menuRef.edge_sound = resolve_audio_path(soundDir + "/menu_edge");
menuRef.wrap_sound = resolve_audio_path(soundDir + "/menu_wrap"); menuRef.wrap_sound = resolve_audio_path(soundDir + "/menu_wrap");
menuRef.open_sound = resolve_audio_path(soundDir + "/menu_open"); menuRef.open_sound = resolve_audio_path(soundDir + "/menu_open");
menuRef.close_sound = resolve_audio_path(soundDir + "/menu_close"); menuRef.close_sound = resolve_audio_path(soundDir + "/menu_close");
} }
// Minimal blocking list menu. // Minimal blocking list menu.
// Returns selected index or -1 on empty input/escape. // Returns selected index or -1 on empty input/escape.
int menu_run_simple(const string introText, string[]@ options, bool wrap = true, int startIndex = 0, const string soundDir = "sounds/menu") { int menu_run_simple(const string introText, string[] @options, bool wrap = true, int startIndex = 0,
if (@options is null || options.length() == 0) { const string soundDir = "sounds/menu") {
return -1; if (@options is null || options.length() == 0) {
} return -1;
}
menu menuRef; menu menuRef;
menu_apply_default_sounds(menuRef, soundDir); menu_apply_default_sounds(menuRef, soundDir);
menuRef.intro_text = introText; menuRef.intro_text = introText;
menuRef.wrap = wrap; menuRef.wrap = wrap;
menuRef.focus_first_item = true; menuRef.focus_first_item = true;
menuRef.add_items(options); menuRef.add_items(options);
if (startIndex >= 0 && startIndex < int(options.length())) { if (startIndex >= 0 && startIndex < int(options.length())) {
menuRef.focused_item = startIndex; menuRef.focused_item = startIndex;
} }
return menuRef.run(); return menuRef.run();
} }
// Returns a-z for menu prefix filtering, or empty string when no letter key was pressed. // Returns a-z for menu prefix filtering, or empty string when no letter key was pressed.
string menu_get_filter_letter() { string menu_get_filter_letter() {
if (key_pressed(KEY_A)) return "a"; if (key_pressed(KEY_A))
if (key_pressed(KEY_B)) return "b"; return "a";
if (key_pressed(KEY_C)) return "c"; if (key_pressed(KEY_B))
if (key_pressed(KEY_D)) return "d"; return "b";
if (key_pressed(KEY_E)) return "e"; if (key_pressed(KEY_C))
if (key_pressed(KEY_F)) return "f"; return "c";
if (key_pressed(KEY_G)) return "g"; if (key_pressed(KEY_D))
if (key_pressed(KEY_H)) return "h"; return "d";
if (key_pressed(KEY_I)) return "i"; if (key_pressed(KEY_E))
if (key_pressed(KEY_J)) return "j"; return "e";
if (key_pressed(KEY_K)) return "k"; if (key_pressed(KEY_F))
if (key_pressed(KEY_L)) return "l"; return "f";
if (key_pressed(KEY_M)) return "m"; if (key_pressed(KEY_G))
if (key_pressed(KEY_N)) return "n"; return "g";
if (key_pressed(KEY_O)) return "o"; if (key_pressed(KEY_H))
if (key_pressed(KEY_P)) return "p"; return "h";
if (key_pressed(KEY_Q)) return "q"; if (key_pressed(KEY_I))
if (key_pressed(KEY_R)) return "r"; return "i";
if (key_pressed(KEY_S)) return "s"; if (key_pressed(KEY_J))
if (key_pressed(KEY_T)) return "t"; return "j";
if (key_pressed(KEY_U)) return "u"; if (key_pressed(KEY_K))
if (key_pressed(KEY_V)) return "v"; return "k";
if (key_pressed(KEY_W)) return "w"; if (key_pressed(KEY_L))
if (key_pressed(KEY_X)) return "x"; return "l";
if (key_pressed(KEY_Y)) return "y"; if (key_pressed(KEY_M))
if (key_pressed(KEY_Z)) return "z"; return "m";
return ""; if (key_pressed(KEY_N))
return "n";
if (key_pressed(KEY_O))
return "o";
if (key_pressed(KEY_P))
return "p";
if (key_pressed(KEY_Q))
return "q";
if (key_pressed(KEY_R))
return "r";
if (key_pressed(KEY_S))
return "s";
if (key_pressed(KEY_T))
return "t";
if (key_pressed(KEY_U))
return "u";
if (key_pressed(KEY_V))
return "v";
if (key_pressed(KEY_W))
return "w";
if (key_pressed(KEY_X))
return "x";
if (key_pressed(KEY_Y))
return "y";
if (key_pressed(KEY_Z))
return "z";
return "";
} }
// Applies a prefix filter to menu options. // Applies a prefix filter to menu options.
void menu_apply_prefix_filter(const string &in filterText, const string[]@ options, int[]@ filteredIndices, string[]@ filteredOptions) { void menu_apply_prefix_filter(const string& in filterText, const string[] @options, int[] @filteredIndices,
filteredIndices.resize(0); string[] @filteredOptions) {
filteredOptions.resize(0); filteredIndices.resize(0);
filteredOptions.resize(0);
if (@options is null) { if (@options is null) {
return; return;
} }
string filterLower = filterText.lower(); string filterLower = filterText.lower();
for (uint optionIndex = 0; optionIndex < options.length(); optionIndex++) { for (uint optionIndex = 0; optionIndex < options.length(); optionIndex++) {
if (filterLower.length() == 0) { if (filterLower.length() == 0) {
filteredIndices.insert_last(optionIndex); filteredIndices.insert_last(optionIndex);
filteredOptions.insert_last(options[optionIndex]); filteredOptions.insert_last(options[optionIndex]);
continue; continue;
} }
string optionLower = options[optionIndex].lower(); string optionLower = options[optionIndex].lower();
if (optionLower.length() >= filterLower.length() && optionLower.substr(0, filterLower.length()) == filterLower) { if (optionLower.length() >= filterLower.length() &&
filteredIndices.insert_last(optionIndex); optionLower.substr(0, filterLower.length()) == filterLower) {
filteredOptions.insert_last(options[optionIndex]); filteredIndices.insert_last(optionIndex);
} filteredOptions.insert_last(options[optionIndex]);
} }
}
} }
// Updates filter text from keyboard input and reapplies filtering. // Updates filter text from keyboard input and reapplies filtering.
bool menu_update_prefix_filter(string &inout filterText, const string[]@ options, int[]@ filteredIndices, string[]@ filteredOptions, int &inout selection) { bool menu_update_prefix_filter(string& inout filterText, const string[] @options, int[] @filteredIndices,
bool filterChanged = false; string[] @filteredOptions, int& inout selection) {
bool filterChanged = false;
if (key_pressed(KEY_BACK) && filterText.length() > 0) { if (key_pressed(KEY_BACK) && filterText.length() > 0) {
filterText = filterText.substr(0, filterText.length() - 1); filterText = filterText.substr(0, filterText.length() - 1);
filterChanged = true; filterChanged = true;
} }
string filterLetter = menu_get_filter_letter(); string filterLetter = menu_get_filter_letter();
if (filterLetter != "") { if (filterLetter != "") {
filterText += filterLetter; filterText += filterLetter;
filterChanged = true; filterChanged = true;
} }
if (filterChanged) { if (filterChanged) {
menu_apply_prefix_filter(filterText, options, filteredIndices, filteredOptions); menu_apply_prefix_filter(filterText, options, filteredIndices, filteredOptions);
if (selection < 0) { if (selection < 0) {
selection = 0; selection = 0;
} }
if (selection >= int(filteredOptions.length())) { if (selection >= int(filteredOptions.length())) {
selection = 0; selection = 0;
} }
} }
return filterChanged; return filterChanged;
} }

View File

@@ -1,30 +1,30 @@
#include "music.nvgt" #include "music.nvgt"
// Resume paused menu music when possible, otherwise start a new track. // Resume paused menu music when possible, otherwise start a new track.
void menu_music_resume_or_play(music_manager &inout manager, const string track, const int fadeInMs = 0) { void menu_music_resume_or_play(music_manager& inout manager, const string track, const int fadeInMs = 0) {
if (manager.resume(fadeInMs)) { if (manager.resume(fadeInMs)) {
return; return;
} }
if (manager.playing) { if (manager.playing) {
// Normalize state before fading back up. // Normalize state before fading back up.
manager.pause(0, true); manager.pause(0, true);
manager.resume(fadeInMs); manager.resume(fadeInMs);
return; return;
} }
manager.play(track); manager.play(track);
} }
// Pause menu music. Blocking is enabled by default so fade-out fully completes. // Pause menu music. Blocking is enabled by default so fade-out fully completes.
bool menu_music_pause(music_manager &inout manager, const int fadeOutMs = 0, const bool blocking = true) { bool menu_music_pause(music_manager& inout manager, const int fadeOutMs = 0, const bool blocking = true) {
if (!manager.playing) { if (!manager.playing) {
return false; return false;
} }
return manager.pause(fadeOutMs, blocking); return manager.pause(fadeOutMs, blocking);
} }
// Stop menu music and reset playback state. // Stop menu music and reset playback state.
void menu_music_stop(music_manager &inout manager, const int fadeOutMs = 0, const bool blocking = false) { void menu_music_stop(music_manager& inout manager, const int fadeOutMs = 0, const bool blocking = false) {
manager.stop(fadeOutMs, blocking); manager.stop(fadeOutMs, blocking);
} }

View File

@@ -1,5 +1,5 @@
// Multikey input helpers. // Multikey input helpers.
bool check_key_down(array<int>@ keys) { bool check_key_down(array<int> @keys) {
// True when at least one key in the set is currently down. // True when at least one key in the set is currently down.
if (keys is null || keys.length() == 0) { if (keys is null || keys.length() == 0) {
return false; return false;
@@ -14,7 +14,7 @@ bool check_key_down(array<int>@ keys) {
return false; return false;
} }
bool check_all_keys(array<int>@ keys) { bool check_all_keys(array<int> @keys) {
// True only when every key in the set is currently down. // True only when every key in the set is currently down.
if (keys is null || keys.length() == 0) { if (keys is null || keys.length() == 0) {
return false; return false;

View File

@@ -1,101 +1,105 @@
// Name and filename sanitizing helpers. // Name and filename sanitizing helpers.
string normalize_name_whitespace(string name) { string normalize_name_whitespace(string name) {
string result = ""; string result = "";
bool lastWasSpace = true; bool lastWasSpace = true;
for (uint charIndex = 0; charIndex < name.length(); charIndex++) { for (uint charIndex = 0; charIndex < name.length(); charIndex++) {
string ch = name.substr(charIndex, 1); string ch = name.substr(charIndex, 1);
bool isSpace = (ch == " " || ch == "\t" || ch == "\r" || ch == "\n"); bool isSpace = (ch == " " || ch == "\t" || ch == "\r" || ch == "\n");
if (isSpace) { if (isSpace) {
if (!lastWasSpace) { if (!lastWasSpace) {
result += " "; result += " ";
lastWasSpace = true; lastWasSpace = true;
} }
continue; continue;
} }
result += ch; result += ch;
lastWasSpace = false; lastWasSpace = false;
} }
if (result.length() > 0 && result.substr(result.length() - 1) == " ") { if (result.length() > 0 && result.substr(result.length() - 1) == " ") {
result = result.substr(0, result.length() - 1); result = result.substr(0, result.length() - 1);
} }
return result; return result;
} }
bool is_windows_reserved_filename(const string&in upperName) { bool is_windows_reserved_filename(const string& in upperName) {
if (upperName == "CON" || upperName == "PRN" || upperName == "AUX" || upperName == "NUL") return true; if (upperName == "CON" || upperName == "PRN" || upperName == "AUX" || upperName == "NUL")
return true;
if (upperName.length() == 4 && upperName.substr(0, 3) == "COM") { if (upperName.length() == 4 && upperName.substr(0, 3) == "COM") {
int num = parse_int(upperName.substr(3)); int num = parse_int(upperName.substr(3));
if (num >= 1 && num <= 9) return true; if (num >= 1 && num <= 9)
} return true;
}
if (upperName.length() == 4 && upperName.substr(0, 3) == "LPT") { if (upperName.length() == 4 && upperName.substr(0, 3) == "LPT") {
int num = parse_int(upperName.substr(3)); int num = parse_int(upperName.substr(3));
if (num >= 1 && num <= 9) return true; if (num >= 1 && num <= 9)
} return true;
}
return false; return false;
} }
string sanitize_filename_base(string name, const int maxLength = 40, const string fallback = "item") { string sanitize_filename_base(string name, const int maxLength = 40, const string fallback = "item") {
string normalized = normalize_name_whitespace(name); string normalized = normalize_name_whitespace(name);
string result = ""; string result = "";
bool lastSeparator = false; bool lastSeparator = false;
for (uint charIndex = 0; charIndex < normalized.length(); charIndex++) { for (uint charIndex = 0; charIndex < normalized.length(); charIndex++) {
string ch = normalized.substr(charIndex, 1); string ch = normalized.substr(charIndex, 1);
bool isUpper = (ch >= "A" && ch <= "Z"); bool isUpper = (ch >= "A" && ch <= "Z");
bool isLower = (ch >= "a" && ch <= "z"); bool isLower = (ch >= "a" && ch <= "z");
bool isDigit = (ch >= "0" && ch <= "9"); bool isDigit = (ch >= "0" && ch <= "9");
if (isUpper || isLower || isDigit) { if (isUpper || isLower || isDigit) {
result += ch; result += ch;
lastSeparator = false; lastSeparator = false;
continue; continue;
} }
if (ch == " " || ch == "_" || ch == "-") { if (ch == " " || ch == "_" || ch == "-") {
if (!lastSeparator && result.length() > 0) { if (!lastSeparator && result.length() > 0) {
result += "_"; result += "_";
lastSeparator = true; lastSeparator = true;
} }
} }
} }
while (result.length() > 0 && result.substr(result.length() - 1) == "_") { while (result.length() > 0 && result.substr(result.length() - 1) == "_") {
result = result.substr(0, result.length() - 1); result = result.substr(0, result.length() - 1);
} }
if (result.length() == 0) { if (result.length() == 0) {
result = fallback; result = fallback;
} }
if (result.length() > maxLength) { if (result.length() > maxLength) {
result = result.substr(0, maxLength); result = result.substr(0, maxLength);
} }
while (result.length() > 0 && result.substr(result.length() - 1) == "_") { while (result.length() > 0 && result.substr(result.length() - 1) == "_") {
result = result.substr(0, result.length() - 1); result = result.substr(0, result.length() - 1);
} }
string upperName = result.upper(); string upperName = result.upper();
if (upperName == "." || upperName == "..") { if (upperName == "." || upperName == "..") {
result = fallback; result = fallback;
upperName = result.upper(); upperName = result.upper();
} }
if (is_windows_reserved_filename(upperName)) { if (is_windows_reserved_filename(upperName)) {
result = "file_" + result; result = "file_" + result;
} }
return result; return result;
} }
string build_filename_from_name(const string&in name, const string&in extension = ".dat", const int maxBaseLength = 40, const string fallback = "item") { string build_filename_from_name(const string& in name, const string& in extension = ".dat",
return sanitize_filename_base(name, maxBaseLength, fallback) + extension; const int maxBaseLength = 40, const string fallback = "item") {
return sanitize_filename_base(name, maxBaseLength, fallback) + extension;
} }

View File

@@ -1,6 +1,6 @@
#include "audio_paths.nvgt" #include "audio_paths.nvgt"
funcdef void notification_speak_callback(const string &in message, bool interrupt); funcdef void notification_speak_callback(const string& in message, bool interrupt);
string[] notificationsHistory; string[] notificationsHistory;
string[] notificationsQueue; string[] notificationsQueue;
@@ -13,163 +13,176 @@ timer notificationsDelayTimer;
sound notificationsSound; sound notificationsSound;
bool notificationsWaitingForSound = false; bool notificationsWaitingForSound = false;
string notificationsSoundPath = "sounds/notify"; string notificationsSoundPath = "sounds/notify";
notification_speak_callback@ notificationsSpeakCallback = null; notification_speak_callback @notificationsSpeakCallback = null;
void notifications_set_speak_callback(notification_speak_callback@ callback) { void notifications_set_speak_callback(notification_speak_callback @callback) {
@notificationsSpeakCallback = @callback; @notificationsSpeakCallback = @callback;
} }
void notifications_set_sound_path(const string soundPath) { void notifications_set_sound_path(const string soundPath) {
notificationsSoundPath = soundPath; notificationsSoundPath = soundPath;
} }
void notifications_set_delay_ms(int delayMs) { void notifications_set_delay_ms(int delayMs) {
if (delayMs < 0) { if (delayMs < 0) {
delayMs = 0; delayMs = 0;
} }
notificationsDelayMs = delayMs; notificationsDelayMs = delayMs;
} }
void notifications_set_max_history(int maxCount) { void notifications_set_max_history(int maxCount) {
if (maxCount < 1) { if (maxCount < 1) {
maxCount = 1; maxCount = 1;
} }
notificationsMaxHistory = maxCount; notificationsMaxHistory = maxCount;
while (notificationsHistory.length() > uint(notificationsMaxHistory)) { while (notificationsHistory.length() > uint(notificationsMaxHistory)) {
notificationsHistory.remove_at(0); notificationsHistory.remove_at(0);
} }
if (notificationsHistory.length() == 0) { if (notificationsHistory.length() == 0) {
notificationsCurrentIndex = -1; notificationsCurrentIndex = -1;
} else if (notificationsCurrentIndex >= int(notificationsHistory.length())) { } else if (notificationsCurrentIndex >= int(notificationsHistory.length())) {
notificationsCurrentIndex = notificationsHistory.length() - 1; notificationsCurrentIndex = notificationsHistory.length() - 1;
} }
} }
void notifications_clear() { void notifications_clear() {
notificationsHistory.resize(0); notificationsHistory.resize(0);
notificationsQueue.resize(0); notificationsQueue.resize(0);
notificationsCurrentIndex = -1; notificationsCurrentIndex = -1;
notificationsActive = false; notificationsActive = false;
notificationsWaitingForSound = false; notificationsWaitingForSound = false;
notificationsSound.close(); notificationsSound.close();
} }
void notifications_speak(const string &in message, bool interrupt) { void notifications_speak(const string& in message, bool interrupt) {
if (@notificationsSpeakCallback !is null) { if (@notificationsSpeakCallback !is null) {
notificationsSpeakCallback(message, interrupt); notificationsSpeakCallback(message, interrupt);
return; return;
} }
screen_reader_speak(message, interrupt); screen_reader_speak(message, interrupt);
} }
void notifications_enqueue(const string &in message) { void notifications_enqueue(const string& in message) {
notificationsQueue.insert_last(message); notificationsQueue.insert_last(message);
notificationsHistory.insert_last(message); notificationsHistory.insert_last(message);
while (notificationsHistory.length() > uint(notificationsMaxHistory)) { while (notificationsHistory.length() > uint(notificationsMaxHistory)) {
notificationsHistory.remove_at(0); notificationsHistory.remove_at(0);
} }
notificationsCurrentIndex = notificationsHistory.length() - 1; notificationsCurrentIndex = notificationsHistory.length() - 1;
} }
bool notifications_try_play_sound() { bool notifications_try_play_sound() {
string resolvedPath = resolve_audio_path(notificationsSoundPath); notificationsSound.close();
if (resolvedPath == "") {
return false;
}
notificationsSound.close(); // Avoid file_exists()-only checks so packaged Android assets can still load.
if (!notificationsSound.load(resolvedPath)) { if (notificationsSound.load(notificationsSoundPath)) {
return false; notificationsSound.play();
} return true;
}
notificationsSound.play(); string oggPath = notificationsSoundPath + ".ogg";
return true; if (notificationsSound.load(oggPath)) {
notificationsSound.play();
return true;
}
string wavPath = notificationsSoundPath + ".wav";
if (!notificationsSound.load(wavPath)) {
return false;
}
notificationsSound.play();
return true;
} }
void notifications_update() { void notifications_update() {
if (notificationsQueue.length() == 0) { if (notificationsQueue.length() == 0) {
if (notificationsActive && notificationsDelayTimer.elapsed >= notificationsDelayMs) { if (notificationsActive && notificationsDelayTimer.elapsed >= notificationsDelayMs) {
notificationsActive = false; notificationsActive = false;
} }
return; return;
} }
if (notificationsActive && notificationsDelayTimer.elapsed < notificationsDelayMs) { if (notificationsActive && notificationsDelayTimer.elapsed < notificationsDelayMs) {
return; return;
} }
if (notificationsWaitingForSound) { if (notificationsWaitingForSound) {
if (notificationsSound.playing) { if (notificationsSound.playing) {
return; return;
} }
notifications_speak(notificationsQueue[0], true); notifications_speak(notificationsQueue[0], true);
notificationsQueue.remove_at(0); notificationsQueue.remove_at(0);
notificationsWaitingForSound = false; notificationsWaitingForSound = false;
notificationsActive = true; notificationsActive = true;
notificationsDelayTimer.restart(); notificationsDelayTimer.restart();
return; return;
} }
if (notifications_try_play_sound()) { if (notifications_try_play_sound()) {
notificationsWaitingForSound = true; notificationsWaitingForSound = true;
return; return;
} }
notifications_speak(notificationsQueue[0], true); notifications_speak(notificationsQueue[0], true);
notificationsQueue.remove_at(0); notificationsQueue.remove_at(0);
notificationsActive = true; notificationsActive = true;
notificationsDelayTimer.restart(); notificationsDelayTimer.restart();
} }
void notifications_check_keys() { void notifications_check_keys() {
if (key_pressed(KEY_LEFTBRACKET)) { if (key_pressed(KEY_LEFTBRACKET)) {
if (notificationsHistory.length() == 0) { if (notificationsHistory.length() == 0) {
notifications_speak("No notifications.", true); notifications_speak("No notifications.", true);
return; return;
} }
notificationsCurrentIndex--; notificationsCurrentIndex--;
if (notificationsCurrentIndex < 0) { if (notificationsCurrentIndex < 0) {
notificationsCurrentIndex = 0; notificationsCurrentIndex = 0;
notifications_speak("Oldest notification. " + notificationsHistory[notificationsCurrentIndex], true); notifications_speak("Oldest notification. " + notificationsHistory[notificationsCurrentIndex], true);
return; return;
} }
int position = notificationsCurrentIndex + 1; int position = notificationsCurrentIndex + 1;
notifications_speak(notificationsHistory[notificationsCurrentIndex] + " " + position + " of " + notificationsHistory.length(), true); notifications_speak(notificationsHistory[notificationsCurrentIndex] + " " + position + " of " +
return; notificationsHistory.length(),
} true);
return;
}
if (key_pressed(KEY_RIGHTBRACKET)) { if (key_pressed(KEY_RIGHTBRACKET)) {
if (notificationsHistory.length() == 0) { if (notificationsHistory.length() == 0) {
notifications_speak("No notifications.", true); notifications_speak("No notifications.", true);
return; return;
} }
notificationsCurrentIndex++; notificationsCurrentIndex++;
if (notificationsCurrentIndex >= int(notificationsHistory.length())) { if (notificationsCurrentIndex >= int(notificationsHistory.length())) {
notificationsCurrentIndex = notificationsHistory.length() - 1; notificationsCurrentIndex = notificationsHistory.length() - 1;
notifications_speak("Newest notification. " + notificationsHistory[notificationsCurrentIndex], true); notifications_speak("Newest notification. " + notificationsHistory[notificationsCurrentIndex], true);
return; return;
} }
int position = notificationsCurrentIndex + 1; int position = notificationsCurrentIndex + 1;
notifications_speak(notificationsHistory[notificationsCurrentIndex] + " " + position + " of " + notificationsHistory.length(), true); notifications_speak(notificationsHistory[notificationsCurrentIndex] + " " + position + " of " +
return; notificationsHistory.length(),
} true);
return;
}
if (key_pressed(KEY_BACKSLASH)) { if (key_pressed(KEY_BACKSLASH)) {
if (notificationsHistory.length() == 0) { if (notificationsHistory.length() == 0) {
notifications_speak("No notifications.", true); notifications_speak("No notifications.", true);
return; return;
} }
notificationsCurrentIndex = notificationsHistory.length() - 1; notificationsCurrentIndex = notificationsHistory.length() - 1;
notifications_speak(notificationsHistory[notificationsCurrentIndex], true); notifications_speak(notificationsHistory[notificationsCurrentIndex], true);
} }
} }

View File

@@ -1,101 +1,102 @@
// Generic save/file helpers. // Generic save/file helpers.
bool save_utils_sort_string_case_insensitive(const string &in a, const string &in b) { bool save_utils_sort_string_case_insensitive(const string& in a, const string& in b) {
return a.lower() < b.lower(); return a.lower() < b.lower();
} }
string[] list_files_with_extension(const string&in extension, const string&in directory = "") { string[] list_files_with_extension(const string& in extension, const string& in directory = "") {
string[] result; string[] result;
if (extension == "") return result; if (extension == "")
return result;
string dirPrefix = directory; string dirPrefix = directory;
if (dirPrefix != "" && dirPrefix.substr(dirPrefix.length() - 1) != "/") { if (dirPrefix != "" && dirPrefix.substr(dirPrefix.length() - 1) != "/") {
dirPrefix += "/"; dirPrefix += "/";
} }
string[]@ items = glob(dirPrefix + "*" + extension); string[] @items = glob(dirPrefix + "*" + extension);
if (@items is null) return result; if (@items is null)
return result;
for (uint itemIndex = 0; itemIndex < items.length(); itemIndex++) { for (uint itemIndex = 0; itemIndex < items.length(); itemIndex++) {
string item = items[itemIndex]; string item = items[itemIndex];
if (item.length() >= extension.length() && if (item.length() >= extension.length() && item.substr(item.length() - extension.length()) == extension) {
item.substr(item.length() - extension.length()) == extension) { result.insert_last(item);
result.insert_last(item); }
} }
}
if (result.length() > 1) { if (result.length() > 1) {
result.sort(save_utils_sort_string_case_insensitive); result.sort(save_utils_sort_string_case_insensitive);
} }
return result; return result;
} }
bool has_files_with_extension(const string&in extension, const string&in directory = "") { bool has_files_with_extension(const string& in extension, const string& in directory = "") {
return list_files_with_extension(extension, directory).length() > 0; return list_files_with_extension(extension, directory).length() > 0;
} }
string strip_file_extension(const string&in filename, const string&in extension) { string strip_file_extension(const string& in filename, const string& in extension) {
if (extension != "" && if (extension != "" && filename.length() >= extension.length() &&
filename.length() >= extension.length() && filename.substr(filename.length() - extension.length()) == extension) {
filename.substr(filename.length() - extension.length()) == extension) { return filename.substr(0, filename.length() - extension.length());
return filename.substr(0, filename.length() - extension.length()); }
} return filename;
return filename;
} }
bool save_string_file(const string&in filename, const string&in data) { bool save_string_file(const string& in filename, const string& in data) {
if (data.length() == 0) { if (data.length() == 0) {
return false; return false;
} }
file outFile; file outFile;
if (!outFile.open(filename, "wb")) { if (!outFile.open(filename, "wb")) {
return false; return false;
} }
if (outFile.write(data) < data.length()) { if (outFile.write(data) < data.length()) {
outFile.close(); outFile.close();
return false; return false;
} }
outFile.close(); outFile.close();
return true; return true;
} }
bool read_string_file(const string&in filename, string&out data, const bool allowEmpty = false) { bool read_string_file(const string& in filename, string& out data, const bool allowEmpty = false) {
file inFile; file inFile;
if (!inFile.open(filename, "rb")) { if (!inFile.open(filename, "rb")) {
return false; return false;
} }
data = inFile.read(); data = inFile.read();
inFile.close(); inFile.close();
return allowEmpty || data.length() > 0; return allowEmpty || data.length() > 0;
} }
string encrypt_string_aes(const string&in rawData, const string&in key) { string encrypt_string_aes(const string& in rawData, const string& in key) {
return string_aes_encrypt(rawData, key); return string_aes_encrypt(rawData, key);
} }
string decrypt_string_aes(const string&in encryptedData, const string&in key) { string decrypt_string_aes(const string& in encryptedData, const string& in key) {
return string_aes_decrypt(encryptedData, key); return string_aes_decrypt(encryptedData, key);
} }
bool save_encrypted_file(const string&in filename, const string&in rawData, const string&in key) { bool save_encrypted_file(const string& in filename, const string& in rawData, const string& in key) {
string encryptedData = encrypt_string_aes(rawData, key); string encryptedData = encrypt_string_aes(rawData, key);
return save_string_file(filename, encryptedData); return save_string_file(filename, encryptedData);
} }
bool read_encrypted_file(const string&in filename, const string&in key, string&out rawData, const bool allowPlaintextFallback = true) { bool read_encrypted_file(const string& in filename, const string& in key, string& out rawData,
string encryptedData = ""; const bool allowPlaintextFallback = true) {
if (!read_string_file(filename, encryptedData, false)) { string encryptedData = "";
return false; if (!read_string_file(filename, encryptedData, false)) {
} return false;
}
rawData = decrypt_string_aes(encryptedData, key); rawData = decrypt_string_aes(encryptedData, key);
if (rawData.length() == 0 && allowPlaintextFallback) { if (rawData.length() == 0 && allowPlaintextFallback) {
rawData = encryptedData; rawData = encryptedData;
} }
return rawData.length() > 0; return rawData.length() > 0;
} }

View File

@@ -7,100 +7,102 @@ int speechHistoryCurrentIndex = -1;
bool speechHistoryDeduplicate = true; bool speechHistoryDeduplicate = true;
void speech_history_set_max_entries(int maxEntries) { void speech_history_set_max_entries(int maxEntries) {
if (maxEntries < 1) { if (maxEntries < 1) {
maxEntries = 1; maxEntries = 1;
} }
speechHistoryMaxEntries = maxEntries; speechHistoryMaxEntries = maxEntries;
while (speechHistory.length() > uint(speechHistoryMaxEntries)) { while (speechHistory.length() > uint(speechHistoryMaxEntries)) {
speechHistory.remove_at(0); speechHistory.remove_at(0);
} }
if (speechHistory.length() == 0) { if (speechHistory.length() == 0) {
speechHistoryCurrentIndex = -1; speechHistoryCurrentIndex = -1;
} else if (speechHistoryCurrentIndex >= int(speechHistory.length())) { } else if (speechHistoryCurrentIndex >= int(speechHistory.length())) {
speechHistoryCurrentIndex = speechHistory.length() - 1; speechHistoryCurrentIndex = speechHistory.length() - 1;
} }
} }
void speech_history_clear() { void speech_history_clear() {
speechHistory.resize(0); speechHistory.resize(0);
speechHistoryCurrentIndex = -1; speechHistoryCurrentIndex = -1;
} }
void speech_history_set_deduplicate(bool deduplicate) { void speech_history_set_deduplicate(bool deduplicate) {
speechHistoryDeduplicate = deduplicate; speechHistoryDeduplicate = deduplicate;
} }
void speech_history_add(const string &in message) { void speech_history_add(const string& in message) {
if (message == "") { if (message == "") {
return; return;
} }
if (speechHistoryDeduplicate) { if (speechHistoryDeduplicate) {
for (uint messageIndex = 0; messageIndex < speechHistory.length(); messageIndex++) { for (uint messageIndex = 0; messageIndex < speechHistory.length(); messageIndex++) {
if (speechHistory[messageIndex] == message) { if (speechHistory[messageIndex] == message) {
speechHistoryCurrentIndex = messageIndex; speechHistoryCurrentIndex = messageIndex;
return; return;
} }
} }
} }
speechHistory.insert_last(message); speechHistory.insert_last(message);
while (speechHistory.length() > uint(speechHistoryMaxEntries)) { while (speechHistory.length() > uint(speechHistoryMaxEntries)) {
speechHistory.remove_at(0); speechHistory.remove_at(0);
} }
speechHistoryCurrentIndex = speechHistory.length() - 1; speechHistoryCurrentIndex = speechHistory.length() - 1;
} }
void speak_with_history(const string &in message, bool interrupt) { void speak_with_history(const string& in message, bool interrupt) {
speech_history_add(message); speech_history_add(message);
screen_reader_speak(message, interrupt); screen_reader_speak(message, interrupt);
} }
void check_speech_history_keys() { void check_speech_history_keys() {
// Comma: older. // Comma: older.
if (key_pressed(KEY_COMMA)) { if (key_pressed(KEY_COMMA)) {
if (speechHistory.length() == 0) { if (speechHistory.length() == 0) {
screen_reader_speak("No speech history.", true); screen_reader_speak("No speech history.", true);
return; return;
} }
speechHistoryCurrentIndex--; speechHistoryCurrentIndex--;
if (speechHistoryCurrentIndex < 0) { if (speechHistoryCurrentIndex < 0) {
speechHistoryCurrentIndex = 0; speechHistoryCurrentIndex = 0;
screen_reader_speak("Oldest message. " + speechHistory[speechHistoryCurrentIndex], true); screen_reader_speak("Oldest message. " + speechHistory[speechHistoryCurrentIndex], true);
return; return;
} }
int position = speechHistoryCurrentIndex + 1; int position = speechHistoryCurrentIndex + 1;
screen_reader_speak(speechHistory[speechHistoryCurrentIndex] + " " + position + " of " + speechHistory.length(), true); screen_reader_speak(speechHistory[speechHistoryCurrentIndex] + " " + position + " of " + speechHistory.length(),
return; true);
} return;
}
// Period: newer. // Period: newer.
if (key_pressed(KEY_PERIOD)) { if (key_pressed(KEY_PERIOD)) {
if (speechHistory.length() == 0) { if (speechHistory.length() == 0) {
screen_reader_speak("No speech history.", true); screen_reader_speak("No speech history.", true);
return; return;
} }
speechHistoryCurrentIndex++; speechHistoryCurrentIndex++;
if (speechHistoryCurrentIndex >= int(speechHistory.length())) { if (speechHistoryCurrentIndex >= int(speechHistory.length())) {
speechHistoryCurrentIndex = speechHistory.length() - 1; speechHistoryCurrentIndex = speechHistory.length() - 1;
screen_reader_speak("Newest message. " + speechHistory[speechHistoryCurrentIndex], true); screen_reader_speak("Newest message. " + speechHistory[speechHistoryCurrentIndex], true);
return; return;
} }
int position = speechHistoryCurrentIndex + 1; int position = speechHistoryCurrentIndex + 1;
screen_reader_speak(speechHistory[speechHistoryCurrentIndex] + " " + position + " of " + speechHistory.length(), true); screen_reader_speak(speechHistory[speechHistoryCurrentIndex] + " " + position + " of " + speechHistory.length(),
} true);
}
} }
string speech_history_latest() { string speech_history_latest() {
if (speechHistory.length() == 0) { if (speechHistory.length() == 0) {
return ""; return "";
} }
return speechHistory[speechHistory.length() - 1]; return speechHistory[speechHistory.length() - 1];
} }

View File

@@ -2,13 +2,13 @@
// Optional compatibility aliases for projects that still call text_reader*. // Optional compatibility aliases for projects that still call text_reader*.
string text_reader(string content, string title = "Text Reader", bool readOnly = true) { string text_reader(string content, string title = "Text Reader", bool readOnly = true) {
return file_viewer(content, title, readOnly); return file_viewer(content, title, readOnly);
} }
string text_reader_lines(string[] lines, string title = "Text Reader", bool readOnly = true) { string text_reader_lines(string[] lines, string title = "Text Reader", bool readOnly = true) {
return file_viewer_lines(lines, title, readOnly); return file_viewer_lines(lines, title, readOnly);
} }
string text_reader_file(string filePath, string title = "", bool readOnly = true) { string text_reader_file(string filePath, string title = "", bool readOnly = true) {
return file_viewer_file(filePath, title, readOnly); return file_viewer_file(filePath, title, readOnly);
} }

57
ui.nvgt
View File

@@ -4,51 +4,52 @@ string uiDefaultWindowTitle = "";
bool uiUsePromptAsDialogTitle = true; bool uiUsePromptAsDialogTitle = true;
void ui_set_default_window_title(const string windowTitle) { void ui_set_default_window_title(const string windowTitle) {
uiDefaultWindowTitle = windowTitle; uiDefaultWindowTitle = windowTitle;
} }
void ui_set_use_prompt_as_dialog_title(const bool enabled) { void ui_set_use_prompt_as_dialog_title(const bool enabled) {
uiUsePromptAsDialogTitle = enabled; uiUsePromptAsDialogTitle = enabled;
} }
string ui_resolve_dialog_title(const string title, const string prompt) { string ui_resolve_dialog_title(const string title, const string prompt) {
if (uiUsePromptAsDialogTitle && prompt != "") { if (uiUsePromptAsDialogTitle && prompt != "") {
return prompt; return prompt;
} }
string dialogTitle = title; string dialogTitle = title;
if (dialogTitle == "") { if (dialogTitle == "") {
dialogTitle = prompt; dialogTitle = prompt;
} }
return dialogTitle; return dialogTitle;
} }
void ui_restore_window(const string windowTitle = "") { void ui_restore_window(const string windowTitle = "") {
string resolvedTitle = windowTitle; string resolvedTitle = windowTitle;
if (resolvedTitle == "") { if (resolvedTitle == "") {
resolvedTitle = uiDefaultWindowTitle; resolvedTitle = uiDefaultWindowTitle;
} }
if (resolvedTitle != "") { if (resolvedTitle != "") {
show_window(resolvedTitle); show_window(resolvedTitle);
} }
} }
string ui_input_box(const string title, const string prompt, const string defaultValue = "", const string windowTitle = "") { string ui_input_box(const string title, const string prompt, const string defaultValue = "",
string dialogTitle = ui_resolve_dialog_title(title, prompt); const string windowTitle = "") {
string result = virtual_input_box(dialogTitle, prompt, defaultValue); string dialogTitle = ui_resolve_dialog_title(title, prompt);
ui_restore_window(windowTitle); string result = virtual_input_box(dialogTitle, prompt, defaultValue);
return result; ui_restore_window(windowTitle);
return result;
} }
int ui_question(const string title, const string prompt, const string windowTitle = "", const bool canCancel = false) { int ui_question(const string title, const string prompt, const string windowTitle = "", const bool canCancel = false) {
string dialogTitle = ui_resolve_dialog_title(title, prompt); string dialogTitle = ui_resolve_dialog_title(title, prompt);
int result = virtual_question(dialogTitle, prompt, canCancel); int result = virtual_question(dialogTitle, prompt, canCancel);
ui_restore_window(windowTitle); ui_restore_window(windowTitle);
return result; return result;
} }
void ui_info_box(const string title, const string heading, const string message, const string windowTitle = "") { void ui_info_box(const string title, const string heading, const string message, const string windowTitle = "") {
virtual_info_box(title, heading, message); virtual_info_box(title, heading, message);
ui_restore_window(windowTitle); ui_restore_window(windowTitle);
} }

View File

@@ -6,91 +6,98 @@ float volumeControlsMaxDb = 0.0f;
float volumeControlsMinDb = -60.0f; float volumeControlsMinDb = -60.0f;
float volumeControlsStepDb = 3.0f; float volumeControlsStepDb = 3.0f;
float volumeControlsCurrentDb = 0.0f; float volumeControlsCurrentDb = 0.0f;
volume_controls_apply_callback@ volumeControlsApplyCallback = null; volume_controls_apply_callback @volumeControlsApplyCallback = null;
void volume_controls_set_apply_callback(volume_controls_apply_callback@ callback) { void volume_controls_set_apply_callback(volume_controls_apply_callback @callback) {
@volumeControlsApplyCallback = @callback; @volumeControlsApplyCallback = @callback;
} }
void volume_controls_configure(float minDb = -60.0f, float maxDb = 0.0f, float stepDb = 3.0f, float initialDb = 0.0f) { void volume_controls_configure(float minDb = -60.0f, float maxDb = 0.0f, float stepDb = 3.0f, float initialDb = 0.0f) {
volumeControlsMinDb = minDb; volumeControlsMinDb = minDb;
volumeControlsMaxDb = maxDb; volumeControlsMaxDb = maxDb;
if (volumeControlsMaxDb < volumeControlsMinDb) { if (volumeControlsMaxDb < volumeControlsMinDb) {
float temp = volumeControlsMaxDb; float temp = volumeControlsMaxDb;
volumeControlsMaxDb = volumeControlsMinDb; volumeControlsMaxDb = volumeControlsMinDb;
volumeControlsMinDb = temp; volumeControlsMinDb = temp;
} }
if (stepDb <= 0.0f) { if (stepDb <= 0.0f) {
stepDb = 1.0f; stepDb = 1.0f;
} }
volumeControlsStepDb = stepDb; volumeControlsStepDb = stepDb;
volume_controls_set_current_db(initialDb, false); volume_controls_set_current_db(initialDb, false);
} }
int volume_controls_percent_from_db(float volumeDb) { int volume_controls_percent_from_db(float volumeDb) {
float range = volumeControlsMaxDb - volumeControlsMinDb; float range = volumeControlsMaxDb - volumeControlsMinDb;
if (range <= 0.0f) return 100; if (range <= 0.0f)
return 100;
float normalized = (volumeDb - volumeControlsMinDb) / range; float normalized = (volumeDb - volumeControlsMinDb) / range;
int volumePercent = int(normalized * 100.0f + 0.5f); int volumePercent = int(normalized * 100.0f + 0.5f);
if (volumePercent < 0) volumePercent = 0; if (volumePercent < 0)
if (volumePercent > 100) volumePercent = 100; volumePercent = 0;
return volumePercent; if (volumePercent > 100)
volumePercent = 100;
return volumePercent;
} }
void volume_controls_apply(float volumeDb) { void volume_controls_apply(float volumeDb) {
if (@volumeControlsApplyCallback !is null) { if (@volumeControlsApplyCallback !is null) {
volumeControlsApplyCallback(volumeDb); volumeControlsApplyCallback(volumeDb);
return; return;
} }
// Default engine-global apply behavior when no callback is provided. // Default engine-global apply behavior when no callback is provided.
sound_master_volume = volumeDb; sound_master_volume = volumeDb;
} }
void volume_controls_set_current_db(float volumeDb, bool announce = true) { void volume_controls_set_current_db(float volumeDb, bool announce = true) {
float clamped = volumeDb; float clamped = volumeDb;
if (clamped > volumeControlsMaxDb) clamped = volumeControlsMaxDb; if (clamped > volumeControlsMaxDb)
if (clamped < volumeControlsMinDb) clamped = volumeControlsMinDb; clamped = volumeControlsMaxDb;
if (clamped < volumeControlsMinDb)
clamped = volumeControlsMinDb;
if (clamped == volumeControlsCurrentDb) return; if (clamped == volumeControlsCurrentDb)
return;
volumeControlsCurrentDb = clamped; volumeControlsCurrentDb = clamped;
volume_controls_apply(volumeControlsCurrentDb); volume_controls_apply(volumeControlsCurrentDb);
if (announce) { if (announce) {
int volumePercent = volume_controls_percent_from_db(volumeControlsCurrentDb); int volumePercent = volume_controls_percent_from_db(volumeControlsCurrentDb);
screen_reader_speak("Volume " + volumePercent + ".", true); screen_reader_speak("Volume " + volumePercent + ".", true);
} }
} }
void volume_controls_init_at_max(bool announce = false) { void volume_controls_init_at_max(bool announce = false) {
volume_controls_set_current_db(volumeControlsMaxDb, announce); volume_controls_set_current_db(volumeControlsMaxDb, announce);
} }
float volume_controls_get_current_db() { float volume_controls_get_current_db() {
return volumeControlsCurrentDb; return volumeControlsCurrentDb;
} }
void volume_controls_handle_keys(int downKey = KEY_PAGEDOWN, int upKey = KEY_PAGEUP, bool announce = true) { void volume_controls_handle_keys(int downKey = KEY_PAGEDOWN, int upKey = KEY_PAGEUP, bool announce = true) {
if (key_pressed(downKey)) { if (key_pressed(downKey)) {
volume_controls_set_current_db(volumeControlsCurrentDb - volumeControlsStepDb, announce); volume_controls_set_current_db(volumeControlsCurrentDb - volumeControlsStepDb, announce);
} }
if (key_pressed(upKey)) { if (key_pressed(upKey)) {
volume_controls_set_current_db(volumeControlsCurrentDb + volumeControlsStepDb, announce); volume_controls_set_current_db(volumeControlsCurrentDb + volumeControlsStepDb, announce);
} }
} }
void safe_destroy_sound_in_pool(sound_pool@ poolRef, int &inout handle) { void safe_destroy_sound_in_pool(sound_pool @poolRef, int& inout handle) {
if (handle == -1) return; if (handle == -1)
if (@poolRef is null) { return;
handle = -1; if (@poolRef is null) {
return; handle = -1;
} return;
if (poolRef.sound_is_active(handle)) { }
poolRef.destroy_sound(handle); if (poolRef.sound_is_active(handle)) {
} poolRef.destroy_sound(handle);
handle = -1; }
handle = -1;
} }