16
16
17
17
package xyz.deathsgun.modmanager.api.gui.list
18
18
19
- import com.mojang.blaze3d.systems.RenderSystem
19
+ import kotlinx.serialization.ExperimentalSerializationApi
20
+ import kotlinx.serialization.encodeToString
21
+ import kotlinx.serialization.json.Json
20
22
import net.minecraft.client.MinecraftClient
21
- import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder
22
- import net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget
23
- import net.minecraft.client.render.GameRenderer
24
- import net.minecraft.client.render.Tessellator
25
- import net.minecraft.client.render.VertexFormat
26
- import net.minecraft.client.render.VertexFormats
27
- import net.minecraft.client.util.math.MatrixStack
28
- import net.minecraft.util.math.MathHelper
29
- import java.util.*
30
- import kotlin.math.max
31
23
32
24
/* *
33
25
* Using ModMenu's implementation because the implementation from
@@ -39,21 +31,14 @@ abstract class MultiSelectListWidget<E : MultiSelectListWidget.Entry<E>>(
39
31
width : Int ,
40
32
height : Int ,
41
33
top : Int ,
42
- var bottom : Int ,
34
+ bottom : Int ,
43
35
itemHeight : Int ,
44
- private val parent : IListScreen
45
- ) : AlwaysSelectedEntryListWidget <E>(client, width, height, top, bottom, itemHeight) {
36
+ parent : IListScreen
37
+ ) : ListWidget <E>(client, width, height, top, bottom, itemHeight, parent ) {
46
38
47
- var renderOutline: Boolean = true
48
39
private var selectedIds = ArrayList <String >()
49
- private var scrolling = false
50
-
51
- abstract fun init ()
52
-
53
- override fun isFocused (): Boolean {
54
- return parent.getFocused() == this
55
- }
56
40
41
+ @OptIn(ExperimentalSerializationApi ::class )
57
42
override fun setSelected (entry : E ? ) {
58
43
super .setSelected(entry)
59
44
if (entry == null ) {
@@ -64,7 +49,11 @@ abstract class MultiSelectListWidget<E : MultiSelectListWidget.Entry<E>>(
64
49
} else {
65
50
selectedIds.add(entry.id)
66
51
}
67
- parent.updateMultipleEntries(this , ArrayList (children().filter { selectedIds.contains(it.id) }))
52
+ println (Json .encodeToString(selectedIds))
53
+ parent.updateMultipleEntries(
54
+ this ,
55
+ ArrayList (children().filter { selectedIds.contains(it.id) }.sortedBy { selectedIds.indexOf(it.id) })
56
+ )
68
57
}
69
58
70
59
fun setSelected (entries : List <E >) {
@@ -86,101 +75,12 @@ abstract class MultiSelectListWidget<E : MultiSelectListWidget.Entry<E>>(
86
75
return i
87
76
}
88
77
89
- override fun renderList (matrices : MatrixStack ? , x : Int , y : Int , mouseX : Int , mouseY : Int , delta : Float ) {
90
- val itemCount = this .entryCount
91
- val tessellator = Tessellator .getInstance()
92
- val buffer = tessellator.buffer
93
-
94
- for (index in 0 until itemCount) {
95
- val entryTop = getRowTop(index) + 2
96
- val entryBottom = getRowTop(index) + itemHeight
97
- if (entryBottom >= top && entryTop <= bottom) {
98
- val entryHeight = itemHeight - 4
99
- val entry: Entry <E > = getEntry(index)
100
- val rowWidth = this .rowWidth
101
- var entryLeft: Int
102
- if (isSelectedEntry(index) && renderOutline) {
103
- entryLeft = rowLeft - 2
104
- val selectionRight = x + rowWidth + 2
105
- RenderSystem .disableTexture()
106
- val color = if (this .isFocused) 1.0f else 0.5f
107
- RenderSystem .setShader { GameRenderer .getPositionShader() }
108
- RenderSystem .setShaderColor(color, color, color, 1.0f )
109
- val matrix = matrices!! .peek().model
110
- buffer.begin(VertexFormat .DrawMode .QUADS , VertexFormats .POSITION )
111
- buffer.vertex(matrix, entryLeft.toFloat(), (entryTop + entryHeight + 2 ).toFloat(), 0.0f ).next()
112
- buffer.vertex(matrix, selectionRight.toFloat(), (entryTop + entryHeight + 2 ).toFloat(), 0.0f ).next()
113
- buffer.vertex(matrix, selectionRight.toFloat(), (entryTop - 2 ).toFloat(), 0.0f ).next()
114
- buffer.vertex(matrix, entryLeft.toFloat(), (entryTop - 2 ).toFloat(), 0.0f ).next()
115
- tessellator.draw()
116
- RenderSystem .setShader { GameRenderer .getPositionShader() }
117
- RenderSystem .setShaderColor(0.0f , 0.0f , 0.0f , 1.0f )
118
- buffer.begin(VertexFormat .DrawMode .QUADS , VertexFormats .POSITION )
119
- buffer.vertex(matrix, (entryLeft + 1 ).toFloat(), (entryTop + entryHeight + 1 ).toFloat(), 0.0f )
120
- .next()
121
- buffer.vertex(matrix, (selectionRight - 1 ).toFloat(), (entryTop + entryHeight + 1 ).toFloat(), 0.0f )
122
- .next()
123
- buffer.vertex(matrix, (selectionRight - 1 ).toFloat(), (entryTop - 1 ).toFloat(), 0.0f ).next()
124
- buffer.vertex(matrix, (entryLeft + 1 ).toFloat(), (entryTop - 1 ).toFloat(), 0.0f ).next()
125
- tessellator.draw()
126
- RenderSystem .enableTexture()
127
- }
128
- entryLeft = this .rowLeft
129
- entry.render(
130
- matrices, index, entryTop, entryLeft, rowWidth, entryHeight, mouseX, mouseY, this .isMouseOver(
131
- mouseX.toDouble(), mouseY.toDouble()
132
- ) && Objects .equals(this .getEntryAtPos(mouseX, mouseY), entry), delta
133
- )
134
- }
135
- }
136
- }
137
-
138
- override fun getRowWidth (): Int {
139
- return width - if (max(0 , this .maxPosition - (bottom - top - 4 )) > 0 ) 18 else 12
140
- }
141
-
142
- override fun getRowLeft (): Int {
143
- return left + 6
144
- }
145
-
146
- override fun getScrollbarPositionX (): Int {
147
- return left + width - 6
148
- }
149
-
150
- override fun getMaxPosition (): Int {
151
- return super .getMaxPosition() + 4
152
- }
153
-
154
- override fun appendNarrations (builder : NarrationMessageBuilder ? ) {
155
- super .appendNarrations(builder)
156
- }
157
-
158
- fun isSelectedEntry (entry : Entry <E >): Boolean {
78
+ override fun isSelectedEntry (entry : ListWidget .Entry <E >): Boolean {
159
79
return selectedIds.contains(entry.id)
160
80
}
161
81
162
- fun getEntryAtPos (x : Int , y : Int ): Entry <E >? {
163
- val int5 = MathHelper .floor(y - top.toDouble()) - headerHeight + scrollAmount.toInt() - 4
164
- val index = int5 / itemHeight
165
- return if (x < this .scrollbarPositionX.toDouble() && x >= rowLeft.toDouble() && x <= (rowLeft + rowWidth).toDouble() && index >= 0
166
- && int5 >= 0 && index < this .entryCount
167
- ) children()[index] else null
168
- }
169
-
170
- fun getElementCount (): Int {
171
- return super .getEntryCount()
172
- }
173
-
174
- public override fun remove (index : Int ): E ? {
175
- return super .remove(index)
176
- }
177
-
178
- public override fun getEntry (index : Int ): E {
179
- return super .getEntry(index)
180
- }
181
-
182
- abstract class Entry <E : Entry <E >>(open val list : MultiSelectListWidget <E >, val id : String ) :
183
- AlwaysSelectedEntryListWidget .Entry <E >() {
82
+ abstract class Entry <E : Entry <E >>(list : MultiSelectListWidget <E >, id : String ) :
83
+ ListWidget .Entry <E >(list, id) {
184
84
@Suppress(" UNCHECKED_CAST" )
185
85
override fun mouseClicked (mouseX : Double , mouseY : Double , button : Int ): Boolean {
186
86
list.setSelected(this as E )
0 commit comments