Skip to content

Commit 705a839

Browse files
committed
Finished draft implementation of ScrollableTextWidget, hardcoded lorem ipsum rn
1 parent f34a673 commit 705a839

File tree

5 files changed

+276
-2
lines changed

5 files changed

+276
-2
lines changed

src/main/java/com/robotgryphon/compactmachines/client/gui/PersonalShrinkingDeviceScreen.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,96 @@
33
import com.mojang.blaze3d.matrix.MatrixStack;
44
import com.mojang.blaze3d.systems.RenderSystem;
55
import com.robotgryphon.compactmachines.CompactMachines;
6+
import com.robotgryphon.compactmachines.client.gui.guide.GuideSection;
67
import net.minecraft.client.Minecraft;
78
import net.minecraft.client.gui.screen.Screen;
89
import net.minecraft.util.ResourceLocation;
910
import net.minecraft.util.text.TranslationTextComponent;
1011

12+
import javax.annotation.Nullable;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
1116
public class PersonalShrinkingDeviceScreen extends Screen {
1217
private final ResourceLocation GUI = new ResourceLocation(CompactMachines.MOD_ID, "textures/gui/psd_screen.png");
1318
private static final int WIDTH = 256;
1419
private static final int HEIGHT = 201;
1520

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+
1627
protected PersonalShrinkingDeviceScreen() {
1728
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;
1853
}
1954

2055
@Override
2156
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+
2267
return false;
2368
}
2469

2570
@Override
2671
public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
2772
this.renderBackground(matrixStack);
28-
73+
2974
RenderSystem.color4f(1, 1, 1, 1);
3075
int relX = (this.width - WIDTH) / 2;
31-
int relY = (this.height - HEIGHT) / 2;
76+
77+
// relY = relative position, places screen against bottom edge of screen
78+
int relY = (this.height - HEIGHT);
3279

3380
matrixStack.push();
3481
matrixStack.translate(relX, relY, 0);
3582

3683
this.minecraft.getTextureManager().bindTexture(GUI);
3784
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+
}
3893

