From dbdc8076df7eb527e65a61a38c85bc6bb080e33e Mon Sep 17 00:00:00 2001 From: William Edwards Date: Sun, 16 Jun 2024 21:44:10 -0700 Subject: [PATCH] feat(AudioTheme): add support for audio/sfx themes --- assets/audio/themes/default.tres | 10 +++ .../icon-park-outline--sound-wave.svg | 1 + .../icon-park-outline--sound-wave.svg.import | 37 +++++++++ core/systems/audio/audio_theme.gd | 75 +++++++++++++++++++ .../effects/play_audio_theme_effect.gd | 45 +++++++++++ .../effects/play_audio_theme_effect.tscn | 9 +++ .../user_interface/audio_theme_setter.gd | 4 + .../user_interface/audio_theme_setter.tscn | 6 ++ 8 files changed, 187 insertions(+) create mode 100644 assets/audio/themes/default.tres create mode 100644 assets/editor-icons/icon-park-outline--sound-wave.svg create mode 100644 assets/editor-icons/icon-park-outline--sound-wave.svg.import create mode 100644 core/systems/audio/audio_theme.gd create mode 100644 core/systems/effects/play_audio_theme_effect.gd create mode 100644 core/systems/effects/play_audio_theme_effect.tscn create mode 100644 core/systems/user_interface/audio_theme_setter.gd create mode 100644 core/systems/user_interface/audio_theme_setter.tscn diff --git a/assets/audio/themes/default.tres b/assets/audio/themes/default.tres new file mode 100644 index 00000000..6fabf822 --- /dev/null +++ b/assets/audio/themes/default.tres @@ -0,0 +1,10 @@ +[gd_resource type="Resource" script_class="AudioTheme" load_steps=2 format=3 uid="uid://d70xost7hk1i"] + +[ext_resource type="Script" path="res://core/systems/audio/audio_theme.gd" id="1_7ff30"] + +[resource] +script = ExtResource("1_7ff30") +intro = "" +focus = "res://assets/audio/interface/536764__egomassive__toss.ogg" +select = "res://assets/audio/interface/96127__bmaczero__contact1.ogg" +open_menu = "" diff --git a/assets/editor-icons/icon-park-outline--sound-wave.svg b/assets/editor-icons/icon-park-outline--sound-wave.svg new file mode 100644 index 00000000..d4a8e5dc --- /dev/null +++ b/assets/editor-icons/icon-park-outline--sound-wave.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/assets/editor-icons/icon-park-outline--sound-wave.svg.import b/assets/editor-icons/icon-park-outline--sound-wave.svg.import new file mode 100644 index 00000000..803b33e3 --- /dev/null +++ b/assets/editor-icons/icon-park-outline--sound-wave.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://brtu0g62ohgn1" +path="res://.godot/imported/icon-park-outline--sound-wave.svg-6694d7867012105e482585279d7a4c37.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://assets/editor-icons/icon-park-outline--sound-wave.svg" +dest_files=["res://.godot/imported/icon-park-outline--sound-wave.svg-6694d7867012105e482585279d7a4c37.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/core/systems/audio/audio_theme.gd b/core/systems/audio/audio_theme.gd new file mode 100644 index 00000000..e81dadf2 --- /dev/null +++ b/core/systems/audio/audio_theme.gd @@ -0,0 +1,75 @@ +@icon("res://assets/editor-icons/icon-park-outline--sound-wave.svg") +extends Resource +class_name AudioTheme + +## Resource for customizing the UI audio sounds + +@export_category("General") +## Unique name of the audio theme +@export var name: String + +@export_category("Global") +## Sounds to play when OpenGamepadUI first launches +@export_file("*.ogg") var intro := "" +## Sound to play when volume is increased +@export_file("*.ogg") var audio_volume_up := "" +## Sound to play when volume is decreased +@export_file("*.ogg") var audio_volume_down := "" +## Ambient background music to play in menus +@export_file("*.ogg") var ambient_music := "" +## Sound to play when a notification is displayed +@export_file("*.ogg") var notification_display := "" + +@export_category("Side Menus") +## Sound to play when side menus (Main menu and QB menu) open +@export_file("*.ogg") var side_menu_open := "" +## Sound to play when side menus (Main menu and QB menu) close +@export_file("*.ogg") var side_menu_close := "" + +@export_category("Button") +## Sound to play when button is focused +@export_file("*.ogg") var button_focus := "res://assets/audio/interface/536764__egomassive__toss.ogg" +## Sound to play when button is selected +@export_file("*.ogg") var button_select := "res://assets/audio/interface/96127__bmaczero__contact1.ogg" + +@export_category("Slider") +## Sound to play when slider is focused +@export_file("*.ogg") var slider_focus := "res://assets/audio/interface/536764__egomassive__toss.ogg" +## Sound to play when slider value changes +@export_file("*.ogg") var slider_change := "" + +@export_category("Toggle") +## Sound to play when toggle is focused +@export_file("*.ogg") var toggle_focus := "res://assets/audio/interface/536764__egomassive__toss.ogg" +## Sound to play when toggle value changes +@export_file("*.ogg") var toggle_change := "" + +## Enumeration of all different components of an audio theme +enum TYPE { + INTRO, + AUDIO_VOLUME_UP, + AUDIO_VOLUME_DOWN, + AMBIENT_MUSIC, + NOTIFICATION_DISPLAY, + SIDE_MENU_OPEN, + SIDE_MENU_CLOSE, + BUTTON_FOCUS, + BUTTON_SELECT, + SLIDER_FOCUS, + SLIDER_CHANGE, + TOGGLE_FOCUS, + TOGGLE_CHANGE, +} + +## Returns the loaded audio stream for the given audio theme component type +func get_stream(type: TYPE) -> AudioStream: + var path := "" + match type: + TYPE.INTRO: + path = intro + TYPE.AUDIO_VOLUME_UP: + path = audio_volume_up + TYPE.AUDIO_VOLUME_DOWN: + path = audio_volume_down + + return null diff --git a/core/systems/effects/play_audio_theme_effect.gd b/core/systems/effects/play_audio_theme_effect.gd new file mode 100644 index 00000000..97139d7f --- /dev/null +++ b/core/systems/effects/play_audio_theme_effect.gd @@ -0,0 +1,45 @@ +@tool +extends Effect +class_name PlayAudioThemeEffect + +const DEFAULT_THEME := "res://assets/audio/themes/default.tres" + +## The type of component that the audio theme should play +@export var type: AudioTheme.TYPE + +@onready var theme := get_audio_theme() +@onready var audio_player := $AudioStreamPlayer as AudioStreamPlayer + +func _ready() -> void: + var on_finished := func(): + effect_finished.emit() + audio_player.finished.connect(on_finished) + audio_player.stream = stream + + +## Returns the audio theme +func get_audio_theme(current: Node = null) -> AudioTheme: + if not current: + current = self + + # If the current node has an audio theme, return it + if current.has_meta("audio_theme"): + return current.get_meta("audio_theme") + + # If this is the root node and no audio theme is defined, use the default + if current == get_node("/root"): + return load(DEFAULT_THEME) + + # Otherwise try to get the parent node's audio theme + var parent := get_parent() + + return get_audio_theme(parent) + + +# Fires when the given signal is emitted +func _on_signal(): + play_sound() + + +func play_sound() -> void: + audio_player.play() diff --git a/core/systems/effects/play_audio_theme_effect.tscn b/core/systems/effects/play_audio_theme_effect.tscn new file mode 100644 index 00000000..a099d5d2 --- /dev/null +++ b/core/systems/effects/play_audio_theme_effect.tscn @@ -0,0 +1,9 @@ +[gd_scene load_steps=2 format=3 uid="uid://djdvu2nxovgxv"] + +[ext_resource type="Script" path="res://core/systems/effects/play_audio_theme_effect.gd" id="1_emgpn"] + +[node name="PlayAudioThemeEffect" type="Node"] +script = ExtResource("1_emgpn") +on_signal = "" + +[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."] diff --git a/core/systems/user_interface/audio_theme_setter.gd b/core/systems/user_interface/audio_theme_setter.gd new file mode 100644 index 00000000..2e5a4cb6 --- /dev/null +++ b/core/systems/user_interface/audio_theme_setter.gd @@ -0,0 +1,4 @@ +@tool +@icon("res://assets/editor-icons/icon-park-outline--sound-wave.svg") +extends Node +class_name AudioThemeSetter diff --git a/core/systems/user_interface/audio_theme_setter.tscn b/core/systems/user_interface/audio_theme_setter.tscn new file mode 100644 index 00000000..099895fa --- /dev/null +++ b/core/systems/user_interface/audio_theme_setter.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://b2o7yd21t4wfg"] + +[ext_resource type="Script" path="res://core/systems/user_interface/audio_theme_setter.gd" id="1_2f67i"] + +[node name="AudioThemeSetter" type="Node"] +script = ExtResource("1_2f67i")