Skip to content

Commit 4b1e263

Browse files
committed
Gui sounds
1 parent 3128295 commit 4b1e263

File tree

15 files changed

+101
-14
lines changed

15 files changed

+101
-14
lines changed

common/src/main/kotlin/com/lambda/core/LambdaSound.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ import net.minecraft.sound.SoundEvent
55
import net.minecraft.util.Identifier
66

77
enum class LambdaSound(val id: Identifier) {
8-
MODULE_TOGGLE("module_toggle".toIdentifier());
8+
BUTTON_CLICK("button_click".toIdentifier()),
9+
10+
BOOLEAN_SETTING_ON("bool_on".toIdentifier()),
11+
BOOLEAN_SETTING_OFF("bool_off".toIdentifier()),
12+
13+
MODULE_ON("module_on".toIdentifier()),
14+
MODULE_OFF("module_off".toIdentifier()),
15+
16+
SETTINGS_OPEN("settings_open".toIdentifier()),
17+
SETTINGS_CLOSE("settings_close".toIdentifier());
918

1019
val event: SoundEvent = SoundEvent.of(id)
1120
}

common/src/main/kotlin/com/lambda/core/SoundManager.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.lambda.core
22

33
import com.lambda.Lambda
44
import com.lambda.Lambda.mc
5+
import com.lambda.util.math.random
56
import net.minecraft.client.sound.PositionedSoundInstance
67
import net.minecraft.registry.Registries
78
import net.minecraft.registry.Registry
@@ -18,9 +19,17 @@ object SoundManager : Loadable {
1819
return "Loaded ${LambdaSound.entries.size} sounds"
1920
}
2021

21-
fun playSound(event: SoundEvent, pitch: Float = 1.0f) {
22+
fun playSound(event: SoundEvent, pitch: Double = 1.0) {
2223
mc.soundManager.play(
23-
PositionedSoundInstance.master(event, pitch)
24+
PositionedSoundInstance.master(event, pitch.toFloat())
25+
)
26+
}
27+
28+
fun playSoundRandomly(event: SoundEvent, pitch: Double = 1.0, pitchRange: Double = 0.05) {
29+
val actualPitch = (pitch - pitchRange..pitch + pitchRange).random()
30+
31+
mc.soundManager.play(
32+
PositionedSoundInstance.master(event, actualPitch.toFloat())
2433
)
2534
}
2635

common/src/main/kotlin/com/lambda/gui/api/component/button/ButtonComponent.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.lambda.gui.api.component.button
22

3+
import com.lambda.core.LambdaSound
4+
import com.lambda.core.SoundManager.playSoundRandomly
35
import com.lambda.graphics.animation.Animation.Companion.exp
46
import com.lambda.gui.api.GuiEvent
57
import com.lambda.gui.api.component.core.list.ChildComponent
68
import com.lambda.gui.api.component.core.list.ChildLayer
79
import com.lambda.module.modules.client.ClickGui
810
import com.lambda.module.modules.client.GuiSettings
11+
import com.lambda.util.Mouse
912
import com.lambda.util.math.ColorUtils.multAlpha
1013
import com.lambda.util.math.MathUtils.lerp
1114
import com.lambda.util.math.Rect
@@ -82,6 +85,11 @@ abstract class ButtonComponent(
8285
}
8386
}
8487

88+
override fun onPress(e: GuiEvent.MouseClick) {
89+
val pitch = if (e.button == Mouse.Button.Left) 1.0 else 0.9
90+
playSoundRandomly(LambdaSound.BUTTON_CLICK.event, pitch)
91+
}
92+
8593
override fun onRelease(e: GuiEvent.MouseClick) {
8694
if (hovered) performClickAction(e)
8795
}

common/src/main/kotlin/com/lambda/gui/impl/clickgui/buttons/ModuleButton.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.lambda.gui.impl.clickgui.buttons
22

33
import com.lambda.config.settings.NumericSetting
44
import com.lambda.config.settings.comparable.BooleanSetting
5+
import com.lambda.core.LambdaSound
6+
import com.lambda.core.SoundManager.playSoundRandomly
57
import com.lambda.graphics.animation.Animation.Companion.exp
68
import com.lambda.graphics.gl.Scissor.scissor
79
import com.lambda.gui.api.GuiEvent
@@ -147,8 +149,11 @@ class ModuleButton(val module: Module, owner: ChildLayer.Drawable<*>) : ListButt
147149
}
148150

