Skip to content

Commit ed73a38

Browse files
committed
👷 improvements + fixes + funsies
- hover stuff - scaling offset anchor shit is now actually working as it should. - refactored the hitscan quite a bit. - things are now able to fade, missed ProgressElement but im lazy i think. - not sure how id get items to fade? - Happenings are basically just things that can happen when you hover over the things. currently made it just for fun, wont exist later. - added performance mods and qol dependencies note: actual insane spaghetti code in the render method in AbstractElement. will hopefully clean it up but it works, so its okay for now
1 parent 9ea13a0 commit ed73a38

13 files changed

Lines changed: 170 additions & 100 deletions

File tree

build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ group = mod_group_id
1111

1212
repositories {
1313
mavenLocal()
14+
15+
maven {
16+
name = "Modrinth"
17+
url = "https://api.modrinth.com/maven"
18+
}
1419
}
1520

1621
base {
@@ -92,6 +97,11 @@ sourceSets.main.resources { srcDir 'src/generated/resources' }
9297

9398

9499
dependencies {
100+
runtimeOnly("maven.modrinth:sodium:${sodium_version}")
101+
runtimeOnly("maven.modrinth:immediatelyfast:${immfast_version}")
102+
runtimeOnly("maven.modrinth:lithium:${lithium_version}")
103+
runtimeOnly("maven.modrinth:better-modlist:${modlist_version}")
104+
runtimeOnly("maven.modrinth:brick-furnace:mDs1svVc")
95105

96106
}
97107

gradle.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ loader_version_range=[4,)
1515
mod_id=structura
1616
mod_name=Structura
1717

18+
sodium_version=mc26.1.1-0.8.9-neoforge
19+
immfast_version=1.15.1+26.1.1-neoforge
20+
lithium_version=mc26.1.1-0.23.0-neoforge
21+
22+
modlist_version=26.1.1
23+
1824
mod_license=MIT
1925
mod_version=0.3.1
2026
mod_group_id=net.raccoon.will

src/main/java/net/raccoon/will/structura/api/feature/Hitscan.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,41 @@ public static HitscanResult performHitscan(Player player, double maxDistance) {
3434
Vec3 eyePos = player.getEyePosition(1f);
3535
Vec3 lookVec = player.getLookAngle();
3636
Vec3 reachPos = eyePos.add(lookVec.scale(maxDistance));
37-
38-
//raytrace block first
3937
BlockHitResult blockHit = level.clip(new ClipContext(
4038
eyePos,
4139
reachPos,
4240
ClipContext.Block.COLLIDER,
4341
ClipContext.Fluid.NONE,
4442
player
4543
));
46-
double blockDist = blockHit.getType() == HitResult.Type.MISS ? Double.MAX_VALUE
44+
45+
if (blockHit.getType() == HitResult.Type.MISS) {
46+
blockHit = null;
47+
}
48+
49+
double blockDist = blockHit == null ? Double.MAX_VALUE
4750
: blockHit.getLocation().distanceTo(eyePos);
4851

49-
//check entities
5052
EntityHitResult entityHit = ProjectileUtil.getEntityHitResult(
5153
level,
5254
player,
5355
eyePos,
5456
reachPos,
55-
player.getBoundingBox().expandTowards(lookVec.scale(maxDistance)).inflate(1.0),
57+
player.getBoundingBox().expandTowards(lookVec.scale(maxDistance)).inflate(0.25),
5658
e -> e instanceof LivingEntity && e != player,
57-
1f
59+
0.1f
5860
);
61+
5962
double entityDist = entityHit == null ? Double.MAX_VALUE : entityHit.getLocation().distanceTo(eyePos);
6063

61-
//returns closest hit
62-
if (entityDist < blockDist) return HitscanResult.entity(entityHit);
63-
if (blockDist < Double.MAX_VALUE) return HitscanResult.block(blockHit);
64+
if (blockDist <= entityDist && blockHit != null) {
65+
return HitscanResult.block(blockHit);
66+
}
67+
68+
if (entityHit != null) {
69+
return HitscanResult.entity(entityHit);
70+
}
71+
6472
return HitscanResult.miss();
6573
}
6674
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.raccoon.will.structura.api.gui.animation;
2+
3+
import net.raccoon.will.structura.api.gui.element.AbstractElement;
4+
5+
public class Happenings {
6+
7+
public static float easeInSine(float t) {
8+
return (float) (1.0 - Math.cos((t * Math.PI) / 2.0));
9+
}
10+
11+
public void scaling(float speed, AbstractElement element) {
12+
float current = element.getScale();
13+
float target = element.isHovered()
14+
? element.getInitialScale() * 2
15+
: element.getInitialScale();
16+
17+
element.setScale(current + (target - current) * speed);
18+
}
19+
20+
public void fading(float speed, AbstractElement element) {
21+
float current = element.getAlpha();
22+
float target = element.isHovered() ? 1.0f : 0.0f;
23+
24+
float eased = easeInSine(speed);
25+
element.setAlpha(current + (target - current) * eased);
26+
}
27+
}

src/main/java/net/raccoon/will/structura/api/gui/element/AbstractElement.java

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ public abstract class AbstractElement {
2626

2727
protected final float initialScale;
2828
protected final int initialWidth, initialHeight, initialX, initialY;
29+
protected float alpha = 1.0F;
2930

30-
public AbstractElement(String id, int width, int height, Anchor anchor, int posX, int posY) {
31+
public AbstractElement(String id, int width, int height, Anchor anchor, int posX, int posY, float scale) {
3132
this.id = id;
3233

3334
this.width = width;
3435
this.height = height;
3536
this.anchor = anchor;
3637
this.x = posX;
3738
this.y = posY;
39+
this.scale = scale;
3840

3941
this.initialWidth = width;
4042
this.initialHeight = height;
@@ -44,15 +46,45 @@ public AbstractElement(String id, int width, int height, Anchor anchor, int posX
4446
}
4547

4648
protected abstract void draw(GuiGraphicsExtractor graphics);
47-
public void render(GuiGraphicsExtractor graphics, int screenWidth, int screenHeight) {
49+
50+
public void render(GuiGraphicsExtractor graphics, int mouseX, int mouseY, int screenWidth, int screenHeight) {
51+
if (alpha == 0.0F) visible = false;
4852
if (!visible) return;
53+
boolean wasHovered = this.isHovered;
4954

5055
int x = getRenderX(screenWidth);
5156
int y = getRenderY(screenHeight);
5257

58+
this.isHovered = isActive()
59+
&& mouseX >= x && mouseY >= y
60+
&& mouseX < x + (int)(width * initialScale)
61+
&& mouseY < y + (int)(height * initialScale);
62+
63+
if (isHovered && !wasHovered) onHoverStart();
64+
else if (!isHovered && wasHovered) onHoverEnd();
65+
if (isHovered) onHover();
66+
5367
graphics.pose().pushMatrix();
68+
69+
//holy spaghetti code
70+
float pivotX = switch (elementAnchor) {
71+
case TOP_LEFT, CENTER_LEFT, BOTTOM_LEFT -> 0;
72+
case TOP_CENTER, CENTER, BOTTOM_CENTER -> width / 2f;
73+
case TOP_RIGHT, CENTER_RIGHT, BOTTOM_RIGHT -> (float) width;
74+
};
75+
76+
float pivotY = switch (elementAnchor) {
77+
case TOP_LEFT, TOP_CENTER, TOP_RIGHT -> 0;
78+
case CENTER_LEFT, CENTER, CENTER_RIGHT -> height / 2f;
79+
case BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT -> (float) height;
80+
};
81+
5482
graphics.pose().translate(x, y);
55-
graphics.pose().scale(scale, scale);
83+
graphics.pose().translate(pivotX * initialScale, pivotY * initialScale);
84+
graphics.pose().scale(scale / initialScale, scale / initialScale);
85+
graphics.pose().translate(-pivotX * initialScale, -pivotY * initialScale);
86+
graphics.pose().scale(initialScale, initialScale);
87+
5688
draw(graphics);
5789

5890
if (debug) {
@@ -67,20 +99,6 @@ public boolean isActive() {
6799
return this.visible && this.active;
68100
}
69101

70-
71-
72-
public int getRight() {
73-
return this.getX() + this.width;
74-
}
75-
76-
public int getBottom() {
77-
return this.getY() + this.height;
78-
}
79-
80-
private boolean areCoordinatesInRectangle(double x, double y) {
81-
return x >= (double)this.getX() && y >= (double)this.getY() && x < (double)this.getRight() && y < (double)this.getBottom();
82-
}
83-
84102
public int getWidth() {
85103
return this.width;
86104
}
@@ -89,13 +107,9 @@ public int getHeight() {
89107
return this.height;
90108
}
91109

92-
public boolean isMouseOver(double mouseX, double mouseY) {
93-
return this.isActive() && this.areCoordinatesInRectangle(mouseX, mouseY);
94-
}
95-
96-
public boolean isHovered() {
97-
return this.isHovered;
98-
}
110+
public void onHoverStart() {}
111+
public void onHover() {}
112+
public void onHoverEnd() {}
99113

100114
//* Misc
101115
public void debug (boolean debug) {
@@ -106,6 +120,14 @@ public String getId() {
106120
return id;
107121
}
108122

123+
public float getAlpha() {
124+
return alpha;
125+
}
126+
127+
public void setAlpha(float alpha) {
128+
this.alpha = Math.max(0f, Math.min(1f, alpha));
129+
}
130+
109131
public void setVisible(boolean visible) {
110132
this.visible = visible;
111133
}
@@ -122,6 +144,10 @@ public float getScale() {
122144
return scale;
123145
}
124146

147+
public float getInitialScale() {
148+
return initialScale;
149+
}
150+
125151
public void setX(int posX) {
126152
this.x = posX;
127153
}
@@ -161,8 +187,6 @@ public ElementAnchor getElementAnchor() {
161187
return elementAnchor;
162188
}
163189

164-
//* Helpers
165-
166190
public void offHandOffset() {
167191
this.setX(this.getInitialX() + 29);
168192
}
@@ -190,6 +214,9 @@ public void update() {
190214
updateSize();
191215
}
192216

217+
public boolean isHovered() {
218+
return isHovered;
219+
}
193220

194221
public static abstract class Builder<T extends Builder<T>> {
195222
protected final String id;
@@ -244,8 +271,8 @@ protected int getRenderX(int screenWidth) {
244271

245272
int adjustmentX = switch (elementAnchor) {
246273
case TOP_LEFT, CENTER_LEFT, BOTTOM_LEFT -> 0;
247-
case TOP_CENTER, CENTER, BOTTOM_CENTER -> (int) (width * scale / 2f);
248-
case TOP_RIGHT, CENTER_RIGHT, BOTTOM_RIGHT -> (int) (width * scale);
274+
case TOP_CENTER, CENTER, BOTTOM_CENTER -> (int) (width * initialScale / 2f);
275+
case TOP_RIGHT, CENTER_RIGHT, BOTTOM_RIGHT -> (int) (width * initialScale);
249276
};
250277

251278
return anchorX - adjustmentX;
@@ -260,8 +287,8 @@ protected int getRenderY(int screenHeight) {
260287

261288
int adjustmentY = switch (elementAnchor) {
262289
case TOP_LEFT, TOP_CENTER, TOP_RIGHT -> 0;
263-
case CENTER_LEFT, CENTER, CENTER_RIGHT -> (int) (height * scale / 2f);
264-
case BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT -> (int) (height * scale);
290+
case CENTER_LEFT, CENTER, CENTER_RIGHT -> (int) (height * initialScale / 2f);
291+
case BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT -> (int) (height * initialScale);
265292
};
266293

267294
return anchorY - adjustmentY;

src/main/java/net/raccoon/will/structura/api/gui/element/BasicElement.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.minecraft.client.gui.GuiGraphicsExtractor;
44
import net.minecraft.client.renderer.RenderPipelines;
55
import net.minecraft.resources.Identifier;
6+
import net.minecraft.util.ARGB;
67
import net.raccoon.will.structura.api.gui.layout.Anchor;
78

89
/**
@@ -15,22 +16,14 @@ public class BasicElement extends AbstractElement {
1516
protected final int texWidth, texHeight;
1617

1718
public BasicElement(Builder builder) {
18-
super(builder.id, builder.width, builder.height, builder.anchor, builder.x, builder.y);
19+
super(builder.id, builder.width, builder.height, builder.anchor, builder.x, builder.y, builder.scale);
1920
this.texture = builder.texture;
2021
this.originalTexture = builder.texture;
2122
this.texWidth = builder.texWidth;
2223
this.texHeight = builder.texHeight;
2324
this.elementAnchor = builder.elementAnchor;
2425
}
2526

26-
//fallback if no texture is provided
27-
public BasicElement(String id, int width, int height, int texWidth, int texHeight, Anchor anchor, int offsetX, int offsetY) {
28-
super(id, width, height, anchor, offsetX, offsetY);
29-
this.texture = null;
30-
this.originalTexture = null;
31-
this.texWidth = texWidth;
32-
this.texHeight = texHeight;
33-
}
3427

3528
public void resetTexture() {
3629
this.texture = originalTexture;
@@ -45,7 +38,9 @@ public void setTexture(Identifier texture) {
4538
@Override
4639
protected void draw(GuiGraphicsExtractor graphics) {
4740
if (texture == null) return;
48-
graphics.blit(RenderPipelines.GUI_TEXTURED, texture, 0, 0, 0, 0, width, height, texWidth, texHeight);
41+
int color = ARGB.color(255,255,255);
42+
int alphaColor = ARGB.multiplyAlpha(color, alpha);
43+
graphics.blit(RenderPipelines.GUI_TEXTURED, texture, 0, 0, 0, 0, width, height, texWidth, texHeight, alphaColor);
4944
}
5045

5146
public static Builder builder(String id) {
@@ -54,8 +49,8 @@ public static Builder builder(String id) {
5449

5550
public static class Builder extends AbstractElement.Builder<Builder> {
5651
private Identifier texture;
57-
private int texWidth = 128;
58-
private int texHeight = 128;
52+
private int texWidth = 64;
53+
private int texHeight = 64;
5954

6055
public Builder(String id) {
6156
super(id);

src/main/java/net/raccoon/will/structura/api/gui/element/ItemElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ItemElement extends AbstractElement {
1515

1616

1717
public ItemElement(Builder builder) {
18-
super(builder.id, builder.width, builder.height, builder.anchor, builder.x, builder.y);
18+
super(builder.id, builder.width, builder.height, builder.anchor, builder.x, builder.y, builder.scale);
1919
this.item = builder.item;
2020
this.originalItem = builder.item;
2121
this.elementAnchor = builder.elementAnchor;

src/main/java/net/raccoon/will/structura/api/gui/element/ProgressElement.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ProgressElement extends AbstractElement {
1818
private float progress;
1919

2020
public ProgressElement(Builder builder) {
21-
super(builder.id, builder.width, builder.height, builder.anchor, builder.x, builder.y);
21+
super(builder.id, builder.width, builder.height, builder.anchor, builder.x, builder.y, builder.scale);
2222
this.texture = builder.texture;
2323
this.texWidth = builder.texWidth;
2424
this.texHeight = builder.texHeight;
@@ -29,34 +29,6 @@ public ProgressElement(Builder builder) {
2929
this.elementAnchor = builder.elementAnchor;
3030
}
3131

32-
/** <P>
33-
* Creates a progress bar GUI element rendered from a single texture.
34-
*</P>
35-
*
36-
* <p>
37-
* Texture coordinates ({@code emptyX}, {@code emptyY}, {@code fullX}, {@code fullY})
38-
* refer to the <b>top-left corner</b> of each bar region within the texture.
39-
* </p>
40-
*
41-
* @param texture the texture containing both bars (empty and full)
42-
* @param texWidth the total width of the texture
43-
* @param texHeight the total height of the texture
44-
* @param barWidth the width of the progress bar
45-
* @param barHeight the height of the progress bar
46-
*/
47-
48-
public ProgressElement(String id, Identifier texture, int texWidth, int texHeight, int barWidth, int barHeight,
49-
int emptyX, int emptyY, int fullX, int fullY, Anchor anchor, int offsetX, int offsetY) {
50-
super(id, barWidth, barHeight, anchor, offsetX, offsetY);
51-
this.texture = texture;
52-
this.texWidth = texWidth;
53-
this.texHeight = texHeight;
54-
this.emptyX = emptyX;
55-
this.emptyY = emptyY;
56-
this.fullX = fullX;
57-
this.fullY = fullY;
58-
}
59-
6032
public void setProgress(float progress) {
6133
this.progress = Math.min(1.0F, Math.max(0.0F, progress));
6234
}

0 commit comments

Comments
 (0)