Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This is for OpenComputers
- -- You can find the ComputerCraft version here: http://pastebin.com/hWNaWTz6
- --[[
- Copyright (c) 2016, Zsolt Tóth
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- ]]
- local term = require("term")
- local component = require("component")
- local gpu = component.gpu
- local keyboard = require("keyboard")
- local event = require("event")
- local white = 0xFFFFFF
- local black = 0x000000
- local menusys = { }
- -- ONE DIMENSIONAL MENUS
- local function displayChoice(choices,state,intro,outro)
- term.clear()
- -- Write out the intro.
- if intro ~= nil then
- if type(intro) == "function" then
- intro()
- else
- print(intro .. "\n")
- end
- else
- print("\n\n")
- end
- -- Present the choices
- for key,value in ipairs(choices) do
- x0,y0 = gpu.getResolution()
- x1,y1 = term.getCursor()
- y1 = y1 + 1
- local tvalue = ""
- if type(value) == "function" then
- tvalue = value()
- else
- tvalue = value
- end
- x1 = math.floor((x0 / 2) - ((string.len(tvalue) + 2) / 2))
- term.setCursor(x1,y1)
- if key == state then
- gpu.setBackground(white)
- gpu.setForeground(black)
- term.write(" " .. tvalue .. " ")
- gpu.setForeground(white)
- gpu.setBackground(black)
- else
- term.write(" " .. tvalue .. " ")
- end
- end
- -- Write out the outro.
- if outro ~= nil then
- if type(outro) == "function" then
- outro()
- else
- print("\n\n" .. outro)
- end
- else
- print("\n\n")
- end
- end
- function menusys.displayMenu(choices,intro,outro,init)
- local status = 1
- if init ~= nil then
- status = init
- end
- local statmax = #choices
- local update = false
- displayChoice(choices,status,intro,outro)
- while true do
- local ev, address, char, code, userName = event.pull("key_down")
- if code == keyboard.keys.space or code == keyboard.keys.enter then
- return status
- elseif code == keyboard.keys.back then
- return 0
- elseif code == keyboard.keys.up then
- status = status - 1
- update = true
- elseif code == keyboard.keys.down then
- status = status + 1
- update = true
- end
- if update == true then
- status = status % statmax
- if status <= 0 then
- status = statmax
- end
- displayChoice(choices,status,intro,outro)
- update = false
- end
- end
- end
- -- TWO-DIMENSIONAL MENUS
- local function displayChoiceMult(choices,xstate,ystate,intro,outro,sizex)
- term.clear()
- x0,y0 = gpu.getResolution()
- x0 = math.floor(x0 / sizex) -- We are going to divide by segments.
- -- Write out the intro.
- if intro ~= nil then
- if type(intro) == "function" then
- intro()
- else
- print(intro .. "\n")
- end
- else
- print("\n\n")
- end
- -- Present the choices
- for ykey,yvalue in ipairs(choices) do
- x1,y1 = term.getCursor()
- y1 = y1 + 1
- for xkey,xvalue in ipairs(yvalue) do
- local tvalue = ""
- if type(xvalue) == "function" then
- tvalue = xvalue()
- else
- tvalue = xvalue
- end
- x1 = math.floor( (x0 / 2) - ((string.len(tvalue) + 2) / 2) + (x0 * (xkey - 1) ) )
- term.setCursor(x1,y1)
- if ykey == ystate and xkey == xstate then
- gpu.setBackground(white)
- gpu.setForeground(black)
- term.write(" " .. tvalue .. " ")
- gpu.setBackground(black)
- gpu.setForeground(white)
- else
- term.write(" " .. tvalue .. " ")
- end
- end
- end
- -- Write out the outro.
- if outro ~= nil then
- if type(outro) == "function" then
- outro()
- else
- print("\n\n" .. outro)
- end
- else
- print("\n\n")
- end
- end
- function menusys.displayMenuMult(choices,intro,outro,initx,inity)
- local x_status = 1
- local y_status = 1
- if initx ~= nil then
- x_status = initx
- end
- if inity ~= nil then
- y_status = inity
- end
- local y_statmax = #choices
- local x_size = 0
- for ykey,yvalue in ipairs(choices) do
- local temp = #yvalue
- if temp > x_size then
- x_size = temp
- end
- end
- local update = false
- displayChoiceMult(choices,x_status,y_status,intro,outro,x_size)
- while true do
- local ev, address, char, code, userName = event.pull("key_down")
- if code == keyboard.keys.space or code == keyboard.keys.enter then
- return x_status,y_status
- elseif code == keyboard.keys.back then
- return 0,0
- elseif code == keyboard.keys.up then
- y_status = y_status - 1
- update = true
- elseif code == keyboard.keys.left then
- x_status = x_status - 1
- update = true
- elseif code == keyboard.keys.down then
- y_status = y_status + 1
- update = true
- elseif code == keyboard.keys.right then
- x_status = x_status + 1
- update = true
- end
- if update == true then
- y_status = y_status % y_statmax
- if y_status <= 0 then
- y_status = y_statmax
- end
- local x_statmax = #choices[y_status]
- x_status = x_status % x_statmax
- if x_status <= 0 then
- x_status = x_statmax
- end
- displayChoiceMult(choices,x_status,y_status,intro,outro,x_size)
- update = false
- end
- end
- end
- return menusys
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement