Skip to content

Commit

Permalink
Move scroll bar textures to GUI sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Sep 9, 2023
1 parent 5109067 commit 8d1d244
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ public final class WidgetTextures {
id("widget/button_disabled_dark"),
id("widget/button_highlighted_dark")
);
private static final ScrollBarTextures LIGHT_SCROLL_BAR = new ScrollBarTextures(
id("widget/scroll_bar/background_light"),
id("widget/scroll_bar/thumb_light"),
id("widget/scroll_bar/thumb_pressed_light"),
id("widget/scroll_bar/thumb_hovered_light")
);
private static final ScrollBarTextures DARK_SCROLL_BAR = new ScrollBarTextures(
id("widget/scroll_bar/background_dark"),
id("widget/scroll_bar/thumb_dark"),
id("widget/scroll_bar/thumb_pressed_dark"),
id("widget/scroll_bar/thumb_hovered_dark")
);

public static ButtonTextures getButtonTextures(boolean dark) {
return dark ? DARK_BUTTON : PressableWidgetAccessor.libgui$getTextures();
Expand All @@ -31,7 +43,14 @@ public static ButtonTextures getLabeledSliderHandleTextures(boolean dark) {
return dark ? DARK_LABELED_SLIDER_HANDLE : LIGHT_LABELED_SLIDER_HANDLE;
}

public static ScrollBarTextures getScrollBarTextures(boolean dark) {
return dark ? DARK_SCROLL_BAR : LIGHT_SCROLL_BAR;
}

private static Identifier id(String path) {
return new Identifier(LibGuiCommon.MOD_ID, path);
}

public record ScrollBarTextures(Identifier background, Identifier thumb, Identifier thumbPressed, Identifier thumbHovered) {
}
}
39 changes: 11 additions & 28 deletions src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
import net.minecraft.client.gui.screen.narration.NarrationPart;
import net.minecraft.util.Identifier;

import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
import io.github.cottonmc.cotton.gui.impl.LibGuiCommon;
import io.github.cottonmc.cotton.gui.impl.client.NarrationMessages;
import io.github.cottonmc.cotton.gui.impl.client.NinePatchTextureRendererImpl;
import io.github.cottonmc.cotton.gui.impl.client.WidgetTextures;
import io.github.cottonmc.cotton.gui.widget.data.Axis;
import io.github.cottonmc.cotton.gui.widget.data.InputResult;
import juuxel.libninepatch.NinePatch;

import static io.github.cottonmc.cotton.gui.client.BackgroundPainter.createNinePatch;

public class WScrollBar extends WWidget {
private static final Identifier FOCUS_TEXTURE = new Identifier(LibGuiCommon.MOD_ID, "widget/scroll_bar/focus");
private static final int SCROLLING_SPEED = 4;

protected Axis axis = Axis.HORIZONTAL;
Expand Down Expand Up @@ -49,34 +46,35 @@ public WScrollBar(Axis axis) {
public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) {
var matrices = context.getMatrices();
boolean darkMode = shouldRenderInDarkMode();
var textures = WidgetTextures.getScrollBarTextures(darkMode);

Painters.BACKGROUND.paintBackground(context, x, y, this);
context.drawGuiTexture(textures.background(), x, y, getWidth(), getHeight());

NinePatch<Identifier> painter = (darkMode ? Painters.SCROLL_BAR_DARK : Painters.SCROLL_BAR);
Identifier thumbTexture = textures.thumb();

if (maxValue <= 0) return;

if (sliding) {
painter = (darkMode ? Painters.SCROLL_BAR_PRESSED_DARK : Painters.SCROLL_BAR_PRESSED);
thumbTexture = textures.thumbPressed();
} else if (isWithinBounds(mouseX, mouseY)) {
painter = (darkMode ? Painters.SCROLL_BAR_HOVERED_DARK : Painters.SCROLL_BAR_HOVERED);
thumbTexture = textures.thumbHovered();
}

matrices.push();

if (axis == Axis.HORIZONTAL) {
matrices.translate(x + 1 + getHandlePosition(), y + 1, 0);
painter.draw(NinePatchTextureRendererImpl.INSTANCE, context, getHandleSize(), height - 2);
context.drawGuiTexture(thumbTexture, 0, 0, getHandleSize(), getHeight() - 2);

if (isFocused()) {
Painters.FOCUS.draw(NinePatchTextureRendererImpl.INSTANCE, context, getHandleSize(), height - 2);
context.drawGuiTexture(FOCUS_TEXTURE, 0, 0, getHandleSize(), getHeight() - 2);
}
} else {
matrices.translate(x + 1, y + 1 + getHandlePosition(), 0);
painter.draw(NinePatchTextureRendererImpl.INSTANCE, context, width - 2, getHandleSize());
context.drawGuiTexture(thumbTexture, 0, 0, getWidth() - 2, getHandleSize());

if (isFocused()) {
Painters.FOCUS.draw(NinePatchTextureRendererImpl.INSTANCE, context, width - 2, getHandleSize());
context.drawGuiTexture(FOCUS_TEXTURE, 0, 0, getWidth() - 2, getHandleSize());
}
}

Expand Down Expand Up @@ -255,19 +253,4 @@ public void addNarrations(NarrationMessageBuilder builder) {
builder.put(NarrationPart.TITLE, NarrationMessages.SCROLL_BAR_TITLE);
builder.put(NarrationPart.USAGE, NarrationMessages.SLIDER_USAGE);
}

@Environment(EnvType.CLIENT)
static final class Painters {
static final NinePatch<Identifier> SCROLL_BAR = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_light.png")).cornerSize(4).cornerUv(0.25f).build();
static final NinePatch<Identifier> SCROLL_BAR_DARK = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_dark.png")).cornerSize(4).cornerUv(0.25f).build();
static final NinePatch<Identifier> SCROLL_BAR_PRESSED = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_pressed_light.png")).cornerSize(4).cornerUv(0.25f).build();
static final NinePatch<Identifier> SCROLL_BAR_PRESSED_DARK = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_pressed_dark.png")).cornerSize(4).cornerUv(0.25f).build();
static final NinePatch<Identifier> SCROLL_BAR_HOVERED = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_hovered_light.png")).cornerSize(4).cornerUv(0.25f).build();
static final NinePatch<Identifier> SCROLL_BAR_HOVERED_DARK = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_hovered_dark.png")).cornerSize(4).cornerUv(0.25f).build();
static final BackgroundPainter BACKGROUND = BackgroundPainter.createLightDarkVariants(
createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/background_light.png")),
createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/background_dark.png"))
);
static final NinePatch<Identifier> FOCUS = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/focus.png")).cornerSize(4).cornerUv(0.25f).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 16,
"height": 16,
"border": 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 16,
"height": 16,
"border": 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 16,
"height": 16,
"border": 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 16,
"height": 16,
"border": 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 16,
"height": 16,
"border": 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 16,
"height": 16,
"border": 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 16,
"height": 16,
"border": 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 16,
"height": 16,
"border": 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 16,
"height": 16,
"border": 1
}
}
}

0 comments on commit 8d1d244

Please sign in to comment.