diff --git a/fxgl-gameplay/src/main/java/module-info.java b/fxgl-gameplay/src/main/java/module-info.java index fae6ebf93..cfab86a9c 100644 --- a/fxgl-gameplay/src/main/java/module-info.java +++ b/fxgl-gameplay/src/main/java/module-info.java @@ -18,7 +18,6 @@ exports com.almasb.fxgl.cutscene; exports com.almasb.fxgl.cutscene.dialogue; exports com.almasb.fxgl.inventory; - exports com.almasb.fxgl.inventory.view; exports com.almasb.fxgl.minigames; exports com.almasb.fxgl.minigames.circuitbreaker; exports com.almasb.fxgl.minigames.sweetspot; diff --git a/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/InventoryListView.kt b/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/InventoryListView.kt new file mode 100644 index 000000000..4e615fa14 --- /dev/null +++ b/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/InventoryListView.kt @@ -0,0 +1,56 @@ +/* + * FXGL - JavaFX Game Library. The MIT License (MIT). + * Copyright (c) AlmasB (almaslvl@gmail.com). + * See LICENSE for details. + */ +package com.almasb.fxgl.inventory + +import javafx.beans.binding.Bindings +import javafx.scene.control.ListCell +import javafx.scene.control.ListView +import javafx.scene.paint.Color +import javafx.scene.text.Text +import javafx.util.Callback + +/** + * + * @author Adam Bocco (adambocco@gmail.com) + */ +class InventoryListView(inventory: Inventory) : ListView>() { + + init { + cellFactory = Callback { InventoryListCell(inventory) } + + Bindings.bindContent(items, inventory.itemsProperty()) + } +} + +class InventoryListCell(private val inventory: Inventory) : ListCell>() { + + override fun updateItem(item: ItemStack?, empty: Boolean) { + super.updateItem(item, empty) + if (empty || item == null) { + text = null + graphic = null + + } else { + val itemData = inventory.getData(item.userItem).get() + + val nameBinding = Bindings.createStringBinding({ + val limit = 20 + + if (itemData.name.length > limit) + itemData.name.substring(0, limit) + "..." + else + itemData.name + }, itemData.nameProperty()) + + val label = Text() + label.textProperty().bind(nameBinding.concat(" x").concat(itemData.quantityProperty())) + label.fill = Color.WHITE + + text = null + graphic = label + } + } +} \ No newline at end of file diff --git a/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/view/InventoryListView.kt b/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/view/InventoryListView.kt deleted file mode 100644 index 81193ed4b..000000000 --- a/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/view/InventoryListView.kt +++ /dev/null @@ -1,41 +0,0 @@ -/* - * FXGL - JavaFX Game Library. The MIT License (MIT). - * Copyright (c) AlmasB (almaslvl@gmail.com). - * See LICENSE for details. - */ -package com.almasb.fxgl.inventory.view - -import com.almasb.fxgl.inventory.Inventory -import javafx.collections.ObservableList -import javafx.scene.control.ListCell -import javafx.scene.control.ListView -import javafx.util.Callback - -/** - * - * @author Adam Bocco (adambocco@gmail.com) - */ -class InventoryListView (items: ObservableList, inventory: Inventory) : ListView(items) { - - init { - cellFactory = Callback { InventoryListCell(inventory) } - } -} - -class InventoryListCell(private val inventory: Inventory) : ListCell() { - - override fun updateItem(item: T?, empty: Boolean) { - super.updateItem(item, empty) - if (empty || item == null) { - text = null - graphic = null - - } else { - val itemData = inventory.getData(item).get() - val limit = 30 - val name = if (itemData.name.length > limit) itemData.name.substring(0, limit) + "..." else itemData.name - - text = "$name - ${itemData.quantity} \n${itemData.description}" - } - } -} \ No newline at end of file diff --git a/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/view/InventoryView.kt b/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/view/InventoryView.kt deleted file mode 100644 index 26655da21..000000000 --- a/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/view/InventoryView.kt +++ /dev/null @@ -1,32 +0,0 @@ -/* - * FXGL - JavaFX Game Library. The MIT License (MIT). - * Copyright (c) AlmasB (almaslvl@gmail.com). - * See LICENSE for details. - */ -package com.almasb.fxgl.inventory.view - -import com.almasb.fxgl.inventory.Inventory -import javafx.collections.FXCollections -import javafx.scene.Parent -import javafx.scene.layout.VBox - -/** - * - * @author Adam Bocco (adambocco@gmail.com) - */ -class InventoryView(inventory: Inventory) : Parent(){ - private val box: VBox = VBox(5.0) - val listView: InventoryListView - - init { - - box.translateX = 25.0 - box.translateY = 25.0 - - listView = InventoryListView(FXCollections.observableArrayList(inventory.itemsProperty().map { it.userItem }), inventory) - listView.translateX = 10.0 - listView.translateY = 25.0 - - children.addAll(box, listView) - } -} \ No newline at end of file diff --git a/fxgl-samples/src/main/java/sandbox/inventory/InventorySample.java b/fxgl-samples/src/main/java/sandbox/inventory/InventorySample.java index f0e162fa5..e3b7c2ba4 100644 --- a/fxgl-samples/src/main/java/sandbox/inventory/InventorySample.java +++ b/fxgl-samples/src/main/java/sandbox/inventory/InventorySample.java @@ -7,13 +7,10 @@ import com.almasb.fxgl.app.GameApplication; import com.almasb.fxgl.app.GameSettings; -import com.almasb.fxgl.entity.Entity; -import com.almasb.fxgl.input.UserAction; import com.almasb.fxgl.inventory.Inventory; -import com.almasb.fxgl.inventory.view.InventoryView; -import com.almasb.fxgl.scene.SubScene; +import com.almasb.fxgl.inventory.InventoryListView; +import com.almasb.fxgl.inventory.ItemConfig; import javafx.scene.control.Button; -import javafx.scene.input.KeyCode; import javafx.scene.layout.VBox; import java.util.List; @@ -47,7 +44,7 @@ protected void initGame() { var wood = new CustomItem("Wood"); var stone = new CustomItem("Stone"); - var crystal = new CustomItem("Crystal"); + var crystal = new CustomItem("Crystal that has a really long name going beyond the limit of 20"); var vbox = new VBox(5); @@ -57,83 +54,21 @@ protected void initGame() { var btnRemove = new Button("Drop " + item.description); btnAdd.setOnAction(e -> { - var scene = getSceneService().getGameScene(); - - System.out.println(scene); - - getSceneService().getMainMenuScene().ifPresent(scene2 -> { - System.out.println(scene2); - }); - - inventory.add(item); - - System.out.println(inventory.getAllData()); + inventory.add(item, new ItemConfig(item.description), +1); }); btnRemove.setOnAction(e -> { inventory.incrementQuantity(item, -1); - - System.out.println(inventory.getAllData()); }); vbox.getChildren().addAll(btnAdd, btnRemove); }); addUINode(vbox, 100, 100); - addUINode(new InventoryView<>(inventory), 400, 100); + addUINode(new InventoryListView<>(inventory), 340, 70); } public static void main(String[] args) { launch(args); } - -// -// private class InventorySubScene extends SubScene { -// -// public Inventory playerInventory = new Inventory(10); -// -// public InventoryView view = new InventoryView<>(playerInventory); -// -// -// public InventorySubScene() { -// getContentRoot().getChildren().addAll(view); -// getContentRoot().setTranslateX(300); -// getContentRoot().setTranslateY(0); -// -// Button dropOne = getUIFactoryService().newButton("Drop One"); -// dropOne.prefHeight(30.0); -// dropOne.prefWidth(135.0); -// dropOne.setTranslateX(35.0); -// dropOne.setTranslateY(320.0); -// -// dropOne.setOnAction(actionEvent -> { -// var selectedItem = (Entity) view.getListView().getSelectionModel().getSelectedItem(); -// -// if (selectedItem != null) { -// var item = inventorySubScene.playerInventory.getData((Entity) selectedItem).get().getUserItem(); -// playerInventory.incrementQuantity(item, -1); -// } -// view.getListView().refresh(); -// }); -// -// Button dropAll = getUIFactoryService().newButton("Drop All"); -// dropAll.prefHeight(30.0); -// dropAll.prefWidth(135.0); -// dropAll.setTranslateX(35.0); -// dropAll.setTranslateY(370.0); -// -// dropAll.setOnAction(actionEvent -> { -// -// var selectedItem = (Entity) view.getListView().getSelectionModel().getSelectedItem(); -// -// if (selectedItem != null) { -// var itemData = inventorySubScene.playerInventory.getData((Entity) selectedItem).get().getUserItem(); -// playerInventory.remove(selectedItem); -// } -// view.getListView().refresh(); -// }); -// -// this.getContentRoot().getChildren().addAll(dropOne, dropAll); -// } -// } } \ No newline at end of file