Skip to content

Commit

Permalink
feat: Create toggle volume screen
Browse files Browse the repository at this point in the history
  • Loading branch information
jadelily18 committed Sep 4, 2024
1 parent 6f05e3a commit 385b195
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 20 deletions.
43 changes: 33 additions & 10 deletions src/main/java/com/lilydev/volubind/VolumeControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.lilydev.volubind.config.ui.VolubindConfigScreen;
import com.lilydev.volubind.config.VolubindConfig;
import com.lilydev.volubind.ui.ToggleVolumeScreen;
import com.lilydev.volubind.util.Utils;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
Expand All @@ -16,6 +17,7 @@

public class VolumeControl {
private static KeyBinding openGui;
private static KeyBinding openToggleGui;
public static KeyBinding toggleMaster;
public static KeyBinding toggleMusic;
public static KeyBinding toggleMusicBlock;
Expand All @@ -28,19 +30,23 @@ public class VolumeControl {
public static KeyBinding toggleVoice;



// Translation keys have extra numbers because the Controls GUI orders them lexicographically
public static void init() {
openGui = buildKeybinding("volubind.key.00.openGui");
toggleMaster = buildKeybinding("volubind.key.01.toggleMaster");
toggleMusic = buildKeybinding("volubind.key.02.toggleMusic");
toggleMusicBlock = buildKeybinding("volubind.key.03.toggleMusicBlock");
toggleWeather = buildKeybinding("volubind.key.04.toggleWeather");
toggleBlock = buildKeybinding("volubind.key.05.toggleBlock");
toggleHostile = buildKeybinding("volubind.key.06.toggleHostile");
toggleFriendly = buildKeybinding("volubind.key.07.toggleFriendly");
togglePlayer = buildKeybinding("volubind.key.08.togglePlayer");
toggleAmbient = buildKeybinding("volubind.key.09.toggleAmbient");
toggleVoice = buildKeybinding("volubind.key.10.toggleVoice");
openToggleGui = buildKeybinding("volubind.key.01.openToggleGui");
toggleMaster = buildKeybinding("volubind.key.02.toggleMaster");
toggleMusic = buildKeybinding("volubind.key.03.toggleMusic");
toggleMusicBlock = buildKeybinding("volubind.key.04.toggleMusicBlock");
toggleWeather = buildKeybinding("volubind.key.05.toggleWeather");
toggleBlock = buildKeybinding("volubind.key.06.toggleBlock");
toggleHostile = buildKeybinding("volubind.key.07.toggleHostile");
toggleFriendly = buildKeybinding("volubind.key.08.toggleFriendly");
togglePlayer = buildKeybinding("volubind.key.09.togglePlayer");
toggleAmbient = buildKeybinding("volubind.key.10.toggleAmbient");
toggleVoice = buildKeybinding("volubind.key.11.toggleVoice");


}

public static void initVolumeOptions(MinecraftClient client) {
Expand Down Expand Up @@ -68,6 +74,19 @@ private static KeyBinding buildKeybinding(String translationKey) {
));
}

public static void toggleVolumeByCategory(VolubindConfig config, SoundCategory category) {
var setToggle = Utils.getVolumeToggleConsumerByCategory(config, category);
boolean isToggled = Utils.getVolumeToggleSupplierByCategory(config, category).get();

int newVolume = Utils.getVolumeSupplierByCategory(
config,
category,
isToggled ? Utils.ConfigVolumeType.UNTOGGLED : Utils.ConfigVolumeType.TOGGLED
).get();
getSoundVolumeOption(MinecraftClient.getInstance(), category).setValue(volumeIntToDouble(newVolume));
setToggle.accept(!isToggled);
}

public static void processKeyPress(MinecraftClient client) {
VolubindConfig config = VolubindClient.CONFIG;
assert client.player != null;
Expand All @@ -76,6 +95,10 @@ public static void processKeyPress(MinecraftClient client) {
client.setScreen(new VolubindConfigScreen(client.currentScreen));
}

if (openToggleGui.wasPressed()) {
client.setScreen(new ToggleVolumeScreen(client.currentScreen));
}

// Not entirely sure if this presents performance issues, but it seems
// fine for now and is a lot cleaner than previous iterations
EnumSet.allOf(SoundCategory.class)
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/com/lilydev/volubind/ui/ToggleVolumeScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.lilydev.volubind.ui;

import com.lilydev.volubind.VolubindClient;
import com.lilydev.volubind.VolumeControl;
import com.lilydev.volubind.util.Utils;
import io.wispforest.owo.ui.base.BaseUIModelScreen;
import io.wispforest.owo.ui.component.ButtonComponent;
import io.wispforest.owo.ui.component.Components;
import io.wispforest.owo.ui.component.LabelComponent;
import io.wispforest.owo.ui.container.Containers;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.container.ScrollContainer;
import io.wispforest.owo.ui.core.Insets;
import io.wispforest.owo.ui.core.Positioning;
import io.wispforest.owo.ui.core.Sizing;
import io.wispforest.owo.ui.core.VerticalAlignment;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.sound.SoundCategory;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import java.util.EnumSet;

public class ToggleVolumeScreen extends BaseUIModelScreen<FlowLayout> {

Screen parent;


public ToggleVolumeScreen(Screen parent) {
super(FlowLayout.class, DataSource.asset(Identifier.of(VolubindClient.MOD_ID, "toggle_volume_screen")));
this.parent = parent;
}

private static void toggleVolume(ButtonComponent buttonComponent, SoundCategory category) {
boolean volumeToggled = Utils.getVolumeToggleSupplierByCategory(VolubindClient.CONFIG, category).get();

Text toggledVolText = Text.translatable("volubind.screen.toggle_volume.entry.button.toggled");
Text untoggledVolText = Text.translatable("volubind.screen.toggle_volume.entry.button.untoggled");

VolumeControl.toggleVolumeByCategory(VolubindClient.CONFIG, category);
if (volumeToggled) {
buttonComponent.setMessage(untoggledVolText);
} else {
buttonComponent.setMessage(toggledVolText);
}
}

@Override
protected void build(FlowLayout rootComponent) {
assert this.client != null;

FlowLayout contentFlow = Containers.verticalFlow(Sizing.content(), Sizing.content());
contentFlow.id("toggle-volume-content");
contentFlow.gap(4);
contentFlow.padding(Insets.horizontal(5));

ScrollContainer<FlowLayout> pageScroll = Containers.verticalScroll(
Sizing.fixed(360),
Sizing.expand(),
contentFlow
);

pageScroll.padding(Insets.bottom(20));

EnumSet.allOf(SoundCategory.class).forEach(soundCategory -> {
FlowLayout buttonFlow = Containers.horizontalFlow(Sizing.fill(), Sizing.fixed(20));
buttonFlow.verticalAlignment(VerticalAlignment.CENTER);

LabelComponent flowLabel = Components.label(
Text.translatable("soundCategory." + soundCategory.getName())
);

boolean volumeToggled = Utils.getVolumeToggleSupplierByCategory(VolubindClient.CONFIG, soundCategory).get();

Text toggledVolText = Text.translatable("volubind.screen.toggle_volume.entry.button.toggled");
Text untoggledVolText = Text.translatable("volubind.screen.toggle_volume.entry.button.untoggled");

Text buttonText = volumeToggled ? toggledVolText : untoggledVolText;

ButtonComponent flowButton = Components.button(
buttonText, buttonComponent -> toggleVolume(buttonComponent, soundCategory)
);

flowButton.horizontalSizing(Sizing.fixed(120));
flowButton.positioning(Positioning.relative(100, 50));

buttonFlow.child(flowLabel);
buttonFlow.child(flowButton);

contentFlow.child(buttonFlow);
});

rootComponent.child(pageScroll);
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/lilydev/volubind/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,42 @@ public enum ConfigVolumeType {
TOGGLED
}

public static Consumer<Boolean> getVolumeToggleConsumerByCategory(
VolubindConfig config,
SoundCategory category
) {
return switch (category) {
case MASTER -> config::masterToggled;
case MUSIC -> config::musicToggled;
case RECORDS -> config::musicBlockToggled;
case WEATHER -> config::weatherToggled;
case BLOCKS -> config::blockToggled;
case HOSTILE -> config::hostileToggled;
case NEUTRAL -> config::friendlyToggled;
case PLAYERS -> config::playerToggled;
case AMBIENT -> config::ambientToggled;
case VOICE -> config::voiceToggled;
};
}

public static Supplier<Boolean> getVolumeToggleSupplierByCategory(
VolubindConfig config,
SoundCategory category
) {
return switch (category) {
case MASTER -> config::masterToggled;
case MUSIC -> config::musicToggled;
case RECORDS -> config::musicBlockToggled;
case WEATHER -> config::weatherToggled;
case BLOCKS -> config::blockToggled;
case HOSTILE -> config::hostileToggled;
case NEUTRAL -> config::friendlyToggled;
case PLAYERS -> config::playerToggled;
case AMBIENT -> config::ambientToggled;
case VOICE -> config::voiceToggled;
};
}

public static Consumer<Consumer<Integer>> getVolumeConsumerByCategory(
VolubindConfig config,
SoundCategory category,
Expand Down
30 changes: 20 additions & 10 deletions src/main/resources/assets/volubind/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,63 +40,73 @@
"text.config.volubind.navigation.vanillaOptions": "Vanilla Options...",

"volubind.key.category": "VoluBind",
"volubind.screen.toggle_volume.title": "Toggle Volume",
"volubind.screen.toggle_volume.entry.label": [
"Toggle ",
{
"index": 0
}
],
"volubind.screen.toggle_volume.entry.button.toggled": "✅ Toggled",
"volubind.screen.toggle_volume.entry.button.untoggled": "❌ Untoggled",

"volubind.key.00.openGui": "Open Config",
"volubind.key.01.toggleMaster": [
"volubind.key.01.openToggleGui": "Open Toggle GUI",
"volubind.key.02.toggleMaster": [
"Toggle ",
{
"translate": "soundCategory.master"
}
],
"volubind.key.02.toggleMusic": [
"volubind.key.03.toggleMusic": [
"Toggle ",
{
"translate": "soundCategory.music"
}
],
"volubind.key.03.toggleMusicBlock": [
"volubind.key.04.toggleMusicBlock": [
"Toggle ",
{
"translate": "soundCategory.record"
}
],
"volubind.key.04.toggleWeather": [
"volubind.key.05.toggleWeather": [
"Toggle ",
{
"translate": "soundCategory.weather"
}
],
"volubind.key.05.toggleBlock": [
"volubind.key.06.toggleBlock": [
"Toggle ",
{
"translate": "soundCategory.block"
}
],
"volubind.key.06.toggleHostile": [
"volubind.key.07.toggleHostile": [
"Toggle ",
{
"translate": "soundCategory.hostile"
}
],
"volubind.key.07.toggleFriendly": [
"volubind.key.08.toggleFriendly": [
"Toggle ",
{
"translate": "soundCategory.neutral"
}
],
"volubind.key.08.togglePlayer": [
"volubind.key.09.togglePlayer": [
"Toggle ",
{
"translate": "soundCategory.player"
}
],
"volubind.key.09.toggleAmbient": [
"volubind.key.10.toggleAmbient": [
"Toggle ",
{
"translate": "soundCategory.ambient"
}
],
"volubind.key.10.toggleVoice": [
"volubind.key.11.toggleVoice": [
"Toggle ",
{
"translate": "soundCategory.voice"
Expand Down
26 changes: 26 additions & 0 deletions src/main/resources/assets/volubind/owo_ui/toggle_volume_screen.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<owo-ui xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/wisp-forest/owo-lib/1.21/owo-ui.xsd">
<components>
<flow-layout direction="vertical" id="toggle-volume-container">
<children>
<flow-layout direction="horizontal" id="toggle-volume-title-bar">
<children>
<label>
<text translate="true">volubind.screen.toggle_volume.title</text>
<margins>
<vertical>10</vertical>
</margins>
</label>
</children>
<vertical-alignment>center</vertical-alignment>
<horizontal-alignment>left</horizontal-alignment>
</flow-layout>
</children>
<vertical-alignment>center</vertical-alignment>
<horizontal-alignment>center</horizontal-alignment>
<surface>
<options-background/>
</surface>
</flow-layout>
</components>
</owo-ui>

0 comments on commit 385b195

Please sign in to comment.