Skip to content

Commit

Permalink
Lots of RoundaLib migration work
Browse files Browse the repository at this point in the history
  • Loading branch information
Roundaround committed Jul 17, 2024
1 parent 04deb8d commit 270453f
Show file tree
Hide file tree
Showing 28 changed files with 898 additions and 1,011 deletions.
Binary file added assets/icons-18.psd
Binary file not shown.
Binary file added assets/icons-20.psd
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package me.roundaround.armorstands.client.gui;

import com.mojang.blaze3d.systems.RenderSystem;
import me.roundaround.armorstands.client.gui.widget.NavigationButtonWidget;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;

Expand Down Expand Up @@ -77,7 +76,7 @@ public void render(Screen screen, DrawContext context) {
TextRenderer textRenderer = client.textRenderer;
int width = textRenderer.getWidth(text);
int x = (screen.width - width) / 2;
int y = screen.height - NavigationButtonWidget.HEIGHT - 1 - 6 - textRenderer.fontHeight;
int y = screen.height - ButtonWidget.DEFAULT_HEIGHT - 1 - 6 - textRenderer.fontHeight;
float opacity = MathHelper.clamp(timeRemaining / 10f, 0f, 1f);

int backgroundAlpha = client.options.getTextBackgroundColor(0) >> 24 & 0xFF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import me.roundaround.armorstands.ArmorStandsMod;
import me.roundaround.armorstands.client.ArmorStandsClientMod;
import me.roundaround.armorstands.client.gui.MessageRenderer;
import me.roundaround.armorstands.client.gui.widget.HelpButtonWidget;
import me.roundaround.armorstands.client.gui.widget.IconButtonWidget;
import me.roundaround.armorstands.client.gui.widget.NavigationButtonWidget;
import me.roundaround.armorstands.client.gui.widget.ArmorStandLayoutWidget;
import me.roundaround.armorstands.client.network.ClientNetworking;
import me.roundaround.armorstands.mixin.ArmorStandEntityAccessor;
import me.roundaround.armorstands.mixin.InGameHudAccessor;
Expand All @@ -15,39 +13,40 @@
import me.roundaround.armorstands.network.ScreenType;
import me.roundaround.armorstands.network.UtilityAction;
import me.roundaround.armorstands.screen.ArmorStandScreenHandler;
import me.roundaround.roundalib.client.gui.GuiUtil;
import me.roundaround.roundalib.asset.icon.BuiltinIcon;
import me.roundaround.roundalib.asset.icon.CustomIcon;
import me.roundaround.roundalib.client.gui.layout.IntRect;
import me.roundaround.roundalib.client.gui.widget.DrawableWidget;
import me.roundaround.roundalib.client.gui.widget.IconButtonWidget;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.decoration.ArmorStandEntity;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

public abstract class AbstractArmorStandScreen extends HandledScreen<ArmorStandScreenHandler> implements
PassesEventsThrough {
protected static final Identifier SELECTION_TEXTURE = new Identifier(
ArmorStandsMod.MOD_ID, "textures/gui/selection_frame.png");

protected static final int NAV_BUTTON_BOTTOM_PADDING = 1;
protected static final int NAV_BUTTON_SPACING = 0;
protected static final Identifier SELECTION_TEXTURE = new Identifier(ArmorStandsMod.MOD_ID, "selection");
protected static final CustomIcon COPY_ICON = new CustomIcon("copy", 20);
protected static final CustomIcon PASTE_ICON = new CustomIcon("paste", 20);

protected final ArmorStandLayoutWidget layout = new ArmorStandLayoutWidget(this);
protected final ArmorStandEntity armorStand;
protected final MessageRenderer messageRenderer;
protected final ArrayList<NavigationButtonWidget> navigationButtons = new ArrayList<>();

protected NavigationButtonWidget activeButton;
protected IconButtonWidget activeButton;
protected boolean supportsUndoRedo = false;
protected boolean utilizesInventory = false;
protected boolean passEvents = true;
Expand Down Expand Up @@ -80,15 +79,96 @@ public boolean shouldPassEvents() {

@Override
public void init() {
initStart();
this.populateLayout();
this.collectChildren();
this.initTabNavigation();
}

super.init();
protected void populateLayout() {
this.initUtilityButtons();
this.initNavigationButtons();
}

initLeft();
initNavigationButtons();
initRight();
protected void initUtilityButtons() {
if (!this.supportsUndoRedo) {
return;
}

initEnd();
this.layout.topLeft.add(IconButtonWidget.builder(BuiltinIcon.HELP_18, ArmorStandsMod.MOD_ID)
.large()
.messageAndTooltip(this.buildHelpTooltipText())
.build());
this.layout.topLeft.add(IconButtonWidget.builder(COPY_ICON, ArmorStandsMod.MOD_ID)
.large()
.messageAndTooltip(Text.translatable("armorstands.utility.copy"))
.onPress((button) -> ClientNetworking.sendUtilityActionPacket(UtilityAction.COPY))
.build());
this.layout.topLeft.add(IconButtonWidget.builder(PASTE_ICON, ArmorStandsMod.MOD_ID)
.large()
.messageAndTooltip(Text.translatable("armorstands.utility.paste"))
.onPress((button) -> ClientNetworking.sendUtilityActionPacket(UtilityAction.PASTE))
.build());
this.layout.topLeft.add(IconButtonWidget.builder(BuiltinIcon.UNDO_18, ArmorStandsMod.MOD_ID)
.large()
.messageAndTooltip(Text.translatable("armorstands.utility.undo"))
.onPress((button) -> ClientNetworking.sendUndoPacket(false))
.build());
this.layout.topLeft.add(IconButtonWidget.builder(BuiltinIcon.REDO_18, ArmorStandsMod.MOD_ID)
.large()
.messageAndTooltip(Text.translatable("armorstands.utility.redo"))
.onPress((button) -> ClientNetworking.sendUndoPacket(true))
.build());
}

private Text buildHelpTooltipText() {
String alt = Text.translatable("armorstands.help.alt").getString();
String inventory = Objects.requireNonNull(this.client).options.inventoryKey.getBoundKeyLocalizedText().getString();
String left = InputUtil.fromKeyCode(GLFW.GLFW_KEY_LEFT, 0).getLocalizedText().getString();
String right = InputUtil.fromKeyCode(GLFW.GLFW_KEY_RIGHT, 0).getLocalizedText().getString();
String highlight = ArmorStandsClientMod.highlightArmorStandKeyBinding.getBoundKeyLocalizedText().getString();
String control = Text.translatable("armorstands.help." + (MinecraftClient.IS_SYSTEM_MAC ? "cmd" : "ctrl"))
.getString();
String z = InputUtil.fromKeyCode(GLFW.GLFW_KEY_Z, 0).getLocalizedText().getString();
String shift = Text.translatable("armorstands.help.shift").getString();
String c = InputUtil.fromKeyCode(GLFW.GLFW_KEY_C, 0).getLocalizedText().getString();
String v = InputUtil.fromKeyCode(GLFW.GLFW_KEY_V, 0).getLocalizedText().getString();

return Text.translatable("armorstands.help", alt, inventory, left, right, highlight, control, z, control, shift, z,
control, c, control, v
);
}

protected void initNavigationButtons() {
for (ScreenType screenType : ScreenType.values()) {
IconButtonWidget navButton = IconButtonWidget.builder(screenType.getIcon(), ArmorStandsMod.MOD_ID)
.onPress((button) -> ClientNetworking.sendRequestScreenPacket(this.getArmorStand(), screenType))
.build();
if (this.getScreenType() == screenType) {
navButton.active = false;
}
this.layout.navRow.add(navButton);
}
}

protected void collectChildren() {
this.layout.forEachChild(this::addDrawableChild);
this.addDrawableChild(new DrawableWidget() {
@Override
protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
IconButtonWidget activeButton = AbstractArmorStandScreen.this.activeButton;
if (activeButton == null) {
return;
}

RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
context.drawGuiTexture(SELECTION_TEXTURE, activeButton.getX() - 2, activeButton.getY() - 2, 24, 24);
}
});
}

@Override
protected void initTabNavigation() {
this.layout.refreshPositions();
}

@Override
Expand Down Expand Up @@ -118,8 +198,6 @@ public void render(DrawContext drawContext, int mouseX, int mouseY, float delta)

super.render(drawContext, adjustedMouseX, adjustedMouseY, delta);

renderActivePageButtonHighlight(drawContext);

this.messageRenderer.render(drawContext);
}

Expand Down Expand Up @@ -197,22 +275,20 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) {
}

