DEADSOFTWARE

Refactor input handrling
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / render / windows / CreativeWindowRenderer.kt
index 3480e2783f9fb12c5d26ede452ede14a11da8fc1..4c43600959ee866972f56b68c909c011c0c6b0f4 100644 (file)
@@ -4,120 +4,80 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch
 import com.badlogic.gdx.graphics.glutils.ShapeRenderer
 import com.badlogic.gdx.math.Rectangle
 import ru.deadsoftware.cavedroid.MainConfig
-import ru.deadsoftware.cavedroid.game.GameInput
 import ru.deadsoftware.cavedroid.game.GameItemsHolder
 import ru.deadsoftware.cavedroid.game.GameScope
+import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
 import ru.deadsoftware.cavedroid.game.render.IGameRenderer
 import ru.deadsoftware.cavedroid.game.render.WindowsRenderer
+import ru.deadsoftware.cavedroid.game.windows.GameWindowsConfigs
 import ru.deadsoftware.cavedroid.misc.Assets
 import javax.inject.Inject
+import kotlin.math.min
 
 @GameScope
 class CreativeWindowRenderer @Inject constructor(
     private val mainConfig: MainConfig,
-    private val gameInput: GameInput,
+    private val gameWindowsManager: GameWindowsManager,
     private val gameItemsHolder: GameItemsHolder,
     private val mobsController: MobsController,
-) : IGameRenderer {
+) : AbstractWindowRenderer(), IGameRenderer {
 
     override val renderLayer get() = WindowsRenderer.RENDER_LAYER
 
     private val creativeWindowTexture get() = requireNotNull(Assets.textureRegions[CREATIVE_WINDOW_KEY])
     private val scrollIndicatorTexture get() = requireNotNull(Assets.textureRegions[SCROLL_INDICATOR_KEY])
 
-    private fun drawItemsGrid(spriteBatch: SpriteBatch, gridX: Float, gridY: Float) {
-        val allItems = gameItemsHolder.getAllItems()
-        val startIndex = gameInput.creativeScroll * CreativeWindowConfig.itemsInRow
-        val endIndex = startIndex + CreativeWindowConfig.itemsOnPage
-
-        for (i in startIndex ..< endIndex) {
-            if (i !in allItems.indices) {
-                break
-            }
-            val item = allItems.elementAt(i)
-
-            if (item.isNone()) {
-                continue
-            }
-
-            val gridIndex = i - startIndex
-
-            val itemX = gridX + (gridIndex % CreativeWindowConfig.itemsInRow) * CreativeWindowConfig.itemsGridColWidth
-            val itemY = gridY + (gridIndex / CreativeWindowConfig.itemsInRow) * CreativeWindowConfig.itemsGridRowHeight
-
-            spriteBatch.draw(item.sprite, itemX, itemY)
-        }
-    }
-
-    private fun drawPlayerInventory(spriteBatch: SpriteBatch, inventoryX: Float, inventoryY: Float) {
-        mobsController.player.inventory.asSequence()
-            .map(InventoryItem::item)
-            .forEachIndexed { index, item ->
-                if (item.isNone()) {
-                    return@forEachIndexed
-                }
-
-                val itemX = inventoryX + index * CreativeWindowConfig.itemsGridColWidth
 
-                spriteBatch.draw(item.sprite, itemX, inventoryY)
-            }
-    }
-
-    private fun drawCreative(spriteBatch: SpriteBatch, viewport: Rectangle) {
+    override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) {
         val creativeWindow = creativeWindowTexture
 
         val windowX = viewport.width / 2 - creativeWindow.regionWidth / 2
         val windowY = viewport.height / 2 - creativeWindow.regionHeight / 2
-        val oneScrollAmount = CreativeWindowConfig.scrollIndicatorFullHeight / gameItemsHolder.getCreativeScrollAmount()
+        val oneScrollAmount = GameWindowsConfigs.Creative.scrollIndicatorFullHeight / gameItemsHolder.getMaxCreativeScrollAmount()
 
         spriteBatch.draw(creativeWindow, windowX, windowY)
         spriteBatch.draw(
             /* region = */ scrollIndicatorTexture,
-            /* x = */ windowX + CreativeWindowConfig.scrollIndicatorMarginLeft,
-            /* y = */ windowY + CreativeWindowConfig.scrollIndicatorMarginTop
-                    + (gameInput.creativeScroll * oneScrollAmount)
+            /* x = */ windowX + GameWindowsConfigs.Creative.scrollIndicatorMarginLeft,
+            /* y = */ windowY + GameWindowsConfigs.Creative.scrollIndicatorMarginTop
+                    + (gameWindowsManager.creativeScrollAmount * oneScrollAmount)
         )
 
+        val allItems = gameItemsHolder.getAllItems()
+        val startIndex = gameWindowsManager.creativeScrollAmount * GameWindowsConfigs.Creative.itemsInRow
+        val endIndex = min(startIndex + GameWindowsConfigs.Creative.itemsOnPage, allItems.size)
+        val items = sequence {
+            for (i in startIndex..<endIndex) {
+                yield(allItems.elementAt(i))
+            }
+        }
+
         drawItemsGrid(
             spriteBatch = spriteBatch,
-            gridX = windowX + CreativeWindowConfig.itemsGridMarginLeft,
-            gridY = windowY + CreativeWindowConfig.itemsGridMarginTop
+            shapeRenderer = shapeRenderer,
+            gridX = windowX + GameWindowsConfigs.Creative.itemsGridMarginLeft,
+            gridY = windowY + GameWindowsConfigs.Creative.itemsGridMarginTop,
+            items = items.asIterable(),
+            itemsInRow = GameWindowsConfigs.Creative.itemsInRow,
+            cellWidth = GameWindowsConfigs.Creative.itemsGridColWidth,
+            cellHeight = GameWindowsConfigs.Creative.itemsGridRowHeight,
         )
 
-        drawPlayerInventory(
+        drawItemsGrid(
             spriteBatch = spriteBatch,
-            inventoryX = windowX + CreativeWindowConfig.itemsGridMarginLeft,
-            inventoryY = windowY + creativeWindow.regionHeight - CreativeWindowConfig.playerInventoryOffsetFromBottom
+            shapeRenderer = shapeRenderer,
+            gridX = windowX + GameWindowsConfigs.Creative.itemsGridMarginLeft,
+            gridY = windowY + creativeWindow.regionHeight - GameWindowsConfigs.Creative.playerInventoryOffsetFromBottom,
+            items = mobsController.player.inventory.asSequence().take(GameWindowsConfigs.Creative.invItems).asIterable(),
+            itemsInRow = GameWindowsConfigs.Creative.invItems,
+            cellWidth = GameWindowsConfigs.Creative.itemsGridColWidth,
+            cellHeight = GameWindowsConfigs.Creative.itemsGridRowHeight,
         )
     }
 
-    override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) {
-            drawCreative(spriteBatch, viewport)
-    }
-
     companion object {
         private const val CREATIVE_WINDOW_KEY = "creative"
         private const val SCROLL_INDICATOR_KEY = "handle"
-
-        private data object CreativeWindowConfig {
-            const val scrollIndicatorMarginLeft = 156f
-            const val scrollIndicatorMarginTop = 18f
-            const val scrollIndicatorFullHeight = 72f
-
-            const val itemsGridMarginLeft = 8f
-            const val itemsGridMarginTop = 18f
-
-            const val itemsGridRowHeight = 18f
-            const val itemsGridColWidth = 18f
-
-            const val itemsInRow = 8
-            const val itemsInCol = 5
-
-            const val playerInventoryOffsetFromBottom = 24f
-
-            val itemsOnPage get() = itemsInCol * itemsInRow
-        }
     }
 }
\ No newline at end of file