Handle click sounds in the screen

This commit is contained in:
Shadowfacts 2019-06-23 22:34:12 -04:00
parent df3523347c
commit 3902b75a27
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
10 changed files with 46 additions and 41 deletions

View File

@ -2,8 +2,10 @@ package net.shadowfacts.cacao
import net.minecraft.client.gui.screen.Screen
import net.minecraft.network.chat.TextComponent
import net.minecraft.sound.SoundEvents
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.util.MouseButton
import net.shadowfacts.cacao.util.RenderHelper
import java.util.*
/**
@ -45,7 +47,9 @@ open class CacaoScreen: Screen(TextComponent("CacaoScreen")) {
override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
val window = windows.lastOrNull()
window?.mouseClicked(Point(mouseX, mouseY), MouseButton.fromMC(button))
if (window?.mouseClicked(Point(mouseX, mouseY), MouseButton.fromMC(button)) == true) {
RenderHelper.playSound(SoundEvents.UI_BUTTON_CLICK)
}
return false
}

View File

@ -81,13 +81,15 @@ class Window {
*
* @param point The point in the window of the click.
* @param mouseButton The mouse button that was used to click.
* @return Whether the mouse click was handled by a view.
*/
fun mouseClicked(point: Point, mouseButton: MouseButton) {
fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
val view = viewAtPoint(point)
if (view != null) {
val pointInView = Point(point.x - view.frame.left, point.y - view.frame.top)
view.mouseClicked(pointInView, mouseButton)
return view.mouseClicked(pointInView, mouseButton)
}
return false
}
}

View File

@ -258,13 +258,15 @@ open class View() {
*
* @param point The point in the coordinate system of this view that the mouse was clicked.
* @param mouseButton The mouse button used to click.
* @return Whether the mouse click was handled by this view or any subviews.
*/
open fun mouseClicked(point: Point, mouseButton: MouseButton) {
open fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
val view = subviewAtPoint(point)
if (view != null) {
val pointInView = convert(point, to = view)
view.mouseClicked(pointInView, mouseButton)
return view.mouseClicked(pointInView, mouseButton)
}
return false
}
/**

View File

@ -65,11 +65,6 @@ abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val
*/
var disabledBackground: View? = NinePatchView(DISABLED_BG)
/**
* If the button will play the Minecraft button click sound when clicked.
*/
var clickSoundEnabled = true
override fun wasAdded() {
solver.dsl {
addSubview(content)
@ -115,8 +110,8 @@ abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val
RenderHelper.popMatrix()
}
override fun mouseClicked(point: Point, mouseButton: MouseButton) {
if (disabled) return
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
if (disabled) return false
val handler = handler
if (handler != null) {
@ -125,11 +120,9 @@ abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val
// For example, an implementing class may be defined as such: `class Button: AbstractButton<Button>`
@Suppress("UNCHECKED_CAST")
handler(this as Impl)
if (clickSoundEnabled && !RenderHelper.disabled) {
RenderHelper.playSound(SoundEvents.UI_BUTTON_CLICK)
}
}
return true
}
}

View File

@ -30,7 +30,7 @@ class EnumButton<E: Enum<E>>(initialValue: E, val localizer: (E) -> String): Abs
label.text = localizer(value)
}
override fun mouseClicked(point: Point, mouseButton: MouseButton) {
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
if (!disabled) {
value = when (mouseButton) {
MouseButton.LEFT -> EnumHelper.next(value)
@ -39,7 +39,7 @@ class EnumButton<E: Enum<E>>(initialValue: E, val localizer: (E) -> String): Abs
}
}
super.mouseClicked(point, mouseButton)
return super.mouseClicked(point, mouseButton)
}
}

View File

@ -35,12 +35,12 @@ class ToggleButton(initialState: Boolean): AbstractButton<ToggleButton>(TextureV
textureView.texture = if (value) ON else OFF
}
override fun mouseClicked(point: Point, mouseButton: MouseButton) {
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
if (!disabled && (mouseButton == MouseButton.LEFT || mouseButton == MouseButton.RIGHT)) {
state = !state
}
super.mouseClicked(point, mouseButton)
return super.mouseClicked(point, mouseButton)
}
}

View File

