Skip to content

Commit

Permalink
[1.21.1] More overriders for 3PanelScreen, and Togglable Button (#118)
Browse files Browse the repository at this point in the history
* Make scrollbar and bottom panel configurable

* Add Toggleable Button and Remove Fabric Mixin

* Add opinion to change bottom panel height

* Set required fabric api version

Update to new Fabric do it our mixin removal

* Formatting fixes
  • Loading branch information
UnRealDinnerbone authored Aug 20, 2024
1 parent 7de707d commit 4a0e323
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 38 deletions.
14 changes: 12 additions & 2 deletions common/src/main/java/dev/ftb/mods/ftblibrary/ui/Button.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public abstract class Button extends Widget {
protected Component title;
protected Icon icon;
private boolean forceButtonSize;

public Button(Panel panel, Component t, Icon i) {
super(panel);
Expand Down Expand Up @@ -38,6 +39,11 @@ public Button setIcon(Icon i) {
return this;
}

public Button setForceButtonSize(boolean forceButtonSize) {
this.forceButtonSize = forceButtonSize;
return this;
}

public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
theme.drawButton(graphics, x, y, w, h, getWidgetType());
}
Expand All @@ -49,9 +55,13 @@ public void drawIcon(GuiGraphics graphics, Theme theme, int x, int y, int w, int
@Override
public void draw(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
GuiHelper.setupDrawing();
var s = h >= 16 ? 16 : 8;
drawBackground(graphics, theme, x, y, w, h);
drawIcon(graphics, theme, x + (w - s) / 2, y + (h - s) / 2, s, s);
if (forceButtonSize) {
var s = h >= 16 ? 16 : 8;
drawIcon(graphics, theme, x + (w - s) / 2, y + (h - s) / 2, s, s);
}else {
drawIcon(graphics, theme, x, y, w, h);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import java.util.List;

public class SimpleButton extends Button {
private final Callback consumer;
private Callback consumer;
private final List<Component> tooltip;

public SimpleButton(Panel panel, Component text, Icon icon, Callback c) {
super(panel, text, icon);
consumer = c;
Expand All @@ -32,14 +33,20 @@ public void addMouseOverText(TooltipList list) {
}
}

public void setConsumer(Callback consumer) {
this.consumer = consumer;
}

@Override
public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
}

@Override
public void onClicked(MouseButton button) {
playClickSound();
consumer.onClicked(this, button);
if (consumer != null) {
consumer.onClicked(this, button);
}
}

public interface Callback {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dev.ftb.mods.ftblibrary.ui;

import dev.ftb.mods.ftblibrary.icon.Icon;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;

public class ToggleableButton extends SimpleButton {

private Component enabledText;
private Component disabledText;

private boolean state;

public ToggleableButton(Panel panel, boolean defaultState, Icon enabled, Icon disabled, ToggleableCallback toggleableCallback) {
super(panel, Component.empty(), defaultState ? enabled : disabled, null);
this.state = defaultState;
this.setConsumer((widget, button) -> {
this.state = !this.state;
widget.setIcon(this.state ? enabled : disabled);
toggleableCallback.onClicked(widget, this.state);
});
this.enabledText = Component.translatable("ftblibrary.gui.enabled").withStyle(ChatFormatting.GREEN);
this.disabledText = Component.translatable("ftblibrary.gui.disabled").withStyle(ChatFormatting.RED);
updateTitle();
}

public Component getEnabledText() {
return enabledText;
}

public ToggleableButton setEnabledText(Component enabledText) {
this.enabledText = enabledText;
updateTitle();
return this;
}

public Component getDisabledText() {
return disabledText;
}

public ToggleableButton setDisabledText(Component disabledText) {
this.disabledText = disabledText;
updateTitle();
return this;
}

private void updateTitle() {
setTitle(state ? enabledText : disabledText);
}

public interface ToggleableCallback {
void onClicked(SimpleButton widget, boolean newState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@ public abstract class AbstractThreePanelScreen<T extends Panel> extends BaseScre
protected final PanelScrollBar scrollBar;
private boolean showBottomPanel = true;
private boolean showCloseButton = false;
private boolean showScrollBar = true;

protected AbstractThreePanelScreen() {
super();

topPanel = createTopPanel();
mainPanel = createMainPanel();
bottomPanel = new BottomPanel();
bottomPanel = createBottomPanel();
scrollBar = new PanelScrollBar(this, ScrollBar.Plane.VERTICAL, mainPanel);
}

@Override
public void addWidgets() {
add(topPanel);
add(mainPanel);
add(scrollBar);
if (showScrollBar) {
add(scrollBar);
}
if (showBottomPanel) {
add(bottomPanel);
}
Expand All @@ -47,18 +50,20 @@ public void alignWidgets() {
topPanel.alignWidgets();

var inset = mainPanelInset();
int bottomPanelHeight = showBottomPanel ? BOTTOM_PANEL_H + inset.getSecond() : 0;
int bottomPanelHeight = showBottomPanel ? getBottomPanelHeight() + inset.getSecond() : 0;

mainPanel.setPosAndSize(inset.getFirst(), topPanelHeight + inset.getSecond(),
width - inset.getFirst() * 2, height - topPanelHeight - inset.getSecond() * 2 - bottomPanelHeight);
mainPanel.alignWidgets();

if (showBottomPanel) {
bottomPanel.setPosAndSize(0, height - BOTTOM_PANEL_H, width, BOTTOM_PANEL_H);
bottomPanel.setPosAndSize(0, height - getBottomPanelHeight(), width, getBottomPanelHeight());
bottomPanel.alignWidgets();
}

scrollBar.setPosAndSize(mainPanel.getPosX() + mainPanel.getWidth() - getScrollbarWidth(), mainPanel.getPosY(), getScrollbarWidth(), mainPanel.getHeight());
if (showScrollBar) {
scrollBar.setPosAndSize(mainPanel.getPosX() + mainPanel.getWidth() - getScrollbarWidth(), mainPanel.getPosY(), getScrollbarWidth(), mainPanel.getHeight());
}
}

@Override
Expand All @@ -72,7 +77,7 @@ public void tick() {
super.tick();

int prevWidth = mainPanel.width;
int newWidth = (scrollBar.shouldDraw() ? getGui().width - getScrollbarWidth() - 2 : getGui().width) - mainPanelInset().getFirst() * 2;
int newWidth = (!showScrollBar && scrollBar.shouldDraw() ? getGui().width - getScrollbarWidth() - 2 : getGui().width) - mainPanelInset().getFirst() * 2;
if (prevWidth != newWidth) {
mainPanel.setWidth(newWidth);
mainPanel.alignWidgets();
Expand All @@ -91,6 +96,10 @@ protected Pair<Integer, Integer> mainPanelInset() {
return NO_INSET;
}

protected int getBottomPanelHeight() {
return BOTTOM_PANEL_H;
}

protected int getScrollbarWidth() {
return SCROLLBAR_WIDTH;
}
Expand All @@ -99,6 +108,10 @@ protected Panel createTopPanel() {
return new TopPanel();
}

protected Panel createBottomPanel() {
return new BottomPanel();
}

public void showBottomPanel(boolean show) {
showBottomPanel = show;
}
Expand All @@ -107,6 +120,10 @@ public void showCloseButton(boolean show) {
showCloseButton = show;
}

public void showScrollBar(boolean show) {
showScrollBar = show;
}

protected class TopPanel extends Panel {
private final SimpleButton closeButton;

Expand Down
2 changes: 2 additions & 0 deletions common/src/main/resources/assets/ftblibrary/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
"ftblibrary.gui.edit_tag_value": "Edit Tag Value",
"ftblibrary.gui.no_selection": "Nothing Selected",
"ftblibrary.gui.key_reference": "Key Reference",
"ftblibrary.gui.enabled": "Enabled",
"ftblibrary.gui.disabled": "Disabled",
"ftblibrary.client_settings": "Client Config",
"ftblibrary.client_settings.tooltips": "Tooltips",
"ftblibrary.client_settings.tooltips.item_modname": "Show Mod Name in Item Select GUI",
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion fabric/src/main/resources/ftblibrary-fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"PlayerMixin"
],
"client": [
"AbstractContainerScreenMixin",
"KeyMappingAccessor"
],
"injectors": {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ neoforge_version_range=[21.1.0,)
neoforge_loader_version=4
fabric_loader_version=0.15.11
fabric_api_version=0.102.1+1.21.1
fabric_api_version_range=>=0.100.1+1.21
fabric_api_version_range=>=0.102.1+1.21.1
architectury_version=13.0.6
# There are too many of these now
rei_version=16.0.754
Expand Down

0 comments on commit 4a0e323

Please sign in to comment.