Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit 341cfea

Browse files
committed
Fix switching between updates and categories
Also reduced duplicated code Signed-off-by: deathsgun <deathsgun@protonmail.com>
1 parent 9327046 commit 341cfea

File tree

4 files changed

+28
-123
lines changed

4 files changed

+28
-123
lines changed

src/main/kotlin/xyz/deathsgun/modmanager/api/gui/list/ListWidget.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ abstract class ListWidget<E : ListWidget.Entry<E>>(
4141
top: Int,
4242
var bottom: Int,
4343
itemHeight: Int,
44-
private val parent: IListScreen
44+
protected val parent: IListScreen
4545
) : AlwaysSelectedEntryListWidget<E>(client, width, height, top, bottom, itemHeight) {
4646

4747
var renderOutline: Boolean = true
@@ -141,7 +141,7 @@ abstract class ListWidget<E : ListWidget.Entry<E>>(
141141
super.appendNarrations(builder)
142142
}
143143

144-
fun isSelectedEntry(entry: Entry<E>): Boolean {
144+
open fun isSelectedEntry(entry: Entry<E>): Boolean {
145145
return selectedId == entry.id
146146
}
147147

src/main/kotlin/xyz/deathsgun/modmanager/api/gui/list/MultiSelectListWidget.kt

Lines changed: 15 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,10 @@
1616

1717
package xyz.deathsgun.modmanager.api.gui.list
1818

19-
import com.mojang.blaze3d.systems.RenderSystem
19+
import kotlinx.serialization.ExperimentalSerializationApi
20+
import kotlinx.serialization.encodeToString
21+
import kotlinx.serialization.json.Json
2022
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
3123

3224
/**
3325
* Using ModMenu's implementation because the implementation from
@@ -39,21 +31,14 @@ abstract class MultiSelectListWidget<E : MultiSelectListWidget.Entry<E>>(
3931
width: Int,
4032
height: Int,
4133
top: Int,
42-
var bottom: Int,
34+
bottom: Int,
4335
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) {
4638

47-
var renderOutline: Boolean = true
4839
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-
}
5640

41+
@OptIn(ExperimentalSerializationApi::class)
5742
override fun setSelected(entry: E?) {
5843
super.setSelected(entry)
5944
if (entry == null) {
@@ -64,7 +49,11 @@ abstract class MultiSelectListWidget<E : MultiSelectListWidget.Entry<E>>(
6449
} else {
6550
selectedIds.add(entry.id)
6651
}
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+
)
6857
}
6958

7059
fun setSelected(entries: List<E>) {
@@ -86,101 +75,12 @@ abstract class MultiSelectListWidget<E : MultiSelectListWidget.Entry<E>>(
8675
return i
8776
}
8877

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 {
15979
return selectedIds.contains(entry.id)
16080
}
16181

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) {
18484
@Suppress("UNCHECKED_CAST")
18585
override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
18686
list.setSelected(this as E)

src/main/kotlin/xyz/deathsgun/modmanager/gui/ModsOverviewScreen.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,17 @@ class ModsOverviewScreen(private val previousScreen: Screen) : Screen(Translatab
256256
page = 0
257257
@Suppress("UNCHECKED_CAST")
258258
selectedCategories = entries as ArrayList<CategoryListEntry>
259-
if (selectedCategories.any { it.category.id == "updatable" } && selectedCategories.size > 1) {
260-
selectedCategories.removeIf { it.category.id != "updatable" }
259+
query = ""
260+
if (selectedCategories.any { it.id == "updatable" } && selectedCategories.size > 1) {
261+
val last = selectedCategories.last()
262+
if (last.id == "updatable") {
263+
selectedCategories.removeIf { it.category.id != "updatable" }
264+
} else {
265+
selectedCategories.removeIf { it.category.id == "updatable" }
266+
}
261267
categoryList.setSelected(selectedCategories)
268+
return
262269
}
263-
query = ""
264270
showModsByCategory()
265271
}
266272

src/main/kotlin/xyz/deathsgun/modmanager/update/UpdateManager.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ class UpdateManager {
381381
if (!meta.containsCustomValue("modmanager")) {
382382
return false
383383
}
384-
val modmenu = meta.getCustomValue("modmanager").asObject
385-
return modmenu.containsKey("disable-checking") && modmenu.get("disable-checking").asBoolean
384+
val modmanager = meta.getCustomValue("modmanager").asObject
385+
return modmanager.containsKey("disable-checking") && modmanager.get("disable-checking").asBoolean
386386
}
387387

388388
private fun Path.delete() {
@@ -428,7 +428,6 @@ class UpdateManager {
428428
val content = try {
429429
Files.readString(jar.toPath())
430430
} catch (e: Exception) {
431-
logger.info("Failed to read file ignoring it: {}", e.message)
432431
""
433432
}
434433
if (content != "MODMANGER") {

0 commit comments

Comments
 (0)