@ -25,11 +25,12 @@ class ViewClickTests {
fun testClickInsideRootView() {
val mouse = CompletableFuture<Point>()
window.addView(object: View(Rect(50.0, 50.0, 100.0, 100.0)) {
override fun mouseClicked(point: Point, mouseButton: MouseButton) {
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
mouse.complete(point)
return true
}
})
window.mouseClicked(Point(75.0, 75.0), MouseButton.LEFT)
assertTrue(window.mouseClicked(Point(75.0, 75.0), MouseButton.LEFT))
assertEquals(Point(25.0, 25.0), mouse.getNow(null))
}
@ -38,11 +39,12 @@ class ViewClickTests {
fun testClickOutsideRootView() {
val clicked = CompletableFuture<Boolean>()
window.addView(object: View(Rect(50.0, 50.0, 100.0, 100.0)) {
override fun mouseClicked(point: Point, mouseButton: MouseButton) {
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
clicked.complete(true)
return true
}
})
window.mouseClicked(Point(25.0, 25.0), MouseButton.LEFT)
assertFalse(window.mouseClicked(Point(25.0, 25.0), MouseButton.LEFT))
assertFalse(clicked.getNow(false))
}
@ -52,11 +54,12 @@ class ViewClickTests {
val mouse = CompletableFuture<Point>()
val root = window.addView(View(Rect(50.0, 50.0, 100.0, 100.0)))
root.addSubview(object: View(Rect(25.0, 25.0, 50.0, 50.0)) {
override fun mouseClicked(point: Point, mouseButton: MouseButton) {
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
mouse.complete(point)
return true
}
})
window.mouseClicked(Point(100.0, 100.0), MouseButton.LEFT)
assertTrue(window.mouseClicked(Point(100.0, 100.0), MouseButton.LEFT))
assertEquals(Point(25.0, 25.0), mouse.getNow(null))
}
@ -66,11 +69,12 @@ class ViewClickTests {
val clicked = CompletableFuture<Boolean>()
val root = window.addView(View(Rect(50.0, 50.0, 100.0, 100.0)))
root.addSubview(object: View(Rect(25.0, 25.0, 50.0, 50.0)) {
override fun mouseClicked(point: Point, mouseButton: MouseButton) {
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
clicked.complete(true)
return true
}
})
window.mouseClicked(Point(0.0, 0.0), MouseButton.LEFT)
assertFalse(window.mouseClicked(Point(0.0, 0.0), MouseButton.LEFT))
assertFalse(clicked.getNow(false))
}

View File

@ -40,8 +40,8 @@ class ButtonClickTests {
button.topAnchor equalTo 0
}
window.layout()
window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT))
assertTrue(clicked.getNow(false))
}
@ -61,8 +61,8 @@ class ButtonClickTests {
button.topAnchor equalTo 0
}
window.layout()
window.mouseClicked(Point(50.0, 50.0), MouseButton.LEFT)
assertFalse(window.mouseClicked(Point(50.0, 50.0), MouseButton.LEFT))
assertFalse(clicked.getNow(false))
}

View File

@ -46,7 +46,7 @@ class EnumButtonTests {
}
})
window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT))
assertTrue(called.getNow(false))
assertEquals(MyEnum.TWO, button.value)
}
@ -58,11 +58,11 @@ class EnumButtonTests {
content.frame = bounds
})
window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT))
assertEquals(MyEnum.TWO, button.value)
window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT))
assertEquals(MyEnum.THREE, button.value)
window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT))
assertEquals(MyEnum.ONE, button.value)
}
@ -73,9 +73,9 @@ class EnumButtonTests {
content.frame = Rect(0.0, 0.0, 25.0, 25.0)
})
window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT))
assertEquals(MyEnum.TWO, button.value)
window.mouseClicked(Point(5.0, 5.0), MouseButton.RIGHT)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.RIGHT))
assertEquals(MyEnum.ONE, button.value)
}
@ -86,7 +86,7 @@ class EnumButtonTests {
content.frame = Rect(0.0, 0.0, 25.0, 25.0)
})
window.mouseClicked(Point(5.0, 5.0), MouseButton.MIDDLE)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.MIDDLE))
assertEquals(MyEnum.ONE, button.value)
}

View File

@ -41,7 +41,7 @@ class ToggleButtonTests {
}
})
window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT))
assertTrue(called.getNow(false))
}
@ -52,9 +52,9 @@ class ToggleButtonTests {
content.frame = bounds
})
window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT))
assertTrue(button.state)
window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.LEFT))
assertFalse(button.state)
}
@ -65,7 +65,7 @@ class ToggleButtonTests {
content.frame = bounds
})
window.mouseClicked(Point(5.0, 5.0), MouseButton.MIDDLE)
assertTrue(window.mouseClicked(Point(5.0, 5.0), MouseButton.MIDDLE))
assertFalse(button.state)
}