Skip to content

Commit 95564af

Browse files
committed
Merge branch 'feat/guide' into 1.16.x
2 parents f300af1 + 8c6600b commit 95564af

File tree

9 files changed

+345
-8
lines changed

9 files changed

+345
-8
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.robotgryphon.compactmachines.client.gui;
2+
3+
import com.mojang.blaze3d.matrix.MatrixStack;
4+
import com.mojang.blaze3d.systems.RenderSystem;
5+
import com.robotgryphon.compactmachines.CompactMachines;
6+
import com.robotgryphon.compactmachines.client.gui.guide.GuideSection;
7+
import net.minecraft.client.Minecraft;
8+
import net.minecraft.client.gui.screen.Screen;
9+
import net.minecraft.util.ResourceLocation;
10+
import net.minecraft.util.text.TranslationTextComponent;
11+
12+
import javax.annotation.Nullable;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
16+
public class PersonalShrinkingDeviceScreen extends Screen {
17+
private final ResourceLocation GUI = new ResourceLocation(CompactMachines.MOD_ID, "textures/gui/psd_screen.png");
18+
private static final int WIDTH = 256;
19+
private static final int HEIGHT = 201;
20+
21+
private final Map<ResourceLocation, GuideSection> sections;
22+
private final ResourceLocation emptySection = new ResourceLocation(CompactMachines.MOD_ID, "empty");
23+
24+
@Nullable
25+
private GuideSection currentSection;
26+
27+
protected PersonalShrinkingDeviceScreen() {
28+
super(new TranslationTextComponent(CompactMachines.MOD_ID + ".gui.psd.title"));
29+
this.sections = new HashMap<>();
30+
31+
GuideSection root = new GuideSection();
32+
sections.put(new ResourceLocation(CompactMachines.MOD_ID, "root"), root);
33+
this.currentSection = root;
34+
}
35+
36+
@Override
37+
public void mouseMoved(double mouseX, double mouseY) {
38+
if(currentSection != null)
39+
currentSection.mouseMoved(mouseX, mouseY);
40+
}
41+
42+
@Override
43+
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
44+
int relX = (this.width - WIDTH) / 2;
45+
46+
// relY = relative position, places screen against bottom edge of screen
47+
int relY = (this.height - HEIGHT);
48+
49+
if(currentSection != null)
50+
return currentSection.mouseScrolled(mouseX - relX - 15, mouseY - relY - 14, delta);
51+
52+
return false;
53+
}
54+
55+
@Override
56+
public boolean mouseClicked(double mouseX, double mouseY, int button) {
57+
super.mouseClicked(mouseX, mouseY, button);
58+
59+
int relX = (this.width - WIDTH) / 2;
60+
61+
// relY = relative position, places screen against bottom edge of screen
62+
int relY = (this.height - HEIGHT);
63+
64+
if(currentSection != null)
65+
return currentSection.mouseClicked(mouseX - relX - 15, mouseY - relY - 14, button);
66+
67+
return false;
68+
}
69+
70+
@Override
71+
public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
72+
this.renderBackground(matrixStack);
73+
74+
RenderSystem.color4f(1, 1, 1, 1);
75+
int relX = (this.width - WIDTH) / 2;
76+
77+
// relY = relative position, places screen against bottom edge of screen
78+
int relY = (this.height - HEIGHT);
79+
80+
matrixStack.push();
81+
matrixStack.translate(relX, relY, 0);
82+
83+
this.minecraft.getTextureManager().bindTexture(GUI);
84+
this.blit(matrixStack, 0, 0, 0, 0, WIDTH, HEIGHT);
85+
matrixStack.pop();
86+
87+
matrixStack.push();
88+
matrixStack.translate(relX + 15, relY + 14, 10);
89+
90+
if(currentSection != null) {
91+
currentSection.render(matrixStack, mouseX - relX - 15, mouseY - relY - 14, partialTicks);
92+
}
93+
94+
matrixStack.pop();
95+
96+
super.render(matrixStack, mouseX, mouseY, partialTicks);
97+
}
98+
99+
@Override
100+
public boolean isPauseScreen() {
101+
return false;
102+
}
103+
104+
public static void show() {
105+
Minecraft.getInstance().displayGuiScreen(new PersonalShrinkingDeviceScreen());
106+
}
107+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.robotgryphon.compactmachines.client.gui.guide;
2+
3+
import com.mojang.blaze3d.matrix.MatrixStack;
4+
import com.robotgryphon.compactmachines.CompactMachines;
5+
import com.robotgryphon.compactmachines.client.gui.widget.AbstractCMGuiWidget;
6+
import com.robotgryphon.compactmachines.client.gui.widget.ScrollableWrappedTextWidget;
7+
import net.minecraft.client.Minecraft;
8+
import net.minecraft.client.gui.AbstractGui;
9+
import net.minecraft.client.gui.FontRenderer;
10+
import net.minecraft.client.gui.IGuiEventListener;
11+
import net.minecraft.client.gui.IRenderable;
12+
import net.minecraft.util.text.TextFormatting;
13+
import net.minecraft.util.text.TranslationTextComponent;
14+
import net.minecraftforge.common.util.Constants;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.Optional;
19+
20+
public class GuidePage implements IRenderable, IGuiEventListener {
21+
22+
protected List<AbstractCMGuiWidget> widgets;
23+
24+
public GuidePage() {
25+
widgets = new ArrayList<>();
26+
27+
ScrollableWrappedTextWidget sc = new ScrollableWrappedTextWidget(CompactMachines.MOD_ID + ".psd.pages.machines", 2, 18, 222, 160);
28+
widgets.add(sc);
29+
}
30+
31+
@Override
32+
public void render(MatrixStack ms, int mouseX, int mouseY, float partialTicks) {
33+
FontRenderer fr = Minecraft.getInstance().fontRenderer;
34+
AbstractGui.drawString(ms, fr,
35+
new TranslationTextComponent(CompactMachines.MOD_ID + ".psd.pages.machines.title")
36+
.mergeStyle(TextFormatting.GOLD),
37+
2, 2, 0);
38+
39+
for(IRenderable comp : widgets)
40+
comp.render(ms, mouseX, mouseY, partialTicks);
41+
}
42+
43+
public Optional<AbstractCMGuiWidget> getWidgetByPosition(double mouseX, double mouseY) {
44+
for(AbstractCMGuiWidget wid : widgets) {
45+
if(wid.isMouseOver(mouseX, mouseY))
46+
return Optional.of(wid);
47+
}
48+
49+
return Optional.empty();
50+
}
51+
52+
@Override
53+
public void mouseMoved(double mouseX, double mouseY) {
54+
getWidgetByPosition(mouseX, mouseY)
55+
.ifPresent(c -> c.mouseMoved(mouseX, mouseY));
56+
}
57+
58+
@Override
59+
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
60+
return getWidgetByPosition(mouseX, mouseY)
61+
.map(c -> c.mouseScrolled(mouseX, mouseY, delta))
62+
.orElse(false);
63+
}
64+
65+
@Override
66+
public boolean mouseClicked(double mouseX, double mouseY, int button) {
67+
return getWidgetByPosition(mouseX, mouseY)
68+
.map(c -> c.mouseClicked(mouseX, mouseY, button))
69+
.orElse(false);
70+
}
71+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.robotgryphon.compactmachines.client.gui.guide;
2+
3+
import com.mojang.blaze3d.matrix.MatrixStack;
4+
import net.minecraft.client.gui.IGuiEventListener;
5+
import net.minecraft.client.gui.IRenderable;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
public class GuideSection implements IRenderable, IGuiEventListener {
11+
private final List<GuidePage> pages;
12+
private int currentPageIndex = 0;
13+
private GuidePage currentPage;
14+
15+
public GuideSection() {
16+
this.pages = new ArrayList<>();
17+
this.currentPage = new GuidePage();
18+
this.pages.add(currentPage);
19+
}
20+
21+
@Override
22+
public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
23+
if(this.currentPage != null)
24+
currentPage.render(matrixStack, mouseX, mouseY, partialTicks);
25+
}
26+
27+
@Override
28+
public void mouseMoved(double mouseX, double mouseY) {
29+
if(this.currentPage != null)
30+
currentPage.mouseMoved(mouseX, mouseY);
31+
}
32+
33+
@Override
34+
public boolean mouseClicked(double mouseX, double mouseY, int button) {
35+
if(this.currentPage != null)
36+
return currentPage.mouseClicked(mouseX, mouseY, button);
37+
38+
return false;
39+
}
40+
41+
@Override
42+
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
43+
if(this.currentPage != null)
44+
return currentPage.mouseScrolled(mouseX, mouseY, delta);
45+
46+
return false;
47+
}
48+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.robotgryphon.compactmachines.client.gui.widget;
2+
3+
import com.mojang.blaze3d.matrix.MatrixStack;
4+
import net.minecraft.client.gui.IGuiEventListener;
5+
import net.minecraft.client.gui.IRenderable;
6+
7+
public class AbstractCMGuiWidget implements IRenderable, IGuiEventListener {
8+
9+
protected final int x, y, width, height;
10+
11+
protected AbstractCMGuiWidget(int x, int y, int width, int height) {
12+
this.x = x;
13+
this.y = y;
14+
this.width = width;
15+
this.height = height;
16+
}
17+
18+
@Override
19+
public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
20+
21+
}
22+
23+
@Override
24+
public boolean isMouseOver(double mouseX, double mouseY) {
25+
return mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height;
26+
}
27+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.robotgryphon.compactmachines.client.gui.widget;
2+
3+
import com.mojang.blaze3d.matrix.MatrixStack;
4+
import net.minecraft.client.Minecraft;
5+
import net.minecraft.client.gui.AbstractGui;
6+
import net.minecraft.client.gui.FontRenderer;
7+
import net.minecraft.client.resources.I18n;
8+
import net.minecraft.util.IReorderingProcessor;
9+
import net.minecraft.util.math.MathHelper;
10+
import net.minecraft.util.text.StringTextComponent;
11+
import net.minecraft.util.text.TextFormatting;
12+
import net.minecraft.util.text.TranslationTextComponent;
13+
14+
import java.util.List;
15+
16+
public class ScrollableWrappedTextWidget extends AbstractCMGuiWidget {
17+
18+
private String localeKey;
19+
private double yScroll = 0;
20+
private FontRenderer fontRenderer;
21+
22+
private int maxLinesToShow;
23+
private int lineIndexStart;
24+
private List<IReorderingProcessor> lines;
25+
private int charSize;
26+
27+
public ScrollableWrappedTextWidget(String key, int x, int y, int width, int height) {
28+
super(x, y, width, height);
29+
this.localeKey = key;
30+
this.fontRenderer = Minecraft.getInstance().fontRenderer;
31+
32+
this.recalculate();
33+
}
34+
35+
@Override
36+
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
37+
double temp = yScroll - delta;
38+
yScroll = MathHelper.clamp(temp, 0, lines.size() - maxLinesToShow - 1);
39+
recalculate();
40+
return true;
41+
}
42+
43+
private void recalculate() {
44+
String t = I18n.format(localeKey);
45+
lines = fontRenderer.trimStringToWidth(new StringTextComponent(t), width);
46+
47+
charSize = fontRenderer.getStringWidth("M");
48+
int maxOnScreen = height / (charSize + 4);
49+
maxLinesToShow = Math.min(lines.size(), maxOnScreen);
50+
51+
// startClamp - either the current line scroll, or the max allowed line
52+
int startClamp = Math.min((int) Math.floor(yScroll), lines.size());
53+
lineIndexStart = MathHelper.clamp(0, startClamp, lines.size() - 1);
54+
}
55+
56+
@Override
57+
public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
58+
matrixStack.push();
59+
matrixStack.translate(x, y, 10);
60+
61+
FontRenderer fr = Minecraft.getInstance().fontRenderer;
62+
63+
try {
64+
for (int y = lineIndexStart; y <= lineIndexStart + maxLinesToShow; y++) {
65+
IReorderingProcessor s = lines.get(y);
66+
fr.func_238407_a_(matrixStack, s, 0, (y - lineIndexStart) * (charSize + 4), 0xFFFFFF);
67+
}
68+
}
69+
70+
catch(Exception ex1) {}
71+
72+
matrixStack.pop();
73+
}
74+
}

