Skip to content

Commit

Permalink
added checkbutton for landing bb rendering in gui and hover color for…
Browse files Browse the repository at this point in the history
… the bounding boxes in the world
  • Loading branch information
kurrycat committed Oct 14, 2022
1 parent a63afbd commit 993adb3
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ public static void init(String mcVersion) {
new EventAPI.EventListener<OnRenderWorldOverlayEvent>(
e -> {
LandingBlockGuiScreen.lbs.forEach(lb -> {
if(lb.boundingBox != null)
if(lb.shouldRender && lb.boundingBox != null)
Renderer3D.drawBox(
lb.boundingBox.expand(0.005D),
new Color(255, 68, 68, 157),
lb.highlight ?
new Color(98, 255, 74, 157) :
new Color(255, 68, 68, 157),
e.partialTicks
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static void startCallbackThread() {
Thread t = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
try {
if (core.isOpen()) core.runCallbacks();
if (core != null && core.isOpen()) core.runCallbacks();
Thread.sleep(100);
} catch (InterruptedException e) {
core.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.awt.*;

public class CheckButton extends Component implements MouseInputListener {
private final CheckButtonCallback checkButtonCallback;
public CheckButtonCallback checkButtonCallback;
public Color checkedColor = new Color(255, 255, 255, 95);
public Color normalColor = new Color(31, 31, 31, 150);
private boolean isChecked = false;
Expand Down Expand Up @@ -40,7 +40,7 @@ public void render(Vector2D mouse) {
if (isChecked)
FontRenderer.drawCenteredString(
"x",
getDisplayPos().add(getSize().div(2)).add(new Vector2D(0.5, 0)),
getDisplayPos().add(getSize().div(2)).add(new Vector2D(0.3, 0)),
Color.WHITE,
false
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.kurrycat.mpkmod.gui.components;

import io.github.kurrycat.mpkmod.compatability.MCClasses.Renderer2D;
import io.github.kurrycat.mpkmod.util.BoundingBox2D;
import io.github.kurrycat.mpkmod.util.MathUtil;
import io.github.kurrycat.mpkmod.util.Mouse;
import io.github.kurrycat.mpkmod.util.Vector2D;
import io.github.kurrycat.mpkmod.util.*;

import java.awt.*;
import java.util.ArrayList;
Expand Down Expand Up @@ -33,7 +30,7 @@ public int getItemCount() {
}

public I getItem(int index) {
return null;
return getItems().get(index);
}

public void render(Vector2D mouse) {
Expand Down Expand Up @@ -69,6 +66,24 @@ public void render(Vector2D mouse) {
);
}

public Pair<I, Vector2D> getItemAndRelMousePosUnderMouse(Vector2D mouse) {
double itemWidth = getSize().getX() - 2;
if (shouldRenderScrollbar()) itemWidth -= scrollBar.barWidth - 1;
if (mouse.getX() < getDisplayPos().getX() + 1 || mouse.getX() > getDisplayPos().getX() + itemWidth + 1)
return null;

double currY = mouse.getY() - 1 - getDisplayPos().getY() + scrollBar.scrollAmount;
for (int i = 0; i < getItemCount(); i++) {
I item = getItem(i);
if (item == null) item = getItems().get(i);
if (currY >= 0 && currY <= item.getHeight()) {
return new Pair<>(item, new Vector2D(mouse.getX() - getDisplayPos().getX() - 1, currY));
}
currY -= item.getHeight() + 1;
}
return null;
}

public void drawTopCover(Vector2D pos, Vector2D size) {
Renderer2D.drawRect(pos, size, Color.DARK_GRAY);
}
Expand All @@ -84,10 +99,25 @@ private boolean shouldRenderScrollbar() {
public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Button button) {
if (shouldRenderScrollbar())
scrollBar.handleMouseInput(state, mousePos, button);

Pair<I, Vector2D> p = getItemAndRelMousePosUnderMouse(mousePos);
if (p != null) {
I item = p.first;
Vector2D relMousePos = p.second;
return item.handleMouseInput(state, mousePos, button);
}

return contains(mousePos);
}

public boolean handleMouseScroll(Vector2D mousePos, int delta) {
Pair<I, Vector2D> p = getItemAndRelMousePosUnderMouse(mousePos);
if (p != null) {
I item = p.first;
Vector2D relMousePos = p.second;
return item.handleMouseScroll(mousePos, delta);
}

if (shouldRenderScrollbar())
scrollBar.scrollBy(-delta);
return contains(mousePos);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.github.kurrycat.mpkmod.gui.components;

import io.github.kurrycat.mpkmod.util.Mouse;
import io.github.kurrycat.mpkmod.util.Vector2D;

public abstract class ScrollableListItem<I extends ScrollableListItem<I>> {
public abstract class ScrollableListItem<I extends ScrollableListItem<I>> implements MouseInputListener, MouseScrollListener {
protected int height;
private ScrollableList<I> parent;

Expand All @@ -17,4 +18,12 @@ public int getHeight() {


public abstract void render(int index, Vector2D pos, Vector2D size, Vector2D mouse);

public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Button button) {
return false;
}

public boolean handleMouseScroll(Vector2D mousePos, int delta) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
import io.github.kurrycat.mpkmod.compatability.MCClasses.WorldInteraction;
import io.github.kurrycat.mpkmod.gui.ComponentScreen;
import io.github.kurrycat.mpkmod.gui.components.Button;
import io.github.kurrycat.mpkmod.gui.components.NumberSlider;
import io.github.kurrycat.mpkmod.gui.components.ScrollableList;
import io.github.kurrycat.mpkmod.gui.components.ScrollableListItem;
import io.github.kurrycat.mpkmod.gui.components.*;
import io.github.kurrycat.mpkmod.landingblock.LandingBlock;
import io.github.kurrycat.mpkmod.util.Colors;
import io.github.kurrycat.mpkmod.util.Vector2D;
import io.github.kurrycat.mpkmod.util.Vector3D;
import io.github.kurrycat.mpkmod.util.*;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class LandingBlockGuiScreen extends ComponentScreen {
public static List<LandingBlock> lbs = new ArrayList<>();
public static Color lbListColorItemEdge = new Color(255, 255, 255, 95);
public static Color lbListColorBg = new Color(31, 31, 31, 150);
public static Color hoverColor = new Color(70, 70, 70, 150);

private LBList lbList;

@Override
public boolean shouldCreateKeyBind() {
return true;
Expand All @@ -33,6 +32,10 @@ public void onGuiInit() {
super.onGuiInit();

Vector2D windowSize = Renderer2D.getScaledSize();
lbList = new LBList(
new Vector2D(windowSize.getX() / 4D, 20),
new Vector2D(windowSize.getX() / 2D, windowSize.getY() - 40)
);

components.add(
new Button(
Expand All @@ -41,6 +44,7 @@ public void onGuiInit() {
new Vector2D(50, 20),
mouseButton -> {
lbs = LandingBlock.asLandingBlocks(WorldInteraction.getCollisionBoundingBoxes(new Vector3D(0, 10, 0)));
lbList.updateList();
}
)
);
Expand All @@ -54,33 +58,41 @@ public void onGuiInit() {
)
);

components.add(
new LBList(
new Vector2D(windowSize.getX() / 4D, 20),
new Vector2D(windowSize.getX() / 2D, windowSize.getY() - 40)
)
);
components.add(lbList);
}

public void drawScreen(Vector2D mouse, float partialTicks) {
super.drawScreen(mouse, partialTicks);
}

@Override
public void onGuiClosed() {
super.onGuiClosed();
for (int i = 0; i < lbList.getItemCount(); i++) {
lbList.getItem(i).landingBlock.highlight = false;
}
}

public static class LBList extends ScrollableList<LBListItem> {
public LBListItem itemInstance;

public LBList(Vector2D pos, Vector2D size) {
super(pos, size);
itemInstance = new LBListItem(this);
updateList();
}

@Override
public LBListItem getItem(int index) {
return itemInstance;
public void updateList() {
items = lbs.stream().map(lb -> new LBListItem(this, lb)).collect(Collectors.toCollection(ArrayList<LBListItem>::new));
}

@Override
public int getItemCount() {
return lbs.size();
public void render(Vector2D mouse) {
super.render(mouse);
for (int i = 0; i < getItemCount(); i++) {
getItem(i).landingBlock.highlight = false;
}
Pair<LBListItem, Vector2D> p = getItemAndRelMousePosUnderMouse(mouse);
if (p != null)
p.first.landingBlock.highlight = true;
}

@Override
Expand All @@ -92,28 +104,69 @@ public void drawTopCover(Vector2D pos, Vector2D size) {
@Override
public void drawBottomCover(Vector2D pos, Vector2D size) {
super.drawBottomCover(pos, size);

}
}

public static class LBListItem extends ScrollableListItem<LBListItem> {
public LBListItem(ScrollableList<LBListItem> parent) {
public CheckButton shouldRender;
public LandingBlock landingBlock;

public LBListItem(ScrollableList<LBListItem> parent, LandingBlock landingBlock) {
super(parent);
this.landingBlock = landingBlock;
shouldRender = new CheckButton(Vector2D.ZERO, checked -> {
landingBlock.shouldRender = checked;
});
shouldRender.setChecked(landingBlock.shouldRender);
}

public void render(int index, Vector2D pos, Vector2D size, Vector2D mouse) {
if(index > lbs.size()) return;
LandingBlock lb = lbs.get(index);
if(lb == null) return;
shouldRender.pos = pos.add(size.getX() / 5 - 11, size.getY() / 2 - 5.5);

Renderer2D.drawRectWithEdge(pos, size, 1, lbListColorBg, lbListColorItemEdge);
FontRenderer.drawCenteredString("minX: " + lb.boundingBox.getMin().getX(), pos.add(size.getX() / 4, size.getY() / 4), Color.WHITE, false);
FontRenderer.drawCenteredString("minY: " + lb.boundingBox.getMin().getY(), pos.add(size.getX() / 4, size.getY() / 2), Color.WHITE, false);
FontRenderer.drawCenteredString("minZ: " + lb.boundingBox.getMin().getZ(), pos.add(size.getX() / 4, size.getY() / 4 * 3), Color.WHITE, false);
FontRenderer.drawCenteredString(
"minX: " + landingBlock.boundingBox.getMin().getX(),
pos.add(size.getX() / 5 * 2,
size.getY() / 4),
Color.WHITE, false
);
FontRenderer.drawCenteredString(
"minY: " + landingBlock.boundingBox.getMin().getY(),
pos.add(size.getX() / 5 * 2,
size.getY() / 2),
Color.WHITE, false
);
FontRenderer.drawCenteredString(
"minZ: " + landingBlock.boundingBox.getMin().getZ(),
pos.add(size.getX() / 5 * 2,
size.getY() / 4 * 3),
Color.WHITE, false
);

FontRenderer.drawCenteredString(
"maxX: " + landingBlock.boundingBox.getMax().getX(),
pos.add(size.getX() / 5 * 4,
size.getY() / 4),
Color.WHITE, false
);
FontRenderer.drawCenteredString(
"maxY: " + landingBlock.boundingBox.getMax().getY(),
pos.add(size.getX() / 5 * 4,
size.getY() / 2),
Color.WHITE, false
);
FontRenderer.drawCenteredString(
"maxZ: " + landingBlock.boundingBox.getMax().getZ(),
pos.add(size.getX() / 5 * 4,
size.getY() / 4 * 3),
Color.WHITE, false
);

shouldRender.render(mouse);
}

FontRenderer.drawCenteredString("maxX: " + lb.boundingBox.getMax().getX(), pos.add(size.getX() / 4 * 3, size.getY() / 4), Color.WHITE, false);
FontRenderer.drawCenteredString("maxY: " + lb.boundingBox.getMax().getY(), pos.add(size.getX() / 4 * 3, size.getY() / 2), Color.WHITE, false);
FontRenderer.drawCenteredString("maxZ: " + lb.boundingBox.getMax().getZ(), pos.add(size.getX() / 4 * 3, size.getY() / 4 * 3), Color.WHITE, false);
public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Button button) {
return shouldRender.handleMouseInput(state, mousePos, button);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
public class LandingBlock {
public LandingMode landingMode = LandingMode.LAND;
public BoundingBox3D boundingBox;
public boolean shouldRender = true;
public boolean highlight = false;

public LandingBlock(BoundingBox3D boundingBox) {
this.boundingBox = boundingBox;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/github/kurrycat/mpkmod/util/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.kurrycat.mpkmod.util;

public class Pair<K, V> {
public K first;
public V second;

public Pair(K first, V second) {
this.first = first;
this.second = second;
}
}

0 comments on commit 993adb3

Please sign in to comment.