Skip to content

Commit

Permalink
简单实现一下拖拽条的GUI(拖拽不了的拖拽条)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Apr 15, 2024
1 parent 03e0d45 commit 216b508
Show file tree
Hide file tree
Showing 44 changed files with 565 additions and 30 deletions.
4 changes: 2 additions & 2 deletions common/src/main/java/dev/dubhe/anvilcraft/AnvilCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void init() {
REGISTRATE.registerRegistrate();
}

public static @NotNull ResourceLocation of(String id) {
return new ResourceLocation(MOD_ID, id);
public static @NotNull ResourceLocation of(String path) {
return new ResourceLocation(MOD_ID, path);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.block.entity.CreativeDynamoBlockEntity;
import dev.dubhe.anvilcraft.init.ModMenuTypes;
import dev.dubhe.anvilcraft.network.SliderInitPack;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;
Expand All @@ -22,6 +30,26 @@ public CreativeDynamoBlock(Properties properties) {
super(properties);
}

@Override
@SuppressWarnings({"deprecation", "UnreachableCode"})
public @NotNull InteractionResult use(
@NotNull BlockState state, @NotNull Level level,
@NotNull BlockPos pos, @NotNull Player player,
@NotNull InteractionHand hand, @NotNull BlockHitResult hit
) {
if (level.isClientSide) {
return InteractionResult.SUCCESS;
}
if (
level.getBlockEntity(pos) instanceof CreativeDynamoBlockEntity entity
&& player instanceof ServerPlayer serverPlayer
) {
ModMenuTypes.open(serverPlayer, entity, pos);
new SliderInitPack(entity.getPower(), -1024, 1024).send(serverPlayer);
}
return InteractionResult.SUCCESS;
}

@Nullable
@Override
public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package dev.dubhe.anvilcraft.block.entity;

import dev.dubhe.anvilcraft.api.power.IPowerConsumer;
import dev.dubhe.anvilcraft.api.power.IPowerProducer;
import dev.dubhe.anvilcraft.api.power.PowerComponentType;
import dev.dubhe.anvilcraft.api.power.PowerGrid;
import dev.dubhe.anvilcraft.init.ModBlockEntities;
import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.inventory.SliderMenu;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CreativeDynamoBlockEntity extends BlockEntity implements IPowerProducer {
@Getter
@Getter
public class CreativeDynamoBlockEntity extends BlockEntity implements IPowerProducer, IPowerConsumer, MenuProvider {
private PowerGrid grid = null;
@Setter
private int power = 16;

public static @NotNull CreativeDynamoBlockEntity createBlockEntity(
Expand Down Expand Up @@ -44,7 +56,17 @@ public void load(@NotNull CompoundTag tag) {

@Override
public int getOutputPower() {
return this.power;
return this.power > 0 ? this.power : 0;
}

@Override
public int getInputPower() {
return this.power < 0 ? -this.power : 0;
}

@Override
public @NotNull PowerComponentType getComponentType() {
return this.power > 0 ? PowerComponentType.PRODUCER : PowerComponentType.CONSUMER;
}

@Override
Expand All @@ -56,4 +78,15 @@ public int getOutputPower() {
public void setGrid(PowerGrid grid) {
this.grid = grid;
}

@Override
public @NotNull Component getDisplayName() {
return ModBlocks.CREATIVE_DYNAMO.get().getName();
}

@Nullable
@Override
public AbstractContainerMenu createMenu(int i, @NotNull Inventory inventory, @NotNull Player player) {
return new SliderMenu(i, -1024, 1024, this::setPower);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
@Getter
public class EnableFilterButton extends Button {
private final Supplier<Boolean> filterEnabled;
private static final ResourceLocation YES = AnvilCraft.of("textures/gui/container/button_yes.png");
private static final ResourceLocation NO = AnvilCraft.of("textures/gui/container/button_no.png");
private static final ResourceLocation YES = AnvilCraft.of("textures/gui/container/machine/button_yes.png");
private static final ResourceLocation NO = AnvilCraft.of("textures/gui/container/machine/button_no.png");
private static final MutableComponent defaultMessage = Component
.translatable("screen.anvilcraft.button.record", Component.translatable("screen.anvilcraft.button.off"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
public class OutputDirectionButton extends Button {
private Direction direction;
private final List<Direction> skip = new ArrayList<>();
private static final ResourceLocation UP = AnvilCraft.of("textures/gui/container/button_u.png");
private static final ResourceLocation DOWN = AnvilCraft.of("textures/gui/container/button_d.png");
private static final ResourceLocation EAST = AnvilCraft.of("textures/gui/container/button_e.png");
private static final ResourceLocation WEST = AnvilCraft.of("textures/gui/container/button_w.png");
private static final ResourceLocation SOUTH = AnvilCraft.of("textures/gui/container/button_s.png");
private static final ResourceLocation NORTH = AnvilCraft.of("textures/gui/container/button_n.png");
private static final ResourceLocation UP = AnvilCraft.of("textures/gui/container/machine/button_u.png");
private static final ResourceLocation DOWN = AnvilCraft.of("textures/gui/container/machine/button_d.png");
private static final ResourceLocation EAST = AnvilCraft.of("textures/gui/container/machine/button_e.png");
private static final ResourceLocation WEST = AnvilCraft.of("textures/gui/container/machine/button_w.png");
private static final ResourceLocation SOUTH = AnvilCraft.of("textures/gui/container/machine/button_s.png");
private static final ResourceLocation NORTH = AnvilCraft.of("textures/gui/container/machine/button_n.png");
private static final MutableComponent defaultMessage =
Component.translatable("screen.anvilcraft.button.direction",
Component.translatable("screen.anvilcraft.button.direction.up"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package dev.dubhe.anvilcraft.client.gui.component;

import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.inventory.SliderMenu;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

public class Slider extends AbstractWidget {
public static final ResourceLocation SLIDER = AnvilCraft.of("textures/gui/container/slider/slider.png");
@Setter
@Getter
private int min;
@Setter
@Getter
private int max;
@Getter
private int value;
private final int posX;
private final int posY;
private final int length;
public final SliderMenu.Update update;
private int tooltipMsDelay;
private long hoverOrFocusedStartTime;
private boolean wasHoveredOrFocused;

/**
* @param x X
* @param y Y
* @param min 最小值
* @param max 最大值
* @param length 长度
* @param update 更新回调
*/
public Slider(int x, int y, int min, int max, int length, SliderMenu.Update update) {
super(x, y, length, 8, Component.literal("Slider"));
this.posX = x;
this.posY = y;
this.min = min;
this.max = max;
this.length = length;
this.update = update;
}

public double getProportion() {
return Math.max(0.0, Math.min(1.0, (double) (value - this.min) / (this.max - this.min)));
}

public void setProportion(double proportion) {
this.value = (int) ((max - min) * proportion + min);
}

public void setValue(int value) {
this.value = Math.max(this.min, Math.min(this.max, value));
}

/**
* @param value 设置 Value 并更新
*/
public void setValueWithUpdate(int value) {
if (this.value == value) return;
this.setValue(value);
this.update();
}

private void update() {
if (this.update != null) this.update.update(this.value);
}

@Override
public boolean mouseDragged(double mouseX, double mouseY, int button, double dragX, double dragY) {
if (button == 0) {
this.onDrag(mouseX, mouseY, dragX, dragY);
return true;
}
return false;
}

@Override
public void onClick(double mouseX, double mouseY) {
super.onClick(mouseX, mouseY);
if (!isInRange(mouseX, mouseY)) return;
if (isInSlider(mouseX, mouseY)) {
super.onClick(mouseX, mouseY);
return;
}
double offset = 16.0 / this.length;
int offsetX = posX + (int) ((length - 16) * this.getProportion());
if (mouseX < offsetX) this.setProportion(Math.max(0.0, this.getProportion() - offset));
else this.setProportion(Math.min(1.0, this.getProportion() + offset));
this.update();
}

@Override
protected void onDrag(double mouseX, double mouseY, double dragX, double dragY) {
super.onDrag(mouseX, mouseY, dragX, dragY);
if (!isInSlider(mouseX, mouseY)) return;
double offset = dragX - mouseX / this.length;
this.setProportion(Math.max(0.0, Math.min(1.0, this.getProportion() + offset)));
this.update();
}

protected boolean isInSlider(double mouseX, double mouseY) {
int offsetX = posX + (int) ((length - 16) * this.getProportion());
return mouseX > offsetX && mouseX < offsetX + 16 && mouseY > posY && mouseY < posY + 8;
}

protected boolean isInRange(double mouseX, double mouseY) {
return mouseX > posX && mouseX < posX + length && mouseY > posY && mouseY < posY + 8;
}

@Override
public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
if (!this.visible) return;
this.isHovered = this.isInRange(mouseX, mouseY);
this.renderWidget(guiGraphics, mouseX, mouseY, partialTick);
this.updateTooltip();
}

@Override
protected void renderWidget(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
int offsetX = posX + (int) ((length - 16) * this.getProportion());
guiGraphics.blit(SLIDER, offsetX, posY, 0, this.isHovered ? 8 : 0, 16, 8, 16, 16);
}

@Override
protected void updateWidgetNarration(@NotNull NarrationElementOutput narrationElementOutput) {
}

private void updateTooltip() {
if (this.getTooltip() == null) return;
boolean bl = this.isHovered || this.isFocused() && Minecraft.getInstance().getLastInputType().isKeyboard();
if (bl != this.wasHoveredOrFocused) {
if (bl) this.hoverOrFocusedStartTime = Util.getMillis();
this.wasHoveredOrFocused = bl;
}
Screen screen;
if (bl
&& Util.getMillis() - this.hoverOrFocusedStartTime > (long) this.tooltipMsDelay
&& (screen = Minecraft.getInstance().screen) != null
) {
screen.setTooltipForNextRenderPass(this.getTooltip(), this.createTooltipPositioner(), this.isFocused());
}
}

@Override
public void setTooltipDelay(int tooltipMsDelay) {
super.setTooltipDelay(tooltipMsDelay);
this.tooltipMsDelay = tooltipMsDelay;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import java.util.function.BiFunction;

public class AutoCrafterScreen extends BaseMachineScreen<AutoCrafterMenu> implements IFilterScreen {
private static final ResourceLocation CONTAINER_LOCATION = AnvilCraft.of("textures/gui/container/auto_crafter.png");
private static final ResourceLocation CONTAINER_LOCATION =
AnvilCraft.of("textures/gui/container/machine/background/auto_crafter.png");
BiFunction<Integer, Integer, EnableFilterButton> enableFilterButtonSupplier = this
.getEnableFilterButtonSupplier(116, 18);
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import java.util.function.BiFunction;

public class ChuteScreen extends BaseMachineScreen<ChuteMenu> implements IFilterScreen {
private static final ResourceLocation CONTAINER_LOCATION = AnvilCraft.of("textures/gui/container/chute.png");
private static final ResourceLocation CONTAINER_LOCATION =
AnvilCraft.of("textures/gui/container/machine/background/chute.png");


BiFunction<Integer, Integer, EnableFilterButton> enableFilterButtonSupplier = this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* 有过滤的 GUI
*/
public interface IFilterScreen {
ResourceLocation DISABLED_SLOT = AnvilCraft.of("textures/gui/container/disabled_slot.png");
ResourceLocation DISABLED_SLOT = AnvilCraft.of("textures/gui/container/machine/disabled_slot.png");

IFilterMenu getFilterMenu();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import org.jetbrains.annotations.NotNull;

public class RoyalAnvilScreen extends ItemCombinerScreen<RoyalAnvilMenu> {
private static final ResourceLocation ANVIL_LOCATION = AnvilCraft.of("textures/gui/container/royal_anvil.png");
private static final ResourceLocation TEXT_LOCATION = AnvilCraft.of("textures/gui/container/text_field.png");
private static final ResourceLocation TEXT_DISABLE_LOCATION = AnvilCraft
.of("textures/gui/container/text_field_disabled.png");
private static final ResourceLocation ANVIL_LOCATION =
AnvilCraft.of("textures/gui/container/smithing/background/royal_anvil.png");
private static final ResourceLocation TEXT_LOCATION =
AnvilCraft.of("textures/gui/container/smithing/text_field.png");
private static final ResourceLocation TEXT_DISABLE_LOCATION =
AnvilCraft.of("textures/gui/container/smithing/text_field_disabled.png");
private EditBox name;
private final Player player;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class RoyalGrindstoneScreen extends AbstractContainerScreen<RoyalGrindstoneMenu> {
private static final ResourceLocation GRINDSTONE_LOCATION =
AnvilCraft.of("textures/gui/container/royal_grindstone.png");
AnvilCraft.of("textures/gui/container/smithing/background/royal_grindstone.png");

public RoyalGrindstoneScreen(
RoyalGrindstoneMenu menu, Inventory playerInventory, @SuppressWarnings("unused") Component title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@

public class RoyalSmithingScreen extends ItemCombinerScreen<RoyalSmithingMenu> {
private static final ResourceLocation SMITHING_LOCATION =
AnvilCraft.of("textures/gui/container/royal_smithing_table.png");
private static final ResourceLocation ERROR = AnvilCraft.of("textures/gui/container/error.png");
AnvilCraft.of("textures/gui/container/smithing/background/royal_smithing_table.png");
private static final ResourceLocation ERROR =
AnvilCraft.of("textures/gui/container/smithing/error.png");
private static final ResourceLocation EMPTY_SLOT_SMITHING_TEMPLATE_ARMOR_TRIM =
new ResourceLocation("item/empty_slot_smithing_template_armor_trim");
private static final ResourceLocation EMPTY_SLOT_SMITHING_TEMPLATE_NETHERITE_UPGRADE =
Expand Down
Loading

0 comments on commit 216b508

Please sign in to comment.