mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-05-05 17:04:14 +00:00
Add mouse support for the menu in edit and paint (#419)
This commit is contained in:
parent
da419b24e7
commit
9748679484
@ -61,7 +61,12 @@ if peripheral.find("printer") then
|
|||||||
end
|
end
|
||||||
table.insert(tMenuItems, "Exit")
|
table.insert(tMenuItems, "Exit")
|
||||||
|
|
||||||
local sStatus = "Press Ctrl to access menu"
|
local sStatus
|
||||||
|
if term.isColour() then
|
||||||
|
sStatus = "Press Ctrl or click here to access menu"
|
||||||
|
else
|
||||||
|
sStatus = "Press Ctrl to access menu"
|
||||||
|
end
|
||||||
if #sStatus > w - 5 then
|
if #sStatus > w - 5 then
|
||||||
sStatus = "Press Ctrl for menu"
|
sStatus = "Press Ctrl for menu"
|
||||||
end
|
end
|
||||||
@ -725,16 +730,35 @@ while bRunning do
|
|||||||
end
|
end
|
||||||
|
|
||||||
elseif sEvent == "mouse_click" then
|
elseif sEvent == "mouse_click" then
|
||||||
|
local cx, cy = param2, param3
|
||||||
if not bMenu then
|
if not bMenu then
|
||||||
if param == 1 then
|
if param == 1 then
|
||||||
-- Left click
|
-- Left click
|
||||||
local cx, cy = param2, param3
|
|
||||||
if cy < h then
|
if cy < h then
|
||||||
local newY = math.min(math.max(scrollY + cy, 1), #tLines)
|
local newY = math.min(math.max(scrollY + cy, 1), #tLines)
|
||||||
local newX = math.min(math.max(scrollX + cx, 1), #tLines[newY] + 1)
|
local newX = math.min(math.max(scrollX + cx, 1), #tLines[newY] + 1)
|
||||||
setCursor(newX, newY)
|
setCursor(newX, newY)
|
||||||
|
else
|
||||||
|
bMenu = true
|
||||||
|
redrawMenu()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
if cy == h then
|
||||||
|
local nMenuPosEnd = 1
|
||||||
|
local nMenuPosStart = 1
|
||||||
|
for n, sMenuItem in ipairs(tMenuItems) do
|
||||||
|
nMenuPosEnd = nMenuPosEnd + #sMenuItem + 1
|
||||||
|
if cx > nMenuPosStart and cx < nMenuPosEnd then
|
||||||
|
doMenuItem(n)
|
||||||
|
end
|
||||||
|
nMenuPosEnd = nMenuPosEnd + 1
|
||||||
|
nMenuPosStart = nMenuPosEnd
|
||||||
|
end
|
||||||
|
else
|
||||||
|
bMenu = false
|
||||||
|
redrawMenu()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif sEvent == "mouse_scroll" then
|
elseif sEvent == "mouse_scroll" then
|
||||||
|
@ -19,7 +19,7 @@ local canvas = {}
|
|||||||
local mChoices = { "Save", "Exit" }
|
local mChoices = { "Save", "Exit" }
|
||||||
|
|
||||||
-- The message displayed in the footer bar
|
-- The message displayed in the footer bar
|
||||||
local fMessage = "Press Ctrl to access menu"
|
local fMessage = "Press Ctrl or click here to access menu"
|
||||||
|
|
||||||
-------------------------
|
-------------------------
|
||||||
-- Initialisation --
|
-- Initialisation --
|
||||||
@ -252,6 +252,29 @@ local function drawCanvas()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local menu_choices = {
|
||||||
|
Save = function()
|
||||||
|
if bReadOnly then
|
||||||
|
fMessage = "Access denied"
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
local success, err = save(sPath)
|
||||||
|
if success then
|
||||||
|
fMessage = "Saved to " .. sPath
|
||||||
|
else
|
||||||
|
if err then
|
||||||
|
fMessage = "Error saving to " .. err
|
||||||
|
else
|
||||||
|
fMessage = "Error saving to " .. sPath
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end,
|
||||||
|
Exit = function()
|
||||||
|
return true
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
Draws menu options and handles input from within the menu.
|
Draws menu options and handles input from within the menu.
|
||||||
returns: true if the program is to be exited; false otherwise
|
returns: true if the program is to be exited; false otherwise
|
||||||
@ -261,6 +284,7 @@ local function accessMenu()
|
|||||||
local selection = 1
|
local selection = 1
|
||||||
|
|
||||||
term.setBackgroundColour(colours.black)
|
term.setBackgroundColour(colours.black)
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
-- Draw the menu
|
-- Draw the menu
|
||||||
term.setCursorPos(1, h)
|
term.setCursorPos(1, h)
|
||||||
@ -269,27 +293,28 @@ local function accessMenu()
|
|||||||
for k, v in pairs(mChoices) do
|
for k, v in pairs(mChoices) do
|
||||||
if selection == k then
|
if selection == k then
|
||||||
term.setTextColour(colours.yellow)
|
term.setTextColour(colours.yellow)
|
||||||
local ox = term.getCursorPos()
|
term.write("[")
|
||||||
term.write("[" .. string.rep(" ", #v) .. "]")
|
|
||||||
term.setCursorPos(ox + 1, h)
|
|
||||||
term.setTextColour(colours.white)
|
term.setTextColour(colours.white)
|
||||||
term.write(v)
|
term.write(v)
|
||||||
term.setCursorPos(term.getCursorPos() + 1, h)
|
term.setTextColour(colours.yellow)
|
||||||
|
term.write("]")
|
||||||
|
term.setTextColour(colours.white)
|
||||||
else
|
else
|
||||||
term.write(" " .. v .. " ")
|
term.write(" " .. v .. " ")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Handle input in the menu
|
-- Handle input in the menu
|
||||||
local id, key = os.pullEvent("key")
|
local id, param1, param2, param3 = os.pullEvent()
|
||||||
if id == "key" then
|
if id == "key" then
|
||||||
-- S and E are shortcuts
|
local key = param1
|
||||||
if key == keys.s then
|
|
||||||
selection = 1
|
-- Handle menu shortcuts.
|
||||||
key = keys.enter
|
for _, menu_item in ipairs(mChoices) do
|
||||||
elseif key == keys.e then
|
local k = keys[menu_item:sub(1, 1):lower()]
|
||||||
selection = 2
|
if k and k == key then
|
||||||
key = keys.enter
|
return menu_choices[menu_item]()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if key == keys.right then
|
if key == keys.right then
|
||||||
@ -308,29 +333,25 @@ local function accessMenu()
|
|||||||
|
|
||||||
elseif key == keys.enter then
|
elseif key == keys.enter then
|
||||||
-- Select an option
|
-- Select an option
|
||||||
if mChoices[selection] == "Save" then
|
return menu_choices[mChoices[selection]]()
|
||||||
if bReadOnly then
|
|
||||||
fMessage = "Access denied"
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
local success, err = save(sPath)
|
|
||||||
if success then
|
|
||||||
fMessage = "Saved to " .. sPath
|
|
||||||
else
|
|
||||||
if err then
|
|
||||||
fMessage = "Error saving to " .. err
|
|
||||||
else
|
|
||||||
fMessage = "Error saving to " .. sPath
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
elseif mChoices[selection] == "Exit" then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
elseif key == keys.leftCtrl or keys == keys.rightCtrl then
|
elseif key == keys.leftCtrl or keys == keys.rightCtrl then
|
||||||
-- Cancel the menu
|
-- Cancel the menu
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
elseif id == "mouse_click" then
|
||||||
|
local cx, cy = param2, param3
|
||||||
|
if cy ~= h then return false end -- Exit the menu
|
||||||
|
|
||||||
|
local nMenuPosEnd = 1
|
||||||
|
local nMenuPosStart = 1
|
||||||
|
for _, sMenuItem in ipairs(mChoices) do
|
||||||
|
nMenuPosEnd = nMenuPosEnd + #sMenuItem + 1
|
||||||
|
if cx > nMenuPosStart and cx < nMenuPosEnd then
|
||||||
|
return menu_choices[sMenuItem]()
|
||||||
|
end
|
||||||
|
nMenuPosEnd = nMenuPosEnd + 1
|
||||||
|
nMenuPosStart = nMenuPosEnd
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -378,6 +399,10 @@ local function handleEvents()
|
|||||||
canvas[p3][p2] = paintColour
|
canvas[p3][p2] = paintColour
|
||||||
|
|
||||||
drawCanvasPixel(p2, p3)
|
drawCanvasPixel(p2, p3)
|
||||||
|
elseif p3 == h and id == "mouse_click" then
|
||||||
|
-- Open menu
|
||||||
|
programActive = not accessMenu()
|
||||||
|
drawInterface()
|
||||||
end
|
end
|
||||||
elseif id == "key" then
|
elseif id == "key" then
|
||||||
if p1 == keys.leftCtrl or p1 == keys.rightCtrl then
|
if p1 == keys.leftCtrl or p1 == keys.rightCtrl then
|
||||||
|
Loading…
x
Reference in New Issue
Block a user