Skip to content

Commit

Permalink
More changes and fixes. Scaling improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishisherewithhh committed Apr 11, 2024
1 parent a350075 commit 031c8af
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import java.util.List;

public class DynamicHudTestTWO implements DynamicHudIntegration {
TextWidget textWidget;
TextWidget exampleWidget;
WidgetRenderer renderer;
@Override
public void init() {
//Global registry
DynamicValueRegistry.registerGlobal("CPS",() -> "NOT FPS");


textWidget = new TextWidget.Builder()
exampleWidget = new TextWidget.Builder()
.setX(150)
.setY(100)
.setDraggable(true)
Expand Down Expand Up @@ -56,7 +56,7 @@ public File getWidgetsFile() {

@Override
public void addWidgets() {
WidgetManager.addWidget(textWidget);
WidgetManager.addWidget(exampleWidget);
}
public void initAfter(){
List<Widget> widgets = WidgetManager.getWidgetsForMod("CustomMod");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.tanishisherewith.dynamichud.newTrial.screens;

import com.tanishisherewith.dynamichud.newTrial.widget.WidgetRenderer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;

public abstract class AbstractMoveableScreen extends Screen {
protected boolean ShouldPause = false; // To pause if the screen is opened or not
protected boolean shouldPause = false; // To pause if the screen is opened or not
public final WidgetRenderer widgetRenderer;
public int snapSize = 100;

Expand Down Expand Up @@ -81,18 +81,12 @@ public void close() {
}

public void setShouldPause(boolean shouldPause) {
this.ShouldPause = shouldPause;
}


@Override
public void resize(MinecraftClient client, int width, int height) {
super.resize(client, width, height);
this.shouldPause = shouldPause;
}

@Override
public boolean shouldPause() {
return ShouldPause;
return shouldPause;
}
public void setSnapSize(int size){
this.snapSize = size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ColorGradientPicker(int x, int y, Color initialColor, Consumer<Color> onC
this.gradientBox.setValue(hsv[2]);

this.alphaSlider = new AlphaSlider(x,y,10,boxSize,initialColor);
this.colorPickerButton = new ColorPickerButton(x + boxSize + 8, y + 20, 35, 20);
this.colorPickerButton = new ColorPickerButton(x + boxSize + 8, y + 20, 30, 18);
}
public void setPos(int x, int y){
this.x = x;
Expand All @@ -50,22 +50,12 @@ public void display() {
public void close() {
display = false;
}
public void tick() {
gradientSlider.tick();
gradientBox.tick();
}
public void defaultValues(){
gradientSlider.defaultValues();
gradientBox.defaultValues();
}

public void render(DrawContext drawContext, int x1, int y1) {
setPos(x1,y1);
if(!display){
defaultValues();
return;
}
tick();
gradientSlider.render(drawContext,x + 30, y +client.textRenderer.fontHeight + 4);
gradientBox.render(drawContext,x + 30, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 10);
colorPickerButton.render(drawContext,x+ 55 + boxSize,y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public ColorOption(String name,ContextMenu parentMenu, Supplier<Color> getter, C
@Override
public void render(DrawContext drawContext, int x, int y) {
super.render(drawContext, x, y);
System.out.println(value.getAlpha());

int color = isVisible ? Color.GREEN.getRGB() : Color.RED.getRGB();
this.height = mc.textRenderer.fontHeight;
Expand All @@ -42,7 +41,7 @@ public void render(DrawContext drawContext, int x, int y) {
1,
1 );

colorPicker.render(drawContext,this.x + parentMenu.width + 10,y - 10);
colorPicker.render(drawContext,this.x + width/3 + parentMenu.width + 10,y - 10);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,19 @@

public class GradientBox {
private final int size;
private final float alphaSpeed = 0.05f;
private int x;
private int y;
private float hue = 0.0f;
private float saturation = 1.0f;
private float value = 1.0f;
private boolean isDragging = false;
private float alpha = 0.0f;


public GradientBox(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
}

public void tick() {
// Update the alpha
alpha += alphaSpeed;
if (alpha > 1.0f) {
alpha = 1.0f;
}
}

public void render(DrawContext drawContext, int x, int y) {
setPosition(x,y);
drawContext.getMatrices().push();
Expand All @@ -41,16 +30,6 @@ public void render(DrawContext drawContext, int x, int y) {

// Draw the gradient
com.tanishisherewith.dynamichud.newTrial.helpers.DrawHelper.drawRoundedGradientRectangle(drawContext.getMatrices().peek().getPositionMatrix(), Color.BLACK, Color.BLACK, Color.getHSBColor(hue, 1.0f, 1.0f), Color.WHITE, x, y, size, size,2);
/* for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
float saturation = (float) i / size;
float value = 1.0f - (float) j / size;
int color = Color.HSBtoRGB(hue, saturation, value);
color = (color & 0x00FFFFFF) | ((int) (alpha * 255) << 24);
drawContext.fill(x + i, y + j, x + i + 1, y + j + 1, color);
}
}
*/

// Draw the handle
float handleSize = 3;
Expand All @@ -60,9 +39,6 @@ public void render(DrawContext drawContext, int x, int y) {
DrawHelper.fillRoundedRect(drawContext, (int) handleX, (int) handleY, (int) (handleX + handleSize), (int) (handleY + handleSize), -1);
drawContext.getMatrices().pop();
}
public void defaultValues(){
alpha = 0.0f;
}


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
public class GradientSlider {
private final int width;
private final int height;
private final float progressSpeed = 0.1f;
private final float alphaSpeed = 0.05f;
private int x;
private int y;
private float hue = 0.0f;
private float progress = 0.0f;
private boolean isDragging = false;
private float alpha = 0.0f;

public GradientSlider(int x, int y, int width, int height) {
this.x = x;
Expand All @@ -26,23 +22,6 @@ public GradientSlider(int x, int y, int width, int height) {
this.height = height;
}

public void tick() {
// Update the progress
progress += progressSpeed;
if (progress > 1.0f) {
progress = 1.0f;
}

// Update the alpha
alpha += alphaSpeed;
if (alpha > 1.0f) {
alpha = 1.0f;
}
}
public void defaultValues(){
progress = 0.0f;
alpha = 0.0f;
}

/**
* Sets position.
Expand All @@ -66,20 +45,18 @@ public void render(DrawContext drawContext, int x, int y) {
for (int i = 0; i < width; i++) {
float hue = (float) i / width;
int color = Color.HSBtoRGB(hue, 1.0f, 1.0f);
color = (color & 0x00FFFFFF) | ((int) (alpha * 255) << 24);
color = (color & 0x00FFFFFF) | (255 << 24);
drawContext.fill(x + i, y, x + i + 1, y + height, color);
}


// Draw the handle
if (progress >= 1.0f) {
float handleWidth = 3;
float handleHeight = height + 4;
float handleX = x + hue * width - handleWidth / 2.0f;
float handleY = y - (handleHeight - height) / 2.0f;

DrawHelper.fillRoundedRect(drawContext, (int) handleX, (int) handleY, (int) (handleX + handleWidth), (int) (handleY + handleHeight), -1);
}
drawContext.getMatrices().pop();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.tanishisherewith.dynamichud.newTrial.widget;

import com.tanishisherewith.dynamichud.helpers.ColorHelper;
import com.tanishisherewith.dynamichud.helpers.DrawHelper;
import com.tanishisherewith.dynamichud.newTrial.config.GlobalConfig;
import com.tanishisherewith.dynamichud.newTrial.helpers.ColorHelper;
import com.tanishisherewith.dynamichud.newTrial.helpers.DrawHelper;
import com.tanishisherewith.dynamichud.newTrial.utils.UID;
import com.tanishisherewith.dynamichud.widget.WidgetBox;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.nbt.NbtCompound;
Expand Down Expand Up @@ -34,9 +33,6 @@ public abstract class Widget {

//To enable/disable snapping
public boolean shiftDown = false;
public int snapBoxWidth,snapBoxHeight;
public static int snapSize = 1;
// Used for dragging and snapping
int startX, startY;

// Absolute position of the widget on screen in pixels.
Expand Down Expand Up @@ -69,7 +65,7 @@ public abstract class Widget {

public Widget(WidgetData<?> DATA, String modId) {
Widget.DATA = DATA;
widgetBox = new WidgetBox(0, 0, 0, 0, 1);
widgetBox = new WidgetBox(0, 0, 0, 0);
this.modId = modId;
init();
}
Expand Down Expand Up @@ -108,8 +104,8 @@ public float getHeight() {
}

public void setPosition(int x, int y) {
this.x = x;
this.y = y;
this.x = x;
this.y = y;
}

public void setDraggable(boolean draggable) {
Expand Down Expand Up @@ -152,22 +148,17 @@ public void render(DrawContext drawContext, int mouseX, int mouseY) {
* Renders the widget on the editor screen.
*/
public void renderInEditor(DrawContext drawContext, int mouseX, int mouseY) {
displayBg(drawContext);

if (shouldScale) {
DrawHelper.scaleAndPosition(drawContext.getMatrices(), getX(), getY(),GlobalConfig.get().scale);
}
// Calculate the size of each snap box
snapBoxWidth = mc.getWindow().getScaledWidth() / snapSize;
snapBoxHeight = mc.getWindow().getScaledHeight() / snapSize;

renderWidgetInEditor(drawContext,mouseX,mouseY);

if (shouldScale) {
DrawHelper.stopScaling(drawContext.getMatrices());
}
}
protected void updateWidgetBox(){
widgetBox.setSizeAndPosition(x,y,getWidth(),getHeight());
}


/**
Expand All @@ -191,7 +182,7 @@ protected void updateWidgetBox(){
* @param context
*/
private void renderWidgetInEditor(DrawContext context,int mouseX, int mouseY) {
displayBg(context);
//displayBg(context);

renderWidget(context,mouseX,mouseY);
}
Expand All @@ -208,29 +199,28 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
}

public boolean mouseDragged(double mouseX, double mouseY, int button, int snapSize) {
Widget.snapSize = snapSize;
if (dragging && button == GLFW.GLFW_MOUSE_BUTTON_LEFT) {
int newX = (int) (mouseX - startX);
int newY = (int) (mouseY - startY);

// Divides the screen into several "grid boxes" which the elements snap to.
// Higher the snapSize, more the grid boxes
if (this.shiftDown) {
// Calculate the index of the snap box that the new position would be in
int snapBoxX = newX / snapBoxWidth;
int snapBoxY = newY / snapBoxHeight;

// Snap the new position to the top-left corner of the snap box
newX = snapBoxX * snapBoxWidth;
newY = snapBoxY * snapBoxHeight;
// Calculate the size of each snap box
int snapBoxWidth = mc.getWindow().getScaledWidth() / snapSize;
int snapBoxHeight = mc.getWindow().getScaledHeight() / snapSize;

// Calculate the index of the snap box that the new position would be in and
// snap the new position to the top-left corner of the snap box
newX = (newX / snapBoxWidth) * snapBoxWidth;
newY = (newY / snapBoxHeight) * snapBoxHeight;
}

this.x = (int) MathHelper.clamp(newX, 0, mc.getWindow().getScaledWidth() - getWidth());
this.y = (int) MathHelper.clamp(newY, 0, mc.getWindow().getScaledHeight() - getHeight());


this.xPercent = (float) this.getX() / mc.getWindow().getScaledWidth();
this.yPercent = (float) this.getY() / mc.getWindow().getScaledHeight();
this.xPercent = (float) this.x / mc.getWindow().getScaledWidth();
this.yPercent = (float) this.y / mc.getWindow().getScaledHeight();

return true;
}
Expand Down Expand Up @@ -267,7 +257,12 @@ public void onClose(){
protected void displayBg(DrawContext context) {
int backgroundColor = this.shouldDisplay() ? ColorHelper.getColor(0, 0, 0, 128) : ColorHelper.getColor(255, 0, 0, 128);
WidgetBox box = this.getWidgetBox();
DrawHelper.fill(context, (int) box.x1, (int) box.y1, (int) box.x2, (int) box.y2, backgroundColor);
DrawHelper.drawRectangle(context.getMatrices().peek().getPositionMatrix(),
box.x,
box.y,
box.getWidth(),
box.getHeight(),
backgroundColor);
}


Expand Down
Loading

0 comments on commit 031c8af

Please sign in to comment.