Skip to content

Commit 2e3bcae

Browse files
committed
Tab Icons implementation
1 parent 2bfc3ac commit 2e3bcae

5 files changed

Lines changed: 114 additions & 20 deletions

File tree

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

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

3+
import com.hanprogramer.androids.AndroidsCraft;
34
import com.hanprogramer.androids.client.screen.base.BaseScreenTab;
4-
import com.hanprogramer.androids.client.screen.widgets.FlatTabButton;
55
import com.hanprogramer.androids.client.screen.widgets.TabButton;
66
import com.hanprogramer.androids.screen.ScreenWithTabsHandler;
77
import net.minecraft.client.gui.DrawContext;
@@ -43,13 +43,26 @@ private void initTabButtons() {
4343
int x = (this.width - this.backgroundWidth) / 2;
4444
int y = (this.height - this.backgroundHeight) / 2;
4545
tabButtons.clear();
46-
for(var i = 0; i < tabs.size(); i++) {
46+
for (var i = 0; i < tabs.size(); i++) {
4747
int finalI = i;
48-
TabButton tab;
49-
addDrawableChild(tab = new TabButton(x - 2 + 32 * i, y - 32 + 4, 32, 32, tabs.get(i).title, button -> {
50-
setActiveTab(finalI);
51-
}, Identifier.of(""), i == activeTab));
52-
tabButtons.add(tab);
48+
TabButton btn;
49+
var tab = tabs.get(i);
50+
51+
// Add the tab with their icon
52+
if (tab.iconTexture != null)
53+
addDrawableChild(btn = new TabButton(x - 2 + 32 * i, y - 32 + 4, 32, 32, tab.title, button -> {
54+
setActiveTab(finalI);
55+
}, tab.iconTexture, i == activeTab));
56+
else if(tab.iconStack != null){
57+
addDrawableChild(btn = new TabButton(x - 2 + 32 * i, y - 32 + 4, 32, 32, tab.title, button -> {
58+
setActiveTab(finalI);
59+
}, tab.iconStack, i == activeTab));
60+
} else {
61+
addDrawableChild(btn = new TabButton(x - 2 + 32 * i, y - 32 + 4, 32, 32, tab.title, button -> {
62+
setActiveTab(finalI);
63+
}, Identifier.of(AndroidsCraft.MOD_ID, "broken_tab_icon"), i == activeTab));
64+
}
65+
tabButtons.add(btn);
5366
}
5467
}
5568

@@ -101,19 +114,19 @@ private DefaultedList<Slot> getSlots() {
101114

102115
public void setActiveTab(int val) {
103116
activeTab = val;
104-
for(var e : elements) {
117+
for (var e : elements) {
105118
remove((Element) e);
106119
}
107120
elements.clear();
108121
tabs.get(activeTab).build(elements);
109-
for(var e : elements) {
122+
for (var e : elements) {
110123
// Add the new elements
111124
addDrawableChild((Element & Drawable & Selectable) e);
112125
}
113126

114127
// Set the button
115128
int i = 0;
116-
for(var b : tabButtons) {
129+
for (var b : tabButtons) {
117130
b.isActive = i == activeTab;
118131
i++;
119132
}
@@ -132,10 +145,10 @@ public void renderMain(DrawContext context, int mouseX, int mouseY, float deltaT
132145
int j = this.y;
133146
super.render(context, mouseX, mouseY, deltaTicks);
134147
context.getMatrices().pushMatrix();
135-
context.getMatrices().translate((float)i, (float)j);
148+
context.getMatrices().translate((float) i, (float) j);
136149
this.drawForeground(context, mouseX, mouseY);
137150
Slot slot = this.focusedSlot;
138-
this.focusedSlot = this.getSlotAt((double)mouseX, (double)mouseY);
151+
this.focusedSlot = this.getSlotAt((double) mouseX, (double) mouseY);
139152
this.drawSlotHighlightBack(context);
140153
this.drawSlots(context);
141154
this.drawSlotHighlightFront(context);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
@Environment(EnvType.CLIENT)
1717
public class AndroidInventoryHandledScreen extends HandledScreenWithTabs<AndroidInventoryScreenHandler> {
1818
protected AndroidEntity entity;
19+
1920
public AndroidInventoryHandledScreen(AndroidInventoryScreenHandler handler, PlayerInventory inventory, Text title) {
2021
super(handler, inventory, title);
2122
resyncEntity();
2223
tabs.add(new InventoryTab(this, Text.of("Android"), ModItems.ANDROID_SPAWNER.create(entity.getMaterial()), null));
24+
var backItem = entity.getInventory().getStack(AndroidEntity.BODY_SLOT);
25+
if (handler.hasBackContainer) {
26+
// TODO: Implement more containers?
27+
tabs.add(new BackInventoryTab(this, Text.of(backItem.getName()), backItem, null));
28+
}
2329
tabs.add(new SettingsTab(this, Text.of("Settings"), null, Identifier.of(AndroidsCraft.MOD_ID, "settings_icon")));
2430
}
2531

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
package com.hanprogramer.androids.client.screen.android;
22

3-
public class BackInventoryTab {
3+
import com.hanprogramer.androids.client.screen.base.BaseScreenTab;
4+
import net.minecraft.client.gl.RenderPipelines;
5+
import net.minecraft.client.gui.DrawContext;
6+
import net.minecraft.client.gui.Drawable;
7+
import net.minecraft.item.ItemStack;
8+
import net.minecraft.text.Text;
9+
import net.minecraft.util.Identifier;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
import java.util.List;
13+
14+
public class BackInventoryTab extends BaseScreenTab<AndroidInventoryHandledScreen> {
15+
private static final Identifier TEXTURE = Identifier.of("androids", "textures/gui/android_chest_inventory.png");
16+
public BackInventoryTab(AndroidInventoryHandledScreen parent, Text title, @Nullable ItemStack iconStack, @Nullable Identifier texture) {
17+
super(parent, title, iconStack, texture);
18+
}
19+
20+
@Override
21+
public void build(List<Drawable> elements) {
22+
23+
}
24+
25+
@Override
26+
public void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {
27+
int i = (this.parent.width - this.parent.backgroundWidth) / 2;
28+
int j = (this.parent.height - this.parent.backgroundHeight) / 2;
29+
context.drawTexture(RenderPipelines.GUI_TEXTURED, TEXTURE, i, j, 0.0F, 0.0F, this.parent.backgroundWidth, this.parent.backgroundHeight, 256, 256);
30+
}
431
}

src/client/java/com/hanprogramer/androids/client/screen/widgets/TabButton.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.minecraft.client.gui.DrawContext;
66
import net.minecraft.client.gui.tooltip.Tooltip;
77
import net.minecraft.client.gui.widget.ButtonWidget;
8+
import net.minecraft.item.ItemStack;
89
import net.minecraft.text.Text;
910
import net.minecraft.util.Identifier;
1011
import org.jetbrains.annotations.Nullable;
@@ -15,6 +16,8 @@ public class TabButton extends ButtonWidget {
1516
private static final int ICON_PADDING = 2;
1617
@Nullable
1718
private final Identifier icon;
19+
@Nullable
20+
private final ItemStack iconItem;
1821
public boolean isActive = false;
1922

2023
private static final Identifier TEXTURE_OFF = Identifier.of(AndroidsCraft.MOD_ID, "tab_off_v");
@@ -24,18 +27,30 @@ public TabButton(int x, int y, int width, int height, Text message, PressAction
2427
super(x, y, width, height, Text.empty(), onPress, Supplier::get);
2528
setTooltip(Tooltip.of(message));
2629
this.icon = icon;
30+
this.iconItem = null;
31+
this.isActive = isActive;
32+
}
33+
public TabButton(int x, int y, int width, int height, Text message, PressAction onPress, @Nullable ItemStack icon, boolean isActive) {
34+
super(x, y, width, height, Text.empty(), onPress, Supplier::get);
35+
setTooltip(Tooltip.of(message));
36+
this.iconItem = icon;
37+
this.icon = null;
2738
this.isActive = isActive;
2839
}
2940

3041
@Override
3142
protected void renderWidget(DrawContext context, int mouseX, int mouseY, float deltaTicks) {
3243
context.drawGuiTexture(RenderPipelines.GUI_TEXTURED, isActive? TEXTURE_ON : TEXTURE_OFF, getX(), getY(), getWidth(), getHeight());
33-
// if (icon != null)
34-
// context.drawTexture(RenderPipelines.GUI_TEXTURED, icon,
35-
// this.getX() + ICON_PADDING, this.getY() + ICON_PADDING,
36-
// 0, 0,
37-
// getWidth() - ICON_PADDING * 2, getHeight() - ICON_PADDING * 2,
38-
// 16, 16,
39-
// 16, 16);
44+
if (icon != null)
45+
context.drawTexture(RenderPipelines.GUI_TEXTURED, icon,
46+
this.getX() + 8, this.getY() + 10,
47+
0, 0,
48+
16, 16,
49+
16, 16,
50+
16, 16);
51+
else if(iconItem != null) {
52+
context.drawItem(iconItem, getX() + 8, getY() + 10);
53+
}
54+
4055
}
4156
}

src/main/java/com/hanprogramer/androids/entities/android/AndroidInventoryScreenHandler.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.minecraft.inventory.Inventory;
1010
import net.minecraft.inventory.SimpleInventory;
1111
import net.minecraft.item.ItemStack;
12+
import net.minecraft.item.Items;
1213
import net.minecraft.screen.slot.Slot;
1314
import net.minecraft.util.collection.DefaultedList;
1415
import org.jetbrains.annotations.Nullable;
@@ -22,6 +23,8 @@ public class AndroidInventoryScreenHandler extends ScreenWithTabsHandler {
2223
private final PlayerInventory playerInventory;
2324

2425
private final DefaultedList<Slot> inventoryScreenSlots = DefaultedList.of();
26+
private final DefaultedList<Slot> backInventoryScreenSlots = DefaultedList.of();
27+
public boolean hasBackContainer = false;
2528

2629
// Client-side constructor (called via ExtendedScreenHandlerType)
2730
public AndroidInventoryScreenHandler(int syncId, PlayerInventory playerInventory, int entityId) {
@@ -40,7 +43,18 @@ public AndroidInventoryScreenHandler(int syncId, PlayerInventory playerInventory
4043

4144
// Add all the slots at start (only hides them client side)
4245
createInventoryTabSlots();
46+
createBackInventoryTabSlots();
47+
4348
this.addAllSlotsOf(inventoryScreenSlots);
49+
assert entity != null;
50+
var backStack = entity.getInventory().getStack(AndroidEntity.BODY_SLOT);
51+
if(backStack != null) {
52+
if(backStack.isOf(Items.CHEST) || backStack.isOf(Items.BARREL)) {
53+
//TODO: Implement more containers
54+
hasBackContainer = true;
55+
this.addAllSlotsOf(backInventoryScreenSlots);
56+
}
57+
}
4458
}
4559

4660
@Override
@@ -107,9 +121,28 @@ private void addAllSlotsOf(DefaultedList<Slot> slots) {
107121
public DefaultedList<Slot> getSlotsForTab(int tabIndex) {
108122
if(tabIndex == 0)
109123
return inventoryScreenSlots;
124+
else if(tabIndex == 1 && hasBackContainer)
125+
return backInventoryScreenSlots;
110126
return DefaultedList.of(); // return no slots by default
111127
}
112128

129+
public void createBackInventoryTabSlots() {
130+
backInventoryScreenSlots.clear();
131+
int m;
132+
int l;
133+
134+
// The player inventory
135+
for (m = 0; m < 3; ++m) {
136+
for (l = 0; l < 9; ++l) {
137+
backInventoryScreenSlots.add(new Slot(playerInventory, l + m * 9 + 9, 8 + l * 18, 84 + m * 18));
138+
}
139+
}
140+
// The player Hotbar
141+
for (m = 0; m < 9; ++m) {
142+
backInventoryScreenSlots.add(new Slot(playerInventory, m, 8 + m * 18, 142));
143+
}
144+
}
145+
113146
public void createInventoryTabSlots() {
114147
inventoryScreenSlots.clear();
115148
int m;

0 commit comments

Comments
 (0)