Skip to content

Commit b551e4d

Browse files
authored
Refactor canvas to be less dependent on itemstacks (#15)
* refactor: generify canvases * clean up * readd old drag for now * make click independent of inventory & remove drag * make LayoutNode internal again
1 parent 1f877f2 commit b551e4d

File tree

28 files changed

+125
-186
lines changed

28 files changed

+125
-186
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ out
1111
eclipse
1212
*.ipr
1313
*.iws
14+
15+
.DS_Store

guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/GuiyExamplePlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.mineinabyss.guiy.example
22

33
import com.mineinabyss.guiy.example.gui.*
4-
import com.mineinabyss.guiy.inventory.guiy
4+
import com.mineinabyss.guiy.canvas.guiy
55
import com.mineinabyss.idofront.commands.brigadier.commands
66
import org.bukkit.plugin.java.JavaPlugin
77

src/main/kotlin/com/mineinabyss/guiy/GuiyPlugin.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.mineinabyss.guiy
22

3-
import com.mineinabyss.guiy.inventory.GuiyEventListener
4-
import com.mineinabyss.guiy.inventory.GuiyInventoryHolder
5-
import com.mineinabyss.guiy.inventory.GuiyScopeManager
3+
import com.mineinabyss.guiy.canvas.inventory.GuiyEventListener
4+
import com.mineinabyss.guiy.canvas.inventory.GuiyInventoryHolder
5+
import com.mineinabyss.guiy.canvas.GuiyScopeManager
66
import com.mineinabyss.idofront.nms.interceptServerbound
77
import com.mineinabyss.idofront.plugin.listeners
88
import kotlinx.coroutines.cancel
99
import kotlinx.coroutines.flow.MutableStateFlow
1010
import kotlinx.coroutines.flow.update
11-
import net.minecraft.network.Connection
1211
import net.minecraft.network.protocol.game.ServerboundRenameItemPacket
1312
import org.bukkit.Bukkit
1413
import org.bukkit.entity.Player

src/main/kotlin/com/mineinabyss/guiy/inventory/CompositionLocals.kt renamed to src/main/kotlin/com/mineinabyss/guiy/canvas/CompositionLocals.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mineinabyss.guiy.inventory
1+
package com.mineinabyss.guiy.canvas
22

33
import androidx.compose.runtime.Composable
44
import androidx.compose.runtime.ProvidableCompositionLocal
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.mineinabyss.guiy.canvas
2+
3+
import com.mineinabyss.guiy.components.state.IntCoordinates
4+
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap
5+
import org.bukkit.inventory.ItemStack
6+
7+
interface GuiyCanvas {
8+
fun subCanvas(x: Int, y: Int): GuiyCanvas
9+
}
10+
11+
class InventoryCanvas private constructor(
12+
private val contents: Long2ObjectOpenHashMap<ItemStack>,
13+
private val offX: Int,
14+
private val offY: Int,
15+
) : GuiyCanvas {
16+
constructor(): this(Long2ObjectOpenHashMap(), 0, 0)
17+
18+
fun set(x: Int, y: Int, item: ItemStack?) {
19+
if (item == null) contents.remove(IntCoordinates(x + offX, y + offY).pair)
20+
else contents[IntCoordinates(x + offX, y + offY).pair] = item
21+
}
22+
23+
fun clear() {
24+
contents.clear()
25+
}
26+
27+
fun contents(): Map<IntCoordinates, ItemStack> {
28+
return contents.toMap().mapKeys { IntCoordinates(it.key) }
29+
}
30+
31+
override fun subCanvas(x: Int, y: Int): GuiyCanvas =
32+
InventoryCanvas(contents, offX + x, offY + y)
33+
}

src/main/kotlin/com/mineinabyss/guiy/inventory/GuiyOwner.kt renamed to src/main/kotlin/com/mineinabyss/guiy/canvas/GuiyOwner.kt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mineinabyss.guiy.inventory
1+
package com.mineinabyss.guiy.canvas
22

33
import androidx.compose.runtime.*
44
import androidx.compose.runtime.snapshots.Snapshot
@@ -8,7 +8,6 @@ import com.mineinabyss.guiy.guiyPlugin
88
import com.mineinabyss.guiy.layout.LayoutNode
99
import com.mineinabyss.guiy.modifiers.Constraints
1010
import com.mineinabyss.guiy.modifiers.click.ClickScope
11-
import com.mineinabyss.guiy.modifiers.drag.DragScope
1211
import com.mineinabyss.guiy.navigation.BackGestureDispatcher
1312
import com.mineinabyss.guiy.navigation.LocalBackGestureDispatcher
1413
import com.mineinabyss.guiy.nodes.GuiyNodeApplier
@@ -32,7 +31,6 @@ data class ClickResult(
3231

3332
interface ClickHandler {
3433
fun processClick(scope: ClickScope): ClickResult
35-
fun processDrag(scope: DragScope)
3634
}
3735

3836
@GuiyUIScopeMarker
@@ -42,6 +40,7 @@ class GuiyOwner(
4240
var hasFrameWaiters = false
4341
val clock = BroadcastFrameClock { hasFrameWaiters = true }
4442
val composeScope = CoroutineScope(guiyPlugin.minecraftDispatcher + Dispatchers.Default) + clock
43+
4544
private val _viewers: MutableStateFlow<Set<Player>> = MutableStateFlow(initialViewers)
4645
val viewers = _viewers.asStateFlow()
4746

@@ -125,21 +124,12 @@ class GuiyOwner(
125124
LocalBackGestureDispatcher provides BackGestureDispatcher(),
126125
LocalClickHandler provides object : ClickHandler {
127126
override fun processClick(scope: ClickScope): ClickResult {
128-
val slot = scope.slot
129-
val width = rootNode.width
130127
return rootNode.children.fold(ClickResult()) { acc, node ->
131-
val w = node.width
132-
val x = if (w == 0) 0 else slot % width
133-
val y = if (w == 0) 0 else slot / width
134-
val processed = rootNode.processClick(scope, x, y)
128+
val processed = rootNode.processClick(scope)
135129
if (processed.clickConsumed == true) return processed
136130
acc.mergeWith(processed)
137131
}
138132
}
139-
140-
override fun processDrag(scope: DragScope) {
141-
rootNode.processDrag(scope)
142-
}
143133
}) {
144134
// A default inventory holder for most usecases
145135
InventoryHolder {

src/main/kotlin/com/mineinabyss/guiy/inventory/GuiyScopeManager.kt renamed to src/main/kotlin/com/mineinabyss/guiy/canvas/GuiyScopeManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mineinabyss.guiy.inventory
1+
package com.mineinabyss.guiy.canvas
22

33
import kotlinx.coroutines.CoroutineScope
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mineinabyss.guiy.inventory
1+
package com.mineinabyss.guiy.canvas
22

33
@DslMarker
44
annotation class GuiyUIScopeMarker

src/main/kotlin/com/mineinabyss/guiy/inventory/GuiyEventListener.kt renamed to src/main/kotlin/com/mineinabyss/guiy/canvas/inventory/GuiyEventListener.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package com.mineinabyss.guiy.inventory
1+
package com.mineinabyss.guiy.canvas.inventory
22

3+
import com.mineinabyss.guiy.components.state.IntCoordinates
34
import com.mineinabyss.guiy.modifiers.click.ClickScope
4-
import com.mineinabyss.idofront.nms.aliases.NMSPlayer
5-
import com.mineinabyss.idofront.nms.aliases.toNMS
65
import org.bukkit.Material
76
import org.bukkit.entity.Player
87
import org.bukkit.event.EventHandler
@@ -11,7 +10,6 @@ import org.bukkit.event.inventory.ClickType.*
1110
import org.bukkit.event.inventory.InventoryClickEvent
1211
import org.bukkit.event.inventory.InventoryCloseEvent
1312
import org.bukkit.event.inventory.InventoryDragEvent
14-
import org.bukkit.event.inventory.PrepareAnvilEvent
1513

1614
class GuiyEventListener : Listener {
1715
@EventHandler
@@ -26,7 +24,7 @@ class GuiyEventListener : Listener {
2624
isCancelled = true
2725

2826
val scope = ClickScope(
29-
click, slot, cursor.takeIf { it.type != Material.AIR }, whoClicked
27+
click, slot, whoClicked
3028
)
3129
guiyHolder.processClick(scope, this)
3230
}
@@ -52,7 +50,7 @@ class GuiyEventListener : Listener {
5250
if (newItems.size == 1 && inGuiy.size == 1) {
5351
isCancelled = true
5452
val clicked = inGuiy.entries.first()
55-
val scope = ClickScope(LEFT, clicked.key, cursor?.takeIf { it.type != Material.AIR }, whoClicked)
53+
val scope = ClickScope(LEFT, clicked.key, whoClicked)
5654
guiyHolder.processClick(scope, this)
5755
} else if (inGuiy.isNotEmpty()) {
5856
isCancelled = true

src/main/kotlin/com/mineinabyss/guiy/inventory/GuiyInventory.kt renamed to src/main/kotlin/com/mineinabyss/guiy/canvas/inventory/GuiyInventory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mineinabyss.guiy.inventory
1+
package com.mineinabyss.guiy.canvas.inventory
22

33
import androidx.compose.runtime.Immutable
44
import org.bukkit.inventory.Inventory

0 commit comments

Comments
 (0)