Skip to content

Commit a825c97

Browse files
hohserg1brachy84
andauthored
Workable ray tracing for SchemaWidget (#135)
* fix npe * workable ray tracing for SchemaWidget * added testing case of SchemaWidget to TestTile * fix TileEntity#world corrupting by SchemaWidget its need to copy tile before adding to fake world, othsewise TileEntity#world of real tile will become fake world * removed redundant imports * make SchemaRenderer#rayTrace protected, so user can override it if have better impl * fix merge * many improvements * better highlight renderer --------- Co-authored-by: brachy84 <[email protected]>
1 parent 90f4265 commit a825c97

File tree

14 files changed

+993
-377
lines changed

14 files changed

+993
-377
lines changed

src/main/java/com/cleanroommc/modularui/factory/GuiData.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import net.minecraft.entity.player.EntityPlayer;
66
import net.minecraft.item.ItemStack;
7+
import net.minecraft.world.World;
78

89
import java.util.Objects;
910

@@ -27,6 +28,10 @@ public EntityPlayer getPlayer() {
2728
return this.player;
2829
}
2930

31+
public World getWorld() {
32+
return this.player.getEntityWorld();
33+
}
34+
3035
public boolean isClient() {
3136
return NetworkUtils.isClient(this.player);
3237
}

src/main/java/com/cleanroommc/modularui/factory/PosGuiData.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import net.minecraft.entity.player.EntityPlayer;
55
import net.minecraft.tileentity.TileEntity;
66
import net.minecraft.util.math.BlockPos;
7-
import net.minecraft.world.World;
87

98
/**
109
* See {@link GuiData} for an explanation for what this is for.
@@ -22,10 +21,6 @@ public PosGuiData(EntityPlayer player, int x, int y, int z) {
2221
this.z = z;
2322
}
2423

25-
public World getWorld() {
26-
return getPlayer().world;
27-
}
28-
2924
public int getX() {
3025
return this.x;
3126
}

src/main/java/com/cleanroommc/modularui/test/TestTile.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import com.cleanroommc.modularui.api.IGuiHolder;
44
import com.cleanroommc.modularui.api.IPanelHandler;
55
import com.cleanroommc.modularui.api.drawable.IKey;
6+
import com.cleanroommc.modularui.api.widget.IWidget;
67
import com.cleanroommc.modularui.drawable.Circle;
78
import com.cleanroommc.modularui.drawable.GuiTextures;
89
import com.cleanroommc.modularui.drawable.ItemDrawable;
910
import com.cleanroommc.modularui.drawable.Rectangle;
1011
import com.cleanroommc.modularui.drawable.text.AnimatedText;
12+
import com.cleanroommc.modularui.factory.GuiData;
1113
import com.cleanroommc.modularui.factory.PosGuiData;
1214
import com.cleanroommc.modularui.network.NetworkUtils;
1315
import com.cleanroommc.modularui.screen.ModularPanel;
@@ -18,6 +20,9 @@
1820
import com.cleanroommc.modularui.utils.Alignment;
1921
import com.cleanroommc.modularui.utils.Color;
2022
import com.cleanroommc.modularui.utils.Interpolation;
23+
import com.cleanroommc.modularui.utils.fakeworld.ArraySchema;
24+
import com.cleanroommc.modularui.utils.fakeworld.BlockHighlight;
25+
import com.cleanroommc.modularui.utils.fakeworld.SchemaRenderer;
2126
import com.cleanroommc.modularui.value.BoolValue;
2227
import com.cleanroommc.modularui.value.IntValue;
2328
import com.cleanroommc.modularui.value.StringValue;
@@ -40,6 +45,7 @@
4045
import com.cleanroommc.modularui.widgets.PageButton;
4146
import com.cleanroommc.modularui.widgets.PagedWidget;
4247
import com.cleanroommc.modularui.widgets.ProgressWidget;
48+
import com.cleanroommc.modularui.widgets.SchemaWidget;
4349
import com.cleanroommc.modularui.widgets.ScrollingTextWidget;
4450
import com.cleanroommc.modularui.widgets.SliderWidget;
4551
import com.cleanroommc.modularui.widgets.SlotGroupWidget;
@@ -174,7 +180,10 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
174180
.tab(GuiTextures.TAB_TOP, 0))
175181
.child(new PageButton(3, tabController)
176182
.tab(GuiTextures.TAB_TOP, 0)
177-
.overlay(new ItemDrawable(Blocks.CHEST).asIcon())))
183+
.overlay(new ItemDrawable(Blocks.CHEST).asIcon()))
184+
.child(new PageButton(4, tabController)
185+
.tab(GuiTextures.TAB_TOP, 0)
186+
.overlay(new ItemDrawable(Items.ENDER_EYE).asIcon())))
178187
.child(new Expandable()
179188
.debugName("expandable")
180189
.top(0)
@@ -438,7 +447,8 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
438447
.child(new DynamicSyncedWidget<>()
439448
.widthRel(1f)
440449
.syncHandler(dynamicSyncHandler)))
441-
)))
450+
)
451+
.addPage(createSchemaPage(guiData))))
442452
.child(SlotGroupWidget.playerInventory(false))
443453
);
444454
/*panel.child(new ButtonWidget<>()
@@ -454,6 +464,19 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
454464
return panel;
455465
}
456466

467+
private IWidget createSchemaPage(GuiData data) {
468+
ParentWidget<?> page = new ParentWidget<>();
469+
page.debugName("page 5 schema");
470+
page.sizeRel(1f);
471+
page.child(IKey.str("schema").asWidget());
472+
if (world.isRemote)
473+
page.child(new SchemaWidget(new SchemaRenderer(ArraySchema.of(data.getPlayer(), 5))
474+
.highlightRenderer(new BlockHighlight(Color.withAlpha(Color.GREEN.brighter(1), 0.9f), 1/32f)))
475+
.pos(20, 20)
476+
.size(100, 100));
477+
return page;
478+
}
479+
457480
public ModularPanel openSecondWindow(PanelSyncManager syncManager, IPanelHandler syncHandler) {
458481
ModularPanel panel = new Dialog<>("second_window", null)
459482
.setDisablePanelsBelow(false)

src/main/java/com/cleanroommc/modularui/utils/MathUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.cleanroommc.modularui.utils;
22

3+
import net.minecraft.util.math.MathHelper;
4+
35
import org.mariuszgromada.math.mxparser.Constant;
46
import org.mariuszgromada.math.mxparser.Expression;
57

68
public class MathUtils {
79

10+
public static final float PI = (float) Math.PI;
11+
public static final float PI2 = 2f * PI;
12+
public static final float PI_HALF = PI / 2f;
13+
public static final float PI_QUART = PI / 4f;
14+
815
// SI prefixes
916
public static final Constant k = new Constant("k", 1e3);
1017
public static final Constant M = new Constant("M", 1e6);
@@ -154,4 +161,16 @@ public static int wrapDegrees(int angle) {
154161
if (angle < -180) angle += 360;
155162
return angle;
156163
}
164+
165+
public static float sin(float v) {
166+
return MathHelper.sin(v);
167+
}
168+
169+
public static float cos(float v) {
170+
return MathHelper.cos(v);
171+
}
172+
173+
public static float tan(float v) {
174+
return MathHelper.sin(v) / MathHelper.cos(v);
175+
}
157176
}

src/main/java/com/cleanroommc/modularui/utils/Vector3f.java

Lines changed: 108 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
package com.cleanroommc.modularui.utils;
22

3+
import net.minecraft.util.math.MathHelper;
4+
import net.minecraft.util.math.Vec3d;
5+
36
import java.nio.FloatBuffer;
7+
import java.util.Objects;
48

59
public class Vector3f {
610

11+
public static final Vector3f UNIT_X = new Vector3f(1.0f, 0.0f, 0.0f);
12+
public static final Vector3f UNIT_Y = new Vector3f(0.0f, 1.0f, 0.0f);
13+
public static final Vector3f UNIT_Z = new Vector3f(0.0f, 0.0f, 1.0f);
14+
15+
public static void resetUnitVectors() {
16+
UNIT_X.set(1.0f, 0.0f, 0.0f);
17+
UNIT_Y.set(0.0f, 1.0f, 0.0f);
18+
UNIT_Z.set(0.0f, 0.0f, 1.0f);
19+
}
20+
721
public float x, y, z;
822

923
/**
@@ -20,6 +34,10 @@ public Vector3f(Vector3f src) {
2034
set(src);
2135
}
2236

37+
public Vector3f(Vec3d src) {
38+
set(src);
39+
}
40+
2341
/**
2442
* Constructor
2543
*/
@@ -44,6 +62,12 @@ public void set(float x, float y, float z) {
4462
this.z = z;
4563
}
4664

65+
public void set(double x, double y, double z) {
66+
this.x = (float) x;
67+
this.y = (float) y;
68+
this.z = (float) z;
69+
}
70+
4771
/**
4872
* Load from another Vec3f
4973
*
@@ -57,6 +81,11 @@ public Vector3f set(Vector3f src) {
5781
return this;
5882
}
5983

84+
public Vector3f set(Vec3d src) {
85+
set(src.x, src.y, src.z);
86+
return this;
87+
}
88+
6089
/**
6190
* @return the length squared of the vector
6291
*/
@@ -82,6 +111,10 @@ public Vector3f translate(float x, float y, float z) {
82111
return this;
83112
}
84113

114+
public Vector3f translate(double x, double y, double z) {
115+
return translate((float) x, (float) y, (float) z);
116+
}
117+
85118
/**
86119
* Add a vector to another vector and place the result in a destination
87120
* vector.
@@ -100,6 +133,20 @@ public static Vector3f add(Vector3f left, Vector3f right, Vector3f dest) {
100133
}
101134
}
102135

136+
public static Vector3f add(Vector3f a, Vector3f b, Vector3f c, Vector3f dest) {
137+
if (dest == null) dest = new Vector3f();
138+
dest.set(a.x + b.x + c.x, a.y + b.y + c.y, a.z + b.z + c.z);
139+
return dest;
140+
}
141+
142+
public Vector3f add(Vector3f v) {
143+
return add(v, this, this);
144+
}
145+
146+
public Vector3f add(Vector3f v, Vector3f dest) {
147+
return add(this, v, dest);
148+
}
149+
103150
/**
104151
* Subtract a vector from another vector and place the result in a destination
105152
* vector.
@@ -134,7 +181,6 @@ public static Vector3f cross(Vector3f left, Vector3f right, Vector3f dest) {
134181
return dest;
135182
}
136183

137-
138184
/**
139185
* Negate a vector
140186
*
@@ -154,14 +200,16 @@ public Vector3f negate() {
154200
* @return the negated vector
155201
*/
156202
public Vector3f negate(Vector3f dest) {
157-
if (dest == null)
158-
dest = new Vector3f();
203+
if (dest == null) dest = new Vector3f();
159204
dest.x = -x;
160205
dest.y = -y;
161206
dest.z = -z;
162207
return dest;
163208
}
164209

210+
public Vector3f normalise() {
211+
return normalise(this);
212+
}
165213

166214
/**
167215
* Normalise this vector and place the result in another vector.
@@ -170,13 +218,11 @@ public Vector3f negate(Vector3f dest) {
170218
* @return the normalised vector
171219
*/
172220
public Vector3f normalise(Vector3f dest) {
173-
float l = length();
174-
175-
if (dest == null)
176-
dest = new Vector3f(x / l, y / l, z / l);
177-
else
178-
dest.set(x / l, y / l, z / l);
179-
221+
if (dest == null) dest = new Vector3f();
222+
float lsq = lengthSquared();
223+
if (lsq == 1) return dest.set(this);
224+
float f = (float) MathHelper.fastInvSqrt(lsq);
225+
dest.set(x * f, y * f, z * f);
180226
return dest;
181227
}
182228

@@ -222,42 +268,68 @@ public Vector3f load(FloatBuffer buf) {
222268
* @see org.lwjgl.vector.Vec#scale(float)
223269
*/
224270
public Vector3f scale(float scale) {
225-
226271
x *= scale;
227272
y *= scale;
228273
z *= scale;
229-
230274
return this;
231-
232275
}
233276

234277
/* (non-Javadoc)
235278
* @see org.lwjgl.vector.Vec#store(FloatBuffer)
236279
*/
237280
public Vector3f store(FloatBuffer buf) {
238-
239281
buf.put(x);
240282
buf.put(y);
241283
buf.put(z);
284+
return this;
285+
}
286+
287+
public float distanceTo(Vector3f vec) {
288+
return MathHelper.sqrt(squareDistanceTo(vec));
289+
}
290+
291+
public float squareDistanceTo(Vector3f v) {
292+
return squareDistanceTo(v.x, v.y, v.z);
293+
}
294+
295+
public float squareDistanceTo(float xIn, float yIn, float zIn) {
296+
float d0 = xIn - this.x;
297+
float d1 = yIn - this.y;
298+
float d2 = zIn - this.z;
299+
return d0 * d0 + d1 * d1 + d2 * d2;
300+
}
301+
302+
public Vector3f rotatePitch(float pitch) {
303+
float cos = MathHelper.cos(pitch);
304+
float sin = MathHelper.sin(pitch);
305+
float x = this.x;
306+
float y = this.y * cos + this.z * sin;
307+
float z = this.z * cos - this.y * sin;
308+
set(x, y, z);
309+
return this;
310+
}
242311

312+
public Vector3f rotateYaw(float yaw) {
313+
float sin = MathHelper.cos(yaw);
314+
float cos = MathHelper.sin(yaw);
315+
float x = this.x * sin + this.z * cos;
316+
float y = this.y;
317+
float z = this.z * sin - this.x * cos;
318+
set(x, y, z);
243319
return this;
244320
}
245321

322+
public Vector3f copy() {
323+
return new Vector3f(x, y, z);
324+
}
325+
326+
public Vec3d toVec3d() {
327+
return new Vec3d(x, y, z);
328+
}
329+
246330
/* (non-Javadoc)
247331
* @see java.lang.Object#toString()
248332
*/
249-
public String toString() {
250-
StringBuilder sb = new StringBuilder(64);
251-
252-
sb.append("Vec3f[");
253-
sb.append(x);
254-
sb.append(", ");
255-
sb.append(y);
256-
sb.append(", ");
257-
sb.append(z);
258-
sb.append(']');
259-
return sb.toString();
260-
}
261333

262334
/**
263335
* @return x
@@ -307,15 +379,21 @@ public float getZ() {
307379
return z;
308380
}
309381

382+
public String toString() {
383+
return "Vec3f[" + x + ", " + y + ", " + z + ']';
384+
}
385+
386+
@Override
310387
public boolean equals(Object obj) {
311388
if (this == obj) return true;
312389
if (obj == null) return false;
313390
if (getClass() != obj.getClass()) return false;
314391
Vector3f other = (Vector3f) obj;
315-
316-
if (x == other.x && y == other.y && z == other.z) return true;
317-
318-
return false;
392+
return x == other.x && y == other.y && z == other.z;
319393
}
320394

395+
@Override
396+
public int hashCode() {
397+
return Objects.hash(x, y, z);
398+
}
321399
}

0 commit comments

Comments
 (0)