Initial commit, reusable code for my nvgt based games.
This commit is contained in:
96
volume_controls.nvgt
Normal file
96
volume_controls.nvgt
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "sound_pool.nvgt"
|
||||
|
||||
funcdef void volume_controls_apply_callback(float volumeDb);
|
||||
|
||||
float volumeControlsMaxDb = 0.0f;
|
||||
float volumeControlsMinDb = -60.0f;
|
||||
float volumeControlsStepDb = 3.0f;
|
||||
float volumeControlsCurrentDb = 0.0f;
|
||||
volume_controls_apply_callback@ volumeControlsApplyCallback = null;
|
||||
|
||||
void volume_controls_set_apply_callback(volume_controls_apply_callback@ callback) {
|
||||
@volumeControlsApplyCallback = @callback;
|
||||
}
|
||||
|
||||
void volume_controls_configure(float minDb = -60.0f, float maxDb = 0.0f, float stepDb = 3.0f, float initialDb = 0.0f) {
|
||||
volumeControlsMinDb = minDb;
|
||||
volumeControlsMaxDb = maxDb;
|
||||
if (volumeControlsMaxDb < volumeControlsMinDb) {
|
||||
float temp = volumeControlsMaxDb;
|
||||
volumeControlsMaxDb = volumeControlsMinDb;
|
||||
volumeControlsMinDb = temp;
|
||||
}
|
||||
|
||||
if (stepDb <= 0.0f) {
|
||||
stepDb = 1.0f;
|
||||
}
|
||||
volumeControlsStepDb = stepDb;
|
||||
|
||||
volume_controls_set_current_db(initialDb, false);
|
||||
}
|
||||
|
||||
int volume_controls_percent_from_db(float volumeDb) {
|
||||
float range = volumeControlsMaxDb - volumeControlsMinDb;
|
||||
if (range <= 0.0f) return 100;
|
||||
|
||||
float normalized = (volumeDb - volumeControlsMinDb) / range;
|
||||
int volumePercent = int(normalized * 100.0f + 0.5f);
|
||||
if (volumePercent < 0) volumePercent = 0;
|
||||
if (volumePercent > 100) volumePercent = 100;
|
||||
return volumePercent;
|
||||
}
|
||||
|
||||
void volume_controls_apply(float volumeDb) {
|
||||
if (@volumeControlsApplyCallback !is null) {
|
||||
volumeControlsApplyCallback(volumeDb);
|
||||
return;
|
||||
}
|
||||
|
||||
// Default engine-global apply behavior when no callback is provided.
|
||||
sound_master_volume = volumeDb;
|
||||
}
|
||||
|
||||
void volume_controls_set_current_db(float volumeDb, bool announce = true) {
|
||||
float clamped = volumeDb;
|
||||
if (clamped > volumeControlsMaxDb) clamped = volumeControlsMaxDb;
|
||||
if (clamped < volumeControlsMinDb) clamped = volumeControlsMinDb;
|
||||
|
||||
if (clamped == volumeControlsCurrentDb) return;
|
||||
|
||||
volumeControlsCurrentDb = clamped;
|
||||
volume_controls_apply(volumeControlsCurrentDb);
|
||||
|
||||
if (announce) {
|
||||
int volumePercent = volume_controls_percent_from_db(volumeControlsCurrentDb);
|
||||
screen_reader_speak("Volume " + volumePercent + ".", true);
|
||||
}
|
||||
}
|
||||
|
||||
void volume_controls_init_at_max(bool announce = false) {
|
||||
volume_controls_set_current_db(volumeControlsMaxDb, announce);
|
||||
}
|
||||
|
||||
float volume_controls_get_current_db() {
|
||||
return volumeControlsCurrentDb;
|
||||
}
|
||||
|
||||
void volume_controls_handle_keys(int downKey = KEY_PAGEDOWN, int upKey = KEY_PAGEUP, bool announce = true) {
|
||||
if (key_pressed(downKey)) {
|
||||
volume_controls_set_current_db(volumeControlsCurrentDb - volumeControlsStepDb, announce);
|
||||
}
|
||||
if (key_pressed(upKey)) {
|
||||
volume_controls_set_current_db(volumeControlsCurrentDb + volumeControlsStepDb, announce);
|
||||
}
|
||||
}
|
||||
|
||||
void safe_destroy_sound_in_pool(sound_pool@ poolRef, int &inout handle) {
|
||||
if (handle == -1) return;
|
||||
if (@poolRef is null) {
|
||||
handle = -1;
|
||||
return;
|
||||
}
|
||||
if (poolRef.sound_is_active(handle)) {
|
||||
poolRef.destroy_sound(handle);
|
||||
}
|
||||
handle = -1;
|
||||
}
|
||||
Reference in New Issue
Block a user