149151
override fun performClickAction(e: GuiEvent.MouseClick) {
150-
when (e.button) {
151-
Mouse.Button.Left -> module.toggle()
152+
val sound = when (e.button) {
153+
Mouse.Button.Left -> {
154+
module.toggle()
155+
if (module.isEnabled) LambdaSound.MODULE_ON else LambdaSound.MODULE_OFF
156+
}
152157
Mouse.Button.Right -> {
153158
// Don't let user spam
154159
val targetHeight = if (isOpen) settingsHeight else 0.0
@@ -157,8 +162,13 @@ class ModuleButton(val module: Module, owner: ChildLayer.Drawable<*>) : ListButt
157162
isOpen = !isOpen
158163
if (isOpen) settingsLayer.onEvent(GuiEvent.Show())
159164
updateHeight()
165+
166+
if (isOpen) LambdaSound.SETTINGS_OPEN else LambdaSound.SETTINGS_CLOSE
160167
}
168+
else -> return
161169
}
170+
171+
playSoundRandomly(sound.event)
162172
}
163173

164174
override fun equals(other: Any?) =

common/src/main/kotlin/com/lambda/gui/impl/clickgui/buttons/setting/BooleanButton.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package com.lambda.gui.impl.clickgui.buttons.setting
22

33
import com.lambda.config.settings.comparable.BooleanSetting
44
import com.lambda.core.LambdaSound
5-
import com.lambda.core.SoundManager
6-
import com.lambda.core.SoundManager.playSound
5+
import com.lambda.core.SoundManager.playSoundRandomly
76
import com.lambda.graphics.animation.Animation.Companion.exp
87
import com.lambda.gui.api.GuiEvent
98
import com.lambda.gui.api.component.core.list.ChildLayer
@@ -49,7 +48,11 @@ class BooleanButton(
4948
}
5049

5150
override fun performClickAction(e: GuiEvent.MouseClick) {
52-
if (e.button == Mouse.Button.Left) value = !value
53-
playSound(LambdaSound.MODULE_TOGGLE.event)
51+
if (e.button != Mouse.Button.Left) return
52+
value = !value
53+
54+
val sound = if (value) LambdaSound.BOOLEAN_SETTING_ON else LambdaSound.BOOLEAN_SETTING_OFF
55+
val pitch = if (value) 1.0 else 0.9
56+
playSoundRandomly(sound.event, pitch)
5457
}
5558
}

common/src/main/kotlin/com/lambda/gui/impl/clickgui/buttons/setting/SliderSetting.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
package com.lambda.gui.impl.clickgui.buttons.setting
22

33
import com.lambda.config.AbstractSetting
4+
import com.lambda.core.LambdaSound
5+
import com.lambda.core.SoundManager.playSound
6+
import com.lambda.core.SoundManager.playSoundRandomly
47
import com.lambda.gui.api.GuiEvent
58
import com.lambda.gui.api.component.core.list.ChildLayer
69
import com.lambda.gui.impl.clickgui.buttons.SettingButton
710
import com.lambda.module.modules.client.GuiSettings
811
import com.lambda.util.math.ColorUtils.multAlpha
12+
import com.lambda.util.math.MathUtils.lerp
913
import com.lambda.util.math.Vec2d
1014
import com.lambda.util.math.transform
15+
import kotlin.math.abs
1116

1217
abstract class SliderSetting <V : Any, T : AbstractSetting<V>>(
1318
setting: T, owner: ChildLayer.Drawable<*>
1419
) : SettingButton<V, T>(setting, owner) {
1520
protected abstract val renderProgress: Double
1621
protected abstract fun setValueByProgress(progress: Double)
22+
private var lastPlayedValue = value
23+
private var lastPlayedTiming = 0L
1724

1825
init {
1926
renderer.filled {
@@ -26,9 +33,20 @@ abstract class SliderSetting <V : Any, T : AbstractSetting<V>>(
2633
override fun onEvent(e: GuiEvent) {
2734
super.onEvent(e)
2835

29-
if (e is GuiEvent.MouseMove && pressed) {
30-
val p = transform(e.mouse.x, rect.left, rect.right, 0.0, 1.0)
31-
setValueByProgress(p.coerceIn(0.0, 1.0))
36+
when (e) {
37+
is GuiEvent.MouseMove -> {
38+
if (!pressed) return
39+
val p = transform(e.mouse.x, rect.left, rect.right, 0.0, 1.0).coerceIn(0.0, 1.0)
40+
setValueByProgress(p)
41+
42+
val time = System.currentTimeMillis()
43+
if (lastPlayedValue == value || time - lastPlayedTiming < 50) return
44+
45+
lastPlayedValue = value
46+
lastPlayedTiming = time
47+
48+
playSound(LambdaSound.BUTTON_CLICK.event, lerp(0.9, 1.2, p))
49+
}
3250
}
3351
}
3452
}
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
{
2-
"module_toggle": {
2+
"button_click": {
33
"sounds": [
4-
"lambda:ui/module_toggle"
4+
"lambda:ui/button_click"
5+
]
6+
},
7+
"bool_on": {
8+
"sounds": [
9+
"lambda:ui/bool_on"
10+
]
11+
},
12+
"bool_off": {
13+
"sounds": [
14+
"lambda:ui/bool_off"
15+
]
16+
},
17+
"module_on": {
18+
"sounds": [
19+
"lambda:ui/module_on"
20+
]
21+
},
22+
"module_off": {
23+
"sounds": [
24+
"lambda:ui/module_off"
25+
]
26+
},
27+
"settings_open": {
28+
"sounds": [
29+
"lambda:ui/settings_open"
30+
]
31+
},
32+
"settings_close": {
33+
"sounds": [
34+
"lambda:ui/settings_close"
535
]
636
}
737
}
7.15 KB
Binary file not shown.
7.91 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)