]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Modules/Audio/event_music.gd
Initial Godot project with Dialogic 2.0-Alpha-17
[wolf-seeking-sheep.git] / addons / dialogic / Modules / Audio / event_music.gd
1 @tool
2 ## Event that can change the currently playing background music.
3 ## This event won't play new music if it's already playing.
4 class_name DialogicMusicEvent
5 extends DialogicEvent
6
7
8 ### Settings
9
10 ## The file to play. If empty, the previous music will be faded out.
11 var file_path := "":
12         set(value):
13                 if file_path != value:
14                         file_path = value
15                         ui_update_needed.emit()
16 ## The channel to use.
17 var channel_id: int = 0
18 ## The length of the fade. If 0 (by default) it's an instant change.
19 var fade_length: float = 0
20 ## The volume the music will be played at.
21 var volume: float = 0
22 ## The audio bus the music will be played at.
23 var audio_bus := ""
24 ## If true, the audio will loop, otherwise only play once.
25 var loop := true
26
27
28 ################################################################################
29 ##                                              EXECUTE
30 ################################################################################
31
32 func _execute() -> void:
33         if not dialogic.Audio.is_music_playing_resource(file_path, channel_id):
34                 dialogic.Audio.update_music(file_path, volume, audio_bus, fade_length, loop, channel_id)
35
36         finish()
37
38 ################################################################################
39 ##                                              INITIALIZE
40 ################################################################################
41
42 func _init() -> void:
43         event_name = "Music"
44         set_default_color('Color7')
45         event_category = "Audio"
46         event_sorting_index = 2
47
48
49 func _get_icon() -> Resource:
50         return load(self.get_script().get_path().get_base_dir().path_join('icon_music.png'))
51
52 ################################################################################
53 ##                                              SAVING/LOADING
54 ################################################################################
55
56 func get_shortcode() -> String:
57         return "music"
58
59
60 func get_shortcode_parameters() -> Dictionary:
61         return {
62                 #param_name : property_info
63                 "path"          : {"property": "file_path",     "default": ""},
64                 "channel"       : {"property": "channel_id",    "default": 0},
65                 "fade"          : {"property": "fade_length",   "default": 0},
66                 "volume"        : {"property": "volume",                "default": 0},
67                 "bus"           : {"property": "audio_bus",     "default": "",
68                                                 "suggestions": get_bus_suggestions},
69                 "loop"          : {"property": "loop",                  "default": true},
70         }
71
72
73 ################################################################################
74 ##                                              EDITOR REPRESENTATION
75 ################################################################################
76
77 func build_event_editor() -> void:
78         add_header_edit('file_path', ValueType.FILE, {
79                         'left_text'             : 'Play',
80                         'file_filter'   : "*.mp3, *.ogg, *.wav; Supported Audio Files",
81                         'placeholder'   : "No music",
82                         'editor_icon'   : ["AudioStreamPlayer", "EditorIcons"]})
83         add_header_edit('channel_id', ValueType.FIXED_OPTIONS, {'left_text':'on:', 'options': get_channel_list()})
84         add_header_edit('file_path', ValueType.AUDIO_PREVIEW)
85         add_body_edit('fade_length', ValueType.NUMBER, {'left_text':'Fade Time:'})
86         add_body_edit('volume', ValueType.NUMBER, {'left_text':'Volume:', 'mode':2}, '!file_path.is_empty()')
87         add_body_edit('audio_bus', ValueType.SINGLELINE_TEXT, {'left_text':'Audio Bus:'}, '!file_path.is_empty()')
88         add_body_edit('loop', ValueType.BOOL, {'left_text':'Loop:'}, '!file_path.is_empty() and not file_path.to_lower().ends_with(".wav")')
89
90
91 func get_bus_suggestions() -> Dictionary:
92         var bus_name_list := {}
93         for i in range(AudioServer.bus_count):
94                 bus_name_list[AudioServer.get_bus_name(i)] = {'value':AudioServer.get_bus_name(i)}
95         return bus_name_list
96
97
98 func get_channel_list() -> Array:
99         var channel_name_list := []
100         for i in ProjectSettings.get_setting('dialogic/audio/max_channels', 4):
101                 channel_name_list.append({
102                         'label': 'Channel %s' % (i + 1),
103                         'value': i,
104                 })
105         return channel_name_list