From ae5a793a6b5128efa5e9ac54bc81510a9a2dbec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 14 Sep 2013 17:17:32 +0200 Subject: [PATCH] cleanup and some fixes --- li/cil/oc/client/components/Screen.scala | 3 +- li/cil/oc/common/block/BlockComputer.scala | 6 ++-- li/cil/oc/common/block/BlockScreen.scala | 20 ++++--------- li/cil/oc/common/components/IScreen.scala | 4 +-- .../common/container/ContainerComputer.scala | 1 - li/cil/oc/common/gui/GuiComputer.scala | 8 ++--- li/cil/oc/common/gui/GuiHandler.scala | 13 ++++++--- li/cil/oc/common/gui/GuiScreen.scala | 26 +++++++++++++++++ li/cil/oc/common/gui/ScreenGui.scala | 29 ------------------- .../common/tileentity/TileEntityScreen.scala | 20 ++++--------- li/cil/oc/common/util/TextBuffer.scala | 2 +- .../oc/server/components/GraphicsCard.scala | 4 +++ li/cil/oc/server/components/Screen.scala | 4 ++- 13 files changed, 64 insertions(+), 76 deletions(-) create mode 100644 li/cil/oc/common/gui/GuiScreen.scala delete mode 100644 li/cil/oc/common/gui/ScreenGui.scala diff --git a/li/cil/oc/client/components/Screen.scala b/li/cil/oc/client/components/Screen.scala index bbb468c3c..ba4d13876 100644 --- a/li/cil/oc/client/components/Screen.scala +++ b/li/cil/oc/client/components/Screen.scala @@ -6,8 +6,9 @@ import li.cil.oc.common.util.TextBuffer class Screen(owner: TileEntityScreen) extends IScreen { val buffer = new TextBuffer(40, 24) - + def resolution = buffer.size + def resolution_=(value: (Int, Int)) = { buffer.size = value owner.updateGui(buffer.toString) diff --git a/li/cil/oc/common/block/BlockComputer.scala b/li/cil/oc/common/block/BlockComputer.scala index 35f706d8e..961e6d39a 100644 --- a/li/cil/oc/common/block/BlockComputer.scala +++ b/li/cil/oc/common/block/BlockComputer.scala @@ -3,6 +3,8 @@ package li.cil.oc.common.block import cpw.mods.fml.common.registry.GameRegistry import li.cil.oc.Config import li.cil.oc.CreativeTab +import li.cil.oc.OpenComputers +import li.cil.oc.common.gui.GuiType import li.cil.oc.common.tileentity.TileEntityComputer import net.minecraft.block.Block import net.minecraft.block.material.Material @@ -13,8 +15,6 @@ import net.minecraft.util.MathHelper import net.minecraft.world.IBlockAccess import net.minecraft.world.World import net.minecraftforge.common.ForgeDirection -import li.cil.oc.common.CommonProxy -import li.cil.oc.OpenComputers class BlockComputer extends Block(Config.blockComputerId, Material.iron) { // ----------------------------------------------------------------------- // @@ -79,7 +79,7 @@ class BlockComputer extends Block(Config.blockComputerId, Material.iron) { else { // Start the computer if it isn't already running and open the GUI. world.getBlockTileEntity(x, y, z).asInstanceOf[TileEntityComputer].turnOn() - player.openGui(OpenComputers, 0, world, x, y, z) + player.openGui(OpenComputers, GuiType.Computer.id, world, x, y, z) true } } diff --git a/li/cil/oc/common/block/BlockScreen.scala b/li/cil/oc/common/block/BlockScreen.scala index 2dc7fa523..6c7950b22 100644 --- a/li/cil/oc/common/block/BlockScreen.scala +++ b/li/cil/oc/common/block/BlockScreen.scala @@ -3,12 +3,13 @@ package li.cil.oc.common.block import cpw.mods.fml.common.registry.GameRegistry import li.cil.oc.Config import li.cil.oc.CreativeTab +import li.cil.oc.OpenComputers +import li.cil.oc.common.gui.GuiType import li.cil.oc.common.tileentity.TileEntityScreen import net.minecraft.block.Block import net.minecraft.block.material.Material -import net.minecraft.world.World import net.minecraft.entity.player.EntityPlayer -import li.cil.oc.OpenComputers +import net.minecraft.world.World class BlockScreen extends Block(Config.blockScreenId, Material.iron) { // ----------------------------------------------------------------------- // @@ -28,19 +29,10 @@ class BlockScreen extends Block(Config.blockScreenId, Material.iron) { override def hasTileEntity(metadata: Int) = true override def createTileEntity(world: World, metadata: Int) = new TileEntityScreen(world.isRemote) - + override def onBlockActivated(world: World, x: Int, y: Int, z: Int, player: EntityPlayer, side: Int, hitX: Float, hitY: Float, hitZ: Float) = { -// if (player.isSneaking()) -// if (canWrench(player, x, y, z)) -// setRotation(world, x, y, z, rotation(world, x, y, z) + 1) -// else -// false -// else { - // Start the computer if it isn't already running and open the GUI. -// world.getBlockTileEntity(x, y, z).asInstanceOf[TileEntityComputer].turnOn() - player.openGui(OpenComputers, 0, world, x, y, z) - true -// } + player.openGui(OpenComputers, GuiType.Screen.id, world, x, y, z) + true } } \ No newline at end of file diff --git a/li/cil/oc/common/components/IScreen.scala b/li/cil/oc/common/components/IScreen.scala index bdfbfa1ba..c60e47ab6 100644 --- a/li/cil/oc/common/components/IScreen.scala +++ b/li/cil/oc/common/components/IScreen.scala @@ -3,9 +3,9 @@ package li.cil.oc.common.components import li.cil.oc.server.components.IComponent trait IScreen { - def resolution_=(value: (Int, Int)): Unit + def resolution: (Int, Int) - def resolution:(Int,Int) // Required for setter. + def resolution_=(value: (Int, Int)): Unit def set(col: Int, row: Int, s: String): Unit diff --git a/li/cil/oc/common/container/ContainerComputer.scala b/li/cil/oc/common/container/ContainerComputer.scala index 88dd57c46..087ccdfe5 100644 --- a/li/cil/oc/common/container/ContainerComputer.scala +++ b/li/cil/oc/common/container/ContainerComputer.scala @@ -6,7 +6,6 @@ import net.minecraft.inventory.Slot import net.minecraft.item.ItemStack class ContainerComputer(playerInventory: InventoryPlayer, computer: TileEntityComputer) extends GenericInventoryContainer(playerInventory, computer) { - // Show the computer's inventory. // TODO nicer layout, separate for types, based on background image once it exists for (slotY <- 0 until 3) { diff --git a/li/cil/oc/common/gui/GuiComputer.scala b/li/cil/oc/common/gui/GuiComputer.scala index f33f39d45..7d6eab1ea 100644 --- a/li/cil/oc/common/gui/GuiComputer.scala +++ b/li/cil/oc/common/gui/GuiComputer.scala @@ -5,7 +5,6 @@ import org.lwjgl.opengl.GL11 import li.cil.oc.common.container.ContainerComputer import li.cil.oc.common.tileentity.TileEntityComputer import net.minecraft.client.gui.GuiButton -import net.minecraft.client.gui.GuiTextField import net.minecraft.client.gui.inventory.GuiContainer import net.minecraft.entity.player.InventoryPlayer import net.minecraft.util.ResourceLocation @@ -13,7 +12,7 @@ import net.minecraft.util.StatCollector class GuiComputer(inventory: InventoryPlayer, val tileEntity: TileEntityComputer) extends GuiContainer(new ContainerComputer(inventory, tileEntity)) { val button = new GuiButton(1, 5, 4, "test") - System.out.println("new Gui") + override def drawGuiContainerForegroundLayer(mouseX: Int, mouseY: Int) = { //draw text and stuff here //the parameters for drawString are: string, x, y, color @@ -23,7 +22,6 @@ class GuiComputer(inventory: InventoryPlayer, val tileEntity: TileEntityComputer } override def drawGuiContainerBackgroundLayer(dt: Float, mouseX: Int, mouseY: Int) = { - //draw your Gui here, only thing you need to change is the path GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); this.mc.renderEngine.func_110577_a(new ResourceLocation("")); val x = (width - xSize) / 2 @@ -33,8 +31,8 @@ class GuiComputer(inventory: InventoryPlayer, val tileEntity: TileEntityComputer override def drawScreen(mouseX: Int, mouseY: Int, dt: Float) = { super.drawScreen(mouseX, mouseY, dt); - button.drawButton(this.mc, mouseX, mouseY) - } + + override def doesGuiPauseGame = false } \ No newline at end of file diff --git a/li/cil/oc/common/gui/GuiHandler.scala b/li/cil/oc/common/gui/GuiHandler.scala index aed5c93d2..2dfc55dca 100644 --- a/li/cil/oc/common/gui/GuiHandler.scala +++ b/li/cil/oc/common/gui/GuiHandler.scala @@ -3,9 +3,14 @@ package li.cil.oc.common.gui import cpw.mods.fml.common.network.IGuiHandler import li.cil.oc.common.container.ContainerComputer import li.cil.oc.common.tileentity.TileEntityComputer +import li.cil.oc.common.tileentity.TileEntityScreen import net.minecraft.entity.player.EntityPlayer import net.minecraft.world.World -import li.cil.oc.common.tileentity.TileEntityScreen + +object GuiType extends Enumeration { + val Computer = Value("Computer") + val Screen = Value("Screen") +} class GuiHandler extends IGuiHandler { override def getServerGuiElement(id: Int, player: EntityPlayer, world: World, x: Int, y: Int, z: Int) = @@ -17,10 +22,10 @@ class GuiHandler extends IGuiHandler { override def getClientGuiElement(id: Int, player: EntityPlayer, world: World, x: Int, y: Int, z: Int) = world.getBlockTileEntity(x, y, z) match { - case tileEntity: TileEntityComputer => + case tileEntity: TileEntityComputer if id == GuiType.Computer.id => new GuiComputer(player.inventory, tileEntity) - case tileEntity:TileEntityScreen => - new ScreenGui(tileEntity) + case tileEntity: TileEntityScreen if id == GuiType.Screen.id => + new GuiScreen(tileEntity) case _ => null } } \ No newline at end of file diff --git a/li/cil/oc/common/gui/GuiScreen.scala b/li/cil/oc/common/gui/GuiScreen.scala new file mode 100644 index 000000000..41c5f42d1 --- /dev/null +++ b/li/cil/oc/common/gui/GuiScreen.scala @@ -0,0 +1,26 @@ +package li.cil.oc.common.gui + +import li.cil.oc.common.tileentity.TileEntityScreen + +class GuiScreen(val tileEntity: TileEntityScreen) extends net.minecraft.client.gui.GuiScreen { + tileEntity.gui = Some(this) + var textField: GuiMultilineTextField = null + + override def initGui() = { + super.initGui() + val (w, h) = tileEntity.component.resolution + val (pixelWidth, pixelHeight) = (w * 5, h * 8) + val x = (width - pixelWidth) / 2 + val y = (height - pixelHeight) / 2 + textField = new GuiMultilineTextField( + this.fontRenderer, x, y, pixelWidth, pixelHeight) + textField.setText(tileEntity.component.toString) + } + + override def drawScreen(mouseX: Int, mouseY: Int, dt: Float) = { + super.drawScreen(mouseX, mouseY, dt); + textField.drawTextBox() + } + + override def doesGuiPauseGame = false +} \ No newline at end of file diff --git a/li/cil/oc/common/gui/ScreenGui.scala b/li/cil/oc/common/gui/ScreenGui.scala deleted file mode 100644 index c2084eb03..000000000 --- a/li/cil/oc/common/gui/ScreenGui.scala +++ /dev/null @@ -1,29 +0,0 @@ -package li.cil.oc.common.gui - -import net.minecraft.client.gui.GuiScreen -import li.cil.oc.common.tileentity.TileEntityScreen - -class ScreenGui(val tileEntity: TileEntityScreen) extends GuiScreen { - tileEntity.gui_=(this) - var textField: GuiMultilineTextField = null - - override def initGui() = { - super.initGui() - var(w,h) = tileEntity.component.resolution - println(" widht: "+w) - println("heigth:" +h) - w *=2 - h *=2 - var x = (width - w)/2 - var y = (height -h)/2 - - textField = new GuiMultilineTextField(this.fontRenderer, x, y, w, h) - textField.setText(tileEntity.text) - } - - override def drawScreen(mouseX: Int, mouseY: Int, dt: Float) = { - super.drawScreen(mouseX, mouseY, dt); - - textField.drawTextBox() - } -} \ No newline at end of file diff --git a/li/cil/oc/common/tileentity/TileEntityScreen.scala b/li/cil/oc/common/tileentity/TileEntityScreen.scala index a1a8debc9..9556caf56 100644 --- a/li/cil/oc/common/tileentity/TileEntityScreen.scala +++ b/li/cil/oc/common/tileentity/TileEntityScreen.scala @@ -2,10 +2,9 @@ package li.cil.oc.common.tileentity import cpw.mods.fml.relauncher._ import li.cil.oc.client.components.{ Screen => ClientScreen } +import li.cil.oc.common.gui.GuiScreen import li.cil.oc.server.components.{ Screen => ServerScreen } import net.minecraft.tileentity.TileEntity -import li.cil.oc.common.gui.ScreenGui -import li.cil.oc.common.gui.ScreenGui class TileEntityScreen(isClient: Boolean) extends TileEntity { def this() = this(false) @@ -14,18 +13,9 @@ class TileEntityScreen(isClient: Boolean) extends TileEntity { if (isClient) new ClientScreen(this) else new ServerScreen(this) - @SideOnly(Side.CLIENT) - def updateGui(value: () => String): Unit = { - // TODO if GUI is open, call value() to get the new display string and show it - println("CLIENT SCREEN: " + value()) - if(_gui != null){ - _gui.textField.setText(value()) - } - } + var gui: Option[GuiScreen] = None - private var _gui:ScreenGui = null - def gui = _gui - def gui_=(value:ScreenGui):Unit = _gui = value - - def text = component.toString() + @SideOnly(Side.CLIENT) + def updateGui(value: () => String): Unit = + gui.foreach(_.textField.setText(value())) } \ No newline at end of file diff --git a/li/cil/oc/common/util/TextBuffer.scala b/li/cil/oc/common/util/TextBuffer.scala index 8b1fd17c0..588d91d97 100644 --- a/li/cil/oc/common/util/TextBuffer.scala +++ b/li/cil/oc/common/util/TextBuffer.scala @@ -14,7 +14,7 @@ class TextBuffer(var width: Int, var height: Int) { def size_=(value: (Int, Int)): Unit = { val (w, h) = value val nbuffer = Array.fill(h, w)(' ') - (0 to (h min height)) foreach { + (0 until (h min height)) foreach { y => Array.copy(buffer(y), 0, nbuffer(y), 0, w min width) } buffer = nbuffer diff --git a/li/cil/oc/server/components/GraphicsCard.scala b/li/cil/oc/server/components/GraphicsCard.scala index 9c0588bf1..039cedcd5 100644 --- a/li/cil/oc/server/components/GraphicsCard.scala +++ b/li/cil/oc/server/components/GraphicsCard.scala @@ -58,6 +58,10 @@ class GraphicsCard(val nbt: NBTTagCompound) extends IComponent { def bind(value: Option[Screen]): Unit = { screen = value + screen.foreach(screen => { + screen.resolution = resolution + fill(0, 0, buffer.width, buffer.height, ' ') + }) writeToNBT() } diff --git a/li/cil/oc/server/components/Screen.scala b/li/cil/oc/server/components/Screen.scala index c050d86b4..7aed053e3 100644 --- a/li/cil/oc/server/components/Screen.scala +++ b/li/cil/oc/server/components/Screen.scala @@ -7,11 +7,13 @@ import li.cil.oc.server.PacketSender class Screen(val owner: TileEntityScreen) extends IScreen with IComponent { id = 2 + def resolution = throw new NotImplementedError + def resolution_=(value: (Int, Int)) = { val (w, h) = value PacketSender.sendScreenResolutionChange(owner, w, h) } - def resolution = throw new NotImplementedError + def set(col: Int, row: Int, s: String) = PacketSender.sendScreenSet(owner, col, row, s)