Skip to content

Commit

Permalink
clock screen, pt 6
Browse files Browse the repository at this point in the history
  • Loading branch information
IchHabeHunger54 committed Jan 6, 2025
1 parent bfb32a5 commit 5322ec8
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ protected void init() {
.pos(leftPos + 7, topPos + 6)
.selected(clock.tickSound)
.build());
triggerPanel = addRenderableWidget(new ClockTriggerPanel(leftPos + 8, topPos + 36, 160, 122, triggers));
addRenderableWidget(Button.builder(ADD_TRIGGER, $ -> Minecraft.getInstance().pushGuiLayer(new ClockTriggerEditScreen(this)))
triggerPanel = addRenderableWidget(new ClockTriggerPanel(leftPos + 8, topPos + 36, 160, 122, triggers, this));
addRenderableWidget(Button.builder(ADD_TRIGGER, $ -> Minecraft.getInstance().pushGuiLayer(new ClockTriggerEditScreen(this, null)))
.bounds(width / 2 - 100, topPos + 170, 98, 20)
.build());
addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, $ -> onClose())
Expand All @@ -72,6 +72,7 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTi
super.render(graphics, mouseX, mouseY, partialTick);
graphics.drawString(Minecraft.getInstance().font, TICK, leftPos + 28, topPos + 11, 0x404040, false);
graphics.drawString(Minecraft.getInstance().font, TRIGGERS, leftPos + 8, topPos + 26, 0x404040, false);
triggerPanel.renderTooltip(graphics, mouseX, mouseY);
}

