58 lines
1.6 KiB
Java
58 lines
1.6 KiB
Java
package net.minecraft.client.gui.components.tabs;
|
|
|
|
import java.util.Objects;
|
|
import java.util.function.Consumer;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.client.Minecraft;
|
|
import net.minecraft.client.gui.components.AbstractWidget;
|
|
import net.minecraft.client.gui.navigation.ScreenRectangle;
|
|
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
|
|
import net.minecraft.sounds.SoundEvents;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public class TabManager {
|
|
private final Consumer<AbstractWidget> addWidget;
|
|
private final Consumer<AbstractWidget> removeWidget;
|
|
@Nullable
|
|
private Tab currentTab;
|
|
@Nullable
|
|
private ScreenRectangle tabArea;
|
|
|
|
public TabManager(Consumer<AbstractWidget> addWidget, Consumer<AbstractWidget> removeWidget) {
|
|
this.addWidget = addWidget;
|
|
this.removeWidget = removeWidget;
|
|
}
|
|
|
|
public void setTabArea(ScreenRectangle tabArea) {
|
|
this.tabArea = tabArea;
|
|
Tab tab = this.getCurrentTab();
|
|
if (tab != null) {
|
|
tab.doLayout(tabArea);
|
|
}
|
|
}
|
|
|
|
public void setCurrentTab(Tab tab, boolean playClickSound) {
|
|
if (!Objects.equals(this.currentTab, tab)) {
|
|
if (this.currentTab != null) {
|
|
this.currentTab.visitChildren(this.removeWidget);
|
|
}
|
|
|
|
this.currentTab = tab;
|
|
tab.visitChildren(this.addWidget);
|
|
if (this.tabArea != null) {
|
|
tab.doLayout(this.tabArea);
|
|
}
|
|
|
|
if (playClickSound) {
|
|
Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Nullable
|
|
public Tab getCurrentTab() {
|
|
return this.currentTab;
|
|
}
|
|
}
|