From e0e90cdd1e030e433e51b5ac5dd98b0bac65ef59 Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 30 Sep 2024 16:00:58 -0400 Subject: [PATCH] toolbar scrolling --- docs/src/progress/renderertodo.md | 1 + net/inventory.json | 7 ++++ .../controls/ControlHandler.java | 20 +++++++++- .../entity/state/attack/ClientAttackTree.java | 2 +- .../state/equip/ClientToolbarState.java | 17 +++++++++ .../state/equip/ServerToolbarState.java | 14 +++++++ .../state/inventory/ClientInventoryState.java | 36 +++++++++++++++++- .../state/inventory/ServerInventoryState.java | 4 ++ .../client/protocol/InventoryProtocol.java | 38 +++---------------- .../parser/net/message/InventoryMessage.java | 32 ++++++++++++++++ .../parser/net/message/NetworkMessage.java | 5 +++ .../net/parser/net/message/TypeBytes.java | 4 +- .../server/protocol/InventoryProtocol.java | 6 +++ 13 files changed, 148 insertions(+), 38 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 9845b935..80ddf74d 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -856,6 +856,7 @@ Filter toolbar slots out of equip menu (09/30/2024) Fix attack tree checks Disable client equip tests until can review +Toolbar scrolling # TODO diff --git a/net/inventory.json b/net/inventory.json index dcb74ac5..5dbbf5e2 100644 --- a/net/inventory.json +++ b/net/inventory.json @@ -112,6 +112,13 @@ "entityId" ] }, + { + "messageName" : "clientUpdateToolbar", + "description" : "Updates the server on the selected toolbar option", + "data" : [ + "toolbarId" + ] + }, { "messageName" : "clientRequestPerformItemAction", "description" : "Requests that the server have the entity perform its equipped item's action for the given equip point", diff --git a/src/main/java/electrosphere/controls/ControlHandler.java b/src/main/java/electrosphere/controls/ControlHandler.java index 910ef0d8..1d78d354 100644 --- a/src/main/java/electrosphere/controls/ControlHandler.java +++ b/src/main/java/electrosphere/controls/ControlHandler.java @@ -90,9 +90,8 @@ import electrosphere.engine.Main; import electrosphere.engine.assetmanager.AssetDataStrings; import electrosphere.entity.Entity; import electrosphere.entity.btree.BehaviorTree; -import electrosphere.entity.state.attack.ClientAttackTree; -import electrosphere.entity.state.attack.ShooterTree; import electrosphere.entity.state.equip.ClientEquipState; +import electrosphere.entity.state.equip.ClientToolbarState; import electrosphere.entity.state.inventory.InventoryUtils; import electrosphere.entity.state.inventory.UnrelationalInventoryState; import electrosphere.entity.state.movement.groundmove.ClientGroundMovementTree; @@ -140,6 +139,7 @@ public class ControlHandler { public static final String ITEM_SECONDARY = "actionItemSecondary"; public static final String INPUT_CODE_PLACE_TERRAIN = "placeTerrain"; public static final String INPUT_CODE_REMOVE_TERRAIN = "removeTerrain"; + public static final String TOOLBAR_SCROLL = "toolbarScroll"; //menu navigation @@ -327,6 +327,7 @@ public class ControlHandler { handler.addControl(INPUT_CODE_DROP, new Control(ControlType.KEY,GLFW_KEY_Y,false,"Drop","Drops the currently equipped item")); handler.addControl(INPUT_CODE_INVENTORY_OPEN, new Control(ControlType.KEY,GLFW.GLFW_KEY_TAB,false,"Inventory","Opens the player's inventory")); handler.addControl(ITEM_SECONDARY, new Control(ControlType.MOUSE_BUTTON,GLFW_MOUSE_BUTTON_RIGHT,false,"Secondary","Uses the secondary equipped item")); + handler.addControl(TOOLBAR_SCROLL, new Control(ControlType.MOUSE_SCROLL,0,false,"","")); /* Map the menu navigation controls @@ -883,6 +884,21 @@ public class ControlHandler { controls.get(ITEM_SECONDARY).setOnRelease(new ControlMethod() {public void execute() { ItemActions.releaseSecondaryItemAction(); }}); + + /** + * Toolbar scrolling + */ + mainGameControlList.add(controls.get(TOOLBAR_SCROLL)); + controls.get(TOOLBAR_SCROLL).setOnScroll(new Control.ScrollCallback() {public void execute(ScrollEvent scrollEvent){ + if(Globals.playerEntity != null && ClientToolbarState.getClientToolbarState(Globals.playerEntity) != null){ + ClientToolbarState clientToolbarState = ClientToolbarState.getClientToolbarState(Globals.playerEntity); + if(scrollEvent.getScrollAmount() > 0){ + clientToolbarState.attemptChangeSelection(clientToolbarState.getSelectedSlot() + 1); + } else { + clientToolbarState.attemptChangeSelection(clientToolbarState.getSelectedSlot() - 1); + } + } + }}); /* Interact diff --git a/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java b/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java index e6666f8e..7e1bbb1a 100644 --- a/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java +++ b/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java @@ -405,7 +405,7 @@ public class ClientAttackTree implements BehaviorTree { if(ClientToolbarState.getClientToolbarState(parent) != null){ ClientToolbarState clientToolbarState = ClientToolbarState.getClientToolbarState(parent); Entity item = clientToolbarState.getCurrentPrimaryItem(); - if(ItemUtils.isWeapon(item)){ + if(item != null && ItemUtils.isWeapon(item)){ currentWeapon = item; switch(ItemUtils.getWeaponClass(item)){ case "sword1h": diff --git a/src/main/java/electrosphere/entity/state/equip/ClientToolbarState.java b/src/main/java/electrosphere/entity/state/equip/ClientToolbarState.java index 0070abe7..bad28868 100644 --- a/src/main/java/electrosphere/entity/state/equip/ClientToolbarState.java +++ b/src/main/java/electrosphere/entity/state/equip/ClientToolbarState.java @@ -38,6 +38,11 @@ import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree; */ @SynchronizedBehaviorTree(name = "clientToolbarState", isServer = false, correspondingTree="serverToolbarState") public class ClientToolbarState implements BehaviorTree { + + /** + * The maximum number of toolbar slots + */ + public static final int MAX_TOOLBAR_SIZE = 10; /** * The selected toolbar slot @@ -237,6 +242,18 @@ public class ClientToolbarState implements BehaviorTree { public Entity getCurrentPrimaryItem(){ return this.equippedEntity; } + + /** + * Attempts to change the selection to a new value + * @param value The value + */ + public void attemptChangeSelection(int value){ + while(value < 0){ + value = value + MAX_TOOLBAR_SIZE; + } + value = value % MAX_TOOLBAR_SIZE; + Globals.clientConnection.queueOutgoingMessage(InventoryMessage.constructclientUpdateToolbarMessage(value)); + } /** diff --git a/src/main/java/electrosphere/entity/state/equip/ServerToolbarState.java b/src/main/java/electrosphere/entity/state/equip/ServerToolbarState.java index 6bc1b930..d598c741 100644 --- a/src/main/java/electrosphere/entity/state/equip/ServerToolbarState.java +++ b/src/main/java/electrosphere/entity/state/equip/ServerToolbarState.java @@ -295,6 +295,20 @@ public class ServerToolbarState implements BehaviorTree { return realWorldItem; } + /** + * Attempts to change the selection to a new value + * @param value The value + */ + public void attemptChangeSelection(int value){ + while(value < 0){ + value = value + ClientToolbarState.MAX_TOOLBAR_SIZE; + } + value = value % ClientToolbarState.MAX_TOOLBAR_SIZE; + this.removeVisuals(); + this.setSelectedSlot(value); + this.visuallyEquipCurrentSlot(); + } + /** *

