Skip to content

Commit

Permalink
New game GUI experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 authored and douira committed Oct 24, 2024
1 parent a94d3b7 commit bb8e33c
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.caffeinemc.mods.sodium.client.gui;

import com.mojang.blaze3d.systems.RenderSystem;
import net.caffeinemc.mods.sodium.client.SodiumClientMod;
import net.caffeinemc.mods.sodium.client.data.fingerprint.HashedFingerprint;
import net.caffeinemc.mods.sodium.client.console.Console;
Expand All @@ -11,6 +12,7 @@
import net.caffeinemc.mods.sodium.client.gui.prompt.ScreenPrompt;
import net.caffeinemc.mods.sodium.client.gui.prompt.ScreenPromptable;
import net.caffeinemc.mods.sodium.client.gui.screen.ConfigCorruptedScreen;
import net.caffeinemc.mods.sodium.client.gui.widgets.CenteredFlatWidget;
import net.caffeinemc.mods.sodium.client.gui.widgets.FlatButtonWidget;
import net.caffeinemc.mods.sodium.client.services.PlatformRuntimeInformation;
import net.caffeinemc.mods.sodium.client.util.Dim2i;
Expand Down Expand Up @@ -54,6 +56,7 @@ public class SodiumOptionsGUI extends Screen implements ScreenPromptable {
private ControlElement<?> hoveredElement;

private @Nullable ScreenPrompt prompt;
private FlatButtonWidget searchButton;

private SodiumOptionsGUI(Screen prevScreen) {
super(Component.literal("Sodium Renderer Settings"));
Expand Down Expand Up @@ -161,13 +164,14 @@ private void rebuildGUI() {
}

this.rebuildGUIPages();
this.rebuildGUIOptions();
int pageY = this.rebuildGUIOptions();

this.undoButton = new FlatButtonWidget(new Dim2i(this.width - 211, this.height - 30, 65, 20), Component.translatable("sodium.options.buttons.undo"), this::undoChanges);
this.applyButton = new FlatButtonWidget(new Dim2i(this.width - 142, this.height - 30, 65, 20), Component.translatable("sodium.options.buttons.apply"), this::applyChanges);
this.closeButton = new FlatButtonWidget(new Dim2i(this.width - 73, this.height - 30, 65, 20), Component.translatable("gui.done"), this::onClose);
this.donateButton = new FlatButtonWidget(new Dim2i(this.width - 128, 6, 100, 20), Component.translatable("sodium.options.buttons.donate"), this::openDonationPage);
this.hideDonateButton = new FlatButtonWidget(new Dim2i(this.width - 26, 6, 20, 20), Component.literal("x"), this::hideDonationButton);
this.undoButton = new FlatButtonWidget(new Dim2i(270, this.height - 30, 65, 20), Component.translatable("sodium.options.buttons.undo"), this::undoChanges, true, false);
this.applyButton = new FlatButtonWidget(new Dim2i(130, this.height - 30, 65, 20), Component.translatable("sodium.options.buttons.apply"), this::applyChanges, true, false);
this.closeButton = new FlatButtonWidget(new Dim2i(200, this.height - 30, 65, 20), Component.translatable("gui.done"), this::onClose, true, false);
this.donateButton = new FlatButtonWidget(new Dim2i(this.width - 128, 6, 100, 20), Component.translatable("sodium.options.buttons.donate"), this::openDonationPage, true, false);
this.hideDonateButton = new FlatButtonWidget(new Dim2i(this.width - 26, 6, 20, 20), Component.literal("x"), this::hideDonationButton, true, false);
this.searchButton = new FlatButtonWidget(new Dim2i(0, this.height - 30, 125, 20), Component.literal("Search...").withStyle(ChatFormatting.ITALIC, ChatFormatting.GRAY), this::hideDonationButton, true, true);

if (SodiumClientMod.options().notifications.hasClearedDonationButton) {
this.setDonationButtonVisibility(false);
Expand All @@ -176,6 +180,7 @@ private void rebuildGUI() {
this.addRenderableWidget(this.undoButton);
this.addRenderableWidget(this.applyButton);
this.addRenderableWidget(this.closeButton);
this.addRenderableWidget(this.searchButton);
this.addRenderableWidget(this.donateButton);
this.addRenderableWidget(this.hideDonateButton);
}
Expand All @@ -199,24 +204,45 @@ private void hideDonationButton() {
}

private void rebuildGUIPages() {
int x = 6;
int y = 6;
int x = 0;
int y = 5;
int width = 125;

CenteredFlatWidget header = new CenteredFlatWidget(new Dim2i(x, y, width, font.lineHeight * 2), Component.literal("Sodium Renderer"), () -> {}, false);

y += font.lineHeight * 2;

this.addRenderableWidget(header);

for (OptionPage page : this.pages) {
int width = 12 + this.font.width(page.getName());

FlatButtonWidget button = new FlatButtonWidget(new Dim2i(x, y, width, 18), page.getName(), () -> this.setPage(page));
CenteredFlatWidget button = new CenteredFlatWidget(new Dim2i(x, y, width, font.lineHeight * 2), page.getName(), () -> this.setPage(page), true);
button.setSelected(this.currentPage == page);

x += width + 6;
y += font.lineHeight * 2;

this.addRenderableWidget(button);
}
/*
CenteredFlatWidget button = new CenteredFlatWidget(new Dim2i(x, y, width, font.lineHeight * 2), Component.literal("" +
"Iris Shaders"), () -> {}, false);
y += font.lineHeight * 2;
this.addRenderableWidget(button);
CenteredFlatWidget button2 = new CenteredFlatWidget(new Dim2i(x, y, width, font.lineHeight * 2), Component.literal("Shader Packs"), () -> {}, true);
y += font.lineHeight * 2;
this.addRenderableWidget(button2);
*/
}

private void rebuildGUIOptions() {
int x = 6;
int y = 28;
private int rebuildGUIOptions() {
int x = (int) (130);
int y = 23;

for (OptionGroup group : this.currentPage.getGroups()) {
// Add each option's control element
Expand All @@ -235,12 +261,15 @@ private void rebuildGUIOptions() {
// Add padding beneath each option group
y += 4;
}

return y;
}

@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
this.updateControls();


super.render(graphics, this.prompt != null ? -1 : mouseX, this.prompt != null ? -1 : mouseY, delta);

if (this.hoveredElement != null) {
Expand All @@ -252,6 +281,13 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
}
}

@Override
protected void renderMenuBackground(GuiGraphics guiGraphics, int i, int j, int k, int l) {
guiGraphics.fillGradient(0, 0, 125, this.minecraft.getMainRenderTarget().height, 0x40000000, 0x90000000);
//graphics.fill(0, 0, 335, 20, 0x40000000);
RenderSystem.enableBlend();
}

private void updateControls() {
ControlElement<?> hovered = this.getActiveControls()
.filter(ControlElement::isHovered)
Expand Down Expand Up @@ -292,10 +328,10 @@ private Stream<ControlElement<?>> getActiveControls() {
private void renderOptionTooltip(GuiGraphics graphics, ControlElement<?> element) {
Dim2i dim = element.getDimensions();

int textPadding = 3;
int boxPadding = 3;
int textPadding = 5;
int boxPadding = 5;

int boxWidth = 200;
int boxWidth = this.width - 340;

int boxY = dim.y();
int boxX = dim.getLimitX() + boxPadding;
Expand All @@ -318,7 +354,7 @@ private void renderOptionTooltip(GuiGraphics graphics, ControlElement<?> element
boxY -= boxYLimit - boxYCutoff;
}

graphics.fillGradient(boxX, boxY, boxX + boxWidth, boxY + boxHeight, 0xE0000000, 0xE0000000);
graphics.fill(boxX, boxY, boxX + boxWidth, boxY + boxHeight, 0x40000000);

for (int i = 0; i < tooltip.size(); i++) {
graphics.drawString(this.font, tooltip.get(i), boxX + textPadding, boxY + textPadding + (i * 12), 0xFFFFFFFF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public void init() {
int boxX = (parentDimensions.width() / 2) - (width / 2);
int boxY = (parentDimensions.height() / 2) - (height / 2);

this.closeButton = new FlatButtonWidget(new Dim2i((boxX + width) - 84, (boxY + height) - 24, 80, 20), Component.literal("Close"), this::close);
this.closeButton = new FlatButtonWidget(new Dim2i((boxX + width) - 84, (boxY + height) - 24, 80, 20), Component.literal("Close"), this::close, true, false);
this.closeButton.setStyle(createButtonStyle());

this.actionButton = new FlatButtonWidget(new Dim2i((boxX + width) - 198, (boxY + height) - 24, 110, 20), this.action.label, this::runAction);
this.actionButton = new FlatButtonWidget(new Dim2i((boxX + width) - 198, (boxY + height) - 24, 110, 20), this.action.label, this::runAction, true, false);
this.actionButton.setStyle(createButtonStyle());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ protected void drawString(GuiGraphics graphics, Component text, int x, int y, in
graphics.drawString(this.font, text, x, y, color);
}

protected void drawCenteredString(GuiGraphics graphics, Component text, int x, int y, int color) {
graphics.drawCenteredString(this.font, text, x, y, color);
}

public boolean isHovered() {
return this.hovered;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package net.caffeinemc.mods.sodium.client.gui.widgets;

import net.caffeinemc.mods.sodium.client.util.Dim2i;
import net.minecraft.client.gui.ComponentPath;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.navigation.CommonInputs;
import net.minecraft.client.gui.navigation.FocusNavigationEvent;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class CenteredFlatWidget extends AbstractWidget implements Renderable {
private final Dim2i dim;
private final Runnable action;
private final boolean isSelectable;

private @NotNull Style style = Style.defaults();

private boolean selected;
private boolean enabled = true;
private boolean visible = true;

private Component label;

public CenteredFlatWidget(Dim2i dim, Component label, Runnable action, boolean isSelectable) {
this.dim = dim;
this.label = label;
this.action = action;
this.isSelectable = isSelectable;
}

@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
if (!this.visible) {
return;
}

this.hovered = this.dim.containsCursor(mouseX, mouseY);

int backgroundColor = this.hovered ? 0x40001101 : (this.selected ? 0x80000000 : 0x40000000);
int textColor = this.selected || !this.isSelectable ? this.style.textDefault : this.style.textDisabled;

int strWidth = this.font.width(this.label);

if (isSelectable) {
this.drawRect(graphics, this.dim.x(), this.dim.y(), this.dim.getLimitX(), this.dim.getLimitY(), backgroundColor);
}

if (selected) {
this.drawRect(graphics, this.dim.getLimitX() - 3, this.dim.y(), this.dim.getLimitX(), this.dim.getLimitY(), 0xFFccfdee);
}

this.drawString(graphics, this.label, (int) (this.dim.x() + 8), (int) Math.ceil(((this.dim.getCenterY() - (font.lineHeight * 0.5f)))), textColor);

if (this.enabled && this.isFocused()) {
this.drawBorder(graphics, this.dim.x(), this.dim.y(), this.dim.getLimitX(), this.dim.getLimitY(), -1);
}
}

public void setStyle(@NotNull Style style) {
Objects.requireNonNull(style);

this.style = style;
}

public void setSelected(boolean selected) {
this.selected = selected;
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (!this.enabled || !this.visible) {
return false;
}

if (button == 0 && this.dim.containsCursor(mouseX, mouseY)) {
doAction();

return true;
}

return false;
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (!this.isFocused())
return false;

if (CommonInputs.selected(keyCode)) {
doAction();
return true;
}

return false;
}

private void doAction() {
this.action.run();
this.playClickSound();
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public void setVisible(boolean visible) {
this.visible = visible;
}

public void setLabel(Component text) {
this.label = text;
}

public Component getLabel() {
return this.label;
}

@Override
public @Nullable ComponentPath nextFocusPath(FocusNavigationEvent event) {
if (!this.enabled || !this.visible)
return null;
return super.nextFocusPath(event);
}

@Override
public ScreenRectangle getRectangle() {
return new ScreenRectangle(this.dim.x(), this.dim.y(), this.dim.width(), this.dim.height());
}

public static class Style {
public int bgHovered, bgDefault, bgDisabled;
public int textDefault, textDisabled;

public static Style defaults() {
var style = new Style();
style.bgHovered = 0xE0000000;
style.bgDefault = 0x90000000;
style.bgDisabled = 0x60000000;
style.textDefault = 0xccfdee;
style.textDisabled = 0xF06f9090;

return style;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
public class FlatButtonWidget extends AbstractWidget implements Renderable {
private final Dim2i dim;
private final Runnable action;
private final boolean drawBackground;
private final boolean leftAlign;

private @NotNull Style style = Style.defaults();

Expand All @@ -25,10 +27,12 @@ public class FlatButtonWidget extends AbstractWidget implements Renderable {

private Component label;

public FlatButtonWidget(Dim2i dim, Component label, Runnable action) {
public FlatButtonWidget(Dim2i dim, Component label, Runnable action, boolean drawBackground, boolean leftAlign) {
this.dim = dim;
this.label = label;
this.action = action;
this.drawBackground = drawBackground;
this.leftAlign = leftAlign;
}

@Override
Expand All @@ -44,14 +48,19 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {

int strWidth = this.font.width(this.label);

this.drawRect(graphics, this.dim.x(), this.dim.y(), this.dim.getLimitX(), this.dim.getLimitY(), backgroundColor);
this.drawString(graphics, this.label, this.dim.getCenterX() - (strWidth / 2), this.dim.getCenterY() - 4, textColor);
if (drawBackground) {
this.drawRect(graphics, this.dim.x(), this.dim.y(), this.dim.getLimitX(), this.dim.getLimitY(), backgroundColor);
}

this.drawString(graphics, this.label, this.leftAlign ? this.dim.x() + 5 : (this.dim.getCenterX() - (strWidth / 2)), this.dim.getCenterY() - 4, textColor);

if (this.enabled && this.selected) {
this.drawRect(graphics, this.dim.x(), this.dim.getLimitY() - 1, this.dim.getLimitX(), this.dim.getLimitY(), 0xFF94E4D3);
}
if (this.enabled && this.isFocused()) {
this.drawBorder(graphics, this.dim.x(), this.dim.y(), this.dim.getLimitX(), this.dim.getLimitY(), -1);

if (!drawBackground) {
this.drawBorder(graphics, this.dim.x(), this.dim.y(), this.dim.getLimitX(), this.dim.getLimitY(), 0x8000FFEE);

}
}

Expand Down

0 comments on commit bb8e33c

Please sign in to comment.