mouse scroll event for tier two and three screens and supporting it in the editor

This commit is contained in:
Florian Nücke 2013-12-13 09:07:07 +01:00
parent cea0875795
commit 0b1f4464d9
5 changed files with 79 additions and 21 deletions

View File

@ -48,9 +48,11 @@ end
local function setCursor(nbx, nby) local function setCursor(nbx, nby)
local w, h = getSize() local w, h = getSize()
nby = math.max(1, math.min(#buffer, nby))
local ncy = nby - scrollY local ncy = nby - scrollY
if ncy > h then if ncy > h then
term.setCursorBlink(false)
local sy = nby - h local sy = nby - h
local dy = math.abs(scrollY - sy) local dy = math.abs(scrollY - sy)
scrollY = sy scrollY = sy
@ -60,6 +62,7 @@ local function setCursor(nbx, nby)
component.gpu.set(1, by - scrollY, str) component.gpu.set(1, by - scrollY, str)
end end
elseif ncy < 1 then elseif ncy < 1 then
term.setCursorBlink(false)
local sy = nby - 1 local sy = nby - 1
local dy = math.abs(scrollY - sy) local dy = math.abs(scrollY - sy)
scrollY = sy scrollY = sy
@ -69,9 +72,12 @@ local function setCursor(nbx, nby)
component.gpu.set(1, by - scrollY, str) component.gpu.set(1, by - scrollY, str)
end end
end end
term.setCursor(term.getCursor(), nby - scrollY)
nbx = math.max(1, math.min(unicode.len(line()) + 1, nbx))
local ncx = nbx - scrollX local ncx = nbx - scrollX
if ncx > w then if ncx > w then
term.setCursorBlink(false)
local sx = nbx - w local sx = nbx - w
local dx = math.abs(scrollX - sx) local dx = math.abs(scrollX - sx)
scrollX = sx scrollX = sx
@ -82,6 +88,7 @@ local function setCursor(nbx, nby)
component.gpu.set(1 + (w - dx), by - scrollY, str) component.gpu.set(1 + (w - dx), by - scrollY, str)
end end
elseif ncx < 1 then elseif ncx < 1 then
term.setCursorBlink(false)
local sx = nbx - 1 local sx = nbx - 1
local dx = math.abs(scrollX - sx) local dx = math.abs(scrollX - sx)
scrollX = sx scrollX = sx
@ -92,8 +99,8 @@ local function setCursor(nbx, nby)
component.gpu.set(1, by - scrollY, str) component.gpu.set(1, by - scrollY, str)
end end
end end
term.setCursor(nbx - scrollX, nby - scrollY) term.setCursor(nbx - scrollX, nby - scrollY)
component.gpu.set(w - 9, h + 1, text.padLeft(string.format("%d,%d", nby, nbx), 10)) component.gpu.set(w - 9, h + 1, text.padLeft(string.format("%d,%d", nby, nbx), 10))
end end
@ -124,7 +131,7 @@ local function right(n)
local cbx, cby = getCursor() local cbx, cby = getCursor()
local be = unicode.len(line()) + 1 local be = unicode.len(line()) + 1
if cbx < be then if cbx < be then
setCursor(math.min(be, cbx + n), cby) setCursor(cbx + n, cby)
elseif cby < #buffer then elseif cby < #buffer then
setCursor(1, cby + 1) setCursor(1, cby + 1)
end end
@ -134,7 +141,7 @@ local function up(n)
n = n or 1 n = n or 1
local cbx, cby = getCursor() local cbx, cby = getCursor()
if cby > 1 then if cby > 1 then
setCursor(cbx, math.max(cby - n, 1)) setCursor(cbx, cby - n)
if getCursor() > unicode.len(line()) then if getCursor() > unicode.len(line()) then
ende() ende()
end end
@ -145,7 +152,7 @@ local function down(n)
n = n or 1 n = n or 1
local cbx, cby = getCursor() local cbx, cby = getCursor()
if cby < #buffer then if cby < #buffer then
setCursor(cbx, math.min(cby + n, #buffer)) setCursor(cbx, cby + n)
if getCursor() > unicode.len(line()) then if getCursor() > unicode.len(line()) then
ende() ende()
end end
@ -157,6 +164,7 @@ local function delete()
local cbx, cby = getCursor() local cbx, cby = getCursor()
local w, h = getSize() local w, h = getSize()
if cbx <= unicode.len(line()) then if cbx <= unicode.len(line()) then
term.setCursorBlink(false)
buffer[cby] = unicode.sub(line(), 1, cbx - 1) .. buffer[cby] = unicode.sub(line(), 1, cbx - 1) ..
unicode.sub(line(), cbx + 1) unicode.sub(line(), cbx + 1)
component.gpu.copy(cx + 1, cy, w - cx, 1, -1, 0) component.gpu.copy(cx + 1, cy, w - cx, 1, -1, 0)
@ -167,6 +175,7 @@ local function delete()
end end
component.gpu.set(w, cy, char) component.gpu.set(w, cy, char)
elseif cby < #buffer then elseif cby < #buffer then
term.setCursorBlink(false)
local append = table.remove(buffer, cby + 1) local append = table.remove(buffer, cby + 1)
buffer[cby] = buffer[cby] .. append buffer[cby] = buffer[cby] .. append
component.gpu.set(cx, cy, append) component.gpu.set(cx, cy, append)
@ -179,6 +188,10 @@ local function delete()
end end
local function insert(value) local function insert(value)
if not value or unicode.len(value) < 1 then
return
end
term.setCursorBlink(false)
local cx, cy = term.getCursor() local cx, cy = term.getCursor()
local cbx, cby = getCursor() local cbx, cby = getCursor()
local w, h = getSize() local w, h = getSize()
@ -213,7 +226,6 @@ local function enter()
end end
local function onKeyDown(char, code) local function onKeyDown(char, code)
term.setCursorBlink(false)
if code == keyboard.keys.back and not readonly then if code == keyboard.keys.back and not readonly then
if left() then if left() then
delete() delete()
@ -275,12 +287,9 @@ local function onKeyDown(char, code)
elseif not keyboard.isControl(char) and not readonly then elseif not keyboard.isControl(char) and not readonly then
insert(unicode.char(char)) insert(unicode.char(char))
end end
term.setCursorBlink(true)
term.setCursorBlink(true) -- force toggle to caret
end end
local function onClipboard(value) local function onClipboard(value)
term.setCursorBlink(false)
local cbx, cby = getCursor() local cbx, cby = getCursor()
local start = 1 local start = 1
local l = value:find("\n", 1, true) local l = value:find("\n", 1, true)
@ -293,18 +302,15 @@ local function onClipboard(value)
until not l until not l
end end
insert(string.sub(value, start)) insert(string.sub(value, start))
term.setCursorBlink(true)
term.setCursorBlink(true) -- force toggle to caret
end end
local function onClick(x, y) local function onClick(x, y)
local w, h = getSize() setCursor(x + scrollX, y + scrollY)
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 end
local function onScroll(direction)
local cbx, cby = getCursor()
setCursor(cbx, cby - direction * 12)
end end
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -337,14 +343,23 @@ do
end end
while running do 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 if type(address) == "string" and component.isPrimary(address) then
local blink = true
if event == "key_down" then if event == "key_down" then
onKeyDown(arg1, arg2) onKeyDown(arg1, arg2)
elseif event == "clipboard" then elseif event == "clipboard" then
onClipboard(arg1) onClipboard(arg1)
elseif event == "touch" or event == "drag" then elseif event == "touch" or event == "drag" then
onClick(arg1, arg2) 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 end
end end

View File

@ -48,13 +48,25 @@ object PacketSender {
def sendMouseClick(t: Buffer, x: Int, y: Int, drag: Boolean) = def sendMouseClick(t: Buffer, x: Int, y: Int, drag: Boolean) =
if (t.tier > 0) { if (t.tier > 0) {
val pb = new PacketBuilder(PacketType.MouseClick) val pb = new PacketBuilder(PacketType.MouseClickOrDrag)
pb.writeTileEntity(t) pb.writeTileEntity(t)
pb.writeInt(x) pb.writeInt(x)
pb.writeInt(y) pb.writeInt(y)
pb.writeBoolean(drag) 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() pb.sendToServer()
} }
} }

View File

@ -5,6 +5,7 @@ import li.cil.oc.client.renderer.MonospaceFontRenderer
import li.cil.oc.client.renderer.gui.BufferRenderer import li.cil.oc.client.renderer.gui.BufferRenderer
import li.cil.oc.common.tileentity import li.cil.oc.common.tileentity
import li.cil.oc.util.RenderState import li.cil.oc.util.RenderState
import org.lwjgl.input.Mouse
import org.lwjgl.opengl.GL11 import org.lwjgl.opengl.GL11
class Screen(val screen: tileentity.Screen) extends Buffer { 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 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) { override protected def mouseClicked(mouseX: Int, mouseY: Int, button: Int) {
super.mouseClicked(mouseX, mouseY, button) super.mouseClicked(mouseX, mouseY, button)
if (screen.tier > 0) { if (screen.tier > 0) {

View File

@ -29,5 +29,6 @@ object PacketType extends Enumeration {
KeyDown, KeyDown,
KeyUp, KeyUp,
Clipboard, Clipboard,
MouseClick = Value MouseClickOrDrag,
MouseScroll = Value
} }

View File

@ -18,7 +18,8 @@ class PacketHandler extends CommonPacketHandler {
case PacketType.KeyDown => onKeyDown(p) case PacketType.KeyDown => onKeyDown(p)
case PacketType.KeyUp => onKeyUp(p) case PacketType.KeyUp => onKeyUp(p)
case PacketType.Clipboard => onClipboard(p) case PacketType.Clipboard => onClipboard(p)
case PacketType.MouseClick => onMouseClick(p) case PacketType.MouseClickOrDrag => onMouseClick(p)
case PacketType.MouseScroll => onMouseScroll(p)
case _ => // Invalid packet. case _ => // Invalid packet.
} }
@ -76,4 +77,17 @@ class PacketHandler extends CommonPacketHandler {
} }
case _ => // Invalid packet. 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.
}
} }