@Override
Expand All @@ -83,6 +84,12 @@ public void renderBackground(GuiGraphics graphics, int mouseX, int mouseY, float
public void addTrigger(ClockTrigger trigger) {
triggers.add(trigger);
triggers.sort(ClockTrigger::compareTo);
triggerPanel.buildElements(triggers);
triggerPanel.rebuildElements(triggers);
}

public void removeTrigger(ClockTrigger trigger) {
triggers.remove(trigger);
triggers.sort(ClockTrigger::compareTo);
triggerPanel.rebuildElements(triggers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

public class ClockTriggerEditScreen extends Screen {
private static final ResourceLocation BACKGROUND = BCUtil.modLoc("textures/gui/clock_edit.png");
Expand All @@ -28,6 +29,8 @@ public class ClockTriggerEditScreen extends Screen {
private static final int WIDTH = 144;
private static final int HEIGHT = 72;
private final ClockScreen parent;
@Nullable
private final ClockTrigger old;
private final int timeWidth;
private final int separatorWidth;
private final int redstoneWidth;
Expand All @@ -41,9 +44,10 @@ public class ClockTriggerEditScreen extends Screen {
private Checkbox redstone;
private Checkbox sound;

public ClockTriggerEditScreen(ClockScreen parent) {
public ClockTriggerEditScreen(ClockScreen parent, @Nullable ClockTrigger old) {
super(TITLE);
this.parent = parent;
this.old = old;
Font font = Minecraft.getInstance().font;
timeWidth = font.width(TIME);
separatorWidth = font.width(TIME_SEPARATOR);
Expand All @@ -65,7 +69,7 @@ protected void init() {
int i = Integer.parseInt(s);
return i >= 0 && i < 24;
} catch (NumberFormatException e) {
return false;
return s.isEmpty();
}
});
minutes = addRenderableWidget(new EditBox(font, contentLeftPos + timeWidth + separatorWidth + 44, contentTopPos, 40, 20, MINUTES));
Expand All @@ -75,18 +79,31 @@ protected void init() {
int i = Integer.parseInt(s);
return i >= 0 && i < 60;
} catch (NumberFormatException e) {
return false;
return s.isEmpty();
}
});
redstone = addRenderableWidget(Checkbox.builder(Component.empty(), font).pos(contentLeftPos, contentTopPos + 22).build());
sound = addRenderableWidget(Checkbox.builder(Component.empty(), font).pos(contentLeftPos, contentTopPos + 41).build());
addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, $ -> {
try {
if (old != null) {
parent.removeTrigger(old);
}
parent.addTrigger(new ClockTrigger(Integer.parseInt(hours.getValue()), Integer.parseInt(minutes.getValue()), redstone.selected(), sound.selected()));
} catch (NumberFormatException ignored) {
}
onClose();
}).bounds(leftPos, topPos + HEIGHT + 4, WIDTH, 20).build());
if (old != null) {
hours.setValue(String.valueOf(old.hour()));
minutes.setValue(String.valueOf(old.minute()));
if (old.redstone()) {
redstone.onPress();
}
if (old.sound()) {
sound.onPress();
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.github.minecraftschurlimods.bibliocraft.client.screen.clock;

import com.github.minecraftschurlimods.bibliocraft.client.SpriteButton;
import com.github.minecraftschurlimods.bibliocraft.content.clock.ClockTrigger;
import com.github.minecraftschurlimods.bibliocraft.util.BCUtil;
import com.github.minecraftschurlimods.bibliocraft.util.ClientUtil;
import com.github.minecraftschurlimods.bibliocraft.util.Translations;
import com.mojang.blaze3d.vertex.PoseStack;
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.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.texture.OverlayTexture;
Expand All @@ -17,27 +18,42 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;

public class ClockTriggerElement extends AbstractWidget {
public class ClockTriggerElement extends Screen {
public static final int WIDTH = 160;
public static final int HEIGHT = 20;
private static final ResourceLocation BACKGROUND = BCUtil.modLoc("textures/gui/clock_trigger.png");
private static final ResourceLocation EDIT = BCUtil.modLoc("edit");
private static final ResourceLocation EDIT_HIGHLIGHTED = BCUtil.modLoc("edit_highlighted");
private static final ResourceLocation DELETE = BCUtil.modLoc("delete");
private static final ResourceLocation DELETE_HIGHLIGHTED = BCUtil.modLoc("delete_highlighted");
private static final Component DELETE_TRIGGER = Component.translatable(Translations.CLOCK_DELETE_TRIGGER);
private static final Component EDIT_TRIGGER = Component.translatable(Translations.CLOCK_EDIT_TRIGGER);
private static final Component EMIT_REDSTONE = Component.translatable(Translations.CLOCK_EMIT_REDSTONE);
private static final Component EMIT_SOUND = Component.translatable(Translations.CLOCK_EMIT_SOUND);
private static final Component SEPARATOR = Component.translatable(Translations.CLOCK_TIME_SEPARATOR);
private static final ItemStack REDSTONE = new ItemStack(Items.REDSTONE);
private static final ItemStack NOTE_BLOCK = new ItemStack(Items.NOTE_BLOCK);
public final ClockTriggerPanel owner;
private final ClockTrigger trigger;
private final ClockTriggerPanel owner;
private int index;
private final int listSize;
private final Button editButton;
private final Button deleteButton;

public ClockTriggerElement(int x, int y, ClockTrigger trigger, ClockTriggerPanel owner, int index) {
super(x, y, WIDTH, HEIGHT, Component.translatable("%s%s%s", trigger.hour(), SEPARATOR.getString(), trigger.minute()));
public ClockTriggerElement(ClockTrigger trigger, ClockTriggerPanel owner, int listSize) {
super(Component.literal(String.format("%02d%s%02d", trigger.hour(), SEPARATOR.getString(), trigger.minute())));
this.width = WIDTH;
this.height = HEIGHT;
this.trigger = trigger;
this.owner = owner;
this.index = index;
this.listSize = listSize;
int width = owner.hasScrollbar(listSize) ? WIDTH - 6 : WIDTH;
editButton = addRenderableWidget(new SpriteButton.RegularAndHighlightSprite(EDIT, EDIT_HIGHLIGHTED, width - 34, 2, 16, 16, $ -> Minecraft.getInstance().pushGuiLayer(new ClockTriggerEditScreen(owner.owner, trigger))));
deleteButton = addRenderableWidget(new SpriteButton.RegularAndHighlightSprite(DELETE, DELETE_HIGHLIGHTED, width - 18, 2, 16, 16, $ -> owner.owner.removeTrigger(trigger)));
}

@Override
protected void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
boolean hasScrollbar = owner.hasScrollbar();
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
boolean hasScrollbar = owner.hasScrollbar(listSize);
graphics.blit(BACKGROUND, 0, 0, 0, hasScrollbar ? 20 : 0, hasScrollbar ? WIDTH - 6 : WIDTH, HEIGHT);
PoseStack pose = graphics.pose();
MultiBufferSource.BufferSource buffer = graphics.bufferSource();
Expand All @@ -53,10 +69,25 @@ protected void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float
ClientUtil.renderGuiItem(NOTE_BLOCK, pose, buffer, LightTexture.FULL_BRIGHT, OverlayTexture.NO_OVERLAY);
}
pose.popPose();
graphics.drawString(Minecraft.getInstance().font, getMessage(), 36, 7, 0x404040, false);
graphics.drawString(Minecraft.getInstance().font, getTitle(), 36, 7, 0x404040, false);
super.render(graphics, mouseX, mouseY, partialTick);
}

@Override
protected void updateWidgetNarration(NarrationElementOutput narrationElementOutput) {
public void renderBackground(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
}

public void renderTooltip(GuiGraphics graphics, int mouseX, int mouseY) {
if (mouseY >= 2 && mouseY < 18) {
if (trigger.redstone() && mouseX >= 2 && mouseX < 18) {
graphics.renderTooltip(Minecraft.getInstance().font, EMIT_REDSTONE, mouseX, mouseY);
} else if (trigger.sound() && mouseX >= 19 && mouseX < 37) {
graphics.renderTooltip(Minecraft.getInstance().font, EMIT_SOUND, mouseX, mouseY);
} else if (editButton.isHovered()) {
graphics.renderTooltip(Minecraft.getInstance().font, EDIT_TRIGGER, mouseX, mouseY);
} else if (deleteButton.isHovered()) {
graphics.renderTooltip(Minecraft.getInstance().font, DELETE_TRIGGER, mouseX, mouseY);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,63 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.neoforged.neoforge.client.gui.widget.ScrollPanel;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class ClockTriggerPanel extends ScrollPanel {
public final ClockScreen owner;
public final int x;
public final int y;
public final int width;
public final int height;
private final List<ClockTriggerElement> elements;

public ClockTriggerPanel(int left, int top, int width, int height, List<ClockTrigger> triggers) {
super(Minecraft.getInstance(), width, height, top, left);
public ClockTriggerPanel(int x, int y, int width, int height, List<ClockTrigger> triggers, ClockScreen owner) {
super(Minecraft.getInstance(), width, height, y, x, 0);
this.owner = owner;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
elements = new ArrayList<>();
buildElements(triggers);
rebuildElements(triggers);
}

@Override
protected int getContentHeight() {
return elements.size() * ClockTriggerElement.HEIGHT - 4;
return elements.size() * ClockTriggerElement.HEIGHT;
}

@Override
protected void drawPanel(GuiGraphics guiGraphics, int entryRight, int relativeY, Tesselator tess, int mouseX, int mouseY) {
PoseStack pose = guiGraphics.pose();
protected void drawPanel(GuiGraphics graphics, int entryRight, int relativeY, Tesselator tess, int mouseX, int mouseY) {
PoseStack pose = graphics.pose();
pose.pushPose();
pose.translate(left, relativeY - ClockTriggerElement.HEIGHT - 4, 0);
for (ClockTriggerElement element : elements) {
pose.translate(0, ClockTriggerElement.HEIGHT, 0);
pose.translate(left, relativeY, 0);
for (int i = 0; i < elements.size(); i++) {
pose.pushPose();
element.renderWidget(guiGraphics, mouseX, mouseY, 1);
elements.get(i).render(graphics, mouseX - left, mouseY - i * ClockTriggerElement.HEIGHT - relativeY, 1);
pose.popPose();
pose.translate(0, ClockTriggerElement.HEIGHT, 0);
}
pose.popPose();
}

public void renderTooltip(GuiGraphics graphics, int mouseX, int mouseY) {
ClockTriggerElement hovered = getHovered(mouseX, mouseY);
if (hovered == null) return;
float y = mouseY - top + scrollDistance;
PoseStack pose = graphics.pose();
pose.pushPose();
pose.translate(left, mouseY, 0);
hovered.renderTooltip(graphics, mouseX - left, (int) (y % ClockTriggerElement.HEIGHT));
pose.popPose();
}

@Override
protected int getScrollAmount() {
return ClockTriggerElement.HEIGHT / 2;
return hasScrollbar(elements.size()) ? ClockTriggerElement.HEIGHT / 2 : 0;
}

@Override
Expand All @@ -53,14 +75,47 @@ public NarrationPriority narrationPriority() {
public void updateNarration(NarrationElementOutput narrationElementOutput) {
}

public boolean hasScrollbar() {
return elements.size() * ClockTriggerElement.HEIGHT > height;
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
ClockTriggerElement hovered = getHovered(mouseX, mouseY);
return hovered == null ? super.mouseClicked(mouseX, mouseY, button) : hovered.mouseClicked(mouseX - left, (mouseY - top + scrollDistance) % ClockTriggerElement.HEIGHT, button);
}

@Override
public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
ClockTriggerElement hovered = getHovered(mouseX, mouseY);
return hovered == null ? super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY) : hovered.mouseDragged(mouseX - left, (mouseY - top + scrollDistance) % ClockTriggerElement.HEIGHT, button, deltaX, deltaY);
}

@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
ClockTriggerElement hovered = getHovered(mouseX, mouseY);
return hovered == null ? super.mouseReleased(mouseX, mouseY, button) : hovered.mouseReleased(mouseX - left, (mouseY - top + scrollDistance) % ClockTriggerElement.HEIGHT, button);
}

@Override
public boolean mouseScrolled(double p_94686_, double p_94687_, double p_94688_, double p_294830_) {
return hasScrollbar(elements.size()) && super.mouseScrolled(p_94686_, p_94687_, p_94688_, p_294830_);
}

public boolean hasScrollbar(int elements) {
return elements * ClockTriggerElement.HEIGHT > height;
}

public void buildElements(List<ClockTrigger> triggers) {
public void rebuildElements(List<ClockTrigger> triggers) {
elements.clear();
for (int i = 0; i < triggers.size(); i++) {
elements.add(new ClockTriggerElement(0, i * ClockTriggerElement.HEIGHT, triggers.get(i), this, i));
elements.add(new ClockTriggerElement(triggers.get(i), this, triggers.size()));
}
}

@Nullable
public ClockTriggerElement getHovered(double mouseX, double mouseY) {
double x = mouseX - left;
if (x < 0 || x >= width) return null;
double y = mouseY - top + scrollDistance;
if (y < scrollDistance || y >= height + scrollDistance) return null;
int index = (int) (y / ClockTriggerElement.HEIGHT);
return index < 0 || index >= elements.size() ? null : elements.get(index);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
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 5322ec8

Please sign in to comment.