Skip to content

Commit 881e53f

Browse files
authored
fix: ToggleButton missing a message, mouseDragged not forwarded properly (#39)
* Fix ToggleButton not showing a message * Fix mouseDragged not being forwarded to children
1 parent 188d185 commit 881e53f

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

api/src/main/kotlin/com/noxcrew/sheeplib/CompoundWidget.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public abstract class CompoundWidget(x: Int, y: Int, width: Int, height: Int) :
102102
super<ContainerEventHandler>.mouseReleased(mouseButtonEvent)
103103

104104
public override fun mouseDragged(mouseButtonEvent: MouseButtonEvent, d: Double, e: Double): Boolean =
105-
getChildAt(d, e)
105+
getChildAt(mouseButtonEvent.x, mouseButtonEvent.y)
106106
.getOrNull()
107107
?.mouseDragged(mouseButtonEvent, d, e) == true
108108

api/src/main/kotlin/com/noxcrew/sheeplib/widget/ThemedButton.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public open class ThemedButton(
4040
private var messageWidth: Int = Minecraft.getInstance().font.width(message)
4141

4242
override fun setMessage(component: Component) {
43+
super.setMessage(component)
4344
messageWidth = Minecraft.getInstance().font.width(message)
4445
}
4546

api/src/main/kotlin/com/noxcrew/sheeplib/widget/ToggleButton.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,17 @@ public class ToggleButton<T>(
4040
)
4141
}
4242

43-
private lateinit var _message: Component
44-
4543
/** The currently selected entry. */
4644
public var current: Entry<T> =
4745
entries.getOrNull(currentIndex) ?: throw IllegalArgumentException("Entries must not be empty")
4846
private set(value) {
4947
field = value
50-
_message = prefix?.copy()?.append(value.text) ?: value.text
48+
message = prefix?.copy()?.append(value.text) ?: value.text
5149
}
5250

53-
5451
/** Delegates to [current.contents][Entry.contents]. */
5552
public operator fun getValue(thisRef: Any?, property: KProperty<*>): T = current.contents
5653

57-
override fun getMessage(): Component = _message
58-
5954
init {
6055
// Set the current to update the text.
6156
current = current

test-mod/src/main/kotlin/com/noxcrew/sheeplib/testmod/ComponentsDialog.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.noxcrew.sheeplib.theme.Themed
99
import com.noxcrew.sheeplib.widget.DropdownButton
1010
import com.noxcrew.sheeplib.widget.SliderWidget
1111
import com.noxcrew.sheeplib.widget.ThemedButton
12+
import com.noxcrew.sheeplib.widget.ToggleButton
1213
import net.minecraft.client.Minecraft
1314
import net.minecraft.client.gui.components.StringWidget
1415
import net.minecraft.client.gui.layouts.Layout
@@ -18,6 +19,14 @@ import net.minecraft.network.chat.Component
1819
/** A dialog showcasing available components. */
1920
public class ComponentsDialog(x: Int, y: Int) : Dialog(x, y), Themed by Theme.Active {
2021

22+
private enum class TestEnum {
23+
One,
24+
Two,
25+
Three,
26+
Four,
27+
Five,
28+
}
29+
2130
override val title: DialogTitleWidget = TextTitleWidget(this, Component.literal("Components"))
2231
override fun layout(): Layout = linear(LinearLayout.Orientation.VERTICAL) {
2332
defaultAlignment(0.5f)
@@ -44,6 +53,7 @@ public class ComponentsDialog(x: Int, y: Int) : Dialog(x, y), Themed by Theme.Ac
4453
displayMapper = Component::literal
4554
)
4655

56+
+ToggleButton.enum<TestEnum>("") {}
4757

4858
+SliderWidget(100, 0, 8, this@ComponentsDialog, initial = -1)
4959
}

0 commit comments

Comments
 (0)