trying to fix the '720p' bug but faild :/

This commit is contained in:
minjaesong
2020-11-21 17:56:22 +09:00
parent b8a7dee7ac
commit e97a74483f
9 changed files with 25 additions and 23 deletions

View File

@@ -0,0 +1,197 @@
package net.torvald.terrarum.modulebasegame.ui
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.AppLoader
import net.torvald.terrarum.blendNormal
import net.torvald.terrarum.fillRect
import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.modulebasegame.BuildingMaker
import net.torvald.terrarum.modulebasegame.ui.ItemSlotImageFactory.CELLCOLOUR_WHITE
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.ui.UIItemImageButton
import net.torvald.terrarum.ui.UIItemTextButtonList
import net.torvald.terrarum.ui.UIItemTextButtonList.Companion.DEFAULT_BACKGROUNDCOL
import kotlin.math.roundToInt
/**
* Created by minjaesong on 2019-02-14.
*/
class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() {
companion object {
const val TILES_X = 16
const val TILES_Y = 14
const val TILESREGION_SIZE = 24
const val MENUBAR_SIZE = 72
const val SCROLLBAR_SIZE = 24
const val WIDTH = TILES_X * TILESREGION_SIZE + SCROLLBAR_SIZE + MENUBAR_SIZE
const val HEIGHT = TILES_Y * TILESREGION_SIZE
}
override var width = WIDTH
override var height = HEIGHT
override var openCloseTime = 0f
private val palette = Array<UIItemImageButton>(TILES_X * TILES_Y) {
// initialise with terrain blocks
UIItemImageButton(
this, ItemCodex.getItemImage(it),
initialX = MENUBAR_SIZE + (it % 16) * TILESREGION_SIZE,
initialY = (it / 16) * TILESREGION_SIZE,
highlightable = false,
width = TILESREGION_SIZE,
height = TILESREGION_SIZE,
highlightCol = Color.WHITE,
activeCol = Color.WHITE
)
}
private val tabs = UIItemTextButtonList(
this, arrayOf("Terrain", "Wall", "Wire"),
0, 0, textAreaWidth = MENUBAR_SIZE, width = MENUBAR_SIZE,
defaultSelection = 0
)
private val closeButton = UIItemTextButtonList(
this, arrayOf("Close"),
0, this.height - UIItemTextButtonList.DEFAULT_LINE_HEIGHT,
width = MENUBAR_SIZE, textAreaWidth = MENUBAR_SIZE
)
init {
palette.forEachIndexed { index, it ->
uiItems.add(it)
it.clickOnceListener = { _, _, _ ->
parent.setPencilColour(paletteScroll * 16 + index)
}
}
}
override fun updateUI(delta: Float) {
parent.tappedOnUI = true
if (!mouseOnScroll) palette.forEach { it.update(delta) }
tabs.update(delta)
closeButton.update(delta)
if (closeButton.mousePushed) {
closeButton.deselect()
closeGracefully()
}
// respond to click
if (Gdx.input.isButtonPressed(AppLoader.getConfigInt("config_mouseprimary"))) {
// scroll bar
if (relativeMouseX in width - SCROLLBAR_SIZE until width && relativeMouseY in 0 until height) {
mouseOnScroll = true
}
if (mouseOnScroll) {
scrollBarPos = relativeMouseY - (scrollBarHeight / 2).roundToInt()
scrollBarPos = scrollBarPos.coerceIn(0, scrollableArea - 1)
}
}
else {
mouseOnScroll = false
}
// rebuild if necessary
val newPaletteScroll = ((scrollBarPos.toFloat() / scrollableArea) * paletteScrollMax).roundToInt()
if (paletteScroll != newPaletteScroll) {
paletteScroll = newPaletteScroll
rebuildPalette()
}
}
private val scrollbarBackCol = Color(0x000000_70)
private var scrollBarPos = 0
private var paletteScroll = 0
private val paletteScrollMax = 256f - 14f
private val scrollBarHeight = (HEIGHT - 16 * 14).toFloat()
private val scrollableArea = HEIGHT - scrollBarHeight.roundToInt()
private var mouseOnScroll = false
private fun closeGracefully() {
this.isVisible = false
parent.tappedOnUI = true
}
private fun rebuildPalette() {
palette.forEachIndexed { index, it ->
it.image = ItemCodex.getItemImage(paletteScroll * 16 + index)
}
}
override fun renderUI(batch: SpriteBatch, camera: Camera) {
palette.forEach { it.render(batch, camera) }
blendNormal(batch)
// gaps between tabs and close button
batch.color = DEFAULT_BACKGROUNDCOL
batch.fillRect(0f, tabs.height.toFloat(), MENUBAR_SIZE.toFloat(), height.toFloat() - (tabs.height + closeButton.height))
// scrollbar back
batch.color = DEFAULT_BACKGROUNDCOL
batch.fillRect(width - SCROLLBAR_SIZE.toFloat(), 0f, SCROLLBAR_SIZE.toFloat(), height.toFloat())
batch.color = scrollbarBackCol
batch.fillRect(width - SCROLLBAR_SIZE.toFloat(), 0f, SCROLLBAR_SIZE.toFloat(), height.toFloat())
// scrollbar
batch.color = CELLCOLOUR_WHITE
batch.fillRect(width - SCROLLBAR_SIZE.toFloat(), scrollBarPos.toFloat(), SCROLLBAR_SIZE.toFloat(), scrollBarHeight)
// the actual buttons
tabs.render(batch, camera)
closeButton.render(batch, camera)
}
private var dragOriginX = 0 // relative mousepos
private var dragOriginY = 0 // relative mousepos
private var dragForReal = false
private fun mouseOnDragHandle() = relativeMouseX in 0 until MENUBAR_SIZE && relativeMouseY in tabs.height until height - closeButton.height
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
if (mouseOnDragHandle()) {
if (dragForReal) {
handler.setPosition(screenX - dragOriginX, screenY - dragOriginY)
}
}
return super.touchDragged(screenX, screenY, pointer)
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
if (mouseOnDragHandle()) {
dragOriginX = relativeMouseX
dragOriginY = relativeMouseY
dragForReal = true
parent.tappedOnUI = true
}
else {
dragForReal = false
}
return super.touchDown(screenX, screenY, pointer, button)
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return super.touchUp(screenX, screenY, pointer, button)
}
override fun doOpening(delta: Float) {
}
override fun doClosing(delta: Float) {
}
override fun endOpening(delta: Float) {
}
override fun endClosing(delta: Float) {
}
override fun dispose() {
// nothing to dispose; you can't dispose the palette as its image is dynamically assigned
}
}

