-
-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated InventoryListView impl and sample
- Loading branch information
Showing
5 changed files
with
61 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/InventoryListView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
41 changes: 0 additions & 41 deletions
41
fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/view/InventoryListView.kt
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/inventory/view/InventoryView.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters