|
| 1 | +package com.lambda.newgui |
| 2 | + |
| 3 | +import com.lambda.Lambda.mc |
| 4 | +import com.lambda.event.Muteable |
| 5 | +import com.lambda.event.events.RenderEvent |
| 6 | +import com.lambda.event.events.TickEvent |
| 7 | +import com.lambda.event.listener.SafeListener.Companion.listener |
| 8 | +import com.lambda.gui.api.GuiEvent |
| 9 | +import com.lambda.util.KeyCode |
| 10 | +import com.lambda.util.Mouse |
| 11 | +import com.lambda.util.Nameable |
| 12 | +import com.lambda.util.math.Vec2d |
| 13 | +import com.mojang.blaze3d.systems.RenderSystem.recordRenderCall |
| 14 | +import net.minecraft.client.gui.DrawContext |
| 15 | +import net.minecraft.client.gui.screen.Screen |
| 16 | +import net.minecraft.text.Text |
| 17 | + |
| 18 | +class LambdaScreen( |
| 19 | + override val name: String, |
| 20 | + val layout: Layout |
| 21 | +) : Screen(Text.of(name)), Nameable, Muteable { |
| 22 | + override val isMuted: Boolean get() = !isOpen |
| 23 | + |
| 24 | + private var screenSize = Vec2d.ZERO |
| 25 | + val isOpen get() = mc.currentScreen == this |
| 26 | + |
| 27 | + init { |
| 28 | + listener<RenderEvent.GUI.Scaled> { event -> |
| 29 | + screenSize = event.screenSize |
| 30 | + layout.onEvent(GuiEvent.Render()) |
| 31 | + } |
| 32 | + |
| 33 | + listener<TickEvent.Pre> { |
| 34 | + layout.onEvent(GuiEvent.Tick()) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + fun show() { |
| 39 | + mc.currentScreen?.close() |
| 40 | + |
| 41 | + recordRenderCall { |
| 42 | + mc.setScreen(this) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + override fun onDisplayed() { |
| 47 | + layout.onEvent(GuiEvent.Show()) |
| 48 | + } |
| 49 | + |
| 50 | + override fun removed() { |
| 51 | + layout.onEvent(GuiEvent.Hide()) |
| 52 | + } |
| 53 | + |
| 54 | + override fun shouldPause() = false |
| 55 | + |
| 56 | + override fun render(context: DrawContext?, mouseX: Int, mouseY: Int, delta: Float) { |
| 57 | + // Let's remove background tint |
| 58 | + } |
| 59 | + |
| 60 | + override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean { |
| 61 | + val translated = KeyCode.virtualMapUS(keyCode, scanCode) |
| 62 | + layout.onEvent(GuiEvent.KeyPress(translated)) |
| 63 | + |
| 64 | + if (keyCode == KeyCode.ESCAPE.keyCode) { |
| 65 | + close() |
| 66 | + } |
| 67 | + |
| 68 | + return true |
| 69 | + } |
| 70 | + |
| 71 | + override fun charTyped(chr: Char, modifiers: Int): Boolean { |
| 72 | + layout.onEvent(GuiEvent.CharTyped(chr)) |
| 73 | + return true |
| 74 | + } |
| 75 | + |
| 76 | + override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean { |
| 77 | + layout.onEvent(GuiEvent.MouseClick(Mouse.Button(button), Mouse.Action.Click, rescaleMouse(mouseX, mouseY))) |
| 78 | + return true |
| 79 | + } |
| 80 | + |
| 81 | + override fun mouseReleased(mouseX: Double, mouseY: Double, button: Int): Boolean { |
| 82 | + layout.onEvent(GuiEvent.MouseClick(Mouse.Button(button), Mouse.Action.Release, rescaleMouse(mouseX, mouseY))) |
| 83 | + return true |
| 84 | + } |
| 85 | + |
| 86 | + override fun mouseMoved(mouseX: Double, mouseY: Double) { |
| 87 | + layout.onEvent(GuiEvent.MouseMove(rescaleMouse(mouseX, mouseY))) |
| 88 | + } |
| 89 | + |
| 90 | + override fun mouseScrolled( |
| 91 | + mouseX: Double, |
| 92 | + mouseY: Double, |
| 93 | + horizontalAmount: Double, |
| 94 | + verticalAmount: Double |
| 95 | + ): Boolean { |
| 96 | + layout.onEvent(GuiEvent.MouseScroll(rescaleMouse(mouseX, mouseY), verticalAmount)) |
| 97 | + return true |
| 98 | + } |
| 99 | + |
| 100 | + private fun rescaleMouse(mouseX: Double, mouseY: Double): Vec2d { |
| 101 | + val mcMouse = Vec2d(mouseX, mouseY) |
| 102 | + val mcWindow = Vec2d(mc.window.scaledWidth, mc.window.scaledHeight) |
| 103 | + |
| 104 | + val uv = mcMouse / mcWindow |
| 105 | + return uv * screenSize |
| 106 | + } |
| 107 | + |
| 108 | + companion object { |
| 109 | + fun Layout.toScreen(name: String) = LambdaScreen(name, this) |
| 110 | + } |
| 111 | +} |
0 commit comments