This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
kylergs's profile picture

[WIP] GUI Early Test *Out-Dated*

Started by kylergs, 18 November 2012 - 04:29 AM
kylergs #1
Posted 18 November 2012 - 05:29 AM
New Post (Sorry, thought I could delete this one):
http://www.computerc...__fromsearch__1



I made a fairly basic GUI program with functions to draw scroll-able lists. It is based off of nitrogen fingers GUI tutorial. It needs some cleaning up as well as quite a few additions before I would do a proper release. Here is the code:

Next thing to add is to read the menu states from a text file (need some help with that), I would also like to add ability to run programs from within the GUI, as well as other things. I would also like to add a history to the program so a 'back' function could work.

Once all of that is done I want it to be able to read the menuoptions from the inbuilt ComputerCraft OS and allow the user to navigate that graphically.

Any idea are welcome.


Edit:
A new version that reads from text files (Basic importation)
Spoiler

--Basic Functions
local user = "guest"
local select = 1
local menustate = "admin"
w,h = term.getSize()
function printCentred(str , h)
  term.setCursorPos(w/2 - #str/2, h)
  term.write(str)
end
function printRight(str , h)
  term.setCursorPos(w - #str, h)
  term.write(str)
end
function printLeft(str , h)
  term.setCursorPos(1, h)
  term.write(str)
end
function returnFileAsTable(aFile) --This function and the two bellow it are credit of Challenge.Accepted on the ComputerCraft Forums
f= fs.open(aFile,"r")
aTable = {}
aLine = f.readLine()
while aLine ~= nil do
  table.insert(aTable, aLine)
  aLine = f.readLine()
end
return aTable
end
function seperateString(aString, sep)
p = string.find(aString, sep)
return string.sub(aString, 1, p-1), string.sub(aString, p+1)
end
function iterateTable(aTable)
local keyTable = {}
local definitionTable = {}
for i=1, #aTable do
  key, def = seperateString(aTable[i], ":")
  table.insert(keyTable, key)
  table.insert(definitionTable, def)
end
return keyTable, definitionTable
end
--Menu state--
local path = "GUI/GUImopt"
fs.makeDir(path)
local mopt = {}
local tAllValues = {}
local i3
local menus = fs.list(path)
if #menus ~= 0 then
for i = 1,#menus do
  mopt[menus[i]] = {}
  tAllValues = returnFileAsTable(path.."/"..menus[i])
  mopt[menus[i]].options = {}
  i3 = 1
  for i2 = 1,#tAllValues,5 do
print(i..":"..i2)
   table.insert(mopt[menus[i]].options, {})
   mopt[menus[i]].options[i3].display = tAllValues[i2]
   mopt[menus[i]].options[i3].link = tAllValues[i2+1]
   mopt[menus[i]].options[i3].ftype = tAllValues[i2+2]
   mopt[menus[i]].options[i3].colour = tonumber(tAllValues[i2+3])
   i3 = i3+1
  end
end
end
--no = assert(loadstring('return '..tAllValues[4]))
sleep(1)
local hist = {}
--Scrolling List--
fitem = 1
rsel = 1
selpos = 5
function drawList(items)
  a = h-6  a = a/2
  if #items > a then l = a
  else l = #items end
  j = 4
  for i = fitem, l+fitem-1 do
   term.setTextColour(items[i].colour)
   printCentred(items[i].display, j)
   j = j +2
  end
  term.setTextColour(items[select].colour)
  local selpos = rsel*2 + 2
  printCentred("[  "..items[select].display.."  ]", selpos)

end
function checkSel(items)
a = h -6  a= a/2

if rsel > a -1 and select<#items then
  rsel = rsel -1
  fitem = fitem+1
elseif rsel == 1 and select > 1 then
  rsel = rsel +1
  fitem = fitem -1
end
end
function changeMenu()
menustate = mopt[menustate].options[select].link
end
function plainText()
end
local typelist = {
["menu"] = {func = changeMenu},
["text"] = {func = plainText},
["special"] = {}
}
--Menus
function drawHeader()
  term.setTextColour(colours.white)
  printCentred("KYLEOS - TEST 0.2", 1)
  printLeft(string.rep("-",w),2)
  printLeft(string.rep("-",w),h-1)
  printLeft(menustate,h)
  printRight("made by kylergs",h)
end
--Run
function runMain()
while true do
  term.clear()
  drawHeader()
  mms = mopt[menustate]
  tftype = mms.options[select].ftype
  if not skip then drawList(mms.options) end
  local id, key = os.pullEvent("key")
  if not skip then
   if key == 200 and select > 1 then select = select-1 rsel=rsel-1 checkSel(mms.options)
   elseif key == 208 and select < #mms.options then select = select + 1 rsel = rsel +1 checkSel(mms.options)  
   elseif key == 28 then
	if mms.options[select].link == "quit" then break end  
	typelist[tftype].func()
   end
  else skip = false
end
end
end
runMain()
term.clear()
term.setCursorPos(1,1)

Edited on 28 November 2012 - 06:40 AM
kintick #2
Posted 20 November 2012 - 07:49 AM
pics?
kylergs #3
Posted 27 November 2012 - 05:40 AM
haha Sorry

Uploaded them now
Tiin57 #4
Posted 27 November 2012 - 05:59 AM
Nice work here. I would +1, but I'm on mobile. :P/>
kylergs #5
Posted 27 November 2012 - 06:08 AM
Thanks, have a look at my other post. It is a better / more recent version (I tried to delete this one)