Skip to content

Commit

Permalink
Buttons :> (for some reason onpress and moving them is broken but ohw)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReclipseTheOne committed Jan 18, 2025
1 parent b7378af commit ef598fd
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
import com.portingdeadmods.researchd.client.screens.queue.ResearchQueue;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
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 ResearchScreen extends Screen {
public static final ResourceLocation TOP_BAR_TEXTURE = Researchd.rl("textures/gui/top_bar.png");
Expand All @@ -32,7 +36,7 @@ public ResearchScreen() {
.map(n -> new TechListEntry(n.getResearch(), 0, 0)).toList()));

// QUEUE
this.researchQueue = new ResearchQueue(0, 0);
this.researchQueue = new ResearchQueue(this, 0, 0);
// this.researchQueue.fillList();

// GRAPH
Expand All @@ -55,6 +59,9 @@ protected void init() {
addRenderableWidget(this.techList.startResearchButton);
addRenderableWidget(this.researchGraphWidget);
addRenderableWidget(this.selectedResearchWidget);
addRenderableWidget(this.researchQueue.leftButton);
addRenderableWidget(this.researchQueue.rightButton);
addRenderableWidget(this.researchQueue.removeButton);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public void onSearchButtonClicked(Button button) {
public void onStartResearchButtonClicked(Button button) {
ResearchQueue queue = this.screen.getResearchQueue();
queue.addEntry(this.screen.getSelectedResearchWidget().getEntry());
System.out.println("Research started");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
import com.portingdeadmods.portingdeadlibs.utils.renderers.GuiUtils;
import com.portingdeadmods.researchd.Researchd;
import com.portingdeadmods.researchd.api.research.ResearchInstance;
import com.portingdeadmods.researchd.client.screens.ResearchScreen;
import com.portingdeadmods.researchd.client.screens.list.EntryType;
import com.portingdeadmods.researchd.client.screens.list.TechListEntry;
import com.portingdeadmods.researchd.client.screens.widgets.QueueControllsButton;
import com.portingdeadmods.researchd.impl.research.SimpleResearch;
import com.portingdeadmods.researchd.registries.Researches;
import com.portingdeadmods.researchd.utils.researches.ResearchGraphCache;
import net.minecraft.ChatFormatting;
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.Button;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.components.WidgetSprites;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
Expand All @@ -29,16 +36,65 @@ public class ResearchQueue extends AbstractWidget {

private final List<TechListEntry> queue;

public ResearchQueue(int x, int y) {
public ImageButton leftButton;
public ImageButton rightButton;
public ImageButton removeButton;

private final ResearchScreen screen;

public ResearchQueue(ResearchScreen screen, int x, int y) {
super(x, y, BACKGROUND_WIDTH, BACKGROUND_HEIGHT, Component.empty());
this.queue = new ArrayList<>();
this.screen = screen;
setLeftButton(-1);
setRightButton(-1);
setRemoveButton(-1);
}

public void addEntry(TechListEntry entry) {
if (this.queue.size() >= 7) return;
if (entry.getResearch().getResearchStatus() == EntryType.RESEARCHED) return;

this.queue.add(entry);
for (TechListEntry e : this.queue) {
if (e.getResearch().getResearch().equals(entry.getResearch().getResearch())) return;
}

this.queue.add(new TechListEntry(entry.getResearch(), 12 + this.queue.size() * TechListEntry.WIDTH, 17));
}

public void removeEntry(int index) {
System.out.println("Removing entry at index: " + index);
ArrayList<TechListEntry> copy = new ArrayList<>(this.queue);

this.queue.clear();
copy.remove(index);

for (TechListEntry entry : copy) {
addEntry(entry);
}
}

public void moveEntry(int index, boolean left) {
if (left && index == 0) return;
if (!left && index == this.queue.size() - 1) return;

if (!left) index++;

String direction = left ? "left" : "right";
System.out.println("Moving entry at index " + index + " to the " + direction);

ArrayList<TechListEntry> copy = new ArrayList<>(this.queue);

TechListEntry entry1 = copy.get(index - 1);
TechListEntry entry2 = copy.get(index);

copy.set(index - 1, entry2);
copy.set(index, entry1);

this.queue.clear();
for (TechListEntry entry : copy) {
addEntry(entry);
}
}

public void fillList() {
Expand All @@ -60,4 +116,66 @@ protected void renderWidget(GuiGraphics guiGraphics, int i, int i1, float v) {
protected void updateWidgetNarration(NarrationElementOutput narrationElementOutput) {

}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
int paddingX = getX() + 12;
int paddingY = 17 + getY();
if (
mouseX > paddingX &&
mouseX < paddingX + this.queue.size() * TechListEntry.WIDTH &&
mouseY > paddingY &&
mouseY < paddingY + TechListEntry.HEIGHT
)
{
int indexX = ((int) mouseX - paddingX) / TechListEntry.WIDTH;
setLeftButton(indexX);
setRightButton(indexX);
setRemoveButton(indexX);
}

return false;
}

private void setLeftButton(int index) {
System.out.println("Setting left button to index: " + index);
if (index == -1) {
this.leftButton = new QueueControllsButton(index, "left", 12 + index * TechListEntry.WIDTH, 17, 0, 0, new WidgetSprites(
Researchd.rl("left_button"),
Researchd.rl("left_button_highlighted")
), button -> moveEntry(index, true), CommonComponents.EMPTY);
}
this.leftButton = new QueueControllsButton(index, "left", 12 + index * TechListEntry.WIDTH, 17 + TechListEntry.HEIGHT + 2, 7, 4, new WidgetSprites(
Researchd.rl("left_button"),
Researchd.rl("left_button_highlighted")
), button -> moveEntry(index, true), CommonComponents.EMPTY);
}

private void setRightButton(int index) {
System.out.println("Setting right button to index: " + index);
if (index == -1) {
this.rightButton = new QueueControllsButton(index, "right", 12 + index * TechListEntry.WIDTH + 16, 17, 0, 0, new WidgetSprites(
Researchd.rl("right_button"),
Researchd.rl("right_button_highlighted")
), button -> moveEntry(index, false), CommonComponents.EMPTY);
}
this.rightButton = new QueueControllsButton(index, "right", 12 + index * TechListEntry.WIDTH + 7 + 4 + 2, 17 + TechListEntry.HEIGHT + 2, 7, 4, new WidgetSprites(
Researchd.rl("right_button"),
Researchd.rl("right_button_highlighted")
), button -> moveEntry(index, false), CommonComponents.EMPTY);
}

private void setRemoveButton(int index) {
System.out.println("Setting remove button to index: " + index);
if (index == -1) {
this.removeButton = new QueueControllsButton(index, "remove", 12 + index * TechListEntry.WIDTH + 32, 17, 0, 0, new WidgetSprites(
Researchd.rl("remove_button"),
Researchd.rl("remove_button_highlighted")
), button -> removeEntry(index), CommonComponents.EMPTY);
}
this.removeButton = new QueueControllsButton(index, "remove", 12 + index * TechListEntry.WIDTH + 7 + 1, 17 + TechListEntry.HEIGHT + 2, 4, 4, new WidgetSprites(
Researchd.rl("remove_button"),
Researchd.rl("remove_button_highlighted")
), button -> removeEntry(index), CommonComponents.EMPTY);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.portingdeadmods.researchd.client.screens.widgets;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.components.WidgetSprites;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class QueueControllsButton extends ImageButton {
protected final WidgetSprites sprites;
private final int index;
private final String type;

public QueueControllsButton(int index, String type, int x, int y, int width, int height, WidgetSprites sprites, Button.OnPress onPress, Component message) {
super(x, y, width, height, sprites, onPress, message);
this.index = index;
this.type = type;
this.sprites = sprites;
}

public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
ResourceLocation resourcelocation = this.sprites.get(this.isActive(), this.isHoveredOrFocused());
guiGraphics.blitSprite(resourcelocation, this.getX(), this.getY(), this.width, this.height);
}

public int getIndex() {
return this.index;
}

public String getType() {
return this.type;
}
}
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.
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 ef598fd

Please sign in to comment.