@Override
public boolean mouseScrolled(
double mouseX, double mouseY, double horizontalAmount, double verticalAmount
) {
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
if (this.cursorLocked) {
return false;
}
for (NavigationButtonWidget button : this.navigationButtons) {
if (button.isMouseOverIgnoreState(mouseX, mouseY)) {
if (verticalAmount > 0) {
goToPreviousScreen();
} else {
goToNextScreen();
}
return true;

if (IntRect.fromWidget(this.layout.navRow).contains(mouseX, mouseY)) {
if (verticalAmount > 0) {
goToPreviousScreen();
} else {
goToNextScreen();
}
return true;
}

return super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
}

Expand Down Expand Up @@ -286,17 +362,13 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
return true;
}

for (int i = 0; i < this.navigationButtons.size(); i++) {
NavigationButtonWidget button = this.navigationButtons.get(i);
ScreenType screenType = button.getScreenType();

for (ScreenType screenType : ScreenType.values()) {
if (screenType == this.getScreenType()) {
continue;
}

if (this.client.options.hotbarKeys[i].matchesKey(keyCode, scanCode)) {
if (this.client.options.hotbarKeys[screenType.getId()].matchesKey(keyCode, scanCode)) {
playClickSound();
button.onPress();
ClientNetworking.sendRequestScreenPacket(this.armorStand, screenType);
return true;
}
}
Expand Down Expand Up @@ -355,87 +427,6 @@ public void updateDisabledSlotsOnClient(int disabledSlots) {
((ArmorStandEntityAccessor) this.armorStand).setDisabledSlots(disabledSlots);
}

protected void initStart() {
this.navigationButtons.clear();
}

protected void initUtilityButtons() {
if (!this.supportsUndoRedo) {
return;
}

addDrawableChild(new HelpButtonWidget(GuiUtil.PADDING, GuiUtil.PADDING));
addDrawableChild(
new IconButtonWidget(GuiUtil.PADDING + IconButtonWidget.WIDTH + (GuiUtil.PADDING / 2), GuiUtil.PADDING, 14,
Text.translatable("armorstands.utility.copy"),
(button) -> ClientNetworking.sendUtilityActionPacket(UtilityAction.COPY)
));
addDrawableChild(
new IconButtonWidget(GuiUtil.PADDING + 2 * (IconButtonWidget.WIDTH + (GuiUtil.PADDING / 2)), GuiUtil.PADDING,
15, Text.translatable("armorstands.utility.paste"),
(button) -> ClientNetworking.sendUtilityActionPacket(UtilityAction.PASTE)
));
addDrawableChild(
new IconButtonWidget(GuiUtil.PADDING + 3 * (IconButtonWidget.WIDTH + (GuiUtil.PADDING / 2)), GuiUtil.PADDING,
17, Text.translatable("armorstands.utility.undo"), (button) -> ClientNetworking.sendUndoPacket(false)
));
addDrawableChild(
new IconButtonWidget(GuiUtil.PADDING + 4 * (IconButtonWidget.WIDTH + (GuiUtil.PADDING / 2)), GuiUtil.PADDING,
18, Text.translatable("armorstands.utility.redo"), (button) -> ClientNetworking.sendUndoPacket(true)
));
}

protected void initLeft() {
initUtilityButtons();
}

protected void initNavigationButtons() {
ScreenType[] screenTypes = ScreenType.values();
int totalWidth = screenTypes.length * NavigationButtonWidget.WIDTH + (screenTypes.length - 1) * NAV_BUTTON_SPACING;

int x = (width - totalWidth) / 2 - 2 * NAV_BUTTON_SPACING;
int y = height - NAV_BUTTON_BOTTOM_PADDING - NavigationButtonWidget.HEIGHT;

for (ScreenType screenType : screenTypes) {
NavigationButtonWidget button = new NavigationButtonWidget(this, x, y, screenType);

if (getScreenType() == screenType) {
this.activeButton = button;
addDrawable(button);
} else {
addDrawableChild(button);
}

x += NAV_BUTTON_SPACING + NavigationButtonWidget.WIDTH;

this.navigationButtons.add(button);
}
}

protected void initRight() {
}

protected void initEnd() {
}

protected void renderActivePageButtonHighlight(DrawContext drawContext) {
if (this.activeButton == null) {
return;
}

RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();

MatrixStack matrixStack = drawContext.getMatrices();
matrixStack.push();
matrixStack.translate(0, 0, 100);
drawContext.drawTexture(SELECTION_TEXTURE, this.activeButton.getX() - 2, this.activeButton.getY() - 2, 0, 0, 24, 24,
24, 24
);
matrixStack.pop();
}

protected void playClickSound() {
Objects.requireNonNull(this.client)
.getSoundManager()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,23 @@ public ScreenType getScreenType() {
return ScreenType.INVENTORY;
}

@Override
protected void initRight() {
this.showArmsToggle = addDrawableChild(new FlagToggleWidget(this.textRenderer, ArmorStandFlag.SHOW_ARMS,
ArmorStandFlag.SHOW_ARMS.getValue(this.armorStand), this.width - GuiUtil.PADDING,
this.height - GuiUtil.PADDING - 2 * FlagToggleWidget.WIDGET_HEIGHT - (GuiUtil.PADDING / 2)
));
this.lockInventoryToggle = addDrawableChild(new FlagToggleWidget(this.textRenderer, ArmorStandFlag.LOCK_INVENTORY,
ArmorStandFlag.LOCK_INVENTORY.getValue(this.armorStand), this.width - GuiUtil.PADDING,
this.height - GuiUtil.PADDING - FlagToggleWidget.WIDGET_HEIGHT
));
protected void populateLayout() {
// this.showArmsToggle = this.layout.bottomRight.add(new FlagToggleWidget(this.textRenderer, ArmorStandFlag.SHOW_ARMS,
// ArmorStandFlag.SHOW_ARMS.getValue(this.armorStand), this.width - GuiUtil.PADDING,
// this.height - GuiUtil.PADDING - 2 * FlagToggleWidget.WIDGET_HEIGHT - (GuiUtil.PADDING / 2)
// ));
// this.lockInventoryToggle = this.layout.bottomRight.add(new FlagToggleWidget(this.textRenderer, ArmorStandFlag.LOCK_INVENTORY,
// ArmorStandFlag.LOCK_INVENTORY.getValue(this.armorStand), this.width - GuiUtil.PADDING,
// this.height - GuiUtil.PADDING - FlagToggleWidget.WIDGET_HEIGHT
// ));
}

@Override
public void handledScreenTick() {
super.handledScreenTick();

this.showArmsToggle.setValue(ArmorStandFlag.SHOW_ARMS.getValue(this.armorStand));
this.lockInventoryToggle.setValue(ArmorStandFlag.LOCK_INVENTORY.getValue(this.armorStand));
// this.showArmsToggle.setValue(ArmorStandFlag.SHOW_ARMS.getValue(this.armorStand));
// this.lockInventoryToggle.setValue(ArmorStandFlag.LOCK_INVENTORY.getValue(this.armorStand));
}

@Override
Expand Down
Loading

0 comments on commit 270453f

Please sign in to comment.