src/main/java/com/robotgryphon/compactmachines/item/ItemPersonalShrinkingDevice.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
package com.robotgryphon.compactmachines.item;
22

33
import com.robotgryphon.compactmachines.CompactMachines;
4+
import com.robotgryphon.compactmachines.client.gui.PersonalShrinkingDeviceScreen;
45
import com.robotgryphon.compactmachines.core.Registration;
56
import com.robotgryphon.compactmachines.util.CompactMachineUtil;
67
import com.robotgryphon.compactmachines.util.PlayerUtil;
8+
import net.minecraft.client.entity.player.ClientPlayerEntity;
79
import net.minecraft.client.gui.screen.Screen;
810
import net.minecraft.client.util.ITooltipFlag;
911
import net.minecraft.entity.player.PlayerEntity;
1012
import net.minecraft.entity.player.ServerPlayerEntity;
1113
import net.minecraft.item.Item;
1214
import net.minecraft.item.ItemStack;
15+
import net.minecraft.item.ItemUseContext;
1316
import net.minecraft.util.ActionResult;
17+
import net.minecraft.util.ActionResultType;
1418
import net.minecraft.util.Hand;
1519
import net.minecraft.util.text.IFormattableTextComponent;
1620
import net.minecraft.util.text.ITextComponent;
1721
import net.minecraft.util.text.TextFormatting;
1822
import net.minecraft.util.text.TranslationTextComponent;
1923
import net.minecraft.world.World;
2024
import net.minecraft.world.server.ServerWorld;
25+
import net.minecraftforge.fml.client.gui.GuiUtils;
26+
import net.minecraftforge.fml.network.NetworkHooks;
2127

