Skip to content

Commit 0609c53

Browse files
committed
Basic display of android props
1 parent 4d29217 commit 0609c53

14 files changed

Lines changed: 311 additions & 26 deletions

File tree

src/client/java/com/hanprogramer/androids/client/screen/HandledScreenWithTabs.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hanprogramer.androids.client.screen;
22

33
import com.hanprogramer.androids.AndroidsCraft;
4+
import com.hanprogramer.androids.client.screen.android.DrawableSelectableElement;
45
import com.hanprogramer.androids.client.screen.base.BaseScreenTab;
56
import com.hanprogramer.androids.client.screen.widgets.TabButton;
67
import com.hanprogramer.androids.screen.ScreenWithTabsHandler;

src/client/java/com/hanprogramer/androids/client/screen/PublicHandledScreen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public abstract class PublicHandledScreen<T extends ScreenHandler> extends Scree
6262
protected Slot lastClickedSlot;
6363
@Nullable
6464
protected LetGoTouchStack letGoTouchStack;
65-
protected int x;
66-
protected int y;
65+
public int x;
66+
public int y;
6767
protected boolean touchIsRightClickDrag;
6868
protected ItemStack touchDragStack;
6969
protected long touchDropTimer;

src/client/java/com/hanprogramer/androids/client/screen/android/AndroidInventoryHandledScreen.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,16 @@ protected void init() {
6767

6868
private void initTab(int tabIndex) {
6969
}
70+
71+
@Override
72+
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
73+
return tabs.get(activeTab).mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
74+
}
75+
76+
@Override
77+
public boolean mouseClicked(double mouseX, double mouseY, int button) {
78+
if(tabs.get(activeTab).mouseClicked(mouseX, mouseY, button))
79+
return true;
80+
return super.mouseClicked(mouseX, mouseY, button);
81+
}
7082
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.hanprogramer.androids.client.screen.android;
2+
3+
import net.minecraft.client.gui.Drawable;
4+
import net.minecraft.client.gui.Element;
5+
import net.minecraft.client.gui.Selectable;
6+
7+
public interface DrawableSelectableElement extends Element, Drawable, Selectable {
8+
}

src/client/java/com/hanprogramer/androids/client/screen/android/InventoryTab.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import net.minecraft.client.gl.RenderPipelines;
99
import net.minecraft.client.gui.DrawContext;
1010
import net.minecraft.client.gui.Drawable;
11+
import net.minecraft.client.gui.Element;
12+
import net.minecraft.client.gui.Selectable;
1113
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
1214
import net.minecraft.item.ItemStack;
1315
import net.minecraft.text.Text;
@@ -32,17 +34,17 @@ public void build(List<Drawable> elements) {
3234
int i = (parent.width - parent.backgroundWidth) / 2;
3335
int j = (parent.height - parent.backgroundHeight) / 2;
3436

35-
elements.add(progressBar = new VerticalProgressBar(i + 141, j + 7, 8, 72, (int) parent.entity.getTotalPower(), (int) parent.entity.getTotalCapacity()));
37+
elements.add( (progressBar = new VerticalProgressBar(i + 141, j + 7, 8, 72, (int) parent.entity.getTotalPower(), (int) parent.entity.getTotalCapacity())));
3638
if (parent.entity != null) {
3739
var mc = MinecraftClient.getInstance();
38-
elements.add(switchWidget = new SwitchWidget(i + 108, j + 18, 24, 24, parent.entity.isOn(), (widget, value) -> {
39-
assert mc.interactionManager != null;
40-
if (value)
41-
mc.interactionManager.clickButton(parent.handler.syncId, AndroidInventoryScreenHandler.SWITCH_BUTTON_ON);
42-
else
43-
mc.interactionManager.clickButton(parent.handler.syncId, AndroidInventoryScreenHandler.SWITCH_BUTTON_OFF);
44-
System.out.println(value);
45-
}));
40+
elements.add( (switchWidget = new SwitchWidget(i + 108, j + 18, 24, 24, parent.entity.isOn(), (widget, value) -> {
41+
assert mc.interactionManager != null;
42+
if (value)
43+
mc.interactionManager.clickButton(parent.handler.syncId, AndroidInventoryScreenHandler.SWITCH_BUTTON_ON);
44+
else
45+
mc.interactionManager.clickButton(parent.handler.syncId, AndroidInventoryScreenHandler.SWITCH_BUTTON_OFF);
46+
System.out.println(value);
47+
})));
4648
}
4749
}
4850

