From 0b1f4464d9bdf8c356d5c8bd12a35d8fa2d31f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Fri, 13 Dec 2013 09:07:07 +0100 Subject: [PATCH] mouse scroll event for tier two and three screens and supporting it in the editor --- assets/opencomputers/lua/rom/bin/edit.lua | 51 +++++++++++++++-------- li/cil/oc/client/PacketSender.scala | 14 ++++++- li/cil/oc/client/gui/Screen.scala | 16 +++++++ li/cil/oc/common/PacketType.scala | 3 +- li/cil/oc/server/PacketHandler.scala | 16 ++++++- 5 files changed, 79 insertions(+), 21 deletions(-) diff --git a/assets/opencomputers/lua/rom/bin/edit.lua b/assets/opencomputers/lua/rom/bin/edit.lua index 162fc3c1f..dd9d8e736 100644 --- a/assets/opencomputers/lua/rom/bin/edit.lua +++ b/assets/opencomputers/lua/rom/bin/edit.lua @@ -48,9 +48,11 @@ end local function setCursor(nbx, nby) local w, h = getSize() + nby = math.max(1, math.min(#buffer, nby)) local ncy = nby - scrollY if ncy > h then + term.setCursorBlink(false) local sy = nby - h local dy = math.abs(scrollY - sy) scrollY = sy @@ -60,6 +62,7 @@ local function setCursor(nbx, nby) component.gpu.set(1, by - scrollY, str) end elseif ncy < 1 then + term.setCursorBlink(false) local sy = nby - 1 local dy = math.abs(scrollY - sy) scrollY = sy @@ -69,9 +72,12 @@ local function setCursor(nbx, nby) component.gpu.set(1, by - scrollY, str) end end + term.setCursor(term.getCursor(), nby - scrollY) + nbx = math.max(1, math.min(unicode.len(line()) + 1, nbx)) local ncx = nbx - scrollX if ncx > w then + term.setCursorBlink(false) local sx = nbx - w local dx = math.abs(scrollX - sx) scrollX = sx @@ -82,6 +88,7 @@ local function setCursor(nbx, nby) component.gpu.set(1 + (w - dx), by - scrollY, str) end elseif ncx < 1 then + term.setCursorBlink(false) local sx = nbx - 1 local dx = math.abs(scrollX - sx) scrollX = sx @@ -92,8 +99,8 @@ local function setCursor(nbx, nby) component.gpu.set(1, by - scrollY, str) end end - term.setCursor(nbx - scrollX, nby - scrollY) + component.gpu.set(w - 9, h + 1, text.padLeft(string.format("%d,%d", nby, nbx), 10)) end @@ -124,7 +131,7 @@ local function right(n) local cbx, cby = getCursor() local be = unicode.len(line()) + 1 if cbx < be then - setCursor(math.min(be, cbx + n), cby) + setCursor(cbx + n, cby) elseif cby < #buffer then setCursor(1, cby + 1) end @@ -134,7 +141,7 @@ local function up(n) n = n or 1 local cbx, cby = getCursor() if cby > 1 then - setCursor(cbx, math.max(cby - n, 1)) + setCursor(cbx, cby - n) if getCursor() > unicode.len(line()) then ende() end @@ -145,7 +152,7 @@ local function down(n) n = n or 1 local cbx, cby = getCursor() if cby < #buffer then - setCursor(cbx, math.min(cby + n, #buffer)) + setCursor(cbx, cby + n) if getCursor() > unicode.len(line()) then ende() end @@ -157,6 +164,7 @@ local function delete() local cbx, cby = getCursor() local w, h = getSize() if cbx <= unicode.len(line()) then + term.setCursorBlink(false) buffer[cby] = unicode.sub(line(), 1, cbx - 1) .. unicode.sub(line(), cbx + 1) component.gpu.copy(cx + 1, cy, w - cx, 1, -1, 0) @@ -167,6 +175,7 @@ local function delete() end component.gpu.set(w, cy, char) elseif cby < #buffer then + term.setCursorBlink(false) local append = table.remove(buffer, cby + 1) buffer[cby] = buffer[cby] .. append component.gpu.set(cx, cy, append) @@ -179,6 +188,10 @@ local function delete() end local function insert(value) + if not value or unicode.len(value) < 1 then + return + end + term.setCursorBlink(false) local cx, cy = term.getCursor() local cbx, cby = getCursor() local w, h = getSize() @@ -213,7 +226,6 @@ local function enter() end local function onKeyDown(char, code) - term.setCursorBlink(false) if code == keyboard.keys.back and not readonly then if left() then delete() @@ -275,12 +287,9 @@ local function onKeyDown(char, code) elseif not keyboard.isControl(char) and not readonly then insert(unicode.char(char)) end - term.setCursorBlink(true) - term.setCursorBlink(true) -- force toggle to caret end local function onClipboard(value) - term.setCursorBlink(false) local cbx, cby = getCursor() local start = 1 local l = value:find("\n", 1, true) @@ -293,18 +302,15 @@ local function onClipboard(value) until not l end insert(string.sub(value, start)) - term.setCursorBlink(true) - term.setCursorBlink(true) -- force toggle to caret end local function onClick(x, y) - local w, h = getSize() - x = math.max(1, math.min(w, x)) - y = math.max(1, math.min(h, #buffer, y)) - setCursor(x, y) - if getCursor() > unicode.len(line()) then - ende() - end + setCursor(x + scrollX, y + scrollY) +end + +local function onScroll(direction) + local cbx, cby = getCursor() + setCursor(cbx, cby - direction * 12) end ------------------------------------------------------------------------------- @@ -337,14 +343,23 @@ do end while running do - local event, address, arg1, arg2 = event.pull() + local event, address, arg1, arg2, arg3 = event.pull() if type(address) == "string" and component.isPrimary(address) then + local blink = true if event == "key_down" then onKeyDown(arg1, arg2) elseif event == "clipboard" then onClipboard(arg1) elseif event == "touch" or event == "drag" then onClick(arg1, arg2) + elseif event == "scroll" then + onScroll(arg3) + else + blink = false + end + if blink then + term.setCursorBlink(true) + term.setCursorBlink(true) -- force toggle to caret end end end diff --git a/li/cil/oc/client/PacketSender.scala b/li/cil/oc/client/PacketSender.scala index 65d3de273..1c0504887 100644 --- a/li/cil/oc/client/PacketSender.scala +++ b/li/cil/oc/client/PacketSender.scala @@ -48,13 +48,25 @@ object PacketSender { def sendMouseClick(t: Buffer, x: Int, y: Int, drag: Boolean) = if (t.tier > 0) { - val pb = new PacketBuilder(PacketType.MouseClick) + val pb = new PacketBuilder(PacketType.MouseClickOrDrag) pb.writeTileEntity(t) pb.writeInt(x) pb.writeInt(y) pb.writeBoolean(drag) + pb.sendToServer() + } + + def sendMouseScroll(t: Buffer, x: Int, y: Int, scroll: Int) = + if (t.tier > 0) { + val pb = new PacketBuilder(PacketType.MouseScroll) + + pb.writeTileEntity(t) + pb.writeInt(x) + pb.writeInt(y) + pb.writeByte(scroll) + pb.sendToServer() } } \ No newline at end of file diff --git a/li/cil/oc/client/gui/Screen.scala b/li/cil/oc/client/gui/Screen.scala index 300d80cbc..c33fa4a7b 100644 --- a/li/cil/oc/client/gui/Screen.scala +++ b/li/cil/oc/client/gui/Screen.scala @@ -5,6 +5,7 @@ import li.cil.oc.client.renderer.MonospaceFontRenderer import li.cil.oc.client.renderer.gui.BufferRenderer import li.cil.oc.common.tileentity import li.cil.oc.util.RenderState +import org.lwjgl.input.Mouse import org.lwjgl.opengl.GL11 class Screen(val screen: tileentity.Screen) extends Buffer { @@ -16,6 +17,21 @@ class Screen(val screen: tileentity.Screen) extends Buffer { private var mx, my = 0 + override def handleMouseInput() { + super.handleMouseInput() + if (Mouse.hasWheel && Mouse.getEventDWheel != 0) { + val mouseX = Mouse.getEventX * width / mc.displayWidth + val mouseY = height - Mouse.getEventY * height / mc.displayHeight - 1 + val bx = (mouseX - x - bufferMargin) / MonospaceFontRenderer.fontWidth + 1 + val by = (mouseY - y - bufferMargin) / MonospaceFontRenderer.fontHeight + 1 + val (bw, bh) = screen.buffer.resolution + if (bx > 0 && by > 0 && bx <= bw && by <= bh) { + val scroll = math.signum(Mouse.getEventDWheel) + PacketSender.sendMouseScroll(buffer.owner, bx, by, scroll) + } + } + } + override protected def mouseClicked(mouseX: Int, mouseY: Int, button: Int) { super.mouseClicked(mouseX, mouseY, button) if (screen.tier > 0) { diff --git a/li/cil/oc/common/PacketType.scala b/li/cil/oc/common/PacketType.scala index 1d03a704b..8d3f95a29 100644 --- a/li/cil/oc/common/PacketType.scala +++ b/li/cil/oc/common/PacketType.scala @@ -29,5 +29,6 @@ object PacketType extends Enumeration { KeyDown, KeyUp, Clipboard, - MouseClick = Value + MouseClickOrDrag, + MouseScroll = Value } \ No newline at end of file diff --git a/li/cil/oc/server/PacketHandler.scala b/li/cil/oc/server/PacketHandler.scala index aded3eb57..d887d2fa7 100644 --- a/li/cil/oc/server/PacketHandler.scala +++ b/li/cil/oc/server/PacketHandler.scala @@ -18,7 +18,8 @@ class PacketHandler extends CommonPacketHandler { case PacketType.KeyDown => onKeyDown(p) case PacketType.KeyUp => onKeyUp(p) case PacketType.Clipboard => onClipboard(p) - case PacketType.MouseClick => onMouseClick(p) + case PacketType.MouseClickOrDrag => onMouseClick(p) + case PacketType.MouseScroll => onMouseScroll(p) case _ => // Invalid packet. } @@ -76,4 +77,17 @@ class PacketHandler extends CommonPacketHandler { } case _ => // Invalid packet. } + + def onMouseScroll(p: PacketParser) = + p.readTileEntity[Buffer]() match { + case Some(s: Screen) => p.player match { + case player: EntityPlayer => + val x = p.readInt() + val y = p.readInt() + val scroll = p.readByte() + s.origin.node.sendToReachable("computer.checked_signal", player, "scroll", Int.box(x), Int.box(y), Int.box(scroll), player.getCommandSenderName) + case _ => + } + case _ => // Invalid packet. + } } \ No newline at end of file