non-global clipboard, ad hoc crafting

This commit is contained in:
kepler155c
2017-10-14 03:39:47 -04:00
parent 3734afed92
commit 1f31f7ef40
3 changed files with 208 additions and 183 deletions

View File

@@ -675,7 +675,7 @@ function listingPage:eventHandler(event)
UI:setPage('learn') UI:setPage('learn')
elseif event.type == 'craft' then elseif event.type == 'craft' then
UI:setPage('craft') UI:setPage('craft', self.grid:getSelected())
elseif event.type == 'forget' then elseif event.type == 'forget' then
local item = self.grid:getSelected() local item = self.grid:getSelected()
@@ -850,7 +850,7 @@ end
local craftPage = UI.Dialog { local craftPage = UI.Dialog {
height = 6, width = UI.term.width - 10, height = 6, width = UI.term.width - 10,
title = 'Enter amount to craft', title = 'Enter amount to craft',
idField = UI.TextEntry { count = UI.TextEntry {
x = 15, x = 15,
y = 3, y = 3,
width = 10, width = 10,
@@ -874,7 +874,8 @@ function craftPage:draw()
self:write(6, 3, 'Quantity') self:write(6, 3, 'Quantity')
end end
function craftPage:enable() function craftPage:enable(item)
self.item = item
craftingPaused = true craftingPaused = true
self:focusFirst() self:focusFirst()
UI.Dialog.enable(self) UI.Dialog.enable(self)
@@ -888,7 +889,15 @@ end
function craftPage:eventHandler(event) function craftPage:eventHandler(event)
if event.type == 'cancel' then if event.type == 'cancel' then
UI:setPreviousPage() UI:setPreviousPage()
--elseif event.type == 'accept' then elseif event.type == 'accept' then
local key = uniqueKey(self.item)
local craftList = { }
craftList[key] = Util.shallowCopy(self.item)
craftList[key].count = tonumber(self.count.value)
craftingPaused = false
craftItems(craftList, inventoryAdapter:listItems())
UI:setPreviousPage()
else else
return UI.Dialog.eventHandler(self, event) return UI.Dialog.eventHandler(self, event)
end end

View File

@@ -1,4 +1,12 @@
shell.setCompletionFunction(shell.getRunningProgram(), function(shell, index, text) local colors = _G.colors
local fs = _G.fs
local keys = _G.keys
local multishell = _ENV.multishell
local os = _G.os
local shell = _ENV.shell
local term = _G.term
shell.setCompletionFunction(shell.getRunningProgram(), function(_, index, text)
if index == 1 then if index == 1 then
return fs.complete(text, shell.dir(), true, false) return fs.complete(text, shell.dir(), true, false)
end end
@@ -32,15 +40,19 @@ local isError
local fileInfo local fileInfo
local dirty = { y = 1, ey = h } local dirty = { y = 1, ey = h }
local mark = { anchor, active, continue } local mark = { }
local keyboard local keyboard
local searchPattern local searchPattern
local undo = { chain = { }, pointer = 0 } local undo = { chain = { }, pointer = 0 }
local complete = { } local complete = { }
local clipboard
if not clipboard then -- do we need a clipboard shim
_G.clipboard = { internal, data } if not multishell or not multishell.hook then -- is this OpusOS ?
clipboard.shim = true if _G.clipboard then -- has it been installed already
clipboard = _G.clipboard
else
clipboard = { }
function clipboard.setData(data) function clipboard.setData(data)
clipboard.data = data clipboard.data = data
@@ -64,6 +76,9 @@ if not clipboard then
clipboard.internal = mode clipboard.internal = mode
end end
end end
_G.clipboard = clipboard
end
end end
local color = { local color = {
@@ -110,7 +125,7 @@ local keyMapping = {
[ 'control-up' ] = 'scroll_up', [ 'control-up' ] = 'scroll_up',
[ 'scrollDown' ] = 'scroll_down', [ 'scrollDown' ] = 'scroll_down',
[ 'control-down' ] = 'scroll_down', [ 'control-down' ] = 'scroll_down',
[ 'mouse_click' ] = 'goto', [ 'mouse_click' ] = 'go_to',
[ 'control-l' ] = 'goto_line', [ 'control-l' ] = 'goto_line',
-- marking -- marking
@@ -140,7 +155,7 @@ local keyMapping = {
[ 'control-x' ] = 'cut', [ 'control-x' ] = 'cut',
[ 'control-c' ] = 'copy', [ 'control-c' ] = 'copy',
[ 'control-v' ] = 'paste', [ 'control-v' ] = 'paste',
[ 'control-t' ] = 'toggle_clipboard', [ 'control-m' ] = 'toggle_clipboard',
-- file -- file
[ 'control-s' ] = 'save', [ 'control-s' ] = 'save',
@@ -246,7 +261,7 @@ local function save( _sPath )
local function innerSave() local function innerSave()
file = fs.open( _sPath, "w" ) file = fs.open( _sPath, "w" )
if file then if file then
for n, sLine in ipairs( tLines ) do for _,sLine in ipairs( tLines ) do
file.write(sLine .. "\n") file.write(sLine .. "\n")
end end
else else
@@ -388,10 +403,13 @@ local function redraw()
end end
if not (w < 32 and #sStatus > 0) then if not (w < 32 and #sStatus > 0) then
local clipboardIndicator = 'S' local clipboardIndicator = ''
if clipboard then
clipboardIndicator = 'S'
if clipboard.isInternal() then if clipboard.isInternal() then
clipboardIndicator = 'I' clipboardIndicator = 'I'
end end
end
local modifiedIndicator = ' ' local modifiedIndicator = ' '
if undo.chain[1] then if undo.chain[1] then
@@ -485,13 +503,6 @@ local __actions = {
addUndo = function(entry) addUndo = function(entry)
local last = undo.chain[#undo.chain] local last = undo.chain[#undo.chain]
if last and last.action == entry.action then if last and last.action == entry.action then
--[[
debug('---')
debug(last)
debug(last.args)
debug(entry)
debug(entry.args)
]]--
if last.action == 'deleteText' then if last.action == 'deleteText' then
if last.args[3] == entry.args[1] and if last.args[3] == entry.args[1] and
last.args[4] == entry.args[2] then last.args[4] == entry.args[2] then
@@ -575,7 +586,7 @@ local __actions = {
goto_line = function() goto_line = function()
local lineNo = tonumber(actions.input('Line: ')) local lineNo = tonumber(actions.input('Line: '))
if lineNo then if lineNo then
actions.goto(1, lineNo) actions.go_to(1, lineNo)
else else
setStatus('Invalid line number') setStatus('Invalid line number')
end end
@@ -593,9 +604,9 @@ local __actions = {
if ny < y or ny == y and nx <= x then if ny < y or ny == y and nx <= x then
setStatus(messages.wrapped) setStatus(messages.wrapped)
end end
actions.goto(nx, ny) actions.go_to(nx, ny)
actions.mark_to(nx + #pattern, ny) actions.mark_to(nx + #pattern, ny)
actions.goto(nx, ny) actions.go_to(nx, ny)
return return
end end
sx = 1 sx = 1
@@ -625,7 +636,7 @@ local __actions = {
if bReadOnly then if bReadOnly then
setError("Access denied") setError("Access denied")
else else
local ok, err = save(sPath) local ok = save(sPath)
if ok then if ok then
setStatus('"%s" %dL, %dC written', setStatus('"%s" %dL, %dC written',
fileInfo.path, #tLines, fs.getSize(fileInfo.abspath)) fileInfo.path, #tLines, fs.getSize(fileInfo.abspath))
@@ -641,7 +652,7 @@ local __actions = {
run = function() run = function()
local sTempPath = "/.temp" local sTempPath = "/.temp"
local ok, err = save(sTempPath) local ok = save(sTempPath)
if ok then if ok then
local nTask = shell.openTab(sTempPath) local nTask = shell.openTab(sTempPath)
if nTask then if nTask then
@@ -732,7 +743,7 @@ local __actions = {
mark_to = function(nx, ny) mark_to = function(nx, ny)
actions.mark_begin() actions.mark_begin()
actions.goto(nx, ny) actions.go_to(nx, ny)
actions.mark_finish() actions.mark_finish()
end, end,
@@ -795,9 +806,7 @@ local __actions = {
actions.dirty_all() actions.dirty_all()
end, end,
setCursor = function(newX, newY) setCursor = function()
local oldX, oldY = lastPos.x, lastPos.y
lastPos.x = x lastPos.x = x
lastPos.y = y lastPos.y = y
@@ -806,27 +815,23 @@ local __actions = {
if screenX < 1 then if screenX < 1 then
scrollX = x - 1 scrollX = x - 1
screenX = 1
actions.dirty_all() actions.dirty_all()
elseif screenX > w then elseif screenX > w then
scrollX = x - w scrollX = x - w
screenX = w
actions.dirty_all() actions.dirty_all()
end end
if screenY < 1 then if screenY < 1 then
scrollY = y - 1 scrollY = y - 1
screenY = 1
actions.dirty_all() actions.dirty_all()
elseif screenY > h - 1 then elseif screenY > h - 1 then
scrollY = y - (h - 1) scrollY = y - (h - 1)
screenY = h - 1
actions.dirty_all() actions.dirty_all()
end end
end, end,
top = function() top = function()
actions.goto(1, 1) actions.go_to(1, 1)
end, end,
bottom = function() bottom = function()
@@ -856,11 +861,11 @@ local __actions = {
end, end,
pageUp = function() pageUp = function()
actions.goto(x, y - (h - 1)) actions.go_to(x, y - (h - 1))
end, end,
pageDown = function() pageDown = function()
actions.goto(x, y + (h - 1)) actions.go_to(x, y + (h - 1))
end, end,
home = function() home = function()
@@ -966,7 +971,7 @@ local __actions = {
local front = tLines[sy]:sub(1, sx - 1) local front = tLines[sy]:sub(1, sx - 1)
local back = tLines[ey]:sub(ex, #tLines[ey]) local back = tLines[ey]:sub(ex, #tLines[ey])
for k = 2, ey - sy + 1 do for _ = 2, ey - sy + 1 do
table.remove(tLines, y + 1) table.remove(tLines, y + 1)
end end
tLines[y] = front .. back tLines[y] = front .. back
@@ -981,7 +986,7 @@ local __actions = {
local count = 0 local count = 0
local lines = { } local lines = { }
for y = csy, cey do for _ = csy, cey do
local line = tLines[y] local line = tLines[y]
if line then if line then
local x = 1 local x = 1
@@ -1042,22 +1047,25 @@ local __actions = {
end, end,
toggle_clipboard = function() toggle_clipboard = function()
if clipboard.shim then if clipboard then
clipboard.setInternal(not clipboard.internal) clipboard.setInternal(not clipboard.internal)
end
if clipboard.isInternal() then if clipboard.isInternal() then
setStatus('Using internal clipboard') setStatus('Using internal clipboard')
else else
setStatus('Using system clipboard') setStatus('Using system clipboard')
end end
end
end, end,
copy_marked = function() copy_marked = function()
local text, size = local text, size = actions.copyText(mark.x, mark.y, mark.ex, mark.ey)
actions.copyText(mark.x, mark.y, mark.ex, mark.ey) if clipboard then
clipboard.setData(text) clipboard.setData(text)
setStatus('%d chars copied', size)
clipboard.useInternal(true) clipboard.useInternal(true)
else
os.queueEvent('clipboard_copy', text)
end
setStatus('%d chars copied', size)
end, end,
cut = function() cut = function()
@@ -1078,7 +1086,7 @@ local __actions = {
if mark.active then if mark.active then
actions.delete() actions.delete()
end end
if clipboard.isInternal() then if clipboard and clipboard.isInternal() then
text = clipboard.getText() text = clipboard.getText()
end end
if text then if text then
@@ -1089,7 +1097,7 @@ local __actions = {
end end
end, end,
goto = function(cx, cy) go_to = function(cx, cy)
y = math.min(math.max(cy, 1), #tLines) y = math.min(math.max(cy, 1), #tLines)
x = math.min(math.max(cx, 1), #tLines[y] + 1) x = math.min(math.max(cx, 1), #tLines[y] + 1)
end, end,
@@ -1120,8 +1128,7 @@ load(sPath)
term.setCursorBlink(true) term.setCursorBlink(true)
redraw() redraw()
if not keyboard then keyboard = { }
keyboard = { control, shift, combo }
function keyboard:translate(event, code) function keyboard:translate(event, code)
if event == 'key' then if event == 'key' then
@@ -1129,14 +1136,18 @@ if not keyboard then
if ch then if ch then
if code == keys.leftCtrl or code == keys.rightCtrl then if code == keys.leftCtrl or code == keys.rightCtrl then
if not self.control then
self.control = true self.control = true
self.combo = false self.combo = false
end
return return
end end
if code == keys.leftShift or code == keys.rightShift then if code == keys.leftShift or code == keys.rightShift then
if not self.shift then
self.shift = true self.shift = true
self.combo = false self.combo = false
end
return return
end end
@@ -1213,7 +1224,6 @@ if not keyboard then
return event return event
end end
end end
end
while bRunning do while bRunning do
local sEvent, param, param2, param3 = os.pullEventRaw() local sEvent, param, param2, param3 = os.pullEventRaw()

View File

@@ -1,8 +1,11 @@
requireInjector(getfenv(1)) _G.requireInjector()
local Point = require('point') local Point = require('point')
local Util = require('util') local Util = require('util')
local os = _G.os
local turtle = _G.turtle
local checkedNodes, nodes local checkedNodes, nodes
local function addNode(node) local function addNode(node)
@@ -47,7 +50,7 @@ local function findObsidian()
if turtle.getItemCount(16) > 0 then if turtle.getItemCount(16) > 0 then
print('Inventory full') print('Inventory full')
print('Enter to continue...') print('Enter to continue...')
read() _G.read()
end end
if b and b.name == 'minecraft:obsidian' then if b and b.name == 'minecraft:obsidian' then
@@ -63,18 +66,19 @@ local function findObsidian()
break break
end end
local node = Point.closest(turtle.point, nodes) node = Point.closest(turtle.point, nodes)
if not turtle.gotoPoint(node) then if not turtle.gotoPoint(node) then
break break
end end
until turtle.abort until turtle.abort
end end
turtle.run(function()
turtle.reset() turtle.reset()
turtle.setPolicy(turtle.policies.digOnly) turtle.setPolicy(turtle.policies.digOnly)
local s, m = turtle.run(function()
repeat
local s, m = pcall(function()
repeat
checkedNodes = { } checkedNodes = { }
nodes = { } nodes = { }
@@ -87,7 +91,7 @@ local s, m = turtle.run(function()
if not turtle.select('minecraft:water_bucket') then if not turtle.select('minecraft:water_bucket') then
break break
end end
turtle.goto(0, 0) turtle._goto(0, 0)
turtle.placeDown() turtle.placeDown()
os.sleep(2) os.sleep(2)
turtle.placeDown() turtle.placeDown()
@@ -95,8 +99,10 @@ local s, m = turtle.run(function()
turtle.select(1) turtle.select(1)
until turtle.abort until turtle.abort
end) end)
turtle.goto(0, 0, 0, 0)
turtle.reset()
if not s and m then if not s and m then
error(m) error(m)
end end
turtle._goto(0, 0, 0, 0)
end)