Working on item management

This commit is contained in:
OLEGSHA 2021-08-28 17:50:45 +03:00
parent 00ea4a6281
commit 2fe84dc59e
Signed by: OLEGSHA
GPG Key ID: E57A4B08D64AFF7A
6 changed files with 150 additions and 64 deletions

View File

@ -20,20 +20,21 @@ package ru.windcorp.progressia.test.inv;
import glm.vec._2.i.Vec2i;
import ru.windcorp.progressia.client.graphics.gui.Button;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutFill;
import ru.windcorp.progressia.common.world.item.ItemContainer;
import ru.windcorp.progressia.common.world.item.ItemSlot;
public class DecoratedSlotComponent extends Button {
private final SlotComponent slotComponent;
public DecoratedSlotComponent(String name, ItemSlot slot) {
public DecoratedSlotComponent(String name, ItemContainer container, int index) {
super(name, null, null);
this.slotComponent = new SlotComponent(name, slot);
this.slotComponent = new SlotComponent(name, container, index);
Vec2i size = slotComponent.getPreferredSize().add(2 * BORDER);
setPreferredSize(size);
addChild(new SlotComponent(name, slot));
addChild(this.slotComponent);
setLayout(new LayoutFill(MARGIN));
}

View File

@ -17,35 +17,16 @@
*/
package ru.windcorp.progressia.test.inv;
import glm.vec._2.i.Vec2i;
import java.util.Collection;
import ru.windcorp.progressia.client.graphics.gui.Component;
import ru.windcorp.progressia.client.graphics.gui.Group;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutBorderHorizontal;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutGrid;
import ru.windcorp.progressia.common.world.item.ItemContainer;
import ru.windcorp.progressia.common.world.item.ItemSlot;
public class InventoryComponent extends Component {
public abstract class InventoryComponent extends Component {
private final Group slots = new Group("Inventory.Slots", new LayoutGrid(15));
public InventoryComponent(ItemContainer container) {
super("Inventory");
setLayout(new LayoutBorderHorizontal(15));
addChild(slots.setLayoutHint(LayoutBorderHorizontal.CENTER));
container.forEach(this::addSlot);
public InventoryComponent(String name) {
super(name);
}
private void addSlot(ItemSlot slot) {
final int maxX = 6;
int i = slots.getChildren().size();
DecoratedSlotComponent component = new DecoratedSlotComponent("Inventory.Slot" + i, slot);
Vec2i pos = new Vec2i(i % maxX, i / maxX);
slots.addChild(component.setLayoutHint(pos));
}
public abstract Collection<DecoratedSlotComponent> getSlots();
}

View File

@ -17,10 +17,15 @@
*/
package ru.windcorp.progressia.test.inv;
import java.util.function.Consumer;
import org.lwjgl.glfw.GLFW;
import glm.vec._2.i.Vec2i;
import ru.windcorp.progressia.client.graphics.Colors;
import ru.windcorp.progressia.client.graphics.backend.InputTracker;
import ru.windcorp.progressia.client.graphics.flat.RenderTarget;
import ru.windcorp.progressia.client.graphics.gui.BasicButton;
import ru.windcorp.progressia.client.graphics.gui.Component;
import ru.windcorp.progressia.client.graphics.gui.Components;
import ru.windcorp.progressia.client.graphics.gui.Group;
@ -30,16 +35,19 @@ import ru.windcorp.progressia.client.graphics.gui.layout.LayoutAlign;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutFill;
import ru.windcorp.progressia.client.graphics.model.Renderable;
import ru.windcorp.progressia.common.world.entity.EntityDataPlayer;
import ru.windcorp.progressia.common.world.item.ItemContainer;
import ru.windcorp.progressia.common.world.item.ItemData;
import ru.windcorp.progressia.common.world.item.ItemSlot;
public class InventoryScreen extends Component {
public static class CursorFollower extends Component {
public CursorFollower(Component child) {
super("CursorFollower");
addChild(child);
setLayout(null);
Vec2i size = child.getPreferredSize();
child.setBounds(-size.x / 2, -size.y / 2, size);
layoutSelf();
@ -55,9 +63,9 @@ public class InventoryScreen extends Component {
(float) InputTracker.getCursorY(),
0
);
renderable.render(renderer);
renderer.popTransform();
});
@ -65,57 +73,89 @@ public class InventoryScreen extends Component {
}
private final Component hands;
private final ItemContainer leftHand;
private final ItemContainer rightHand;
public InventoryScreen(String name, Component mainInventory, EntityDataPlayer player) {
public InventoryScreen(String name, InventoryComponent mainInventory, EntityDataPlayer player) {
super(name);
setLayout(new LayoutFill(0));
addChild(new Panel(name + ".Background", new LayoutAlign(10), Colors.toVector(0x66000000), null));
Panel mainInventoryPanel = new Panel(name + ".Content", new LayoutFill(20));
mainInventoryPanel.addChild(mainInventory);
addChild(Components.center(mainInventoryPanel));
this.hands = createHands(name, player);
addChild(new CursorFollower(this.hands));
this.leftHand = player.getLeftHand();
this.rightHand = player.getRightHand();
addChild(new CursorFollower(createHands(name, leftHand, rightHand)));
addListeners(mainInventory);
}
private Component createHands(String name, EntityDataPlayer player) {
private Component createHands(String name, ItemContainer leftHand, ItemContainer rightHand) {
SlotComponent leftComponent = new SlotComponent(name + ".HandLeft", leftHand, 0);
SlotComponent rightComponent = new SlotComponent(name + ".HandRight", rightHand, 0);
SlotComponent leftHand = new SlotComponent(name + ".HandLeft", player.getLeftHand().getSlot(0));
SlotComponent rightHand = new SlotComponent(name + ".HandRight", player.getRightHand().getSlot(0));
final int gap = 15;
final int offset = 60;
Component hands = new Group(name + ".Hands", null, leftHand, rightHand);
Component hands = new Group(name + ".Hands", null, leftComponent, rightComponent);
hands.setLayout(new Layout() {
@Override
public void layout(Component c) {
Vec2i leftSize = leftHand.getPreferredSize();
Vec2i rightSize = rightHand.getPreferredSize();
leftHand.setBounds(c.getX(), c.getY(), leftSize);
rightHand.setBounds(c.getX() + leftSize.x + gap, c.getY() + offset, rightSize);
Vec2i leftSize = leftComponent.getPreferredSize();
Vec2i rightSize = rightComponent.getPreferredSize();
leftComponent.setBounds(c.getX(), c.getY(), leftSize);
rightComponent.setBounds(c.getX() + leftSize.x + gap, c.getY() + offset, rightSize);
}
@Override
public Vec2i calculatePreferredSize(Component c) {
Vec2i leftSize = leftHand.getPreferredSize();
Vec2i rightSize = rightHand.getPreferredSize();
Vec2i leftSize = leftComponent.getPreferredSize();
Vec2i rightSize = rightComponent.getPreferredSize();
return new Vec2i(
leftSize.x + gap + rightSize.x,
Math.max(leftSize.y + offset, rightSize.y + offset)
);
}
});
return hands;
}
private void addListeners(InventoryComponent mainInventory) {
Consumer<BasicButton> pickIntoLeft = createPickAction(leftHand.getSlot(0), rightHand.getSlot(0));
for (DecoratedSlotComponent component : mainInventory.getSlots()) {
component.addAction(pickIntoLeft);
}
}
private Consumer<BasicButton> createPickAction(ItemSlot toWithoutCtrl, ItemSlot toWithCtrl) {
return button -> {
boolean hasCtrl = InputTracker.isKeyPressed(GLFW.GLFW_KEY_LEFT_CONTROL)
|| InputTracker.isKeyPressed(GLFW.GLFW_KEY_RIGHT_CONTROL);
ItemSlot to = hasCtrl ? toWithCtrl : toWithoutCtrl;
ItemSlot from = ((DecoratedSlotComponent) button).getSlot();
ItemData fromData = from.getContents();
ItemData toData = to.getContents();
from.setContents(toData);
to.setContents(fromData);
requestReassembly();
};
}
}

View File

@ -0,0 +1,60 @@
/*
* Progressia
* Copyright (C) 2020-2021 Wind Corporation and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ru.windcorp.progressia.test.inv;
import java.util.ArrayList;
import java.util.Collection;
import glm.vec._2.i.Vec2i;
import ru.windcorp.progressia.client.graphics.gui.Group;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutBorderHorizontal;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutGrid;
import ru.windcorp.progressia.common.world.item.ItemContainer;
public class SimpleInventoryComponent extends InventoryComponent {
private final Group slots = new Group("Inventory.Slots", new LayoutGrid(15));
private final Collection<DecoratedSlotComponent> slotCollection = new ArrayList<>();
public SimpleInventoryComponent(ItemContainer container) {
super("Inventory");
setLayout(new LayoutBorderHorizontal(15));
addChild(slots.setLayoutHint(LayoutBorderHorizontal.CENTER));
for (int i = 0; i < container.getSlotCount(); ++i) {
addSlot(container, i);
}
}
private void addSlot(ItemContainer container, int index) {
final int maxX = 6;
DecoratedSlotComponent component = new DecoratedSlotComponent("Inventory.Slot." + index, container, index);
Vec2i pos = new Vec2i(index % maxX, index / maxX);
slots.addChild(component.setLayoutHint(pos));
slotCollection.add(component);
}
@Override
public Collection<DecoratedSlotComponent> getSlots() {
return slotCollection;
}
}

View File

@ -25,6 +25,7 @@ import ru.windcorp.progressia.client.graphics.gui.DynamicLabel;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutAlign;
import ru.windcorp.progressia.client.world.item.ItemRenderRegistry;
import ru.windcorp.progressia.client.world.item.ItemRenderable;
import ru.windcorp.progressia.common.world.item.ItemContainer;
import ru.windcorp.progressia.common.world.item.ItemData;
import ru.windcorp.progressia.common.world.item.ItemSlot;
@ -33,15 +34,18 @@ public class SlotComponent extends Component {
static final float TEXTURE_SIZE = 24;
static final float SCALE = 2;
private final ItemSlot slot;
private final ItemContainer container;
private final int index;
private ItemRenderable itemRenderer = null;
private int sizeDisplayInt = 0;
private String sizeDisplayString = "";
public SlotComponent(String name, ItemSlot slot) {
public SlotComponent(String name, ItemContainer container, int index) {
super(name);
this.slot = slot;
this.container = container;
this.index = index;
int side = (int) (TEXTURE_SIZE * SCALE);
setPreferredSize(side, side);
@ -53,7 +57,7 @@ public class SlotComponent extends Component {
}
public ItemSlot getSlot() {
return slot;
return container.getSlot(index);
}
@Override
@ -66,7 +70,7 @@ public class SlotComponent extends Component {
}
private void updateItemRenderer() {
ItemData contents = slot.getContents();
ItemData contents = getSlot().getContents();
if (contents == null) {
itemRenderer = null;

View File

@ -46,7 +46,7 @@ public class TestInventoryGUILayer extends GUILayer {
display = null;
if (container != null) {
display = new InventoryScreen("Screen", new InventoryComponent(container), player);
display = new InventoryScreen("Screen", new SimpleInventoryComponent(container), player);
getRoot().addChild(display);
invalidate();