Skip to content

Commit

Permalink
updated InventoryListView impl and sample
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Mar 26, 2024
1 parent ace11d1 commit 27adeae
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 144 deletions.
1 change: 0 additions & 1 deletion fxgl-gameplay/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T>(inventory: Inventory<T>) : ListView<ItemStack<T>>() {

init {
cellFactory = Callback { InventoryListCell(inventory) }

Bindings.bindContent(items, inventory.itemsProperty())
}
}

class InventoryListCell<T>(private val inventory: Inventory<T>) : ListCell<ItemStack<T>>() {

override fun updateItem(item: ItemStack<T>?, 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
}
}
}

This file was deleted.

This file was deleted.

75 changes: 5 additions & 70 deletions fxgl-samples/src/main/java/sandbox/inventory/InventorySample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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<Entity> 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);
// }
// }
}

0 comments on commit 27adeae

Please sign in to comment.