Reworked button widgets.

This commit is contained in:
Florian Nücke
2022-01-13 10:45:24 +01:00
parent a4549ab212
commit c97bf66bc4
5 changed files with 79 additions and 133 deletions

View File

@@ -39,10 +39,8 @@ public abstract class AbstractMachineInventoryScreen<T extends AbstractMachineTe
super.init();
addRenderableWidget(new ToggleImageButton(
this, leftPos - Sprites.SIDEBAR_3.width + 4, topPos + CONTROLS_TOP + 4,
leftPos - Sprites.SIDEBAR_3.width + 4, topPos + CONTROLS_TOP + 4,
12, 12,
new TranslatableComponent(Constants.COMPUTER_SCREEN_POWER_CAPTION),
new TranslatableComponent(Constants.COMPUTER_SCREEN_POWER_DESCRIPTION),
Sprites.POWER_BUTTON_BASE,
Sprites.POWER_BUTTON_PRESSED,
Sprites.POWER_BUTTON_ACTIVE
@@ -57,13 +55,14 @@ public abstract class AbstractMachineInventoryScreen<T extends AbstractMachineTe
public boolean isToggled() {
return menu.getVirtualMachine().isRunning();
}
});
}).withTooltip(
new TranslatableComponent(Constants.COMPUTER_SCREEN_POWER_CAPTION),
new TranslatableComponent(Constants.COMPUTER_SCREEN_POWER_DESCRIPTION)
);
addRenderableWidget(new ImageButton(
this, leftPos - Sprites.SIDEBAR_3.width + 4, topPos + CONTROLS_TOP + 4 + 14,
leftPos - Sprites.SIDEBAR_3.width + 4, topPos + CONTROLS_TOP + 4 + 14,
12, 12,
new TranslatableComponent(Constants.MACHINE_OPEN_TERMINAL_CAPTION),
null,
Sprites.INVENTORY_BUTTON_ACTIVE,
Sprites.INVENTORY_BUTTON_INACTIVE
) {
@@ -71,7 +70,7 @@ public abstract class AbstractMachineInventoryScreen<T extends AbstractMachineTe
public void onPress() {
menu.switchToTerminal();
}
});
}.withTooltip(new TranslatableComponent(Constants.MACHINE_OPEN_TERMINAL_CAPTION)));
}
@Override

View File

@@ -110,10 +110,8 @@ public abstract class AbstractMachineTerminalScreen<T extends AbstractMachineTer
setFocusIndicatorEditBox(focusIndicatorEditBox);
addRenderableWidget(new ToggleImageButton(
this, leftPos - Sprites.SIDEBAR_3.width + 4, topPos + CONTROLS_TOP + 4,
leftPos - Sprites.SIDEBAR_3.width + 4, topPos + CONTROLS_TOP + 4,
12, 12,
new TranslatableComponent(Constants.COMPUTER_SCREEN_POWER_CAPTION),
new TranslatableComponent(Constants.COMPUTER_SCREEN_POWER_DESCRIPTION),
Sprites.POWER_BUTTON_BASE,
Sprites.POWER_BUTTON_PRESSED,
Sprites.POWER_BUTTON_ACTIVE
@@ -128,13 +126,14 @@ public abstract class AbstractMachineTerminalScreen<T extends AbstractMachineTer
public boolean isToggled() {
return menu.getVirtualMachine().isRunning();
}
});
}).withTooltip(
new TranslatableComponent(Constants.COMPUTER_SCREEN_POWER_CAPTION),
new TranslatableComponent(Constants.COMPUTER_SCREEN_POWER_DESCRIPTION)
);
addRenderableWidget(new ToggleImageButton(
this, leftPos - Sprites.SIDEBAR_3.width + 4, topPos + CONTROLS_TOP + 4 + 14,
leftPos - Sprites.SIDEBAR_3.width + 4, topPos + CONTROLS_TOP + 4 + 14,
12, 12,
new TranslatableComponent(Constants.TERMINAL_CAPTURE_INPUT_CAPTION),
new TranslatableComponent(Constants.TERMINAL_CAPTURE_INPUT_DESCRIPTION),
Sprites.INPUT_BUTTON_BASE,
Sprites.INPUT_BUTTON_PRESSED,
Sprites.INPUT_BUTTON_ACTIVE
@@ -149,13 +148,14 @@ public abstract class AbstractMachineTerminalScreen<T extends AbstractMachineTer
public boolean isToggled() {
return isInputCaptureEnabled;
}
});
}).withTooltip(
new TranslatableComponent(Constants.TERMINAL_CAPTURE_INPUT_CAPTION),
new TranslatableComponent(Constants.TERMINAL_CAPTURE_INPUT_DESCRIPTION)
);
addRenderableWidget(new ImageButton(
this, leftPos - Sprites.SIDEBAR_3.width + 4, topPos + CONTROLS_TOP + 4 + 14 + 14,
leftPos - Sprites.SIDEBAR_3.width + 4, topPos + CONTROLS_TOP + 4 + 14 + 14,
12, 12,
new TranslatableComponent(Constants.MACHINE_OPEN_INVENTORY_CAPTION),
null,
Sprites.INVENTORY_BUTTON_INACTIVE,
Sprites.INVENTORY_BUTTON_ACTIVE
) {
@@ -163,7 +163,7 @@ public abstract class AbstractMachineTerminalScreen<T extends AbstractMachineTer
public void onPress() {
menu.switchToInventory();
}
});
}).withTooltip(new TranslatableComponent(Constants.MACHINE_OPEN_INVENTORY_CAPTION));
}
@Override

