blob: fa31808c525929cefa534ad7e645a2840dfbd436 [file] [log] [blame] [raw]
package li.cil.oc.client.gui
import li.cil.oc.client.PacketSender
import li.cil.oc.client.renderer.MonospaceFontRenderer
import li.cil.oc.client.renderer.gui.BufferRenderer
import li.cil.oc.common.component
import li.cil.oc.util.RenderState
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiScreen
import org.lwjgl.input.Keyboard
import org.lwjgl.opengl.GL11
import scala.collection.mutable
trait Buffer extends GuiScreen {
protected def buffer: component.Buffer
private val pressedKeys = mutable.Map.empty[Int, Char]
private var currentWidth, currentHeight = -1.0
private var shouldRecompileDisplayLists = true
protected var scale = 0.0
def recompileDisplayLists() {
shouldRecompileDisplayLists = true
}
override def doesGuiPauseGame = false
override def initGui() = {
super.initGui()
MonospaceFontRenderer.init(Minecraft.getMinecraft.renderEngine)
BufferRenderer.init(Minecraft.getMinecraft.renderEngine)
Keyboard.enableRepeatEvents(true)
}
override def onGuiClosed() = {
super.onGuiClosed()
for ((code, char) <- pressedKeys) {
PacketSender.sendKeyUp(buffer.owner, char, code)
}
Keyboard.enableRepeatEvents(false)
}
override def drawScreen(mouseX: Int, mouseY: Int, dt: Float): Unit = {
super.drawScreen(mouseX, mouseY, dt)
if (shouldRecompileDisplayLists) {
val (w, h) = buffer.resolution
if (w != currentWidth || h != currentHeight) {
currentWidth = w
currentHeight = h
scale = changeSize(currentWidth, currentHeight)
}
BufferRenderer.compileText(scale, buffer.lines, buffer.color, buffer.depth)
}
GL11.glPushMatrix()
RenderState.disableLighting()
drawBuffer()
GL11.glPopMatrix()
}
protected def drawBuffer()
override def handleKeyboardInput() {
super.handleKeyboardInput()
val code = Keyboard.getEventKey
if (code != Keyboard.KEY_ESCAPE && code != Keyboard.KEY_F11)
if (code == Keyboard.KEY_INSERT && GuiScreen.isShiftKeyDown) {
if (Keyboard.getEventKeyState)
PacketSender.sendClipboard(buffer.owner, GuiScreen.getClipboardString)
}
else if (Keyboard.getEventKeyState) {
val char = Keyboard.getEventCharacter
PacketSender.sendKeyDown(buffer.owner, char, code)
pressedKeys += code -> char
}
else pressedKeys.remove(code) match {
case Some(char) => PacketSender.sendKeyUp(buffer.owner, char, code)
case _ => // Wasn't pressed while viewing the screen.
}
}
override protected def mouseClicked(x: Int, y: Int, button: Int) {
super.mouseClicked(x, y, button)
if (button == 2)
PacketSender.sendClipboard(buffer.owner, GuiScreen.getClipboardString)
}
protected def changeSize(w: Double, h: Double): Double
}