Skip to content

Commit

Permalink
GuiDescription: Add get/setUseDefaultRootBackground
Browse files Browse the repository at this point in the history
This is useful for removing no-op addPainters overrides and replacing them
with a one-line method call when you don't want the default vanilla-style
background for a panel.
  • Loading branch information
Juuxel committed Oct 23, 2024
1 parent b8e2a7c commit a51dd5e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
21 changes: 20 additions & 1 deletion src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,26 @@ public interface GuiDescription {
/** Guis should use this method to add clientside styles and BackgroundPainters to their controls */
@Environment(EnvType.CLIENT)
public void addPainters();


/**
* Tests whether the root panel should get a {@linkplain io.github.cottonmc.cotton.gui.client.BackgroundPainter#VANILLA
* vanilla background} in the default {@link #addPainters} implementations.
*
* @return {@code true} if the default background painter is applied, {@code false} otherwise
* @since 12.0.0
*/
public boolean getUseDefaultRootBackground();

/**
* Enables or disables the default {@linkplain io.github.cottonmc.cotton.gui.client.BackgroundPainter#VANILLA
* vanilla background} for root panels.
*
* @param useDefaultRootBackground {@code true} if the default background painter is applied, {@code false} otherwise
* @see #getUseDefaultRootBackground
* @since 12.0.0
*/
public void setUseDefaultRootBackground(boolean useDefaultRootBackground);

/** Gets the object which manages the integer properties used by WBars and such. */
@Nullable
public PropertyDelegate getPropertyDelegate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio

protected WWidget focus;
private Vec2i titlePos = new Vec2i(8, 6);
private boolean useDefaultRootBackground = true;

/**
* Constructs a new synced GUI description without a block inventory or a property delegate.
Expand Down Expand Up @@ -123,11 +124,21 @@ public SyncedGuiDescription setTitleColor(int lightColor, int darkColor) {

@Environment(EnvType.CLIENT)
public void addPainters() {
if (this.rootPanel!=null && !fullscreen) {
if (this.rootPanel!=null && !fullscreen && getUseDefaultRootBackground()) {
this.rootPanel.setBackgroundPainter(BackgroundPainter.VANILLA);
}
}


@Override
public boolean getUseDefaultRootBackground() {
return useDefaultRootBackground;
}

@Override
public void setUseDefaultRootBackground(boolean useDefaultRootBackground) {
this.useDefaultRootBackground = useDefaultRootBackground;
}

public void addSlotPeer(ValidatedSlot slot) {
this.addSlot(slot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class LightweightGuiDescription implements GuiDescription {
protected boolean titleVisible = true;
protected HorizontalAlignment titleAlignment = HorizontalAlignment.LEFT;
private Vec2i titlePos = new Vec2i(8, 6);
private boolean useDefaultRootBackground = true;

@Override
public WPanel getRootPanel() {
Expand Down Expand Up @@ -60,11 +61,21 @@ public GuiDescription setTitleColor(int lightColor, int darkColor) {

@Override
public void addPainters() {
if (this.rootPanel!=null && !fullscreen) {
if (this.rootPanel!=null && !fullscreen && getUseDefaultRootBackground()) {
this.rootPanel.setBackgroundPainter(BackgroundPainter.VANILLA);
}
}

@Override
public boolean getUseDefaultRootBackground() {
return useDefaultRootBackground;
}

@Override
public void setUseDefaultRootBackground(boolean useDefaultRootBackground) {
this.useDefaultRootBackground = useDefaultRootBackground;
}

@Override
public void addSlotPeer(ValidatedSlot slot) {
//NO-OP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ public TabTestGui() {

tabs.setSize(7 * 18, 5 * 18);
setRootPanel(tabs);
setUseDefaultRootBackground(false);
getRootPanel().validate(this);
}

@Override
public void addPainters() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public TextAlignmentTestGui() {
tabPanel.add(labelPanel, builder -> builder.title(Text.of("WLabel")));
tabPanel.add(textPanel, builder -> builder.title(Text.of("WText")));
setRootPanel(tabPanel);
setUseDefaultRootBackground(false);
tabPanel.validate(this);
}

Expand All @@ -68,8 +69,4 @@ private static <E extends Enum<E>> WLabeledSlider forEnum(Class<E> type, Consume
slider.setValueChangeListener(value -> consumer.accept(values[value - 1]));
return slider;
}

@Override
public void addPainters() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ public TextureTestGui() {
root.add(createPanel(simpleSprite), tab -> tab.icon(new TextureIcon(simpleSprite)).tooltip(Text.literal("Simple sprite")));
root.add(createPanel(panelTexture), tab -> tab.icon(new TextureIcon(panelTexture)).tooltip(Text.literal("Standalone")));
setRootPanel(root);
setUseDefaultRootBackground(false);
root.validate(this);
}

@Override
public void addPainters() {
// Remove tab panel background
}

private WPanel createPanel(Texture texture) {
WSprite sprite = new WSprite(texture);

Expand Down

0 comments on commit a51dd5e

Please sign in to comment.