View File

@@ -61,11 +61,8 @@ public final class BusInterfaceScreen extends Screen {
setInitialFocus(nameField);
addRenderableWidget(new ImageButton(
this,
left + CONFIRM_LEFT, top + CONFIRM_TOP,
Sprites.CONFIRM_BASE.width, Sprites.CONFIRM_BASE.height,
new TranslatableComponent(Constants.TOOLTIP_CONFIRM),
null,
Sprites.CONFIRM_BASE,
Sprites.CONFIRM_PRESSED
) {
@@ -75,13 +72,11 @@ public final class BusInterfaceScreen extends Screen {
setInterfaceName(nameField.getValue());
onClose();
}
});
}).withTooltip(new TranslatableComponent(Constants.TOOLTIP_CONFIRM));
addRenderableWidget(new ImageButton(
this,
left + CANCEL_LEFT, top + CANCEL_TOP,
Sprites.CANCEL_BASE.width, Sprites.CANCEL_BASE.height,
new TranslatableComponent(Constants.TOOLTIP_CANCEL),
null,
Sprites.CANCEL_BASE,
Sprites.CANCEL_PRESSED
) {
@@ -90,7 +85,7 @@ public final class BusInterfaceScreen extends Screen {
super.onPress();
onClose();
}
});
}).withTooltip(new TranslatableComponent(Constants.TOOLTIP_CANCEL));
}
@Override

View File

