Improve button audio feedback

This commit is contained in:
Kae 2023-06-27 22:17:57 +10:00
parent 359624119a
commit 2496789ea7
7 changed files with 31 additions and 3 deletions

View File

@ -15,5 +15,11 @@
}, },
"debugFont" : "", "debugFont" : "",
"planetNameFormatString" : "- {} -" // Change planet name to support the new internal string formatting.
"planetNameFormatString" : "- {} -",
"buttonClickSound" : [ "/sfx/interface/button/click.wav" ],
"buttonReleaseSound" : [ "/sfx/interface/button/release.wav" ],
"buttonHoverSound" : [ "/sfx/interface/button/hover.wav" ],
"buttonHoverOffSound" : [ "/sfx/interface/button/hover_off.wav" ]
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -27,6 +27,11 @@ ButtonWidget::ButtonWidget() {
m_fontSize = interfaceConfig.query("font.buttonSize").toInt(); m_fontSize = interfaceConfig.query("font.buttonSize").toInt();
m_fontDirectives = interfaceConfig.queryString("font.defaultDirectives", ""); m_fontDirectives = interfaceConfig.queryString("font.defaultDirectives", "");
m_font = interfaceConfig.query("font.defaultFont").toString(); m_font = interfaceConfig.query("font.defaultFont").toString();
m_clickSounds = jsonToStringList(assets->json("/interface.config:buttonClickSound"));
m_releaseSounds = jsonToStringList(assets->json("/interface.config:buttonReleaseSound"));
m_hoverSounds = jsonToStringList(assets->json("/interface.config:buttonHoverSound"));
m_hoverOffSounds = jsonToStringList(assets->json("/interface.config:buttonHoverOffSound"));
} }
ButtonWidget::ButtonWidget(WidgetCallbackFunc callback, ButtonWidget::ButtonWidget(WidgetCallbackFunc callback,
@ -112,7 +117,7 @@ bool ButtonWidget::sendEvent(InputEvent const& event) {
if (inMember(*context()->mousePosition(event))) { if (inMember(*context()->mousePosition(event))) {
if (!isPressed()) { if (!isPressed()) {
auto assets = Root::singleton().assets(); auto assets = Root::singleton().assets();
auto sound = Random::randValueFrom(assets->json("/interface.config:buttonClickSound").toArray(), "").toString(); auto sound = Random::randValueFrom(m_clickSounds, "");
if (!sound.empty()) if (!sound.empty())
context()->playAudio(sound); context()->playAudio(sound);
} }
@ -126,6 +131,12 @@ bool ButtonWidget::sendEvent(InputEvent const& event) {
return false; return false;
} }
} else if (event.is<MouseButtonUpEvent>()) { } else if (event.is<MouseButtonUpEvent>()) {
if (isPressed()) {
auto assets = Root::singleton().assets();
auto sound = Random::randValueFrom(m_releaseSounds, "");
if (!sound.empty())
context()->playAudio(sound);
}
setPressed(false); setPressed(false);
return false; return false;
} }
@ -139,7 +150,7 @@ void ButtonWidget::mouseOver() {
if (!m_disabled) { if (!m_disabled) {
if (!m_hovered) { if (!m_hovered) {
auto assets = Root::singleton().assets(); auto assets = Root::singleton().assets();
auto sound = Random::randValueFrom(assets->json("/interface.config:buttonHoverSound").toArray(), "").toString(); auto sound = Random::randValueFrom(m_hoverSounds);
if (!sound.empty()) if (!sound.empty())
context()->playAudio(sound); context()->playAudio(sound);
} }
@ -149,6 +160,12 @@ void ButtonWidget::mouseOver() {
void ButtonWidget::mouseOut() { void ButtonWidget::mouseOut() {
Widget::mouseOut(); Widget::mouseOut();
if (!m_disabled && m_hovered) {
auto assets = Root::singleton().assets();
auto sound = Random::randValueFrom(m_hoverOffSounds);
if (!sound.empty())
context()->playAudio(sound);
}
m_hovered = false; m_hovered = false;
m_pressed = false; m_pressed = false;
} }

View File

@ -127,6 +127,11 @@ protected:
String m_text; String m_text;
Vec2I m_textOffset; Vec2I m_textOffset;
StringList m_clickSounds;
StringList m_releaseSounds;
StringList m_hoverSounds;
StringList m_hoverOffSounds;
bool m_sustain; bool m_sustain;
private: private: