From c108a129e91db232bad824091303fadc2daeb8ad Mon Sep 17 00:00:00 2001 From: ceikry Date: Fri, 29 Oct 2021 21:11:16 -0500 Subject: [PATCH] Multiplane support, improved state management --- src/main/kotlin/NPCSpawnPanel.kt | 2 +- src/main/kotlin/Rs2MapEditor.kt | 100 +++++++++--------- .../definition/encoder/GroundItemEncoder.kt | 3 + .../definition/encoder/NPCSpawnEncoder.kt | 2 +- src/main/kotlin/misc/GlobalKeybinds.kt | 4 + src/main/kotlin/misc/ItemSpawnPanel.kt | 2 +- 6 files changed, 62 insertions(+), 51 deletions(-) diff --git a/src/main/kotlin/NPCSpawnPanel.kt b/src/main/kotlin/NPCSpawnPanel.kt index 15d3645..6203ec9 100644 --- a/src/main/kotlin/NPCSpawnPanel.kt +++ b/src/main/kotlin/NPCSpawnPanel.kt @@ -31,7 +31,7 @@ class NPCSpawnPanel : JPanel() { remove(it) } Rs2MapEditor.npcRows.clear() - Rs2MapEditor.npcs.forEach { + Rs2MapEditor.npcs.filter { it.plane == Rs2MapEditor.plane }.forEach { val row = NPCRow(it, this) row.border = rowBorder Rs2MapEditor.npcRows.add(row) diff --git a/src/main/kotlin/Rs2MapEditor.kt b/src/main/kotlin/Rs2MapEditor.kt index 3f483b5..63541a5 100644 --- a/src/main/kotlin/Rs2MapEditor.kt +++ b/src/main/kotlin/Rs2MapEditor.kt @@ -31,6 +31,7 @@ object Rs2MapEditor { var npcsUpdated = false var itemsUpdated = false var tileDataUpdated = false + var plane = 0 lateinit var mapPanel: MapPane lateinit var npcPanel: NPCSpawnPanel lateinit var itemPanel: ItemSpawnPanel @@ -121,47 +122,7 @@ object Rs2MapEditor { gbc.fill = GridBagConstraints.NONE gbc.weightx = 1.0 SwingUtilities.invokeLater { - for (row in 0 until mapHeight) { - for (column in 0 until mapWidth) { - gbc.gridx = column - gbc.gridy = row - val border: Border = - MatteBorder( - 1, - 1, - if (row == mapHeight) 1 else 0, - if (column == mapWidth) 1 else 0, - Color.GRAY - ) - val flippedY = (64 - row) - 1 - val point = Point(column, flippedY) - colorPointMap[point] = MapTileParser.definition.getTile(column, flippedY, 0).underlayId - val mapCell = MapCell() - - val overlayID = MapTileParser.definition.getTile(column, flippedY, 0).overlayId - val floDef = overlayMap[overlayID]!! - - if (overlayID == 0) { - mapCell.background = underlayMap[MapTileParser.definition.getTile(column, flippedY, 0).underlayId]!!.getRGB() - } else { - mapCell.background = overlayMap[overlayID]!!.getRGB() - } - - mapCell.border = border - val npcsHere = npcs.filter { it.x == column && it.y == flippedY && it.plane == 0 }.toList() - if (npcsHere.isNotEmpty()) { - mapCell.layout = BorderLayout() - mapCell.add(JLabel(YELLOW_DOT)) - } - val itemSpawnsHere = items.filter { it.x == column && it.y == flippedY && it.plane == 0 }.toList() - if (itemSpawnsHere.isNotEmpty()) { - mapCell.layout = BorderLayout() - mapCell.add(JLabel(RED_DOT)) - } - componentPointMap[point] = mapCell - add(mapCell, gbc) - } - } + repaintMap() } } @@ -174,23 +135,45 @@ object Rs2MapEditor { gbc.gridx = column gbc.gridy = row val border: Border = - MatteBorder(1, 1, if (row == mapHeight) 1 else 0, if (column == mapWidth) 1 else 0, Color.GRAY) + MatteBorder( + 1, + 1, + if (row == mapHeight) 1 else 0, + if (column == mapWidth) 1 else 0, + Color.GRAY + ) val flippedY = (64 - row) - 1 val point = Point(column, flippedY) - colorPointMap[point] = MapTileParser.definition.getTile(column, flippedY, 0).underlayId + colorPointMap[point] = MapTileParser.definition.getTile(column, flippedY, plane).underlayId val mapCell = MapCell() - mapCell.background = - underlayMap[MapTileParser.definition.getTile(column, flippedY, 0).underlayId]!!.getRGB() + + val overlayID = MapTileParser.definition.getTile(column, flippedY, plane).overlayId + val floDef = overlayMap[overlayID]!! + + if (overlayID == 0) { + mapCell.background = underlayMap[MapTileParser.definition.getTile(column, flippedY, plane).underlayId]!!.getRGB() + } else { + mapCell.background = overlayMap[overlayID]!!.getRGB() + } + mapCell.border = border - val npcsHere = npcs.filter { it.x == column && it.y == flippedY && it.plane == 0 }.toList() + val npcsHere = npcs.filter { it.x == column && it.y == flippedY && it.plane == plane }.toList() if (npcsHere.isNotEmpty()) { mapCell.layout = BorderLayout() mapCell.add(JLabel(YELLOW_DOT)) } + val itemSpawnsHere = items.filter { it.x == column && it.y == flippedY && it.plane == plane }.toList() + if (itemSpawnsHere.isNotEmpty()) { + mapCell.layout = BorderLayout() + mapCell.add(JLabel(RED_DOT)) + } componentPointMap[point] = mapCell add(mapCell, gbc) } } + SwingUtilities.invokeLater { + this.repaint() + } } } @@ -248,7 +231,7 @@ object Rs2MapEditor { return } - val npc = NPC(id, selectedPointX, selectedPointY, 0) + val npc = NPC(id, selectedPointX, selectedPointY, plane) npcs.add(npc) componentPointMap.filter { it.key.x == selectedPointX && it.key.y == selectedPointY }.forEach {(_, cell) -> var hasLabel = false @@ -269,7 +252,7 @@ object Rs2MapEditor { return } - val item = Item(id, selectedPointX, selectedPointY, 0, respawnTime, amount) + val item = Item(id, selectedPointX, selectedPointY, plane, respawnTime, amount) items.add(item) componentPointMap.filter { it.key.x == selectedPointX && it.key.y == selectedPointY }.forEach {(_, cell) -> var hasLabel = false @@ -516,6 +499,25 @@ object Rs2MapEditor { add(RegionMenuOption()) add(PrefMenuOption()) add(ToolsMenuOption()) + add(JSeparator(JSeparator.VERTICAL)) + add(JLabel("Plane: ")) + add(PlaneButton(0)) + add(PlaneButton(1)) + add(PlaneButton(2)) + add(PlaneButton(3)) + } + } + + class PlaneButton(val plane: Int) : JButton(plane.toString()){ + init { + addActionListener { + if(Rs2MapEditor.plane != this.plane){ + Rs2MapEditor.plane = this.plane + npcPanel.redrawRows() + itemPanel.redrawRows() + mapPanel.repaintMap() + } + } } } @@ -533,6 +535,7 @@ object Rs2MapEditor { NPCSearchTool.caller = { id, _ -> npcIdInput.text = id.toString() infoPane.selectedIndex = 3 + state = EditorState.ADD_NPC } } } @@ -545,6 +548,7 @@ object Rs2MapEditor { ItemSearchTool.caller = {id, _ -> itemIdInput.text = id.toString() infoPane.selectedIndex = 4 + state = EditorState.ADD_GROUNDITEM } } } diff --git a/src/main/kotlin/cacheops/cache/definition/encoder/GroundItemEncoder.kt b/src/main/kotlin/cacheops/cache/definition/encoder/GroundItemEncoder.kt index 2dbd7f6..4849002 100644 --- a/src/main/kotlin/cacheops/cache/definition/encoder/GroundItemEncoder.kt +++ b/src/main/kotlin/cacheops/cache/definition/encoder/GroundItemEncoder.kt @@ -4,6 +4,7 @@ import Rs2MapEditor import cacheops.cache.definition.decoder.Item import const.Indices import java.nio.ByteBuffer +import javax.swing.JOptionPane object GroundItemEncoder { @@ -33,6 +34,8 @@ object GroundItemEncoder { if (dataArray != null) { Rs2MapEditor.library.put(Indices.LANDSCAPES, "i${x}_${y}", dataArray, intArrayOf(0,0,0,0)) Rs2MapEditor.library.update() + JOptionPane.showMessageDialog(Rs2MapEditor.mapPanel, "Items written to cache [✓]") + Rs2MapEditor.itemsUpdated = false } else { System.err.println("Panic! Something went VERY wrong!") } diff --git a/src/main/kotlin/cacheops/cache/definition/encoder/NPCSpawnEncoder.kt b/src/main/kotlin/cacheops/cache/definition/encoder/NPCSpawnEncoder.kt index 1d1de66..4268e29 100644 --- a/src/main/kotlin/cacheops/cache/definition/encoder/NPCSpawnEncoder.kt +++ b/src/main/kotlin/cacheops/cache/definition/encoder/NPCSpawnEncoder.kt @@ -26,7 +26,7 @@ object NPCSpawnEncoder { if (dataArray != null) { Rs2MapEditor.library.put(Indices.LANDSCAPES, "n${x}_${y}", dataArray, intArrayOf(0,0,0,0)) Rs2MapEditor.library.update() - JOptionPane.showMessageDialog(Rs2MapEditor.mapPanel, "NPCs written to cache. [✓]") + JOptionPane.showMessageDialog(Rs2MapEditor.mapPanel, "NPCs written to cache [✓]") Rs2MapEditor.npcsUpdated = false } else { System.err.println("Panic! Something went VERY wrong!") diff --git a/src/main/kotlin/misc/GlobalKeybinds.kt b/src/main/kotlin/misc/GlobalKeybinds.kt index 792b09e..25662e3 100644 --- a/src/main/kotlin/misc/GlobalKeybinds.kt +++ b/src/main/kotlin/misc/GlobalKeybinds.kt @@ -1,5 +1,6 @@ package misc +import cacheops.cache.definition.encoder.GroundItemEncoder import cacheops.cache.definition.encoder.NPCSpawnEncoder import java.awt.Event import java.awt.event.KeyEvent @@ -29,6 +30,9 @@ object GlobalKeybinds{ if(Rs2MapEditor.npcsUpdated){ NPCSpawnEncoder.write(Rs2MapEditor.npcs) } + if(Rs2MapEditor.itemsUpdated){ + GroundItemEncoder.write(Rs2MapEditor.items) + } } diff --git a/src/main/kotlin/misc/ItemSpawnPanel.kt b/src/main/kotlin/misc/ItemSpawnPanel.kt index 4091425..03628b8 100644 --- a/src/main/kotlin/misc/ItemSpawnPanel.kt +++ b/src/main/kotlin/misc/ItemSpawnPanel.kt @@ -35,7 +35,7 @@ class ItemSpawnPanel : JPanel() { remove(it) } Rs2MapEditor.itemRows.clear() - Rs2MapEditor.items.forEach { + Rs2MapEditor.items.filter { it.plane == Rs2MapEditor.plane }.forEach { val row = ItemRow(it, this) row.border = rowBorder Rs2MapEditor.itemRows.add(row)