Skip to content

Commit

Permalink
Move sliders to SmyLib
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jan 21, 2024
1 parent a77e063 commit e5ad679
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import fr.thesmyler.smylibgui.widgets.buttons.TexturedButtonWidget;
import fr.thesmyler.smylibgui.widgets.buttons.TexturedButtonWidget.IncludedTexturedButtons;
import fr.thesmyler.smylibgui.widgets.buttons.ToggleButtonWidget;
import fr.thesmyler.smylibgui.widgets.sliders.FloatSliderWidget;
import fr.thesmyler.smylibgui.widgets.sliders.IntegerSliderWidget;
import fr.thesmyler.smylibgui.widgets.sliders.OptionSliderWidget;
import net.smyler.smylib.gui.widgets.sliders.FloatSliderWidget;
import net.smyler.smylib.gui.widgets.sliders.IntegerSliderWidget;
import net.smyler.smylib.gui.widgets.sliders.OptionSliderWidget;
import fr.thesmyler.smylibgui.widgets.text.TextAlignment;
import fr.thesmyler.smylibgui.widgets.text.TextFieldWidget;
import fr.thesmyler.smylibgui.widgets.text.TextWidget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import static net.smyler.smylib.SmyLib.getGameClient;

@Deprecated
public final class RenderUtil {

@Deprecated
Expand Down Expand Up @@ -45,6 +46,7 @@ public static void drawGradientRect(double xLeft, double yTop, double xRight, do
drawGradientRect(0, xLeft, yTop, xRight, yBottom, upperLeftColor, lowerLeftColor, lowerRightColor, upperRightColor);
}

@Deprecated
public static void drawModalRectWithCustomSizedTexture(double x, double y, double z, double u, double v, double width, double height, double textureWidth, double textureHeight) {
double f = 1.0f / textureWidth;
double f1 = 1.0f / textureHeight;
Expand All @@ -62,10 +64,12 @@ public static void drawModalRectWithCustomSizedTexture(double x, double y, doubl
GlStateManager.disableBlend();
}

@Deprecated
public static void drawModalRectWithCustomSizedTexture(double x, double y, double u, double v, double width, double height, double textureWidth, double textureHeight) {
drawModalRectWithCustomSizedTexture(x, y, 0d, u, v, width, height, textureWidth, textureHeight);
}

@Deprecated
public static void drawTexturedModalRect(double x, double y, double z, double minU, double minV, double maxU, double maxV) {
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder builder = tessellator.getBuffer();
Expand All @@ -81,6 +85,7 @@ public static void drawTexturedModalRect(double x, double y, double z, double mi
GlStateManager.disableBlend();
}

@Deprecated
public static void drawTexturedModalRect(double x, double y, double minU, double minV, double maxU, double maxV) {
drawTexturedModalRect(x, y, 0, minU, minV, maxU, maxV);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.thesmyler.terramap.eventhandlers;

import fr.thesmyler.smylibgui.event.HudScreenInitEvent;
import fr.thesmyler.smylibgui.screen.TestScreen;
import fr.thesmyler.terramap.TerramapClientContext;
import fr.thesmyler.terramap.TerramapMod;
import fr.thesmyler.terramap.gui.HudScreenHandler;
Expand All @@ -14,6 +15,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiChat;
import net.minecraft.client.gui.GuiDownloadTerrain;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.client.event.GuiScreenEvent.InitGuiEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.smyler.smylib.Color;
import fr.thesmyler.smylibgui.widgets.buttons.TexturedButtonWidget;
import fr.thesmyler.smylibgui.widgets.buttons.ToggleButtonWidget;
import fr.thesmyler.smylibgui.widgets.sliders.FloatSliderWidget;
import net.smyler.smylib.gui.widgets.sliders.FloatSliderWidget;
import fr.thesmyler.smylibgui.widgets.text.TextAlignment;
import fr.thesmyler.smylibgui.widgets.text.TextWidget;
import fr.thesmyler.terramap.gui.screens.config.LayerConfigurationPopup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import fr.thesmyler.smylibgui.widgets.buttons.TexturedButtonWidget;
import fr.thesmyler.smylibgui.widgets.buttons.TexturedButtonWidget.IncludedTexturedButtons;
import fr.thesmyler.smylibgui.widgets.buttons.ToggleButtonWidget;
import fr.thesmyler.smylibgui.widgets.sliders.IntegerSliderWidget;
import fr.thesmyler.smylibgui.widgets.sliders.OptionSliderWidget;
import net.smyler.smylib.gui.widgets.sliders.IntegerSliderWidget;
import net.smyler.smylib.gui.widgets.sliders.OptionSliderWidget;
import fr.thesmyler.smylibgui.widgets.text.TextAlignment;
import fr.thesmyler.smylibgui.widgets.text.TextWidget;
import fr.thesmyler.terramap.MapContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import fr.thesmyler.smylibgui.widgets.buttons.TexturedButtonWidget;
import fr.thesmyler.smylibgui.widgets.buttons.TexturedButtonWidget.IncludedTexturedButtons;
import fr.thesmyler.smylibgui.widgets.buttons.ToggleButtonWidget;
import fr.thesmyler.smylibgui.widgets.sliders.IntegerSliderWidget;
import fr.thesmyler.smylibgui.widgets.sliders.OptionSliderWidget;
import net.smyler.smylib.gui.widgets.sliders.IntegerSliderWidget;
import net.smyler.smylib.gui.widgets.sliders.OptionSliderWidget;
import fr.thesmyler.smylibgui.widgets.text.TextAlignment;
import fr.thesmyler.smylibgui.widgets.text.TextFieldWidget;
import fr.thesmyler.smylibgui.widgets.text.TextWidget;
Expand Down
12 changes: 10 additions & 2 deletions smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,18 @@ default void drawClosedStrokeLine(Color color, float size, double... points) {
this.drawClosedStrokeLine(0d, color, size, points);
}

void drawSprite(double x, double y, double z, Sprite sprite);
default void drawSprite(double x, double y, double z, Sprite sprite) {
this.drawSpriteCropped(x, y, z, sprite, 0d, 0d, 0d, 0d);
}

default void drawSprite(double x, double y, Sprite sprite) {
this.drawSprite(x, y, 0d, sprite);
this.drawSpriteCropped(x, y, 0d, sprite, 0d, 0d, 0d, 0d);
}

void drawSpriteCropped(double x, double y, double z, Sprite sprite, double leftCrop, double topCrop, double rightCrop, double bottomCrop);

default void drawSpriteCropped(double x, double y, Sprite sprite, double leftCrop, double topCrop, double rightCrop, double bottomCrop) {
this.drawSpriteCropped(x, y, 0d, sprite, leftCrop, topCrop, rightCrop, bottomCrop);
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package fr.thesmyler.smylibgui.widgets.sliders;
package net.smyler.smylib.gui.widgets.sliders;

import net.smyler.smylib.Identifier;
import net.smyler.smylib.gui.DrawContext;
import net.smyler.smylib.gui.GlState;
import net.smyler.smylib.gui.Sprite;
import org.jetbrains.annotations.Nullable;

import fr.thesmyler.smylibgui.SmyLibGuiTextures;
import net.smyler.smylib.game.GameClient;
import net.smyler.smylib.game.Key;

import net.smyler.smylib.gui.containers.WidgetContainer;
import net.smyler.smylib.Color;
import fr.thesmyler.smylibgui.util.RenderUtil;
import net.smyler.smylib.gui.widgets.Widget;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.smyler.smylib.gui.Font;

import static net.smyler.smylib.Color.WHITE;
import static java.lang.Math.min;
import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.smylib.math.Math.saturate;

Expand All @@ -30,6 +27,10 @@
*/
public abstract class AbstractSliderWidget implements Widget {

private static final Identifier TEXTURE = new Identifier("minecraft", "textures/gui/widgets.png");
private static final Sprite BACKGROUND = new Sprite(TEXTURE, 256d, 256d, 0d, 46d, 200d, 66d);
private static final Sprite SLIDER = new Sprite(TEXTURE, 256d, 256d, 0d, 66d, 200d, 86d);

protected float x, y, width, height;
private final int z;

Expand Down Expand Up @@ -117,47 +118,46 @@ public void onKeyTyped(char typedChar, @Nullable Key key, @Nullable WidgetContai

@Override
public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) {
GameClient game = getGameClient();
GlState glState = context.glState();
Minecraft.getMinecraft().getTextureManager().bindTexture(SmyLibGuiTextures.BUTTON_TEXTURES);
glState.setColor(WHITE);
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
float leftWidth = this.width / 2;
float rightWidth = this.width - leftWidth;
float splitHeight = Math.min(10, this.height / 2);
RenderUtil.drawTexturedModalRect(x, y, 0, 46, leftWidth, splitHeight);
RenderUtil.drawTexturedModalRect(x + leftWidth, y, 200 - rightWidth, 46, rightWidth, splitHeight);

double leftWidth = this.width / 2;
double splitHeight = min(10, this.height / 2);
double xCrop = BACKGROUND.width() - this.width / 2d;
double yCrop = BACKGROUND.height() - min(10d, this.height / 2);
context.drawSpriteCropped(x, y, BACKGROUND, 0d, 0d, xCrop, yCrop);
context.drawSpriteCropped(x + leftWidth, y, BACKGROUND, xCrop, 0, 0, yCrop);
for(int i=0; i*18 < this.height - 20; i++) {
RenderUtil.drawTexturedModalRect(x, y + splitHeight + 18*i, 0, 47, leftWidth, 18);
RenderUtil.drawTexturedModalRect(x + leftWidth, y + splitHeight + 18*i, 200 - rightWidth, 47, rightWidth, 18);
context.drawSpriteCropped(x, y + splitHeight + 16d * i, BACKGROUND, 0d, 2d, xCrop, 2d);
context.drawSpriteCropped(x + leftWidth, y + splitHeight + 16d * i, BACKGROUND, xCrop, 2d, 0d, 2d);
}
RenderUtil.drawTexturedModalRect(x, y + this.height - splitHeight, 0, 46 + 20 - splitHeight, leftWidth, splitHeight);
RenderUtil.drawTexturedModalRect(x + leftWidth, y + this.height - splitHeight, 200 - rightWidth, 46 + 20 - splitHeight, rightWidth, splitHeight);
context.drawSpriteCropped(x, y + this.height - splitHeight, BACKGROUND, 0, yCrop, xCrop, 0);
context.drawSpriteCropped(x + leftWidth, y + this.height - splitHeight, BACKGROUND, xCrop, yCrop, 0, 0);

float sliderPosition = this.getPosition();
Minecraft.getMinecraft().getTextureManager().bindTexture(SmyLibGuiTextures.BUTTON_TEXTURES);
glState.setColor(WHITE);

float sliderX = x + sliderPosition * (this.width - 8);
RenderUtil.drawTexturedModalRect(sliderX, y, 0, 66, 4, splitHeight);
RenderUtil.drawTexturedModalRect(sliderX + 4, y, 196, 66, 4, splitHeight);
context.drawSpriteCropped(sliderX, y, SLIDER, 0, 0, SLIDER.width() - 4d, SLIDER.height() - splitHeight);
context.drawSpriteCropped(sliderX + 4, y, SLIDER, SLIDER.width() - 4d, 0d, 0d, SLIDER.height() - splitHeight);
for(int i=0; i*18 < this.height - 20; i++) {
RenderUtil.drawTexturedModalRect(sliderX, y + splitHeight + 18*i, 0, 68, 4, 15);
RenderUtil.drawTexturedModalRect(sliderX + 4, y + splitHeight + 18*i, 196, 68, 4, 15);
context.drawSpriteCropped(sliderX, y + splitHeight + 16*i, SLIDER, 0d, 2d, SLIDER.width() - 4d, 2d);
context.drawSpriteCropped(sliderX + 4d, y + splitHeight + 16*i, SLIDER, SLIDER.width() - 4d, 2d, 0d, 2d);
}
RenderUtil.drawTexturedModalRect(sliderX, y + this.height - splitHeight, 0, 66 + 20 - splitHeight, 4, splitHeight);
RenderUtil.drawTexturedModalRect(sliderX + 4, y + this.height - splitHeight, 196, 66 + 20 -splitHeight, 4, splitHeight);
context.drawSpriteCropped(sliderX, y + this.height - splitHeight, SLIDER, 0d, SLIDER.height() - splitHeight, SLIDER.width() - 4d, 0d);
context.drawSpriteCropped(sliderX + 4d, y + this.height - splitHeight, SLIDER, SLIDER.width() - 4d, SLIDER.height() - splitHeight, 0d, 0d);

Color textColor = this.enabledTextColor;
if (!this.isEnabled()) textColor = this.disabledTextColor;
else if (hovered || hasFocus) textColor = this.activeTextColor;
if (!this.isEnabled()) {
textColor = this.disabledTextColor;
} else if (hovered || hasFocus) {
textColor = this.activeTextColor;
}

GameClient game = getGameClient();
float fontSize = game.defaultFont().height();
double gameScale = game.scaleFactor();
float fontScale = 1f;
while(fontSize / fontScale > this.height - 1 && fontScale < gameScale) fontScale++;
while(fontSize / fontScale > this.height - 1 && fontScale < gameScale) {
fontScale++;
}
Font font = game.defaultFont().withScale(1 / fontScale + 0.0001f);
font.drawCenteredString(x + this.width / 2, y + (this.height - font.height() + 1) / 2, this.getDisplayPrefix() + this.getDisplayString(), textColor, false);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.thesmyler.smylibgui.widgets.sliders;
package net.smyler.smylib.gui.widgets.sliders;

import java.util.function.Consumer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.thesmyler.smylibgui.widgets.sliders;
package net.smyler.smylib.gui.widgets.sliders;

import java.util.function.Consumer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.thesmyler.smylibgui.widgets.sliders;
package net.smyler.smylib.gui.widgets.sliders;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.thesmyler.smylibgui.widgets.sliders;
package net.smyler.smylib.gui.widgets.sliders;

import net.smyler.smylib.gui.TestingWidgetContainer;
import net.smyler.smylib.SmyLibTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,21 @@ public void drawClosedStrokeLine(double z, Color color, float size, double... po
}

@Override
public void drawSprite(double x, double y, double z, Sprite sprite) {
public void drawSpriteCropped(double x, double y, double z, Sprite sprite, double leftCrop, double topCrop, double rightCrop, double bottomCrop) {
final ResourceLocation location = new ResourceLocation(sprite.texture.namespace, sprite.texture.path);
Minecraft.getMinecraft().getTextureManager().bindTexture(location);
double uLeft = sprite.xLeft / sprite.textureWidth;
double uRight = sprite.xRight / sprite.textureWidth;
double vTop = sprite.yTop / sprite.textureHeight;
double vBottom = sprite.yBottom / sprite.textureHeight;
double width = sprite.width();
double height = sprite.height();
double uLeft = (sprite.xLeft + leftCrop) / sprite.textureWidth;
double uRight = (sprite.xRight - rightCrop) / sprite.textureWidth;
double vTop = (sprite.yTop + topCrop) / sprite.textureHeight;
double vBottom = (sprite.yBottom - bottomCrop) / sprite.textureHeight;
double width = sprite.width() - leftCrop - rightCrop;
double height = sprite.height() - topCrop - bottomCrop;

GlStateManager.color(1f, 1f, 1f, 1f);
GlStateManager.enableAlpha();
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);

Tessellator tessellator = Tessellator.getInstance();
BufferBuilder builder = tessellator.getBuffer();
Expand Down

0 comments on commit e5ad679

Please sign in to comment.