Skip to content

Commit

Permalink
Working on API
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishisherewithhh committed Apr 10, 2024
1 parent 4eb6360 commit ac25bf7
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class GlobalConfig {
.build())
.build();

/**
* Common scale for all widgets. Set by the user using YACL.
*/
@SerialEntry
public float scale = 1.0f;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.tanishisherewith.dynamichud.newTrial.helpers;

import net.minecraft.util.math.MathHelper;

import java.awt.*;

/**
* This class provides helper methods for working with colors.
*/
public class ColorHelper {
public static int r, g, b, a;
public ColorHelper(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
this.a = 255;
validate();
}
public ColorHelper() {
}
public void validate() {
if (r < 0) r = 0;
else if (r > 255) r = 255;

if (g < 0) g = 0;
else if (g > 255) g = 255;

if (b < 0) b = 0;
else if (b > 255) b = 255;

if (a < 0) a = 0;
else if (a > 255) a = 255;
}
/**
* Returns a color as an integer value given its red, green and blue components.
*
* @param red The red component of the color
* @param green The green component of the color
* @param blue The blue component of the color
* @return The color as an integer value
*/
public static int getColor(int red, int green, int blue) {
return getColor(red, green, blue, 255);
}

/**
* Returns a color as an integer value given its red, green, blue and alpha components.
*
* @param red The red component of the color
* @param green The green component of the color
* @param blue The blue component of the color
* @param alpha The alpha component of the color
* @return The color as an integer value
*/
public static int getColor(int red, int green, int blue, int alpha) {
return (alpha << 24) | (red << 16) | (green << 8) | blue;
}

public static Color getColorFromInt(int color) {
int red = (color >> 16) & 0xFF;
int green = (color >> 8) & 0xFF;
int blue = color & 0xFF;
return new Color(red, green, blue);
}

/**
* Returns a color as an integer value given its hue.
*
* @param hue The hue of the color
* @return The color as an integer value
*/
public static int getColorFromHue(float hue) {
return Color.HSBtoRGB(hue, 1.0f, 1.0f);
}

/**
* Converts a color to an integer.
*
* @param color The color to convert
* @return The integer representation of the JWT color
*/
public static int ColorToInt(Color color) {
return color.getRGB();
}
public static float[] getRainbowColor()
{
float x = System.currentTimeMillis() % 2000 / 1000F;
float pi = (float)Math.PI;

float[] rainbow = new float[3];
rainbow[0] = 0.5F + 0.5F * MathHelper.sin(x * pi);
rainbow[1] = 0.5F + 0.5F * MathHelper.sin((x + 4F / 3F) * pi);
rainbow[2] = 0.5F + 0.5F * MathHelper.sin((x + 8F / 3F) * pi);
return rainbow;
}

/**
* Rainbow color with custom speed.
*
* @param speed
* @return Current rainbow color.
*/
public static Color getRainbowColor(int speed) {
float hue = (System.currentTimeMillis() % (speed * 100)) / (speed * 100.0f);
return Color.getHSBColor(hue, 1.0f, 1.0f);
}


/**
* Changes alpha on color.
*
* @param color Target color.
* @param alpha Target alpha.
* @return Color with changed alpha.
*/
public static Color changeAlpha(Color color, int alpha) {
if (color != null)
return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
else
return new Color(0);
}
public static int fromRGBA(int r, int g, int b, int a) {
return (r << 16) + (g << 8) + (b) + (a << 24);
}

public static int toRGBAR(int color) {
return (color >> 16) & 0x000000FF;
}

public static int toRGBAG(int color) {
return (color >> 8) & 0x000000FF;
}

public static int toRGBAB(int color) {
return (color) & 0x000000FF;
}

public static int toRGBAA(int color) {
return (color >> 24) & 0x000000FF;
}
public int toInt()
{
return new Color(r,b,g,a).getRGB();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
package com.tanishisherewith.dynamichud.newTrial.helpers;

import com.mojang.blaze3d.systems.RenderSystem;
import com.tanishisherewith.dynamichud.util.CustomItemRenderer;
import com.tanishisherewith.dynamichud.util.CustomTextRenderer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;

/**
* This class provides helper methods for drawing textures on the screen.
*/
public class TextureHelper extends DrawContext {
public static CustomTextRenderer customTextRenderer;
public static CustomItemRenderer customItemRenderer;


public TextureHelper(MinecraftClient client, VertexConsumerProvider.Immediate vertexConsumers) {
super(client, vertexConsumers);
}

/**
* Draws an item texture on the screen.
*
* @param itemStack The item stack to render the texture for
* @param x The x position to draw the texture at
* @param y The y position to draw the texture at
*/
public static void drawItemTexture(DrawContext drawContext,
ItemStack itemStack,
int x,
int y) {
drawContext.drawItem(itemStack, x, y);
}

/**
* Draws the texture of the item in the player's main hand on the screen.
*
* @param client The Minecraft client instance
* @param x The x position to draw the texture at
* @param y The y position to draw the texture at
*/
public static void drawMainHandTexture(DrawContext drawContext,
MinecraftClient client,
int x,
int y) {
assert client.player != null;
ItemStack mainHandItem = client.player.getMainHandStack();
drawItemTexture(drawContext, mainHandItem, x, y);
}

/**
* Draws a textured rectangle on the screen.
*
* @param x The x position of the top left corner of the rectangle
* @param y The y position of the top left corner of the rectangle
* @param u The x position of the texture within the texture image
* @param v The y position of the texture within the texture image
* @param width The width of the rectangle
* @param height The height of the rectangle
* @param textureWidth The width of the texture image
* @param textureHeight The height of the texture image
*/
public static void drawTexture(DrawContext drawContext, Identifier texture, int x, int y, int u, int v, int width, int height, int textureWidth, int textureHeight) {
drawContext.drawTexture(texture, x, y, u, v, width, height, textureWidth, textureHeight);
}

/**
* Draws a textured rectangle on the screen with a specified color.
*
* @param x The x position of the top left corner of the rectangle
* @param y The y position of the top left corner of the rectangle
* @param u The x position of the texture within the texture image
* @param v The y position of the texture within the texture image
* @param width The width of the rectangle
* @param height The height of the rectangle
* @param color The color to draw the rectangle with
*/
public static void drawTexturedRect(DrawContext drawContext, Identifier texture, int x, int y, int u, int v, int width, int height, int color) {
RenderSystem.setShaderColor((color >> 16 & 255) / 255.0F,
(color >> 8 & 255) / 255.0F,
(color & 255) / 255.0F,
(color >> 24 & 255) / 255.0F);
drawContext.drawTexture(texture, x, y, u, v, width, height);
}

/**
* Draws an item texture on the screen with text at a specified position relative to it.
*
* @param textRenderer The text renderer instance used for rendering the text
* @param itemStack The item stack to render the texture for
* @param x The x position to draw the texture at
* @param y The y position to draw the texture at
* @param text The text to draw relative to the texture
* @param color The color to draw the text with
* @param position The position of the text relative to the texture (ABOVE, BELOW, LEFT, or RIGHT)
* @param scale The scale factor to apply to the text (1.0 is normal size)
*/
public static void drawItemTextureWithText(DrawContext drawContext,
TextRenderer textRenderer,
ItemStack itemStack,
int x,
int y,
String text,
int color,
Position position,
float scale,
boolean textBackground
) {
// Calculate the position of the text based on its size and the specified position
int textWidth = (int) (textRenderer.getWidth(text) * scale);
int textHeight = (int) (textRenderer.fontHeight * scale);
int textX = 0;
int textY = 0;
switch (position) {
case ABOVE -> {
textX = x + (16 - textWidth) / 2;
textY = y - textHeight;
}
case BELOW -> {
textX = x + (17 - textWidth) / 2;
textY = y + 16;
}
case LEFT -> {
textX = x - textWidth - 2;
textY = y + (16 - textHeight) / 2;
}
case RIGHT -> {
textX = x + 18;
textY = y + (16 - textHeight) / 2;
}
}

// Draw semi-opaque black rectangle
if (text != null) {
if (textBackground && !text.trim().isEmpty()) {
int backgroundColor = 0x40000000; // ARGB format: 50% opaque black
drawContext.fill(textX - 1, textY - 1, textX + textWidth + 1, textY + textHeight + 1, backgroundColor);
}
// Draw the scaled text at the calculated position
drawContext.getMatrices().push();
drawContext.getMatrices().scale(scale, scale, 1.0f);
float scaledX = textX / scale;
float scaledY = textY / scale;
drawContext.drawText(textRenderer, text, (int) scaledX, (int) scaledY, color, false);
drawContext.getMatrices().pop();
}
// Draw the item texture
drawContext.drawItem(itemStack, x, y);
}

/**
* Draws an item texture on the screen with text at a specified position relative to it.
*
* @param itemScale The scale for the item to be rendered at
* @param textRenderer The text renderer instance used for rendering the text
* @param itemStack The item stack to render the texture for
* @param x The x position to draw the texture at
* @param y The y position to draw the texture at
* @param text The text to draw relative to the texture
* @param color The color to draw the text with
* @param position The position of the text relative to the texture (ABOVE, BELOW, LEFT, or RIGHT)
* @param textScale The scale factor to apply to the text (1.0 is normal size)
*/
public static void drawItemTextureWithTextAndScale(DrawContext drawContext,
float itemScale,
TextRenderer textRenderer,
ItemStack itemStack,
int x,
int y,
String text,
int color,
Position position,
float textScale,
boolean textBackground) {
if (text != null && !text.trim().isEmpty()) {
// Calculate the position of the text based on its size and the specified position
int textWidth = (int) (textRenderer.getWidth(text) * textScale);
int textHeight = (int) (textRenderer.fontHeight * textScale);
int textX = switch (position) {
case ABOVE, BELOW -> x + (int) ((16 * itemScale - textWidth) / 2);
case LEFT -> x - textWidth - 2;
case RIGHT -> x + (int) (16 * itemScale + 2);
};
int textY = switch (position) {
case ABOVE -> y - textHeight - 2;
case BELOW -> y + (int) (16 * itemScale + 2);
case LEFT, RIGHT -> y + (int) ((16 * itemScale - textHeight) / 2);
};

// Draw semi-opaque black rectangle
if (textBackground) {
int backgroundColor = 0x40000000; // ARGB format: 50% opaque black
drawContext.fill(textX, textY - 1, textX + textWidth + 1, textY + textHeight + 1, backgroundColor);
}

// Draw the scaled text at the calculated position
drawContext.getMatrices().push();
drawContext.getMatrices().scale(textScale, textScale, 11.0f);
float scaledX = textX / textScale;
float scaledY = textY / textScale;
drawContext.drawText(textRenderer, text, (int) scaledX + 1, (int) scaledY +1, color, false);
drawContext.getMatrices().pop();
}
customItemRenderer = new CustomItemRenderer(itemStack, itemScale);
customItemRenderer.draw(drawContext, x, y, color);
}

public enum Position {
ABOVE("Above"),
RIGHT("Right"),
BELOW("Below"),
LEFT("Left");
private final String name;

Position(String name) {
this.name = name;
}

public static Position getByUpperCaseName(String name) {
if (name == null || name.isEmpty()) {
return null;
}

return Position.valueOf(name.toUpperCase());
}
}


}
Loading

0 comments on commit ac25bf7

Please sign in to comment.