View File

@@ -0,0 +1,201 @@
package net.torvald.terrarum.modulebasegame.ui
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.*
import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.modulebasegame.BuildingMaker
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.ui.UIItemImageButton
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import org.dyn4j.geometry.Vector2
import kotlin.math.roundToInt
/**
* Created by minjaesong on 2019-02-16.
*/
class UIBuildingMakerPenMenu(val parent: BuildingMaker): UICanvas() {
companion object {
const val SIZE = 330
const val RADIUS = SIZE / 2.0
const val RADIUSF = RADIUS.toFloat()
const val BLOCKS_ROW_RADIUS = 120.0
const val TOOLS_ROW_RADIUS = 56.0
const val BLOCK_BACK_SIZE = 72
const val BLOCK_BACK_RADIUS = BLOCK_BACK_SIZE / 2f
const val ICON_SIZE = 38
const val ICON_SIZEH = ICON_SIZE / 2f
const val CLOSE_BUTTON_SIZE = 48
const val CLOSE_BUTTON_RADIUS = CLOSE_BUTTON_SIZE / 2f
const val PALETTE_SIZE = 10
const val TOOLS_SIZE = 5
}
private val backCol = ItemSlotImageFactory.CELLCOLOUR_BLACK
private val blockCellCol = ItemSlotImageFactory.CELLCOLOUR_WHITE
/** Centre pos. */
private val blockCellPos = Array<Vector2>(PALETTE_SIZE) {
Vector2(0.0, 0.0 - BLOCKS_ROW_RADIUS)
.rotate(Math.PI / 5.0 * it)
.plus(Vector2(RADIUS, RADIUS))
}
private val toolIcons = TextureRegionPack(ModMgr.getGdxFile("basegame", "gui/buildingmaker/penmenu_icons.tga"), 38, 38)
private val toolButtons = Array<UIItemImageButton>(TOOLS_SIZE) {
val newvec = Vector2(TOOLS_ROW_RADIUS, 0.0)
.rotate(Math.PI / 2.5 * it)
.plus(Vector2(RADIUS - ICON_SIZEH, RADIUS - ICON_SIZEH))
UIItemImageButton(
this, toolIcons.get(it, 0),
backgroundCol = Color(0),
highlightBackCol = Color(0),
activeBackCol = Color(0),
initialX = newvec.x.roundToInt(),
initialY = newvec.y.roundToInt(),
width = ICON_SIZE, height = ICON_SIZE,
highlightable = false
)
}
private val toolButtonsJob = arrayOf(
{ parent.currentPenMode = BuildingMaker.PENMODE_MARQUEE },
{ parent.currentPenMode = BuildingMaker.PENMODE_MARQUEE_ERASE },
{
parent.uiPalette.isVisible = true
parent.uiPalette.setPosition(Gdx.input.x - parent.uiPalette.width / 2, Gdx.input.y - parent.uiPalette.height / 2)
parent.uiPalette.posX = parent.uiPalette.posX.coerceIn(0, AppLoader.screenW - parent.uiPalette.width)
parent.uiPalette.posY = parent.uiPalette.posY.coerceIn(0, AppLoader.screenH - parent.uiPalette.height)
},
{
parent.currentPenMode = BuildingMaker.PENMODE_PENCIL_ERASE
parent.currentPenTarget = BuildingMaker.PENTARGET_TERRAIN
},
{
parent.currentPenMode = BuildingMaker.PENMODE_PENCIL_ERASE
parent.currentPenTarget = BuildingMaker.PENTARGET_WALL
}
)
override var width = SIZE
override var height = SIZE
override var openCloseTime = 0f//UIQuickslotBar.COMMON_OPEN_CLOSE
private var mouseVec = Vector2(0.0, 0.0)
private var mouseOnCloseButton = false
private var mouseOnBlocksSlot: Int? = null
init {
// make toolbox work
toolButtons.forEachIndexed { index, button ->
uiItems.add(button)
button.clickOnceListener = { _, _, b ->
if (b == AppLoader.getConfigInt("config_mouseprimary")) {
toolButtonsJob[index].invoke()
closeGracefully()
}
}
}
}
override fun updateUI(delta: Float) {
mouseVec.x = relativeMouseX.toDouble()
mouseVec.y = relativeMouseY.toDouble()
toolButtons.forEach { it.update(delta) }
// determine if cursor is above shits
mouseOnCloseButton = (mouseVec.distanceSquared(RADIUS, RADIUS) <= CLOSE_BUTTON_RADIUS.sqr())
// --> blocks slot
for (i in 0 until PALETTE_SIZE) {
val posVec = blockCellPos[i]
if (mouseVec.distanceSquared(posVec) <= BLOCK_BACK_RADIUS.sqr()) {
mouseOnBlocksSlot = i
break
}
mouseOnBlocksSlot = null
// actually selecting the slot is handled by renderUI()
}
if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
this.isVisible = false
parent.tappedOnUI = false
}
// primary click
if (Gdx.input.isButtonPressed(AppLoader.getConfigInt("config_mouseprimary"))) {
// close by clicking close button or out-of-boud
if (mouseVec.distanceSquared(RADIUS, RADIUS) !in CLOSE_BUTTON_RADIUS.sqr()..RADIUSF.sqr()) {
closeGracefully()
}
}
}
private fun closeGracefully() {
this.isVisible = false
parent.tappedOnUI = true
}
override fun renderUI(batch: SpriteBatch, camera: Camera) {
// draw back
batch.color = backCol
batch.fillCircle(0f, 0f, SIZE.toFloat(), SIZE.toFloat())
// draw blocks slot
batch.color = blockCellCol
val slotConfig = AppLoader.getConfigIntArray("buildingmakerfavs")
for (i in 0 until PALETTE_SIZE) {
val x = blockCellPos[i].x.roundToInt().toFloat()
val y = blockCellPos[i].y.roundToInt().toFloat()
batch.color = blockCellCol
repeat((i == mouseOnBlocksSlot).toInt() + 1) { batch.fillCircle(x - BLOCK_BACK_RADIUS, y - BLOCK_BACK_RADIUS, BLOCK_BACK_SIZE.toFloat(), BLOCK_BACK_SIZE.toFloat()) }
batch.color = Color.WHITE
batch.draw(ItemCodex.getItemImage(slotConfig[i]), x - 16, y - 16, 32f, 32f)
// update as well while looping
if (i == mouseOnBlocksSlot && Gdx.input.isButtonPressed(AppLoader.getConfigInt("config_mouseprimary"))) {
parent.setPencilColour(slotConfig[i])
closeGracefully()
}
}
// draw close button
batch.color = blockCellCol
repeat(mouseOnCloseButton.toInt() + 1) { batch.fillCircle(RADIUSF - CLOSE_BUTTON_RADIUS, RADIUSF - CLOSE_BUTTON_RADIUS, CLOSE_BUTTON_SIZE.toFloat(), CLOSE_BUTTON_SIZE.toFloat()) }
batch.color = if (mouseOnCloseButton) toolButtons[0].activeCol else toolButtons[0].inactiveCol
batch.draw(toolIcons.get(5, 0), RADIUSF - ICON_SIZEH, RADIUSF - ICON_SIZEH)
// draw icons
toolButtons.forEach {
it.render(batch, camera)
}
}
override fun doOpening(delta: Float) {
}
override fun doClosing(delta: Float) {
}
override fun endOpening(delta: Float) {
}
override fun endClosing(delta: Float) {
}
override fun dispose() {
toolIcons.dispose()
}
}