|
5 | 5 | import net.minecraft.client.gui.DrawContext; |
6 | 6 | import net.minecraft.client.gui.screen.Screen; |
7 | 7 | import net.minecraft.text.Text; |
| 8 | +import org.jspecify.annotations.NonNull; |
8 | 9 |
|
9 | 10 | import java.util.ArrayList; |
10 | 11 | import java.util.List; |
11 | 12 |
|
| 13 | +import static dev.nectar.Nectar.mc; |
| 14 | + |
12 | 15 | public class ClickGUI extends Screen { |
13 | 16 |
|
14 | 17 | public static final ClickGUI INSTANCE = new ClickGUI(); |
15 | | - private final List<Frame> frames = new ArrayList<>();; |
| 18 | + private final List<Component> components = new ArrayList<>(); |
| 19 | + private final List<CategoryButton> categoryButtons = new ArrayList<>(); |
16 | 20 | private final ModulesContainer modulesContainer; |
| 21 | + private CategoryButton selectedCategory = null; |
17 | 22 |
|
18 | 23 | private ClickGUI() { |
19 | 24 | super(Text.literal("ClickGUI")); |
20 | 25 |
|
21 | | - int offsetY = 20; |
22 | | - int offsetX = 20; |
| 26 | + modulesContainer = initClickGUI(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Init ClickGUI |
| 31 | + * |
| 32 | + * @return |
| 33 | + */ |
| 34 | + private @NonNull ModulesContainer initClickGUI() { |
| 35 | + final ModulesContainer modulesContainer; |
| 36 | + int mcWidth = 500; |
| 37 | + int mcHeight = 350; |
| 38 | + int modulesWidth = 100; |
| 39 | + int modulesHeight = mcHeight / 6; |
23 | 40 |
|
24 | | - modulesContainer = new ModulesContainer(20, 20, 500, 350); |
| 41 | + int modulesX = mc.getWindow().getWidth() / 2 - mcWidth - (mcWidth / 2) - modulesWidth; |
| 42 | + int modulesY = mc.getWindow().getHeight() / 2 - mcHeight - (mcHeight / 4); |
25 | 43 |
|
26 | 44 | for (Module.Category category : Module.Category.values()) { |
27 | | - frames.add(new Frame(offsetX, 20 + offsetY, 100, 20, category)); |
28 | | - offsetX += 100; |
| 45 | + CategoryButton newButton = new CategoryButton(modulesX, modulesY, modulesWidth, modulesHeight, category); |
| 46 | + |
| 47 | + categoryButtons.add(newButton); |
| 48 | + components.add(newButton); |
| 49 | + |
| 50 | + modulesY += modulesHeight; |
29 | 51 | } |
| 52 | + |
| 53 | + // Select the first category by default |
| 54 | + selectedCategory = categoryButtons.getFirst(); |
| 55 | + selectedCategory.select(); |
| 56 | + |
| 57 | + int mcX = modulesX + modulesWidth; |
| 58 | + int mcY = mc.getWindow().getHeight() / 2 - mcHeight - (mcHeight / 4); |
| 59 | + |
| 60 | + modulesContainer = new ModulesContainer(mcX, mcY, mcWidth, mcHeight); |
| 61 | + components.add(modulesContainer); |
| 62 | + |
| 63 | + return modulesContainer; |
30 | 64 | } |
31 | 65 |
|
32 | 66 | @Override |
33 | 67 | public void render(DrawContext context, int mouseX, int mouseY, float deltaTicks) { |
34 | | - modulesContainer.render(context); |
35 | | - |
36 | | - for (Frame frame : frames) { |
37 | | - frame.render(context, mouseX, mouseY, deltaTicks); |
38 | | - frame.updatePos(mouseX, mouseY); |
39 | | - } |
| 68 | + components.forEach(component -> component.render(context, mouseX, mouseY)); |
40 | 69 | } |
41 | 70 |
|
42 | 71 | @Override |
43 | 72 | public boolean mouseClicked(Click click, boolean doubled) { |
44 | | - for (Frame frame : frames) { |
45 | | - frame.mouseClicked(click.x(), click.y(), click.button()); |
46 | | - } |
47 | | - return true; |
48 | | - } |
| 73 | + components.forEach(component -> { |
| 74 | + if (!(component instanceof CategoryButton)) component.mouseClicked(click.x(), click.y(), click.button()); |
49 | 75 |
|
50 | | - @Override |
51 | | - public boolean mouseReleased(Click click) { |
52 | | - for (Frame frame : frames) { |
53 | | - frame.mouseReleased(click.x(), click.y(), click.button()); |
54 | | - } |
| 76 | + // Handle ModulesButton |
| 77 | + if (!component.mouseClicked(click.x(), click.y(), click.button())) return; |
| 78 | + |
| 79 | + // Update the selected category |
| 80 | + selectedCategory.deselect(); |
| 81 | + selectedCategory = (CategoryButton) component; |
| 82 | + modulesContainer.updateCategory(selectedCategory.category); |
| 83 | + }); |
55 | 84 | return true; |
56 | 85 | } |
57 | 86 | } |
0 commit comments