forked from BVengo/sound-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoverableButtonWidget.java
More file actions
48 lines (39 loc) · 2 KB
/
HoverableButtonWidget.java
File metadata and controls
48 lines (39 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.bvengo.soundcontroller.gui.buttons;
import com.bvengo.soundcontroller.SoundController;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.resources.Identifier;
/**
* Custom button widget that is used as a trigger rather than a toggle.
* i.e. it is only active while the button is being pressed.
*/
@Environment(EnvType.CLIENT)
public class HoverableButtonWidget extends Button {
protected boolean isPressed = false;
protected final Identifier ON_TEXTURE;
protected final Identifier OFF_TEXTURE;
protected final Identifier ON_HOVER_TEXTURE;
protected final Identifier OFF_HOVER_TEXTURE;
String buttonId;
public HoverableButtonWidget(String buttonId, int x, int y, int width, int height, OnPress onPress) {
super(x, y, width, height, CommonComponents.EMPTY, onPress, DEFAULT_NARRATION);
this.buttonId = buttonId;
ON_TEXTURE = Identifier.fromNamespaceAndPath(SoundController.MOD_ID, buttonId + "_button_on");
OFF_TEXTURE = Identifier.fromNamespaceAndPath(SoundController.MOD_ID, buttonId + "_button_off");
ON_HOVER_TEXTURE = Identifier.fromNamespaceAndPath(SoundController.MOD_ID, buttonId + "_button_on_hovered");
OFF_HOVER_TEXTURE = Identifier.fromNamespaceAndPath(SoundController.MOD_ID, buttonId + "_button_off_hovered");
}
protected Identifier getTextureIdentifier() {
return isPressed ? (isHovered ? ON_HOVER_TEXTURE : ON_TEXTURE)
: (isHovered ? OFF_HOVER_TEXTURE : OFF_TEXTURE);
}
@Override
public void extractContents(GuiGraphicsExtractor context, int mouseX, int mouseY, float delta) {
Identifier texture = getTextureIdentifier();
context.blitSprite(RenderPipelines.GUI_TEXTURED, texture, getX(), getY(), width, height);
}
}