3994
matrixStack.pop();
95+
4096
super.render(matrixStack, mouseX, mouseY, partialTicks);
4197
}
4298

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.robotgryphon.compactmachines.client.gui.guide;
2+
3+
import com.mojang.blaze3d.matrix.MatrixStack;
4+
import com.robotgryphon.compactmachines.client.gui.widget.AbstractCMGuiWidget;
5+
import com.robotgryphon.compactmachines.client.gui.widget.ScrollableWrappedTextWidget;
6+
import net.minecraft.client.gui.IGuiEventListener;
7+
import net.minecraft.client.gui.IRenderable;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.Optional;
12+
13+
public class GuidePage implements IRenderable, IGuiEventListener {
14+
15+
protected List<AbstractCMGuiWidget> widgets;
16+
17+
public GuidePage() {
18+
widgets = new ArrayList<>();
19+
20+
String ex = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse posuere nunc enim, " +
21+
"nec euismod libero ornare accumsan. Nulla dapibus eros lectus, et pellentesque magna finibus ut. " +
22+
"Quisque ornare id sem id luctus. Nam pulvinar dolor purus.\n\nFusce interdum, nisl vitae congue " +
23+
"feugiat, enim erat porttitor lorem, eu iaculis lorem est sed quam. Aliquam condimentum sed " +
24+
"dolor ut lobortis. Nunc eget turpis eget ligula malesuada volutpat. Aliquam nec magna nec massa " +
25+
"varius finibus.";
26+
27+
ex += "Nunc sollicitudin pellentesque interdum. In tempus, eros sed tincidunt semper, lorem ante volutpat mi, at sodales mauris nulla eget orci. Ut fermentum eros et massa condimentum tempus. Donec et convallis sem. In et ante non elit vehicula sagittis. Fusce sed nunc mauris. Vivamus at leo condimentum sem mollis pharetra vitae sit amet justo. Proin ac felis porta, consectetur tellus eget, volutpat dui. Vivamus porttitor gravida odio, vel sodales velit tempor at. Quisque lorem mauris, sagittis eget sem sit amet, iaculis elementum sem. Sed sed nibh quis tortor mollis ornare gravida eget nibh. Cras consectetur elit eros, dignissim hendrerit metus luctus vitae. Fusce ante orci, sollicitudin ut tempor et, fermentum id velit.";
28+
29+
ScrollableWrappedTextWidget sc = new ScrollableWrappedTextWidget(ex, 0, 0, 226, 100);
30+
widgets.add(sc);
31+
}
32+
33+
@Override
34+
public void render(MatrixStack ms, int mouseX, int mouseY, float partialTicks) {
35+
for(IRenderable comp : widgets)
36+
comp.render(ms, mouseX, mouseY, partialTicks);
37+
}
38+
39+
public Optional<AbstractCMGuiWidget> getWidgetByPosition(double mouseX, double mouseY) {
40+
for(AbstractCMGuiWidget wid : widgets) {
41+
if(wid.isMouseOver(mouseX, mouseY))
42+
return Optional.of(wid);
43+
}
44+
45+
return Optional.empty();
46+
}
47+
48+
@Override
49+
public void mouseMoved(double mouseX, double mouseY) {
50+
getWidgetByPosition(mouseX, mouseY)
51+
.ifPresent(c -> c.mouseMoved(mouseX, mouseY));
52+
}
53+
54+
@Override
55+
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
56+
return getWidgetByPosition(mouseX, mouseY)
57+
.map(c -> c.mouseScrolled(mouseX, mouseY, delta))
58+
.orElse(false);
59+
}
60+
61+
@Override
62+
public boolean mouseClicked(double mouseX, double mouseY, int button) {
63+
return getWidgetByPosition(mouseX, mouseY)
64+
.map(c -> c.mouseClicked(mouseX, mouseY, button))
65+
.orElse(false);
66+
}
67+
}
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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.FontRenderer;
6+
import net.minecraft.client.gui.IGuiEventListener;
7+
import net.minecraft.client.gui.IRenderable;
8+
import net.minecraft.util.IReorderingProcessor;
9+
import net.minecraft.util.math.MathHelper;
10+
import net.minecraft.util.text.StringTextComponent;
11+
12+
import java.util.List;
13+
14+
public class ScrollableWrappedTextWidget extends AbstractCMGuiWidget {
15+
16+
private String text;
17+
private double yScroll = 0;
18+
private FontRenderer fontRenderer;
19+
20+
private int maxLinesToShow;
21+
private int lineIndexStart;
22+
private List<IReorderingProcessor> lines;
23+
private int charSize;
24+
25+
public ScrollableWrappedTextWidget(String text, int x, int y, int width, int height) {
26+
super(x, y, width, height);
27+
this.text = text;
28+
this.fontRenderer = Minecraft.getInstance().fontRenderer;
29+
30+
this.recalculate();
31+
}
32+
33+
@Override
34+
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
35+
double temp = yScroll - delta;
36+
yScroll = MathHelper.clamp(temp, 0, lines.size() - maxLinesToShow - 1);
37+
recalculate();
38+
return true;
39+
}
40+
41+
private void recalculate() {
42+
lines = fontRenderer.trimStringToWidth(new StringTextComponent(text), width);
43+
44+
charSize = fontRenderer.getStringWidth("M");
45+
int maxOnScreen = height / (charSize + 4);
46+
maxLinesToShow = Math.min(lines.size(), maxOnScreen);
47+
48+
// startClamp - either the current line scroll, or the max allowed line
49+
int startClamp = Math.min((int) Math.floor(yScroll), lines.size());
50+
lineIndexStart = MathHelper.clamp(0, startClamp, lines.size() - 1);
51+
}
52+
53+
@Override
54+
public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
55+
matrixStack.push();
56+
matrixStack.translate(0, 0, 10);
57+
58+
FontRenderer fr = Minecraft.getInstance().fontRenderer;
59+
60+
try {
61+
for (int y = lineIndexStart; y <= lineIndexStart + maxLinesToShow; y++) {
62+
IReorderingProcessor s = lines.get(y);
63+
fr.func_238407_a_(matrixStack, s, 0, (y - lineIndexStart) * (charSize + 4), 0xFFFFFF);
64+
}
65+
}
66+
67+
catch(Exception ex1) {}
68+
69+
matrixStack.pop();
70+
}
71+
72+
@Override
73+
public boolean mouseClicked(double mouseX, double mouseY, int button) {
74+
return false;
75+
}
76+
}

0 commit comments

Comments
 (0)