buildingmaker palette

This commit is contained in:
minjaesong
2023-10-15 12:39:04 +09:00
parent 1af172d354
commit 20f2f2f7b1
3 changed files with 47 additions and 10 deletions

View File

@@ -18,7 +18,7 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
companion object {
const val TILES_X = 8
const val TILES_Y = 8
const val TILES_Y = 10
const val TILESREGION_SIZE = 24
const val MENUBAR_SIZE = 72
@@ -37,7 +37,7 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
// TODO scrolling of the palette, as the old method flat out won't work with The Flattening
private val tabs = UIItemTextButtonList(
this, 36, arrayOf("Terrain", "Wall", "Wire"),
this, 36, arrayOf("Terrain", "Wall", "Wire"), // TODO use inventory icons
0, 0, textAreaWidth = MENUBAR_SIZE, width = MENUBAR_SIZE,
defaultSelection = 0,
backgroundCol = UIItemTextButtonList.DEFAULT_BACKGROUNDCOL
@@ -49,6 +49,14 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
backgroundCol = UIItemTextButtonList.DEFAULT_BACKGROUNDCOL
)
private var scroll = 0
private val <E> List<E>.visible: List<E>
get() = this.subList(
TILES_X * scroll,
(TILES_X * scroll + TILES_X * TILES_Y).coerceAtMost(this.size)
)
init {
BlockCodex.getAll().filter {
@@ -58,7 +66,7 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
catch (e: NullPointerException) {
null
}) != null
}.filter { !it.hasTag("INTERNAL") }.sortedBy { it.id }.forEachIndexed { index, prop ->
}.filter { !it.hasTag("INTERNAL") }.sortedBy { it.id }.forEachIndexed { index, prop -> // FIXME no lexicographic sorting
val paletteItem = UIItemImageButton(
this, ItemCodex.getItemImage(prop.id)!!,
initialX = MENUBAR_SIZE + (index % TILES_X) * TILESREGION_SIZE,
@@ -67,14 +75,14 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
width = TILESREGION_SIZE,
height = TILESREGION_SIZE,
highlightCol = Color.WHITE,
activeCol = Color.WHITE
activeCol = Color.WHITE,
backgroundCol = Color(0)
)
paletteItem.clickOnceListener = { _, _ ->
parent.setPencilColour(prop.id)
}
uiItems.add(paletteItem)
palette.add(paletteItem)
}
}
@@ -82,13 +90,13 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
private val scrollOverflowSize = ((palette.size - TILES_X * TILES_Y).toDouble() / TILES_X).ceilToDouble().coerceAtLeast(0.0)
private val scrollBar = UIItemVertSlider(
this, WIDTH - UIItemVertSlider.WIDTH, 0, 0.0, 0.0, scrollOverflowSize, height, SCROLLBAR_SIZE
this, WIDTH - UIItemVertSlider.WIDTH, 0, 0.0, 0.0, scrollOverflowSize, height, (height * TILES_Y / (scrollOverflowSize + TILES_Y)).ceilToInt()
).also {
println("scrollOverflowSize = $scrollOverflowSize")
addUIitem(it)
it.scrolledListener = { scrollX, scrollY ->
it.selectionChangeListener = { value ->
scroll = value.roundToInt()
}
}
@@ -102,6 +110,10 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
closeGracefully()
}
palette.visible.forEach {
it.update(delta)
}
uiItems.forEach { it.update(delta) }
}
@@ -117,6 +129,10 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
override fun renderUI(batch: SpriteBatch, camera: OrthographicCamera) {
blendNormalStraightAlpha(batch)
// border
batch.color = Toolkit.Theme.COL_INACTIVE
Toolkit.drawBoxBorder(batch, -1, -1, width + 2, height + 2)
// background
batch.color = DEFAULT_BACKGROUNDCOL
Toolkit.fillArea(batch, 0, 0, width, height)
@@ -129,6 +145,10 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
tabs.render(batch, camera)
closeButton.render(batch, camera)
palette.visible.forEach {
it.render(batch, camera, 0, -scroll * TILESREGION_SIZE)
}
uiItems.forEach { it.render(batch, camera) }
}
@@ -162,6 +182,10 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
dragForReal = false
}
palette.visible.forEach {
it.touchDown(screenX, screenY, pointer, button)
}
return super.touchDown(screenX, screenY, pointer, button)
}
@@ -169,6 +193,12 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
return super.touchUp(screenX, screenY, pointer, button)
}
override fun scrolled(amountX: Float, amountY: Float): Boolean {
scrollBar.scrolledForce(amountX, amountY)
return super.scrolled(amountX, amountY)
}
override fun doOpening(delta: Float) {
}

View File

@@ -54,7 +54,10 @@ open class UIItemImageButton(
var highlighted = false
var extraDrawOp: (UIItem, SpriteBatch) -> Unit = { _,_ -> }
override fun render(batch: SpriteBatch, camera: OrthographicCamera) {
fun render(batch: SpriteBatch, camera: OrthographicCamera, offX: Int, offY: Int) {
val posX = this.posX + offX
val posY = this.posY + offY
// draw background
if (highlighted) {
BlendMode.resolve(highlightBackBlendMode, batch)
@@ -90,6 +93,10 @@ open class UIItemImageButton(
extraDrawOp(this, batch)
}
override fun render(batch: SpriteBatch, camera: OrthographicCamera) {
render(batch, camera, 0, 0)
}
override fun dispose() {
image.texture.dispose()
}

View File

@@ -31,7 +31,7 @@ class UIItemVertSlider(
private var mouseOnHandle = false
private val handleTravelDist = height - handleHeight
private var handlePos = (initialValue / max).times(handleTravelDist).coerceIn(0.0, handleTravelDist.toDouble())
private var handlePos = if (max == 0.0) 0.0 else (initialValue / max).times(handleTravelDist).coerceIn(0.0, handleTravelDist.toDouble())
var value: Double = initialValue; private set
var selectionChangeListener: (Double) -> Unit = {}