(initially) Automatically generated

*

diff --git a/src/main/java/electrosphere/entity/state/inventory/ClientInventoryState.java b/src/main/java/electrosphere/entity/state/inventory/ClientInventoryState.java index a7efac4f..e29d9049 100644 --- a/src/main/java/electrosphere/entity/state/inventory/ClientInventoryState.java +++ b/src/main/java/electrosphere/entity/state/inventory/ClientInventoryState.java @@ -9,6 +9,7 @@ import electrosphere.entity.Entity; import electrosphere.entity.btree.BehaviorTree; import electrosphere.entity.state.equip.ClientEquipState; import electrosphere.entity.state.equip.ClientToolbarState; +import electrosphere.game.data.creature.type.equip.EquipPoint; import electrosphere.net.parser.net.message.InventoryMessage; import electrosphere.net.server.protocol.InventoryProtocol; @@ -143,12 +144,45 @@ public class ClientInventoryState implements BehaviorTree { } break; } } break; + case SERVERCOMMANDEQUIPITEM: { + //translate equipper id + Entity equipper = Globals.clientSceneWrapper.getEntityFromServerId(message.getequipperId()); + //spawn in world id + Entity inWorldEntity = Globals.clientSceneWrapper.getEntityFromServerId(message.getentityId()); + if(inWorldEntity != null){ + //translate id + Globals.clientSceneWrapper.mapIdToId(inWorldEntity.getId(), message.getentityId()); + switch(message.getcontainerType()){ + case electrosphere.net.server.protocol.InventoryProtocol.INVENTORY_TYPE_NATURAL: { + throw new UnsupportedOperationException("unsupported!"); + } + case electrosphere.net.server.protocol.InventoryProtocol.INVENTORY_TYPE_EQUIP: { + //grab equip state + ClientEquipState equipState = ClientEquipState.getEquipState(equipper); + //create entity from template in message + //get equippoint + String equipPointName = message.getequipPointId(); + EquipPoint equipPoint = equipState.getEquipPoint(equipPointName); + //attach + equipState.attemptEquip(inWorldEntity, equipPoint); + } + case electrosphere.net.server.protocol.InventoryProtocol.INVENTORY_TYPE_TOOLBAR: { + //grab toolbar state + ClientToolbarState toolbarState = ClientToolbarState.getClientToolbarState(equipper); + //attach + toolbarState.attemptEquip(inWorldEntity); + } + } + } else { + throw new UnsupportedOperationException("todo"); + } + } break; + case CLIENTUPDATETOOLBAR: case CLIENTREQUESTADDNATURAL: case CLIENTREQUESTADDTOOLBAR: case CLIENTREQUESTEQUIPITEM: case CLIENTREQUESTUNEQUIPITEM: case CLIENTREQUESTPERFORMITEMACTION: - case SERVERCOMMANDEQUIPITEM: break; } } diff --git a/src/main/java/electrosphere/entity/state/inventory/ServerInventoryState.java b/src/main/java/electrosphere/entity/state/inventory/ServerInventoryState.java index 5551466d..aca51814 100644 --- a/src/main/java/electrosphere/entity/state/inventory/ServerInventoryState.java +++ b/src/main/java/electrosphere/entity/state/inventory/ServerInventoryState.java @@ -72,6 +72,10 @@ public class ServerInventoryState implements BehaviorTree { ServerToolbarState serverToolbarState = ServerToolbarState.getServerToolbarState(parent); serverToolbarState.attemptEquip(itemEnt, message.gettoolbarId()); } break; + case CLIENTUPDATETOOLBAR: { + ServerToolbarState serverToolbarState = ServerToolbarState.getServerToolbarState(parent); + serverToolbarState.attemptChangeSelection(message.gettoolbarId()); + } break; case CLIENTREQUESTPERFORMITEMACTION: case SERVERCOMMANDUNEQUIPITEM: case SERVERCOMMANDEQUIPITEM: diff --git a/src/main/java/electrosphere/net/client/protocol/InventoryProtocol.java b/src/main/java/electrosphere/net/client/protocol/InventoryProtocol.java index 088fbf86..77d31d8e 100644 --- a/src/main/java/electrosphere/net/client/protocol/InventoryProtocol.java +++ b/src/main/java/electrosphere/net/client/protocol/InventoryProtocol.java @@ -1,12 +1,8 @@ package electrosphere.net.client.protocol; import electrosphere.engine.Globals; -import electrosphere.entity.Entity; -import electrosphere.entity.state.equip.ClientEquipState; -import electrosphere.entity.state.equip.ClientToolbarState; import electrosphere.entity.state.inventory.ClientInventoryState; import electrosphere.entity.state.inventory.InventoryUtils; -import electrosphere.game.data.creature.type.equip.EquipPoint; import electrosphere.logger.LoggerInterface; import electrosphere.net.parser.net.message.InventoryMessage; import electrosphere.net.template.ClientProtocolTemplate; @@ -35,36 +31,11 @@ public class InventoryProtocol implements ClientProtocolTemplate= TypeBytes.INVENTORY_MESSAGE_TYPE_CLIENTUPDATETOOLBAR_SIZE){ + return true; + } else { + return false; + } case TypeBytes.INVENTORY_MESSAGE_TYPE_CLIENTREQUESTPERFORMITEMACTION: return InventoryMessage.canParseclientRequestPerformItemActionMessage(byteBuffer); } @@ -447,6 +454,20 @@ public class InventoryMessage extends NetworkMessage { return rVal; } + public static InventoryMessage parseclientUpdateToolbarMessage(CircularByteBuffer byteBuffer){ + InventoryMessage rVal = new InventoryMessage(InventoryMessageType.CLIENTUPDATETOOLBAR); + stripPacketHeader(byteBuffer); + rVal.settoolbarId(ByteStreamUtils.popIntFromByteQueue(byteBuffer)); + return rVal; + } + + public static InventoryMessage constructclientUpdateToolbarMessage(int toolbarId){ + InventoryMessage rVal = new InventoryMessage(InventoryMessageType.CLIENTUPDATETOOLBAR); + rVal.settoolbarId(toolbarId); + rVal.serialize(); + return rVal; + } + public static boolean canParseclientRequestPerformItemActionMessage(CircularByteBuffer byteBuffer){ int currentStreamLength = byteBuffer.getRemaining(); List temporaryByteQueue = new LinkedList(); @@ -666,6 +687,17 @@ public class InventoryMessage extends NetworkMessage { rawBytes[2+i] = intValues[i]; } break; + case CLIENTUPDATETOOLBAR: + rawBytes = new byte[2+4]; + //message header + rawBytes[0] = TypeBytes.MESSAGE_TYPE_INVENTORY; + //entity messaage header + rawBytes[1] = TypeBytes.INVENTORY_MESSAGE_TYPE_CLIENTUPDATETOOLBAR; + intValues = ByteStreamUtils.serializeIntToBytes(toolbarId); + for(int i = 0; i < 4; i++){ + rawBytes[2+i] = intValues[i]; + } + break; case CLIENTREQUESTPERFORMITEMACTION: rawBytes = new byte[2+4+equipPointId.length()+4+4]; //message header diff --git a/src/main/java/electrosphere/net/parser/net/message/NetworkMessage.java b/src/main/java/electrosphere/net/parser/net/message/NetworkMessage.java index 5d4262c7..9101878d 100644 --- a/src/main/java/electrosphere/net/parser/net/message/NetworkMessage.java +++ b/src/main/java/electrosphere/net/parser/net/message/NetworkMessage.java @@ -322,6 +322,11 @@ COMBAT_MESSAGE, rVal = InventoryMessage.parseclientRequestAddNaturalMessage(byteBuffer); } break; + case TypeBytes.INVENTORY_MESSAGE_TYPE_CLIENTUPDATETOOLBAR: + if(InventoryMessage.canParseMessage(byteBuffer,secondByte)){ + rVal = InventoryMessage.parseclientUpdateToolbarMessage(byteBuffer); + } + break; case TypeBytes.INVENTORY_MESSAGE_TYPE_CLIENTREQUESTPERFORMITEMACTION: if(InventoryMessage.canParseMessage(byteBuffer,secondByte)){ rVal = InventoryMessage.parseclientRequestPerformItemActionMessage(byteBuffer); diff --git a/src/main/java/electrosphere/net/parser/net/message/TypeBytes.java b/src/main/java/electrosphere/net/parser/net/message/TypeBytes.java index 9abe1e1e..caf8e007 100644 --- a/src/main/java/electrosphere/net/parser/net/message/TypeBytes.java +++ b/src/main/java/electrosphere/net/parser/net/message/TypeBytes.java @@ -138,13 +138,15 @@ Message categories public static final byte INVENTORY_MESSAGE_TYPE_CLIENTREQUESTUNEQUIPITEM = 6; public static final byte INVENTORY_MESSAGE_TYPE_CLIENTREQUESTADDTOOLBAR = 7; public static final byte INVENTORY_MESSAGE_TYPE_CLIENTREQUESTADDNATURAL = 8; - public static final byte INVENTORY_MESSAGE_TYPE_CLIENTREQUESTPERFORMITEMACTION = 9; + public static final byte INVENTORY_MESSAGE_TYPE_CLIENTUPDATETOOLBAR = 9; + public static final byte INVENTORY_MESSAGE_TYPE_CLIENTREQUESTPERFORMITEMACTION = 10; /* Inventory packet sizes */ public static final byte INVENTORY_MESSAGE_TYPE_REMOVEITEMFROMINVENTORY_SIZE = 6; public static final byte INVENTORY_MESSAGE_TYPE_CLIENTREQUESTADDTOOLBAR_SIZE = 10; public static final byte INVENTORY_MESSAGE_TYPE_CLIENTREQUESTADDNATURAL_SIZE = 6; + public static final byte INVENTORY_MESSAGE_TYPE_CLIENTUPDATETOOLBAR_SIZE = 6; /* Synchronization subcategories */ diff --git a/src/main/java/electrosphere/net/server/protocol/InventoryProtocol.java b/src/main/java/electrosphere/net/server/protocol/InventoryProtocol.java index 0dd1c9ad..102deddb 100644 --- a/src/main/java/electrosphere/net/server/protocol/InventoryProtocol.java +++ b/src/main/java/electrosphere/net/server/protocol/InventoryProtocol.java @@ -74,6 +74,12 @@ public class InventoryProtocol implements ServerProtocolTemplate