2019-06-22 20:08:00 +00:00
|
|
|
package net.shadowfacts.cacao
|
|
|
|
|
|
|
|
import net.minecraft.client.gui.screen.Screen
|
|
|
|
import net.minecraft.network.chat.TextComponent
|
2019-06-24 02:34:12 +00:00
|
|
|
import net.minecraft.sound.SoundEvents
|
2019-06-22 20:08:00 +00:00
|
|
|
import net.shadowfacts.cacao.geometry.Point
|
2019-06-23 15:41:32 +00:00
|
|
|
import net.shadowfacts.cacao.util.MouseButton
|
2019-06-24 02:34:12 +00:00
|
|
|
import net.shadowfacts.cacao.util.RenderHelper
|
2019-06-22 20:08:00 +00:00
|
|
|
import java.util.*
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class serves as the bridge between Cacao and a Minecraft [Screen]. It renders Cacao [Window]s in Minecraft and
|
|
|
|
* sends input events from Minecraft back to Cacao objects.
|
|
|
|
*
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
|
|
|
open class CacaoScreen: Screen(TextComponent("CacaoScreen")) {
|
|
|
|
|
|
|
|
// _windows is the internal, mutable object, since we only want it to by mutated by the add/removeWindow methods.
|
|
|
|
private val _windows = LinkedList<Window>()
|
|
|
|
/**
|
|
|
|
* The list of windows that belong to this screen.
|
|
|
|
* This list should never be modified directly, only by using the [addWindow]/[removeWindow] methods.
|
|
|
|
*/
|
|
|
|
val windows: List<Window> = _windows
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the given window to this screen's window list.
|
|
|
|
* By default, the new window is added at the tail of the window list, making it the active window.
|
|
|
|
* Only the active window will receive input events.
|
|
|
|
*
|
|
|
|
* @param window The Window to add to this screen.
|
|
|
|
* @param index The index to insert the window into the window list at.
|
2019-06-26 01:52:17 +00:00
|
|
|
* @return The window that was added, as a convenience.
|
2019-06-22 20:08:00 +00:00
|
|
|
*/
|
2019-06-26 01:52:17 +00:00
|
|
|
fun <T: Window> addWindow(window: T, index: Int = _windows.size): T {
|
2019-06-22 20:08:00 +00:00
|
|
|
_windows.add(index, window)
|
2019-06-26 01:52:17 +00:00
|
|
|
window.screen = this
|
|
|
|
return window
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the given window from this screen's window list.
|
|
|
|
*/
|
|
|
|
fun removeWindow(window: Window) {
|
|
|
|
_windows.remove(window)
|
2019-06-22 20:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun render(mouseX: Int, mouseY: Int, delta: Float) {
|
2019-06-26 01:52:17 +00:00
|
|
|
if (minecraft != null) {
|
|
|
|
// workaround this.minecraft sometimes being null causing a crash
|
|
|
|
renderBackground()
|
|
|
|
}
|
2019-06-23 22:23:59 +00:00
|
|
|
|
2019-06-22 20:08:00 +00:00
|
|
|
val mouse = Point(mouseX, mouseY)
|
|
|
|
windows.forEach {
|
|
|
|
it.draw(mouse, delta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 15:41:32 +00:00
|
|
|
override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
|
|
|
|
val window = windows.lastOrNull()
|
2019-06-26 01:52:17 +00:00
|
|
|
val result = window?.mouseClicked(Point(mouseX, mouseY), MouseButton.fromMC(button))
|
|
|
|
when (result) {
|
|
|
|
true ->
|
|
|
|
RenderHelper.playSound(SoundEvents.UI_BUTTON_CLICK)
|
|
|
|
false ->
|
|
|
|
if (windows.size > 1) {
|
|
|
|
removeWindow(windows.last())
|
|
|
|
}
|
2019-06-24 02:34:12 +00:00
|
|
|
}
|
2019-06-23 15:41:32 +00:00
|
|
|
|
2019-06-26 01:52:17 +00:00
|
|
|
return result == true
|
2019-06-23 15:41:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 20:08:00 +00:00
|
|
|
}
|