Skip to content

Commit

Permalink
Move GL color management to SmyLib
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jan 19, 2024
1 parent 5796d1d commit 7a4c1ae
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import fr.thesmyler.smylibgui.container.RootContainer;
import net.smyler.smylib.gui.GlState;
import net.smyler.smylib.gui.containers.WidgetContainer;
import fr.thesmyler.smylibgui.event.HudScreenInitEvent;
import net.smyler.smylib.Color;
Expand Down Expand Up @@ -58,6 +59,7 @@ public static void onRenderHUD(RenderGameOverlayEvent.Pre e) {
GameClient game = getGameClient();
Mouse mouse = game.mouse();
DrawContext drawContext = game.guiDrawContext();
GlState glState = drawContext.glState();
Scissor scissor = drawContext.scissor();
boolean chatOpen = Minecraft.getMinecraft().currentScreen instanceof GuiChat;
float width = game.windowWidth();
Expand All @@ -70,12 +72,12 @@ public static void onRenderHUD(RenderGameOverlayEvent.Pre e) {
float mouseX = mouse.x();
float mouseY = mouse.y();
CONTAINER.onUpdate(mouseX, mouseY, null);
Color color = currentColor();
Color color = glState.getColor();
scissor.push();
scissor.cropScreen(-1f, -1f, renderWidth + 1f, renderHeight + 1f);
CONTAINER.draw(drawContext, 0, 0, mouseX, mouseY, chatOpen && !isOverChat(mouseX, mouseY), false, null);
drawContext.glState().enableAlpha();
applyColor(color); // Reset color to what it was
glState.setColor(color); // Reset color to what it was
}

@SubscribeEvent
Expand Down
13 changes: 0 additions & 13 deletions forge/src/main/java/fr/thesmyler/smylibgui/util/RenderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,4 @@ public static void drawClosedStrokeLine(Color color, float size, double... point
drawClosedStrokeLine(0d, color, size, points);
}

public static void applyColor(Color color) {
GlStateManager.color(
color.redf(),
color.greenf(),
color.bluef(),
color.alphaf()
);
}

public static Color currentColor() {
return new Color(GL11.glGetInteger(GL11.GL_CURRENT_COLOR));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.smyler.smylib.gui.widgets.AbstractSolidWidget;

import static net.smyler.smylib.Color.WHITE;
import static fr.thesmyler.smylibgui.util.RenderUtil.applyColor;

public class WarningWidget extends AbstractSolidWidget {

Expand All @@ -18,7 +17,7 @@ public WarningWidget(float x, float y, int z) {

@Override
public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) {
applyColor(WHITE);
context.glState().setColor(WHITE);
Minecraft.getMinecraft().getTextureManager().bindTexture(SmyLibGuiTextures.WIDGET_TEXTURES);
RenderUtil.drawModalRectWithCustomSizedTexture(x, y, 15, 54, this.width, this.height, 256, 256);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.smyler.smylib.gui.DrawContext;

import static net.smyler.smylib.Color.WHITE;
import static fr.thesmyler.smylibgui.util.RenderUtil.applyColor;
import static net.smyler.smylib.SmyLib.getGameClient;

public class TextButtonWidget extends AbstractButtonWidget {
Expand Down Expand Up @@ -50,7 +49,7 @@ public TextButtonWidget(int z, String str) {
public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) {
Minecraft mc = Minecraft.getMinecraft();
mc.getTextureManager().bindTexture(SmyLibGuiTextures.BUTTON_TEXTURES);
applyColor(WHITE);
context.glState().setColor(WHITE);
int textureDelta = 1;
Color textColor = this.enabledTextColor;
if (!this.isEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.util.ResourceLocation;

import static net.smyler.smylib.Color.WHITE;
import static fr.thesmyler.smylibgui.util.RenderUtil.applyColor;

public class TexturedButtonWidget extends AbstractButtonWidget {

Expand Down Expand Up @@ -86,7 +85,7 @@ public TexturedButtonWidget(int z, IncludedTexturedButtons properties) {
public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) {
Minecraft mc = Minecraft.getMinecraft();
mc.getTextureManager().bindTexture(this.texture);
applyColor(WHITE);
context.glState().setColor(WHITE);
int u = this.u;
int v = this.v;
if(!this.isEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.smyler.smylib.gui.DrawContext;

import static net.smyler.smylib.Color.WHITE;
import static fr.thesmyler.smylibgui.util.RenderUtil.applyColor;

public class ToggleButtonWidget extends AbstractButtonWidget {

Expand Down Expand Up @@ -88,7 +87,7 @@ public ToggleButtonWidget(int z, boolean startValue) {
public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) {
Minecraft mc = Minecraft.getMinecraft();
mc.getTextureManager().bindTexture(SmyLibGuiTextures.WIDGET_TEXTURES);
applyColor(WHITE);
context.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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.thesmyler.smylibgui.widgets.sliders;

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

import fr.thesmyler.smylibgui.SmyLibGuiTextures;
Expand All @@ -16,7 +17,6 @@
import net.smyler.smylib.gui.Font;

import static net.smyler.smylib.Color.WHITE;
import static fr.thesmyler.smylibgui.util.RenderUtil.applyColor;
import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.smylib.math.Math.saturate;

Expand Down Expand Up @@ -118,8 +118,9 @@ 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);
applyColor(WHITE);
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);
Expand All @@ -137,7 +138,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous

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

float sliderX = x + sliderPosition * (this.width - 8);
RenderUtil.drawTexturedModalRect(sliderX, y, 0, 66, 4, splitHeight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.util.function.Consumer;
import java.util.function.Predicate;

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

import fr.thesmyler.smylibgui.SmyLibGuiTextures;
Expand All @@ -18,15 +20,11 @@
import net.smyler.smylib.gui.widgets.Widget;
import net.smyler.smylib.gui.widgets.MenuWidget;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.smyler.smylib.gui.Font;

import static net.smyler.smylib.Color.BLUE;
import static net.smyler.smylib.Color.WHITE;
import static fr.thesmyler.smylibgui.util.RenderUtil.applyColor;
import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.smylib.game.Key.*;
import static net.smyler.smylib.math.Math.clamp;
Expand Down Expand Up @@ -127,6 +125,8 @@ public TextFieldWidget(int z) {
@Override
public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) {

GlState glState = context.glState();

this.cursorAnimation.update();

Color borderColor = this.borderColorNormal;
Expand All @@ -146,9 +146,9 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
}

if(this.isSearchBar) {
applyColor(WHITE);
glState.setColor(WHITE);
Minecraft.getMinecraft().getTextureManager().bindTexture(SmyLibGuiTextures.WIDGET_TEXTURES);
context.glState().enableAlpha();
glState.enableAlpha();
GlStateManager.enableBlend();
RenderUtil.drawModalRectWithCustomSizedTexture(x + this.width - 17, y + 2, 131, 0, 15, 15, 256, 256);
}
Expand Down Expand Up @@ -192,32 +192,24 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous

if (displaySelectionEnd != displaySelectionStart) {
float selectionBoxRenderRight = textRenderX + this.font.getStringWidth(string.substring(0, displaySelectionEnd));
this.drawSelectionHighlight(x, y, cursorX, textRenderY - 1, selectionBoxRenderRight - 1, textRenderY + 1 + 9);
this.drawSelectionHighlight(context, x, y, cursorX, textRenderY - 1, selectionBoxRenderRight - 1, textRenderY + 1 + 9);
}


}

private void drawSelectionHighlight(float x, float y, float x1, float y1, float x2, float y2) {
float dispX1 = Math.max(x1, x2);
float dispY1 = Math.max(y1, y2);
float dispX2 = Math.min(x1, x2);
float dispY2 = Math.min(y1, y2);
dispX2 = Math.min(dispX2, x + this.getEffectiveWidth());
dispX1 = Math.min(dispX1, x + this.getEffectiveWidth());
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder bufferBuilder = tessellator.getBuffer();
applyColor(BLUE);
private void drawSelectionHighlight(DrawContext context, float x, float y, float x1, float y1, float x2, float y2) {
float xRight = Math.max(x1, x2);
float yBottom = Math.max(y1, y2);
float xLeft = Math.min(x1, x2);
float yTop = Math.min(y1, y2);
xLeft = Math.min(xLeft, x + this.getEffectiveWidth());
xRight = Math.min(xRight, x + this.getEffectiveWidth());
GlStateManager.disableTexture2D();
GlStateManager.enableColorLogic();
GlStateManager.colorLogicOp(GlStateManager.LogicOp.OR_REVERSE);
bufferBuilder.begin(7, DefaultVertexFormats.POSITION);
bufferBuilder.pos(dispX1, dispY2, 0.0D).endVertex();
bufferBuilder.pos(dispX2, dispY2, 0.0D).endVertex();
bufferBuilder.pos(dispX2, dispY1, 0.0D).endVertex();
bufferBuilder.pos(dispX1, dispY1, 0.0D).endVertex();
tessellator.draw();
GlStateManager.disableColorLogic();
GlState state = context.glState();
state.enableColorLogic(ColorLogic.OR_REVERSE);
context.drawRectangle(xLeft, yTop, xRight, yBottom, BLUE);
state.disableColorLogic();
GlStateManager.enableTexture2D();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.thesmyler.terramap.gui.widgets;

import net.smyler.smylib.gui.DrawContext;
import net.smyler.smylib.gui.GlState;
import net.smyler.smylib.math.Vec2dMutable;
import org.lwjgl.opengl.GL11;

Expand All @@ -16,7 +17,6 @@
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;

import static net.smyler.smylib.Color.WHITE;
import static fr.thesmyler.smylibgui.util.RenderUtil.applyColor;

public class CircularCompassWidget implements Widget {

Expand Down Expand Up @@ -64,14 +64,15 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous

float radius = this.size / 2;

GlState glState = context.glState();
GlStateManager.pushMatrix();
GlStateManager.translate(x + radius, y + radius, 0);
context.drawPolygon(background, this.vertices);
//RenderUtil.drawClosedStrokeLine(Color.BLACK, 1f, vertices);
GlStateManager.rotate(this.azimuth, 0, 0, 1);
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder builder = tessellator.getBuffer();
applyColor(WHITE);
glState.setColor(WHITE);
GlStateManager.shadeModel(7425);
context.glState().enableAlpha();
GlStateManager.enableBlend();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.thesmyler.terramap.gui.widgets;

import net.smyler.smylib.gui.DrawContext;
import net.smyler.smylib.gui.GlState;
import org.lwjgl.opengl.GL11;

import net.smyler.smylib.gui.containers.WidgetContainer;
Expand All @@ -17,7 +18,6 @@
import net.minecraft.util.ResourceLocation;

import static net.smyler.smylib.Color.WHITE;
import static fr.thesmyler.smylibgui.util.RenderUtil.applyColor;

public class RibbonCompassWidget implements Widget {

Expand Down Expand Up @@ -61,11 +61,12 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
double rightU = leftU + (double) this.width / this.textureWidth;
double rightCU = rightU - blendBorder/this.textureWidth;

GlState glState = context.glState();
Tessellator tess = Tessellator.getInstance();
BufferBuilder buff = tess.getBuffer();

GlStateManager.enableTexture2D();
context.glState().enableAlpha();
glState.enableAlpha();
GlStateManager.enableBlend();
Minecraft.getMinecraft().getTextureManager().bindTexture(COMPASS_BACKGROUND_TEXTURE);
GlStateManager.shadeModel(GL11.GL_SMOOTH);
Expand All @@ -90,7 +91,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
tess.draw();

Minecraft.getMinecraft().getTextureManager().bindTexture(COMPASS_INDICATOR_TEXTURE);
applyColor(WHITE);
glState.setColor(WHITE);
buff.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
double indX = x + (double)(this.width - this.indicatorWidth) / 2;
double indY = y + (double)(this.height - this.indicatorHeight) / 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashSet;
import java.util.Set;

import net.smyler.smylib.gui.GlState;
import net.smyler.smylib.gui.containers.WidgetContainer;
import fr.thesmyler.smylibgui.util.*;
import fr.thesmyler.terramap.gui.widgets.map.MapLayer;
Expand All @@ -28,7 +29,6 @@
import net.smyler.terramap.util.geo.WebMercatorUtil;

import static net.smyler.smylib.Color.WHITE;
import static fr.thesmyler.smylibgui.util.RenderUtil.applyColor;
import static net.smyler.smylib.SmyLib.getGameClient;

abstract public class RasterMapLayer extends MapLayer {
Expand Down Expand Up @@ -65,6 +65,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
Font smallFont = getGameClient().smallestFont();
Minecraft mc = Minecraft.getMinecraft();
TextureManager textureManager = mc.getTextureManager();
GlState glState = context.glState();
float rotation = this.getRotation();

boolean perfectDraw = true;
Expand Down Expand Up @@ -216,7 +217,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
dY += factorY * renderSizedSize;
}

applyColor(WHITE);
glState.setColor(WHITE);
ResourceLocation texture = defaultTexture;
try {
if(tile.isTextureAvailable()) texture = tile.getTexture();
Expand Down Expand Up @@ -249,7 +250,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
smallFont.drawString((float)dispX + 2, (float)(dispY + displayHeight/2), GeoServices.formatGeoCoordForDisplay(dispX), lineColor, false);
smallFont.drawCenteredString((float)(dispX + displayWidth/2), (float)dispY + 2, GeoServices.formatGeoCoordForDisplay(dispY), lineColor, false);
}
applyColor(WHITE);
glState.setColor(WHITE);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.thesmyler.terramap.gui.widgets.markers.markers.entities;

import net.smyler.smylib.gui.GlState;
import net.smyler.smylib.gui.containers.WidgetContainer;
import net.smyler.smylib.Color;
import fr.thesmyler.smylibgui.util.RenderUtil;
Expand All @@ -20,7 +21,6 @@
import net.minecraft.util.text.ITextComponent;

import static net.smyler.smylib.Color.WHITE;
import static fr.thesmyler.smylibgui.util.RenderUtil.applyColor;

public abstract class AbstractLivingMarker extends AbstractMovingMarker {

Expand All @@ -44,16 +44,16 @@ public AbstractLivingMarker(MarkerController<?> controller, float width, float h

@Override
public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) {
applyColor(WHITE);
GlState glState = context.glState();
boolean drawName = hovered;
if(parent instanceof MapWidget) {
MapWidget map = (MapWidget) parent;
drawName = drawName && !map.getContext().equals(MapContext.MINIMAP);
}
context.glState().enableAlpha();
glState.enableAlpha();
if(hovered) context.drawRectangle(x +1, y +1, x + 1 + this.width, y + 1 + this.height, Color.LIGHT_OVERLAY);
Minecraft.getMinecraft().getTextureManager().bindTexture(this.texture);
applyColor(WHITE);
glState.setColor(WHITE);
GlStateManager.enableBlend();
RenderUtil.drawModalRectWithCustomSizedTexture(x, y, this.u, this.v, this.width, this.height, this.textureWidth, this.textureHeight);

Expand Down
Loading

0 comments on commit 7a4c1ae

Please sign in to comment.