Fixed issues with inventory hover and picking up inventory items
This commit is contained in:
parent
c60d03428d
commit
af72420f9d
|
|
@ -16,6 +16,7 @@ import projectzombie.entity.player.EntityPlayer;
|
|||
import projectzombie.init.Entities;
|
||||
import projectzombie.init.Items;
|
||||
import projectzombie.init.LayerGenerators;
|
||||
import projectzombie.init.Recipies;
|
||||
import projectzombie.init.Resources;
|
||||
import projectzombie.init.Sounds;
|
||||
import projectzombie.init.Tasks;
|
||||
|
|
@ -68,6 +69,7 @@ public class Main
|
|||
Tasks.init();
|
||||
Tiles.init();
|
||||
LayerGenerators.init();
|
||||
Recipies.init();
|
||||
|
||||
// Create the display
|
||||
window = new DisplayWindow();
|
||||
|
|
|
|||
|
|
@ -11,6 +11,19 @@ public class Recipies
|
|||
{
|
||||
public static ArrayList<Recipe> recipies;
|
||||
|
||||
public static void init()
|
||||
{
|
||||
recipies = new ArrayList<Recipe>();
|
||||
|
||||
recipies.add(new RecipeBasic(
|
||||
new ItemStack[] {
|
||||
new ItemStack(Items.FLINT, 2, (short)0),
|
||||
new ItemStack(Items.PLANT_FIBRE, 5, (short)0),
|
||||
}, new Crafting[] {
|
||||
Crafting.BASIC,
|
||||
}, new ItemStack(Items.AMMO, 99, (short)0)));
|
||||
}
|
||||
|
||||
public static Recipe[] getCraftableRecipies(Crafting tool)
|
||||
{
|
||||
int size = 0;
|
||||
|
|
@ -32,17 +45,4 @@ public class Recipies
|
|||
|
||||
return craftable_recipies;
|
||||
}
|
||||
|
||||
public static void init()
|
||||
{
|
||||
recipies = new ArrayList<Recipe>();
|
||||
|
||||
recipies.add(new RecipeBasic(
|
||||
new ItemStack[] {
|
||||
new ItemStack(Items.FLINT, 2, (short)0),
|
||||
new ItemStack(Items.PLANT_FIBRE, 5, (short)0),
|
||||
}, new Crafting[] {
|
||||
Crafting.BASIC,
|
||||
}, new ItemStack(Items.AMMO, 99, (short)0)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package projectzombie.input.types;
|
|||
import gl_engine.MathHelpers;
|
||||
import gl_engine.vec.Vec2d;
|
||||
import projectzombie.Main;
|
||||
import projectzombie.inventory.Crafting;
|
||||
import projectzombie.menu.MenuGamePause;
|
||||
import projectzombie.menu.MenuInventory;
|
||||
import projectzombie.menu.MenuInventoryBasic;
|
||||
|
|
@ -77,7 +78,7 @@ public class InputGame implements Input
|
|||
|
||||
@Override
|
||||
public void openInventory() {
|
||||
Main.menu = new MenuInventoryBasic(Main.menu);
|
||||
Main.menu = new MenuInventoryBasic(Main.menu, Crafting.BASIC);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ public abstract class MenuInventory extends Menu
|
|||
|
||||
Inventory inventory = Main.player.getInventory();
|
||||
|
||||
itemHolder = new GUIItemHolder();
|
||||
itemHolder = new GUIItemHolder(top_gui);
|
||||
item_slots = new GUIItemSlot[42];
|
||||
armor_slots = new GUIItemSlot[6];
|
||||
|
||||
|
|
@ -235,7 +235,7 @@ public abstract class MenuInventory extends Menu
|
|||
public void update() {
|
||||
super.update();
|
||||
|
||||
top_gui.update(new Vec2d(CursorPosCallback.mx, CursorPosCallback.my));
|
||||
top_gui.update(CursorPosCallback.getCursorPos());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,22 +2,55 @@ package projectzombie.menu;
|
|||
|
||||
import gl_engine.matrix.Matrix4;
|
||||
import gl_engine.vec.Vec2d;
|
||||
import projectzombie.init.Items;
|
||||
import projectzombie.init.Models;
|
||||
import projectzombie.init.Recipies;
|
||||
import projectzombie.inventory.Crafting;
|
||||
import projectzombie.inventory.recipe.Recipe;
|
||||
import projectzombie.menu.gui.GUIContainerSlider;
|
||||
import projectzombie.menu.gui.GUIItemSlot;
|
||||
import projectzombie.menu.gui.GUIItemSlotGetter;
|
||||
import projectzombie.util.math.ItemStack;
|
||||
|
||||
public class MenuInventoryBasic extends MenuInventory
|
||||
{
|
||||
private GUIContainerSlider slider;
|
||||
|
||||
public MenuInventoryBasic(Menu parent) {
|
||||
public MenuInventoryBasic(Menu parent, Crafting tool) {
|
||||
super(parent);
|
||||
|
||||
Recipe[] recipies = Recipies.getCraftableRecipies(tool);
|
||||
|
||||
slider = new GUIContainerSlider(new Vec2d(
|
||||
Models.UI_INVENTORY.getWidth() * 1 / 256.0,
|
||||
Models.UI_INVENTORY.getWidth() * 21 / 256.0,
|
||||
-Models.UI_INVENTORY.getHeight() * 127 / 256.0), new Vec2d(
|
||||
Models.UI_INVENTORY.getWidth() * 254 / 256.0,
|
||||
Models.UI_INVENTORY.getWidth() * 234 / 256.0,
|
||||
Models.UI_INVENTORY.getHeight() * 254 / 256.0), 100);
|
||||
|
||||
for(int i=0;i<recipies.length;i++)
|
||||
{
|
||||
GUIItemSlot slot = new GUIItemSlot(itemHolder, 1.5, true, new GUIItemSlotGetter()
|
||||
{
|
||||
@Override
|
||||
public void setItemStack(ItemStack stack) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAllowed(ItemStack stack) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getItemStack() {
|
||||
return new ItemStack(Items.ACORN, 3, (short)0);
|
||||
}
|
||||
});
|
||||
|
||||
slot.setPos(new Vec2d(4, i));
|
||||
|
||||
slider.add(slot);
|
||||
}
|
||||
|
||||
gui.add(slider);
|
||||
}
|
||||
|
||||
|
|
@ -32,5 +65,4 @@ public class MenuInventoryBasic extends MenuInventory
|
|||
|
||||
super.render();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ public class GUI implements GUIContainer
|
|||
|
||||
@Override
|
||||
public boolean checkMouseHover(Vec2d pos) {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -238,4 +238,28 @@ public class GUI implements GUIContainer
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public GUIItemSlot getHoveringItemSlot(Vec2d mousePos)
|
||||
{
|
||||
for(GUIComponent c : components)
|
||||
{
|
||||
if(c.checkMouseHover(mousePos))
|
||||
{
|
||||
if(c instanceof GUIItemSlot) {
|
||||
return (GUIItemSlot)c;
|
||||
}
|
||||
|
||||
if(c instanceof GUIContainer)
|
||||
{
|
||||
GUIItemSlot slot = ((GUIContainer)c).getHoveringItemSlot(mousePos);
|
||||
|
||||
if(slot != null) {
|
||||
return slot;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -110,4 +110,9 @@ public class GUIButtonGroup implements GUIContainer
|
|||
buttons.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GUIItemSlot getHoveringItemSlot(Vec2d mousePos) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
package projectzombie.menu.gui;
|
||||
|
||||
import gl_engine.vec.Vec2d;
|
||||
|
||||
public interface GUIContainer extends GUIComponent
|
||||
{
|
||||
public GUIItemSlot getHoveringItemSlot(Vec2d mousePos);
|
||||
|
||||
public void add(GUIComponent c);
|
||||
public void clear();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -168,4 +168,35 @@ public class GUIContainerSlider implements GUIComponent, GUIContainer
|
|||
components.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GUIItemSlot getHoveringItemSlot(Vec2d mousePos)
|
||||
{
|
||||
if(mousePos.y < pos.y || mousePos.y > pos.y + size.y) {
|
||||
return null;
|
||||
}
|
||||
|
||||
mousePos = mousePos.add(new Vec2d(0, -scroll));
|
||||
|
||||
for(GUIComponent c : components)
|
||||
{
|
||||
if(c.checkMouseHover(mousePos))
|
||||
{
|
||||
if(c instanceof GUIItemSlot) {
|
||||
return (GUIItemSlot)c;
|
||||
}
|
||||
|
||||
if(c instanceof GUIContainer)
|
||||
{
|
||||
GUIItemSlot slot = ((GUIContainer)c).getHoveringItemSlot(mousePos);
|
||||
|
||||
if(slot != null) {
|
||||
return slot;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,9 +14,10 @@ public class GUIItemHolder implements GUIComponent
|
|||
{
|
||||
private GUIItemSlot hover = null;
|
||||
private ItemStack holding = ItemStack.getEmpty();
|
||||
private GUIContainer gui;
|
||||
|
||||
public void setHover(GUIItemSlot slot) {
|
||||
this.hover = slot;
|
||||
public GUIItemHolder(GUIContainer gui) {
|
||||
this.gui = gui;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -38,14 +39,11 @@ public class GUIItemHolder implements GUIComponent
|
|||
Text.render("" + holding.count, text_matrix);
|
||||
}
|
||||
}
|
||||
|
||||
hover = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Vec2d mousePos) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
hover = gui.getHoveringItemSlot(mousePos);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -89,18 +87,29 @@ public class GUIItemHolder implements GUIComponent
|
|||
}
|
||||
|
||||
// Put 1 item into the slot if the hovered slot is empty or equal to the holding slot
|
||||
else if((hover.isEmpty() && this.hover.isItemAllowed(holding)) || hover.stackEquals(holding))
|
||||
else
|
||||
{
|
||||
hover.count += 1;
|
||||
holding.count -= 1;
|
||||
ItemStack stack_from = hover;
|
||||
ItemStack stack_to = holding;
|
||||
|
||||
hover.item = holding.item;
|
||||
hover.meta = holding.meta;
|
||||
if(!this.hover.isItemAllowed(stack_to)) {
|
||||
stack_from = holding;
|
||||
stack_to = hover;
|
||||
}
|
||||
|
||||
if(
|
||||
stack_from.count + 1 <= stack_from.item.max && stack_to.count - 1 <= stack_to.item.max &&
|
||||
(stack_from.isEmpty() || stack_from.stackEquals(stack_to))
|
||||
) {
|
||||
stack_from.count += 1;
|
||||
stack_to.count -= 1;
|
||||
|
||||
stack_from.item = stack_to.item;
|
||||
stack_from.meta = stack_to.meta;
|
||||
|
||||
this.hover.setItemStack(hover);
|
||||
}
|
||||
|
||||
this.hover = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -128,23 +137,28 @@ public class GUIItemHolder implements GUIComponent
|
|||
// Are these the same item
|
||||
if(holding.stackEquals(hover) && !holding.isEmpty() && !hover.isEmpty())
|
||||
{
|
||||
if(this.hover.isItemAllowed(holding))
|
||||
{
|
||||
ItemStack stack_from = hover;
|
||||
ItemStack stack_to = holding;
|
||||
|
||||
if(!this.hover.isItemAllowed(stack_to)) {
|
||||
stack_from = holding;
|
||||
stack_to = hover;
|
||||
}
|
||||
|
||||
// Merge the hover stack into the holding stack
|
||||
if(holding.count + hover.count > holding.item.max) {
|
||||
int overflow = holding.count + hover.count - holding.item.max;
|
||||
hover.count = holding.item.max;
|
||||
holding.count = overflow;
|
||||
if(stack_to.count + stack_from.count > stack_to.item.max) {
|
||||
int overflow = stack_to.count + stack_from.count - stack_to.item.max;
|
||||
stack_from.count = stack_to.item.max;
|
||||
stack_to.count = overflow;
|
||||
}
|
||||
|
||||
else {
|
||||
hover.count += holding.count;
|
||||
holding.count = 0;
|
||||
stack_from.count += stack_to.count;
|
||||
stack_to.count = 0;
|
||||
}
|
||||
|
||||
this.hover.setItemStack(hover);
|
||||
}
|
||||
}
|
||||
|
||||
else if(holding.isEmpty() || this.hover.isItemAllowed(holding))
|
||||
{
|
||||
|
|
@ -161,8 +175,6 @@ public class GUIItemHolder implements GUIComponent
|
|||
|
||||
this.hover.setItemStack(hover);
|
||||
}
|
||||
|
||||
this.hover = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -71,8 +71,6 @@ public class GUIItemSlot implements GUIComponent
|
|||
|
||||
Models.UI_ITEM_HOVER.setModel(hover_matrix);
|
||||
Models.UI_ITEM_HOVER.render();
|
||||
|
||||
itemHolder.setHover(this);
|
||||
}
|
||||
|
||||
if(renderItem && !stack.isEmpty())
|
||||
|
|
@ -98,16 +96,6 @@ public class GUIItemSlot implements GUIComponent
|
|||
return pos.x > px && pos.x < px + hitboxSize && pos.y > py && pos.y < py + hitboxSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMouseClick(Vec2d pos) {
|
||||
itemHolder.setHover(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRightClick(Vec2d pos) {
|
||||
itemHolder.setHover(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivate() {
|
||||
}
|
||||
|
|
@ -124,4 +112,12 @@ public class GUIItemSlot implements GUIComponent
|
|||
public void onScroll(Vec2d mousePos, double amount) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRightClick(Vec2d mousePos) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMouseClick(Vec2d mousePos) {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,10 +29,14 @@ public class ItemStack implements IBdfClassManager
|
|||
return false;
|
||||
}
|
||||
|
||||
public boolean stackEquals(ItemStack other) {
|
||||
if(other.item != item) return false;
|
||||
if(other.meta != meta) return false;
|
||||
public boolean stackEquals(ItemStack other)
|
||||
{
|
||||
if(other.item != item)
|
||||
return false;
|
||||
if(other.meta == -1 || meta == -1)
|
||||
return true;
|
||||
|
||||
return (other.meta == meta);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Reference in New Issue