Skip to content

Commit

Permalink
Replace Atomic Disassembler mode switch with radial selector. (#6422)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott MacDonald committed Aug 13, 2020
1 parent 5e128d6 commit 4eb2eae
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main/java/mekanism/client/ClientTickHandler.java
Expand Up @@ -298,7 +298,7 @@ private <TYPE extends Enum<TYPE> & IRadialSelectorEnum<TYPE>> void updateSelecto
return ((IRadialModeItem<TYPE>) s.getItem()).getMode(s);
}
}
return modeClass.getEnumConstants()[0];
return modeItem.getDefaultMode();
}, type -> {
if (minecraft.player != null) {
Mekanism.packetHandler.sendToServer(new PacketRadialModeChange(EquipmentSlotType.MAINHAND, type.ordinal()));
Expand Down
71 changes: 61 additions & 10 deletions src/main/java/mekanism/client/gui/GuiRadialSelector.java
Expand Up @@ -2,9 +2,11 @@

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import mekanism.api.IDisableableEnum;
import mekanism.client.render.MekanismRenderer;
import mekanism.common.item.interfaces.IRadialSelectorEnum;
import net.minecraft.client.Minecraft;
Expand All @@ -29,6 +31,7 @@ public class GuiRadialSelector<TYPE extends Enum<TYPE> & IRadialSelectorEnum<TYP
private final TYPE[] types;
private final Supplier<TYPE> curSupplier;
private final Consumer<TYPE> changeHandler;
private final boolean isDisableable;

private TYPE selection = null;

Expand All @@ -37,6 +40,7 @@ public GuiRadialSelector(Class<TYPE> enumClass, Supplier<TYPE> curSupplier, Cons
this.enumClass = enumClass;
this.curSupplier = curSupplier;
this.changeHandler = changeHandler;
isDisableable = IDisableableEnum.class.isAssignableFrom(enumClass);
types = enumClass.getEnumConstants();
}

Expand All @@ -53,58 +57,105 @@ public void render(@Nonnull MatrixStack matrix, int mouseX, int mouseY, float pa
matrix.translate(centerX, centerY, 0);
RenderSystem.disableTexture();

// Calculate number of available modes to switch between
int activeModes;
if (isDisableable) {
activeModes = (int) Arrays.stream(types).filter(type -> ((IDisableableEnum) type).isEnabled()).count();
} else {
activeModes = types.length;
}

// draw base
RenderSystem.color4f(0.3F, 0.3F, 0.3F, 0.5F);
drawTorus(matrix, 0, 360);

TYPE cur = curSupplier.get();
// Draw segments
if (cur != null) {
// draw current selected
if (cur.getColor() == null) {
RenderSystem.color4f(0.4F, 0.4F, 0.4F, 0.7F);
} else {
MekanismRenderer.color(cur.getColor(), 0.3F);
}
drawTorus(matrix, -90F + 360F * (-0.5F + cur.ordinal()) / types.length, 360F / types.length);
int section;
if (isDisableable) {
//Calculate the proper section to highlight as green in case one of the earlier ones is disabled
section = 0;
for (TYPE type : types) {
if (((IDisableableEnum) type).isEnabled()) {
if (type == cur) {
break;
}
section++;
}
}
} else {
section = cur.ordinal();
}
drawTorus(matrix, -90F + 360F * (-0.5F + section) / activeModes, 360F / activeModes);

double xDiff = mouseX - centerX;
double yDiff = mouseY - centerY;
if (Math.sqrt(xDiff * xDiff + yDiff * yDiff) >= SELECT_RADIUS) {
// draw mouse selection highlight
float angle = (float) Math.toDegrees(Math.atan2(yDiff, xDiff));
RenderSystem.color4f(0.8F, 0.8F, 0.8F, 0.3F);
drawTorus(matrix, 360F * (-0.5F / types.length) + angle, 360F / types.length);
drawTorus(matrix, 360F * (-0.5F / activeModes) + angle, 360F / activeModes);

float selectionAngle = angle + 90F + (360F * (0.5F / types.length));
float selectionAngle = angle + 90F + (360F * (0.5F / activeModes));
while (selectionAngle < 0) {
selectionAngle += 360F;
}
selection = types[(int) (selectionAngle * (types.length / 360F))];
int selection_drawn_pos = (int) (selectionAngle * (activeModes / 360F));
if (isDisableable) {
int count = 0;
for (TYPE type : types) {
if (((IDisableableEnum) type).isEnabled()) {
if (count == selection_drawn_pos) {
selection = type;
break;
}
count++;
}
}
} else {
selection = types[selection_drawn_pos];
}

// draw hovered selection
RenderSystem.color4f(0.6F, 0.6F, 0.6F, 0.7F);
drawTorus(matrix, -90F + 360F * (-0.5F + selection.ordinal()) / types.length, 360F / types.length);
drawTorus(matrix, -90F + 360F * (-0.5F + selection_drawn_pos) / activeModes, 360F / activeModes);
} else {
selection = null;
}
}

MekanismRenderer.resetColor();

// Icons & Labels
RenderSystem.enableTexture();
for (int i = 0; i < types.length; i++) {
double angle = Math.toRadians(270 + 360 * ((float) i / types.length));
int position = 0;
for (TYPE type : types) {
if (isDisableable && !((IDisableableEnum) type).isEnabled()) {
// Mode disabled, skip it.
continue;
}

double angle = Math.toRadians(270 + 360 * ((float) position / activeModes));
float x = (float) Math.cos(angle) * (INNER + OUTER) / 2F;
float y = (float) Math.sin(angle) * (INNER + OUTER) / 2F;
// draw icon
minecraft.textureManager.bindTexture(types[i].getIcon());
minecraft.textureManager.bindTexture(type.getIcon());
blit(matrix, Math.round(x - 12), Math.round(y - 20), 24, 24, 0, 0, 18, 18, 18, 18);
// draw label
matrix.push();
int width = minecraft.fontRenderer.getStringWidth(types[i].getShortText().getString());
int width = minecraft.fontRenderer.func_238414_a_(type.getShortText());
matrix.translate(x, y, 0);
matrix.scale(0.6F, 0.6F, 0.6F);
minecraft.fontRenderer.func_238422_b_(matrix, types[i].getShortText(), -width / 2F, 8, 0xCCFFFFFF);
minecraft.fontRenderer.func_238422_b_(matrix, type.getShortText(), -width / 2F, 8, 0xCCFFFFFF);
matrix.pop();
position++;
}

MekanismRenderer.resetColor();
Expand Down
65 changes: 50 additions & 15 deletions src/main/java/mekanism/common/item/gear/ItemAtomicDisassembler.java
Expand Up @@ -19,16 +19,19 @@
import mekanism.api.math.FloatingLong;
import mekanism.api.math.MathUtils;
import mekanism.api.text.EnumColor;
import mekanism.api.text.IHasTranslationKey;
import mekanism.api.text.IHasTextComponent;
import mekanism.api.text.ILangEntry;
import mekanism.client.render.item.ISTERProvider;
import mekanism.common.Mekanism;
import mekanism.common.MekanismLang;
import mekanism.common.block.BlockBounding;
import mekanism.common.config.MekanismConfig;
import mekanism.common.item.ItemEnergized;
import mekanism.common.item.gear.ItemAtomicDisassembler.DisassemblerMode;
import mekanism.common.item.interfaces.IItemHUDProvider;
import mekanism.common.item.interfaces.IModeItem;
import mekanism.common.item.interfaces.IRadialModeItem;
import mekanism.common.item.interfaces.IRadialSelectorEnum;
import mekanism.common.network.PacketLightningRender;
import mekanism.common.network.PacketLightningRender.LightningPreset;
import mekanism.common.tags.MekanismTags;
Expand All @@ -52,6 +55,7 @@
import net.minecraft.stats.Stats;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.DamageSource;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Util;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d;
Expand All @@ -62,7 +66,7 @@
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.capabilities.ICapabilityProvider;

public class ItemAtomicDisassembler extends ItemEnergized implements IItemHUDProvider, IModeItem {
public class ItemAtomicDisassembler extends ItemEnergized implements IItemHUDProvider, IModeItem, IRadialModeItem<DisassemblerMode> {

private final Multimap<Attribute, AttributeModifier> attributes;

Expand Down Expand Up @@ -228,12 +232,24 @@ private FloatingLong getDestroyEnergy(ItemStack itemStack, float hardness) {
return hardness == 0 ? destroyEnergy.divide(2) : destroyEnergy;
}

@Override
public DisassemblerMode getMode(ItemStack itemStack) {
return DisassemblerMode.byIndexStatic(ItemDataUtils.getInt(itemStack, NBTConstants.MODE));
}

public void setMode(ItemStack itemStack, DisassemblerMode mode) {
ItemDataUtils.setInt(itemStack, NBTConstants.MODE, mode.ordinal());
@Override
public DisassemblerMode getModeByIndex(int ordinal) {
return DisassemblerMode.byIndexStatic(ordinal);
}

@Override
public void setMode(ItemStack stack, PlayerEntity player, DisassemblerMode mode) {
ItemDataUtils.setInt(stack, NBTConstants.MODE, mode.ordinal());
}

@Override
public Class<DisassemblerMode> getModeClass() {
return DisassemblerMode.class;
}

@Nonnull
Expand All @@ -254,7 +270,7 @@ public void changeMode(@Nonnull PlayerEntity player, @Nonnull ItemStack stack, i
DisassemblerMode mode = getMode(stack);
DisassemblerMode newMode = mode.adjust(shift);
if (mode != newMode) {
setMode(stack, newMode);
setMode(stack, player, newMode);
if (displayChangeMessage) {
player.sendMessage(MekanismLang.LOG_FORMAT.translateColored(EnumColor.DARK_BLUE, MekanismLang.MEKANISM,
MekanismLang.DISASSEMBLER_MODE_CHANGE.translateColored(EnumColor.GRAY, EnumColor.INDIGO, newMode, EnumColor.AQUA, newMode.getEfficiency())),
Expand All @@ -279,24 +295,28 @@ public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundN
return super.initCapabilities(stack, nbt);
}

public enum DisassemblerMode implements IDisableableEnum<DisassemblerMode>, IHasTranslationKey {
NORMAL(MekanismLang.DISASSEMBLER_NORMAL, 20, () -> true),
SLOW(MekanismLang.DISASSEMBLER_SLOW, 8, MekanismConfig.gear.disassemblerSlowMode),
FAST(MekanismLang.DISASSEMBLER_FAST, 128, MekanismConfig.gear.disassemblerFastMode),
VEIN(MekanismLang.DISASSEMBLER_VEIN, 20, MekanismConfig.gear.disassemblerVeinMining),
EXTENDED_VEIN(MekanismLang.DISASSEMBLER_EXTENDED_VEIN, 20, MekanismConfig.gear.disassemblerExtendedMining),
OFF(MekanismLang.DISASSEMBLER_OFF, 0, () -> true);
public enum DisassemblerMode implements IDisableableEnum<DisassemblerMode>, IRadialSelectorEnum<DisassemblerMode>, IHasTextComponent {
NORMAL(MekanismLang.DISASSEMBLER_NORMAL, 20, () -> true, EnumColor.BRIGHT_GREEN, MekanismUtils.getResource(MekanismUtils.ResourceType.GUI, "disassembler_normal.png")),
SLOW(MekanismLang.DISASSEMBLER_SLOW, 8, MekanismConfig.gear.disassemblerSlowMode, EnumColor.BRIGHT_GREEN, MekanismUtils.getResource(MekanismUtils.ResourceType.GUI, "disassembler_slow.png")),
FAST(MekanismLang.DISASSEMBLER_FAST, 128, MekanismConfig.gear.disassemblerFastMode, EnumColor.BRIGHT_GREEN, MekanismUtils.getResource(MekanismUtils.ResourceType.GUI, "disassembler_fast.png")),
VEIN(MekanismLang.DISASSEMBLER_VEIN, 20, MekanismConfig.gear.disassemblerVeinMining, EnumColor.BRIGHT_GREEN, MekanismUtils.getResource(MekanismUtils.ResourceType.GUI, "disassembler_vein.png")),
EXTENDED_VEIN(MekanismLang.DISASSEMBLER_EXTENDED_VEIN, 20, MekanismConfig.gear.disassemblerExtendedMining, EnumColor.BRIGHT_GREEN, MekanismUtils.getResource(MekanismUtils.ResourceType.GUI, "disassembler_extended_vein.png")),
OFF(MekanismLang.DISASSEMBLER_OFF, 0, () -> true, EnumColor.BRIGHT_GREEN, MekanismUtils.getResource(MekanismUtils.ResourceType.GUI, "void.png"));

private static final DisassemblerMode[] MODES = values();

private final BooleanSupplier checkEnabled;
private final ILangEntry langEntry;
private final int efficiency;
private final EnumColor color;
private final ResourceLocation icon;

DisassemblerMode(ILangEntry langEntry, int efficiency, BooleanSupplier checkEnabled) {
DisassemblerMode(ILangEntry langEntry, int efficiency, BooleanSupplier checkEnabled, EnumColor color, ResourceLocation icon) {
this.langEntry = langEntry;
this.efficiency = efficiency;
this.checkEnabled = checkEnabled;
this.color = color;
this.icon = icon;
}

/**
Expand All @@ -315,8 +335,13 @@ public DisassemblerMode byIndex(int index) {
}

@Override
public String getTranslationKey() {
return langEntry.getTranslationKey();
public ITextComponent getTextComponent() {
return langEntry.translate(color);
}

@Override
public ITextComponent getShortText() {
return getTextComponent();
}

public int getEfficiency() {
Expand All @@ -327,5 +352,15 @@ public int getEfficiency() {
public boolean isEnabled() {
return checkEnabled.getAsBoolean();
}

@Override
public ResourceLocation getIcon() {
return icon;
}

@Override
public EnumColor getColor() {
return color;
}
}
}
Expand Up @@ -7,6 +7,10 @@ public interface IRadialModeItem<TYPE extends Enum<TYPE> & IRadialSelectorEnum<T

Class<TYPE> getModeClass();

default TYPE getDefaultMode() {
return getModeClass().getEnumConstants()[0];
}

TYPE getModeByIndex(int ordinal);

TYPE getMode(ItemStack stack);
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4eb2eae

Please sign in to comment.