-
Notifications
You must be signed in to change notification settings - Fork 811
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
224 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
common/src/main/java/net/caffeinemc/mods/sodium/client/gui/widgets/CenteredFlatWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters