Skip to content

Commit

Permalink
Add item description
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCheung0422 authored and ChristofferHolmesland committed Jul 8, 2023
1 parent 6c4734a commit 33c5083
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/client/java/minicraft/item/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public String getData() {
return name;
}

/** Gets the description used for display item information. */
public String getDescription() {
return getName();
}

public final String getName() { return name; }

// Returns the String that should be used to display this item in a menu or list.
Expand Down
52 changes: 42 additions & 10 deletions src/client/java/minicraft/screen/PlayerInvDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,77 @@
import minicraft.entity.mob.Player;
import minicraft.gfx.Color;
import minicraft.gfx.Font;
import minicraft.gfx.Point;
import minicraft.gfx.Screen;
import minicraft.item.Inventory;
import minicraft.item.Item;
import minicraft.item.Items;
import minicraft.item.StackableItem;
import minicraft.screen.entry.StringEntry;

public class PlayerInvDisplay extends Display {

private static final int padding = 10;

private final Player player;

private String itemDescription = "";
private Menu.Builder descriptionMenuBuilder;

private final boolean creativeMode;
private final Inventory creativeInv;

public PlayerInvDisplay(Player player) {
super(new InventoryMenu(player, player.getInventory(), "minicraft.display.menus.inventory", RelPos.LEFT));

this.player = player;
descriptionMenuBuilder = new Menu.Builder(true, 3, RelPos.TOP_LEFT);
creativeMode = Game.isMode("minicraft.settings.mode.creative");
itemDescription = getDescription();
Menu descriptionMenu = descriptionMenuBuilder.setPositioning(new Point(padding, menus[0].getBounds().getBottom() + 8), RelPos.BOTTOM_RIGHT)
.setEntries(StringEntry.useLines(Color.WHITE, false, itemDescription.split("\n")))
.setSelectable(false)
.createMenu();
if (creativeMode) {
creativeInv = Items.getCreativeModeInventory();
menus = new Menu[] {
menus[0],
new InventoryMenu(player, creativeInv, "minicraft.displays.player_inv.container_title.items", RelPos.RIGHT) {{
creativeInv = true;
}}
}},
descriptionMenu
};

menus[1].translate(menus[0].getBounds().getWidth() + padding, 0);
update();

if(menus[0].getNumOptions() == 0) onSelectionChange(0, 1);
} else creativeInv = null;

this.player = player;
} else {
creativeInv = null;
menus = new Menu[] { menus[0], descriptionMenu };
}

onScreenKeyboardMenu = OnScreenKeyboardMenu.checkAndCreateMenu();
if (onScreenKeyboardMenu != null)
onScreenKeyboardMenu.setVisible(false);
}

private String getDescription() {
if (selection == 0) {
Inventory inv = player.getInventory();
return inv.invSize() == 0 ? "" : inv.get(menus[0].getSelection()).getDescription();
} else {
return creativeInv.invSize() == 0 ? "" : creativeInv.get(menus[1].getSelection()).getDescription();
}
}

OnScreenKeyboardMenu onScreenKeyboardMenu;

@Override
public void tick(InputHandler input) {
boolean acted = false; // Checks if typing action is needed to be handled.
boolean mainMethod = false;

itemDescription = getDescription();
Menu curMenu = menus[selection];
if (onScreenKeyboardMenu == null || !curMenu.isSearcherBarActive() && !onScreenKeyboardMenu.isVisible()) {
super.tick(input);
Expand Down Expand Up @@ -91,7 +114,7 @@ public void tick(InputHandler input) {
}
}

if (mainMethod || !onScreenKeyboardMenu.isVisible())
if (mainMethod || !onScreenKeyboardMenu.isVisible()) {
if (creativeMode) {
int otherIdx = getOtherIdx();

Expand Down Expand Up @@ -156,16 +179,22 @@ public void tick(InputHandler input) {
Game.exitDisplay();
}
}
}
}

@Override
public void render(Screen screen) {
if (itemDescription.isEmpty()) menus[creativeMode ? 2 : 1].shouldRender = false;
else {
menus[creativeMode ? 2 : 1] = descriptionMenuBuilder.setEntries(StringEntry.useLines(Color.WHITE, itemDescription.split("\n")))
.createMenu(); // This resizes menu
}

super.render(screen);

// Searcher help text
String text = Localization.getLocalized("minicraft.displays.player_inv.display.help", Game.input.getMapping("SEARCHER-BAR"));

Font.draw(text, screen, 12, Screen.h/ 2 + 8, Color.WHITE);
Font.draw(text, screen, selection == 0 ? 12 : Screen.w - 12 - Font.textWidth(text), menus[creativeMode ? 2 : 1].getBounds().getBottom() + 8, Color.WHITE);

if (onScreenKeyboardMenu != null)
onScreenKeyboardMenu.render(screen);
Expand All @@ -183,8 +212,10 @@ protected void onSelectionChange(int oldSel, int newSel) {
int shift = 0;
if(newSel == 0) shift = padding - menus[0].getBounds().getLeft();
if(newSel == 1) shift = (Screen.w - padding) - menus[1].getBounds().getRight();
for(Menu m: menus)
m.translate(shift, 0);
menus[0].translate(shift, 0);
menus[1].translate(shift, 0);
if (newSel == 0) descriptionMenuBuilder.setPositioning(new Point(padding, menus[0].getBounds().getBottom() + 8), RelPos.BOTTOM_RIGHT);
if (newSel == 1) descriptionMenuBuilder.setPositioning(new Point(Screen.w - padding, menus[1].getBounds().getBottom() + 8), RelPos.BOTTOM_LEFT);
}
}

Expand All @@ -197,5 +228,6 @@ private void update() {
}};
menus[1].translate(menus[0].getBounds().getWidth() + padding, 0);
onSelectionChange(0, selection);
itemDescription = getDescription();
}
}

0 comments on commit 33c5083

Please sign in to comment.