Files
libstorm-nvgt/notifications.nvgt
2026-02-16 19:49:29 -05:00

176 lines
4.7 KiB
Plaintext

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