@@ -1,20 +1,22 @@
package li.cil.oc2.client.gui.widget;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import li.cil.oc2.common.util.TooltipUtils;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.AbstractButton;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextColor;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.util.Mth;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.Collections.emptyList;
import static li.cil.oc2.common.util.TextFormatUtils.withFormat;
public abstract class ImageButton extends AbstractButton {
private static final long PRESS_DURATION = 200;
@@ -22,35 +24,36 @@ public abstract class ImageButton extends AbstractButton {
///////////////////////////////////////////////////////////////////
private final Screen parent;
private final List<? extends Component> tooltip;
private final Sprite baseImage;
private final Sprite pressedImage;
private List<Component> tooltip = emptyList();
private long lastPressedAt;
private long hoveringStartedAt;
///////////////////////////////////////////////////////////////////
public ImageButton(final Screen parent,
final int x, final int y,
final int width, final int height,
final Component caption,
@Nullable final Component description,
final Sprite baseImage,
final Sprite pressedImage) {
super(x, y, width, height, caption);
this.parent = parent;
if (description == null) {
this.tooltip = singletonList(caption);
} else {
this.tooltip = asList(caption, new TextComponent("").withStyle(style -> style.withColor(TextColor.fromLegacyFormat(ChatFormatting.GRAY))).append(description));
}
protected ImageButton(final int x, final int y, final int width, final int height, final Sprite baseImage, final Sprite pressedImage) {
super(x, y, width, height, TextComponent.EMPTY);
this.baseImage = baseImage;
this.pressedImage = pressedImage;
}
///////////////////////////////////////////////////////////////////
public ImageButton withMessage(final Component component) {
setMessage(component);
return this;
}
public ImageButton withTooltip(final Component... components) {
tooltip = Arrays.asList(components);
for (int i = 1; i < tooltip.size(); i++) {
final Component component = tooltip.get(i);
tooltip.set(i, withFormat(component, ChatFormatting.GRAY));
}
return this;
}
@Override
public void onPress() {
lastPressedAt = System.currentTimeMillis();
@@ -58,12 +61,16 @@ public abstract class ImageButton extends AbstractButton {
@Override
public void renderButton(final PoseStack stack, final int mouseX, final int mouseY, final float partialTicks) {
Sprite background = baseImage;
if ((System.currentTimeMillis() - lastPressedAt) < PRESS_DURATION) {
background = pressedImage;
}
renderBackground(stack, mouseX, mouseY, partialTicks);
background.draw(stack, x, y);
renderToolTip(stack, mouseX, mouseY);
}
@Override
public void renderToolTip(final PoseStack stack, final int mouseX, final int mouseY) {
if (tooltip.isEmpty()) {
return;
}
if (isHoveredOrFocused()) {
if (hoveringStartedAt == 0) {
@@ -71,20 +78,34 @@ public abstract class ImageButton extends AbstractButton {
}
if ((System.currentTimeMillis() - hoveringStartedAt) > TOOLTIP_DELAY) {
renderToolTip(stack, mouseX, mouseY);
TooltipUtils.drawTooltip(stack, tooltip, mouseX, mouseY, 200);
}
} else {
hoveringStartedAt = 0;
}
}
@Override
public void renderToolTip(final PoseStack stack, final int mouseX, final int mouseY) {
TooltipUtils.drawTooltip(stack, tooltip, mouseX, mouseY, 200);
}
@Override
public void updateNarration(final NarrationElementOutput element) {
this.defaultButtonNarrationText(element);
}
///////////////////////////////////////////////////////////////////
protected void renderBackground(final PoseStack stack, final int mouseX, final int mouseY, final float partialTicks) {
RenderSystem.enableDepthTest();
Sprite background = baseImage;
if ((System.currentTimeMillis() - lastPressedAt) < PRESS_DURATION) {
background = pressedImage;
}
background.draw(stack, x, y);
if (!Objects.equals(getMessage(), TextComponent.EMPTY)) {
drawCenteredString(stack, Minecraft.getInstance().font, getMessage(),
x + width / 2, y + (height - 8) / 2,
getFGColor() | Mth.ceil(alpha * 255) << 24);
}
}
}

View File

@@ -1,66 +1,25 @@
package li.cil.oc2.client.gui.widget;
import com.mojang.blaze3d.vertex.PoseStack;
import li.cil.oc2.common.util.TooltipUtils;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.components.AbstractButton;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextColor;
import net.minecraft.network.chat.TextComponent;
import javax.annotation.Nullable;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
public abstract class ToggleImageButton extends AbstractButton {
private static final long PRESS_DURATION = 200;
private static final long TOOLTIP_DELAY = 250;
///////////////////////////////////////////////////////////////////
private final Screen parent;
private final List<? extends Component> tooltip;
private final Sprite baseImage;
private final Sprite pressedImage;
public abstract class ToggleImageButton extends ImageButton {
private final Sprite activeImage;
private boolean isToggled;
private long lastPressedAt;
private long hoveringStartedAt;
///////////////////////////////////////////////////////////////////
public ToggleImageButton(
final Screen parent,
final int x, final int y,
final int width, final int height,
final Component caption,
@Nullable final Component description,
final Sprite baseImage,
final Sprite pressedImage,
final Sprite activeImage) {
super(x, y, width, height, caption);
this.parent = parent;
if (description == null) {
this.tooltip = singletonList(caption);
} else {
this.tooltip = asList(caption, new TextComponent("").withStyle(style -> style.withColor(TextColor.fromLegacyFormat(ChatFormatting.GRAY))).append(description));
}
this.baseImage = baseImage;
this.pressedImage = pressedImage;
super(x, y, width, height, baseImage, pressedImage);
this.activeImage = activeImage;
}
///////////////////////////////////////////////////////////////////
@Override
public void onPress() {
lastPressedAt = System.currentTimeMillis();
}
public boolean isToggled() {
return isToggled;
}
@@ -70,38 +29,10 @@ public abstract class ToggleImageButton extends AbstractButton {
}
@Override
public void renderButton(final PoseStack stack, final int mouseX, final int mouseY, final float partialTicks) {
Sprite background = baseImage;
if ((System.currentTimeMillis() - lastPressedAt) < PRESS_DURATION) {
background = pressedImage;
}
background.draw(stack, x, y);
protected void renderBackground(final PoseStack stack, final int mouseX, final int mouseY, final float partialTicks) {
super.renderBackground(stack, mouseX, mouseY, partialTicks);
if (isToggled()) {
activeImage.draw(stack, x, y);
}
if (isHoveredOrFocused()) {
if (hoveringStartedAt == 0) {
hoveringStartedAt = System.currentTimeMillis();
}
if ((System.currentTimeMillis() - hoveringStartedAt) > TOOLTIP_DELAY) {
renderToolTip(stack, mouseX, mouseY);
}
} else {
hoveringStartedAt = 0;
}
}
@Override
public void renderToolTip(final PoseStack stack, final int mouseX, final int mouseY) {
TooltipUtils.drawTooltip(stack, tooltip, mouseX, mouseY, 200);
}
@Override
public void updateNarration(final NarrationElementOutput element) {
this.defaultButtonNarrationText(element);
}
}