2228
import javax.annotation.Nullable;
2329
import java.util.List;
@@ -58,12 +64,13 @@ public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player
5864
return ActionResult.resultFail(stack);
5965
}
6066

61-
// TODO: Machine enter/exit dimension
62-
// if(world.provider.getDimension() != ConfigurationHandler.Settings.dimensionId) {
63-
// // player.openGui(compactmachines.instance, GuiIds.PSD_GUIDE.ordinal(), world, (int) player.posX, (int) player.posY, (int) player.posZ);
64-
// return new ActionResult(ActionResultType.SUCCESS, stack);
65-
// }
66-
//
67+
// If we aren't in the compact dimension, allow PSD guide usage
68+
// Prevents misfiring if a player is trying to leave a machine or set their spawn
69+
if(world.isRemote && world.getDimensionKey() != Registration.COMPACT_DIMENSION) {
70+
PersonalShrinkingDeviceScreen.show();
71+
return ActionResult.resultSuccess(stack);
72+
}
73+
6774
if (world instanceof ServerWorld && player instanceof ServerPlayerEntity) {
6875
ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
6976

src/main/java/com/robotgryphon/compactmachines/reference/Resources.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class Resources {
77
public static final class Gui {
88
protected static final String path = "textures/gui/";
99

10-
public static final ResourceLocation PSD_SCREEN = new ResourceLocation(CompactMachines.MOD_ID, path + "psdscreen.png");
10+
public static final ResourceLocation PSD_SCREEN = new ResourceLocation(CompactMachines.MOD_ID, path + "psd_screen.png");
1111
}
1212
}

0 commit comments

Comments
 (0)