src/client/java/com/hanprogramer/androids/client/screen/android/LogsTab.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import net.minecraft.client.gl.RenderPipelines;
55
import net.minecraft.client.gui.DrawContext;
66
import net.minecraft.client.gui.Drawable;
7+
import net.minecraft.client.gui.Element;
8+
import net.minecraft.client.gui.Selectable;
79
import net.minecraft.item.ItemStack;
810
import net.minecraft.text.Text;
911
import net.minecraft.util.Identifier;
@@ -21,6 +23,7 @@ public LogsTab(AndroidInventoryHandledScreen parent, Text title, @Nullable ItemS
2123
public void build(List<Drawable> elements) {
2224

2325
}
26+
2427
@Override
2528
public void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {
2629
int i = (this.parent.width - this.parent.backgroundWidth) / 2;

src/client/java/com/hanprogramer/androids/client/screen/android/SettingsTab.java

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,138 @@
11
package com.hanprogramer.androids.client.screen.android;
22

33
import com.hanprogramer.androids.client.screen.base.BaseScreenTab;
4+
import com.hanprogramer.androids.entities.android.AndroidScriptProperty;
5+
import net.minecraft.client.MinecraftClient;
46
import net.minecraft.client.gl.RenderPipelines;
57
import net.minecraft.client.gui.DrawContext;
68
import net.minecraft.client.gui.Drawable;
7-
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
9+
import net.minecraft.client.gui.Element;
10+
import net.minecraft.client.gui.Selectable;
11+
import net.minecraft.client.gui.widget.ButtonWidget;
12+
import net.minecraft.client.gui.widget.ElementListWidget;
813
import net.minecraft.item.ItemStack;
914
import net.minecraft.text.Text;
15+
import net.minecraft.util.Colors;
1016
import net.minecraft.util.Identifier;
17+
import net.minecraft.util.math.BlockPos;
1118
import org.jetbrains.annotations.Nullable;
12-
import org.joml.Quaternionf;
13-
import org.joml.Vector3f;
1419

1520
import java.util.List;
21+
import java.util.Objects;
1622

1723
public class SettingsTab extends BaseScreenTab<AndroidInventoryHandledScreen> {
1824
private static final Identifier TEXTURE = Identifier.of("androids", "textures/gui/android_settings.png");
25+
private ElementListWidget<SettingsEntry> list;
26+
27+
public class SettingsEntry extends ElementListWidget.Entry<SettingsEntry> {
28+
private final Text title;
29+
private final ButtonWidget button;
30+
private final String id;
31+
private Object value;
32+
33+
public SettingsEntry(Text title, String entryType, String id, Object value) {
34+
this.title = title;
35+
this.id = id;
36+
this.value = value;
37+
if (Objects.equals(entryType, AndroidScriptProperty.TYPE_BOOLEAN)) {
38+
var bool = (Boolean) value;
39+
button = ButtonWidget.builder(bool ? Text.of("ON") : Text.of("OFF"), button1 -> {
40+
System.out.println("Clicked");
41+
}).build();
42+
}
43+
else if (Objects.equals(entryType, AndroidScriptProperty.TYPE_BLOCKPOS)) {
44+
var val = (BlockPos) value;
45+
button = ButtonWidget.builder(Text.of(val.toString()), button1 -> {
46+
System.out.println("Clicked");
47+
}).build();
48+
}
49+
else {
50+
button = ButtonWidget.builder(Text.of("..."), button1 -> {
51+
System.out.println("Clicked");
52+
}).build();
53+
}
54+
55+
}
56+
57+
@Override
58+
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickProgress) {
59+
var textRenderer = SettingsTab.this.parent.getTextRenderer();
60+
context.fill(x, y, x + entryWidth, y + entryHeight, Colors.DARK_GRAY);
61+
context.drawText(textRenderer, title, x + 2, y + (entryHeight - 8) / 2, Colors.WHITE, true);
62+
63+
int btnW = 40, btnH = 20;
64+
int btnX = x + entryWidth - btnW - 4;
65+
int btnY = y + (entryHeight - btnH) / 2;
66+
this.button.setPosition(btnX, btnY);
67+
this.button.setWidth(btnW);
68+
this.button.setHeight(btnH);
69+
this.button.render(context, mouseX, mouseY, tickProgress);
70+
}
71+
72+
@Override
73+
public List<? extends Selectable> selectableChildren() {
74+
return List.of();
75+
}
76+
77+
78+
@Override
79+
public boolean mouseClicked(double mx, double my, int btn) {
80+
return this.button.mouseClicked(mx, my, btn) || super.mouseClicked(mx, my, btn);
81+
}
82+
83+
@Override
84+
public boolean mouseReleased(double mx, double my, int btn) {
85+
return this.button.mouseReleased(mx, my, btn) || super.mouseReleased(mx, my, btn);
86+
}
87+
88+
@Override
89+
public List<? extends Element> children() {
90+
return java.util.List.of(this.button);
91+
}
92+
}
1993

2094
public SettingsTab(AndroidInventoryHandledScreen parent, Text title, @Nullable ItemStack iconStack, @Nullable Identifier texture) {
2195
super(parent, title, iconStack, texture);
2296
}
2397

2498
@Override
2599
public void build(List<Drawable> elements) {
100+
var x = parent.x;
101+
var y = parent.y;
102+
list = new ElementListWidget<>(MinecraftClient.getInstance(), 160, 150, y + 8, 20) {
103+
@Override
104+
protected int getScrollbarX() {
105+
return x + 160;
106+
}
107+
108+
@Override
109+
public int getRowLeft() {
110+
return this.getX();
111+
}
112+
113+
@Override
114+
public int getRowWidth() {
115+
return 160 - 10;
116+
}
117+
};
118+
list.setDimensionsAndPosition(160, 150, x + 8, y + 8);
119+
for (var p : parent.entity.getProperties()) {
120+
list.children().add(new SettingsEntry(Text.of(p.displayName), p.type, p.id, p.value));
121+
}
122+
elements.add(list);
123+
}
124+
26125

126+
@Override
127+
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
128+
return list.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
129+
}
130+
131+
@Override
132+
public boolean mouseClicked(double mouseX, double mouseY, int button) {
133+
if (isPointWithinBounds(list.getX(), list.getY(), list.getWidth(), list.getHeight(), mouseX, mouseY))
134+
return list.mouseClicked(mouseX, mouseY, button);
135+
return false;
27136
}
28137

29138
@Override

src/client/java/com/hanprogramer/androids/client/screen/base/BaseScreenTab.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.hanprogramer.androids.client.screen.base;
22

33
import com.hanprogramer.androids.client.screen.HandledScreenWithTabs;
4+
import net.minecraft.client.MinecraftClient;
45
import net.minecraft.client.gui.DrawContext;
56
import net.minecraft.client.gui.Drawable;
6-
import net.minecraft.client.gui.Element;
7-
import net.minecraft.client.gui.Selectable;
87
import net.minecraft.item.ItemStack;
98
import net.minecraft.text.Text;
109
import net.minecraft.util.Identifier;
@@ -14,7 +13,7 @@
1413

1514
public abstract class BaseScreenTab<T extends HandledScreenWithTabs<?>> {
1615
public Identifier BACKGROUND_TEXTURE;
17-
protected T parent;
16+
public T parent;
1817

1918
@Nullable public ItemStack iconStack;
2019
@Nullable public Identifier iconTexture;
@@ -30,5 +29,22 @@ public BaseScreenTab(T parent, Text title, @Nullable ItemStack iconStack, @Nulla
3029

3130
public abstract void build(List<Drawable> elements);
3231
public void handledScreenTick() {}
32+
33+
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount){
34+
return false;
35+
}
36+
37+
protected boolean isPointWithinBounds(int x, int y, int width, int height, double pointX, double pointY) {
38+
int i = this.parent.x;
39+
int j = this.parent.y;
40+
pointX -= i;
41+
pointY -= j;
42+
return pointX >= (double)(x - 1) && pointX < (double)(x + width + 1) && pointY >= (double)(y - 1) && pointY < (double)(y + height + 1);
43+
}
44+
45+
public boolean mouseClicked(double mouseX, double mouseY, int button) {
46+
return false;
47+
}
48+
3349
public void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {}
3450
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.hanprogramer.androids.client.screen.widgets;
2+
3+
public class ScrollableButtonList {
4+
}

src/main/java/com/hanprogramer/androids/AndroidsCraft.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import static com.hanprogramer.androids.ModEntities.ANDROID;
2424
import static com.hanprogramer.androids.entities.android.AndroidEntity.ANDROID_MATERIAL_TRACKED_DATA_HANDLER;
25+
import static com.hanprogramer.androids.entities.android.AndroidEntity.ANDROID_PROPERTIES_TRACKED_DATA_HANDLER;
26+
import static com.hanprogramer.androids.entities.android.util.PropertyListDataHandler.PROPERTY_LIST_DATA_HANDLER;
2527

2628
public class AndroidsCraft implements ModInitializer {
2729
public static final String MOD_ID = "androids";
@@ -55,6 +57,7 @@ public void onInitialize() {
5557

5658

5759
FabricTrackedDataRegistry.register(Identifier.of("androids:material_data_handler"), ANDROID_MATERIAL_TRACKED_DATA_HANDLER);
60+
FabricTrackedDataRegistry.register(Identifier.of("androids:android_property_list"), ANDROID_PROPERTIES_TRACKED_DATA_HANDLER); // do this once during mod initialization
5861
}
5962

6063
public static final ExtendedScreenHandlerType<ComputerScreenHandler, ComputerOSInitPacketS2CPayload> COMPUTER_SCREEN_HANDLER =

0 commit comments

Comments
 (0)