Skip to content

Commit

Permalink
new artnet config screen with support for multiple universes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushmead committed Aug 3, 2024
1 parent 836da9e commit 40aa711
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@
"ui.control.fadeOut": "Fade out",
"ui.control.modes.program": "Program Mode",
"ui.control.modes.run": "Run Mode",
"ui.control.step": "Step - %s"
"ui.control.step": "Step - %s",
"screen.artnetconfig.entry.subnet": "Subnet: %s",
"screen.artnetconfig.entry.universe": "Universe: %s",
"screen.artnetconfig.subnet": "DMX Subnet",
"screen.artnetconfig.networkUniverse": "Network Universe",
"screen.artnetconfig.networkEnabled": "Enabled"
}
15 changes: 9 additions & 6 deletions common/src/main/java/dev/imabad/theatrical/TheatricalClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dev.imabad.theatrical.client.blockentities.MovingLightRenderer;
import dev.imabad.theatrical.client.dmx.ArtNetToNetworkClientData;
import dev.imabad.theatrical.config.TheatricalConfig;
import dev.imabad.theatrical.config.UniverseConfig;
import dev.imabad.theatrical.dmx.DMXDevice;
import dev.imabad.theatrical.client.dmx.TheatricalArtNetClient;
import dev.imabad.theatrical.lighting.LightManager;
Expand Down Expand Up @@ -174,14 +175,15 @@ public static byte[] UUID2Bytes(UUID uuid) {
public static void handleConsumerChange(NotifyConsumerChange notifyConsumerChange){
if(TheatricalConfig.INSTANCE.CLIENT.artnetEnabled){
TheatricalArtNetClient artNetClient = getArtNetManager().getClient();
if(artNetClient.isSubscribedTo(notifyConsumerChange.getUniverse())){
if(TheatricalConfig.INSTANCE.CLIENT.universes.containsKey(notifyConsumerChange.getUniverse())){
UniverseConfig universeConfig = TheatricalConfig.INSTANCE.CLIENT.universes.get(notifyConsumerChange.getUniverse());
DMXDevice dmxDevice = notifyConsumerChange.getDmxDevice();
if(notifyConsumerChange.getChangeType() == NotifyConsumerChange.ChangeType.ADD){
artNetClient.addDevice(notifyConsumerChange.getUniverse(), dmxDevice.getDeviceId(), dmxDevice);
artNetClient.addDevice((short) universeConfig.subnet,(short) universeConfig.universe, dmxDevice.getDeviceId(), dmxDevice);
} else if(notifyConsumerChange.getChangeType() == NotifyConsumerChange.ChangeType.UPDATE) {
artNetClient.updateDevice(notifyConsumerChange.getUniverse(), dmxDevice.getDeviceId(), dmxDevice);
artNetClient.updateDevice((short) universeConfig.subnet,(short) universeConfig.universe, dmxDevice.getDeviceId(), dmxDevice);
} else {
artNetClient.removeDevice(notifyConsumerChange.getUniverse(), dmxDevice.getDeviceId());
artNetClient.removeDevice((short) universeConfig.subnet,(short) universeConfig.universe, dmxDevice.getDeviceId());
}
}
}
Expand All @@ -190,9 +192,10 @@ public static void handleConsumerChange(NotifyConsumerChange notifyConsumerChang
public static void handleListConsumers(ListConsumers listConsumers){
if(TheatricalConfig.INSTANCE.CLIENT.artnetEnabled) {
TheatricalArtNetClient artNetClient = getArtNetManager().getClient();
if (artNetClient.isSubscribedTo(listConsumers.getUniverse())) {
if(TheatricalConfig.INSTANCE.CLIENT.universes.containsKey(listConsumers.getUniverse())) {
UniverseConfig universeConfig = TheatricalConfig.INSTANCE.CLIENT.universes.get(listConsumers.getUniverse());
for (DMXDevice dmxDevice : listConsumers.getDmxDevices()) {
artNetClient.addDevice(listConsumers.getUniverse(), dmxDevice.getDeviceId(), dmxDevice);
artNetClient.addDevice((short) universeConfig.subnet, (short) universeConfig.universe, dmxDevice.getDeviceId(), dmxDevice);
}
}
}
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package dev.imabad.theatrical.client.gui.widgets;

import dev.imabad.theatrical.client.gui.screen.ArtNetConfigurationScreen;
import dev.imabad.theatrical.config.UniverseConfig;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.layouts.LayoutElement;
import net.minecraft.client.gui.screens.packs.PackSelectionScreen;
import net.minecraft.network.chat.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class ArtNetUniverseConfigurationList extends ObjectSelectionList<ArtNetUniverseConfigurationList.Entry> implements LayoutElement {

private ArtNetConfigurationScreen parent;
public ArtNetUniverseConfigurationList(Minecraft minecraft, ArtNetConfigurationScreen screen, int width, int height, Component title) {
super(minecraft, width, height, 32, height - 55 + 4, 30);
this.parent = screen;
this.setRenderBackground(true);
this.setRenderHeader(false, 0);
this.setRenderTopAndBottom(false);
}

public void setEntries(Map<Integer, UniverseConfig> configs){
this.clearEntries();
configs.forEach((key, value) -> addEntry(new Entry(parent, key, value)));
}

@Override
protected int getScrollbarPosition() {
return this.getX() + this.getRowWidth() + 6;
}

@Override
public int getRowWidth() {
return width - 10;
}

@Override
public void setX(int x) {
setLeftPos(x);
}

@Override
public void setY(int y) {
this.y0 = y;
this.y1 = y + height;
}

@Override
public int getX() {
return x0;
}

@Override
public int getY() {
return y0;
}

@Override
public int getWidth() {
return x1 - x0;
}

@Override
public int getHeight() {
return height;
}

@Override
public void visitWidgets(Consumer<AbstractWidget> consumer) {
}

@Environment(EnvType.CLIENT)
public static class Entry extends ObjectSelectionList.Entry<Entry> implements AutoCloseable {

private final ArtNetConfigurationScreen parent;
private UniverseConfig config;
private int networkUniverse;
public Entry(ArtNetConfigurationScreen parent, int networkUniverse, UniverseConfig config) {
this.parent = parent;
this.config = config;
this.networkUniverse = networkUniverse;
}

@Override
public Component getNarration() {
return Component.empty();
}

public void close() {
}

public UniverseConfig getConfig() {
return config;
}

public int getNetworkUniverse() {
return networkUniverse;
}

@Override
public void render(GuiGraphics guiGraphics, int index, int top, int left, int width, int height, int mouseX, int mouseY, boolean hovering, float partialTick) {
Font font = Minecraft.getInstance().font;
guiGraphics.drawString(font, Component.translatable("screen.artnetconfig.entry.universe", networkUniverse), left, top + 1, 16777215 );
// guiGraphics.drawString(font, Component.translatable("screen.artnetconfig.entry.subnet", config.getSubnet()), left, top + 1, 16777215 );
// guiGraphics.drawString(font, Component.translatable("screen.artnetconfig.entry.universe", config.getUniverse()), left, top + 4 + font.lineHeight, 16777215 );
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
this.parent.setSelected(this);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class LabeledEditBox extends EditBox {

private int color = 4210752;
private boolean shadow = false;
private int textOffsetY = 0;

public LabeledEditBox(Font font, int x, int y, int width, int height, Component message) {
super(font, x, y, width, height, message);
Expand Down Expand Up @@ -58,14 +59,19 @@ public LabeledEditBox shadow(boolean shadow) {
return this;
}

public LabeledEditBox textOffsetY(int textOffsetY) {
this.textOffsetY = textOffsetY;
return this;
}

@Override
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
super.renderWidget(guiGraphics, mouseX, mouseY, partialTick);
Component component = this.getMessage();
int i = this.getWidth();
int j = font.width(component);
int k = this.getX() + Math.round(this.alignX * (float)(i - j));
int l = this.getY() + (this.getHeight() - 9) / 2;
int l = (this.getY() + (this.getHeight() - 9) / 2) + textOffsetY;
// j > i ? this.clipText(component, i) :
FormattedCharSequence formattedCharSequence = component.getVisualOrderText();
guiGraphics.drawString(font, formattedCharSequence, k, l - (font.lineHeight), color, shadow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.inspector.TagInspector;
import org.yaml.snakeyaml.representer.Representer;

import java.io.File;
import java.io.FileReader;
Expand Down Expand Up @@ -49,7 +53,11 @@ private ConfigHandler(Path configFolder){
options.setIndent(2);
options.setPrettyFlow(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
this.yaml = new Yaml(options);
LoaderOptions loaderOptions = new LoaderOptions();
TagInspector taginspector =
tag -> tag.getClassName().equals(UniverseConfig.class.getName());
loaderOptions.setTagInspector(taginspector);
this.yaml = new Yaml(new Constructor(loaderOptions), new Representer(options));
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import dev.imabad.theatrical.Theatrical;
import dev.imabad.theatrical.config.api.TheatricalConfigItem;
import io.netty.util.collection.IntObjectHashMap;
import io.netty.util.collection.IntObjectMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TheatricalConfig {

Expand Down Expand Up @@ -29,15 +36,8 @@ public static class ClientConfig extends BaseConfig{
public boolean artnetEnabled = false;
@TheatricalConfigItem
public String artNetIP = "";
@TheatricalConfigItem(minValue = "0", maxValue = "16")
public int universe1 = 1;
@TheatricalConfigItem(minValue = "-1", maxValue = "16")
public int universe2 = -1;
@TheatricalConfigItem(minValue = "-1", maxValue = "16")
public int universe3 = -1;
@TheatricalConfigItem(minValue = "-1", maxValue = "16")
public int universe4 = -1;

@TheatricalConfigItem
public Map<Integer, UniverseConfig> universes = new IntObjectHashMap<>();
}

public static class ServerConfig extends BaseConfig{
Expand All @@ -49,4 +49,5 @@ public static class ServerConfig extends BaseConfig{
@TheatricalConfigItem(minValue = "25")
public int defaultMaxLightDist = 25;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dev.imabad.theatrical.config;

import java.util.Objects;

public final class UniverseConfig {

public int subnet;
public int universe;
public boolean enabled;

public UniverseConfig(){}

public UniverseConfig(int subnet, int universe, boolean enabled) {
this.subnet = subnet;
this.universe = universe;
this.enabled = enabled;
}
public int getSubnet() {
return subnet;
}

public int getUniverse() {
return universe;
}

public void setSubnet(int subnet) {
this.subnet = subnet;
}

public void setUniverse(int universe) {
this.universe = universe;
}

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (UniverseConfig) obj;
return this.subnet == that.subnet &&
this.universe == that.universe;
}

@Override
public int hashCode() {
return Objects.hash(subnet, universe);
}

@Override
public String toString() {
return "UniverseConfig[" +
"subnet=" + subnet + ", " +
"universe=" + universe + ']';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ protected void addTranslations() {
add("screen.configurationcard.autoincrement", "Address Auto Increment");
add("screen.configurationcard", "Configuration Card");
add("screen.artnetconfig.network", "Network");
add("screen.artnetconfig.entry", "Subnet: %s Universe: %s");
add("item.configurationcard.success", "Configured device to %s network, universe %s and address %s - next address is %s.");
add("screen.artnetconfig.entry.subnet": "Subnet: %s");
add("screen.artnetconfig.entry.universe", "Universe: %s");
add("screen.artnetconfig.subnet", "DMX Subnet");
add("screen.artnetconfig.networkUniverse", "Network Universe");
add("screen.artnetconfig.networkEnabled", "Enabled");
}
}

Expand Down

0 comments on commit 40aa711

Please sign in to comment.