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

[Lua] [Question] Trying to combine Wraithbone's GUI functions on startup

Started by pivotdude, 04 June 2013 - 01:44 AM
pivotdude #1
Posted 04 June 2013 - 03:44 AM
Hi i am trying to get some simple programs and some of Wraithbone's simple GUI functions to work together on startup on an Advanced Computer , I am not getting errors and the time and menus will only execute manually and show blank on startup.
(prepare for lots of code)

Loadbar:
Spoiler

print("PTOS V0.1.2")

print("loading PTOS Core System Files And Settings")
function HorzLoadBar(sx, fx, sym)
x,y =term.getCursorPos()
if y >= 18 then
term.clear()
term.setCursorPos(sx,1)
x,y = sx,1
end
term.setCursorPos(sx,y)
write("[")
term.setCursorPos(sx,y)
term.setCursorPos(fx-1,y)
write("]")
mdp = math.floor((sx + fx)/2) - 3
for i = (sx+1),(fx-2) do
term.setCursorPos(i,y)
write(sym)
sleep(0.1)
term.setCursorPos(mdp,y+1)
write(string.format("%d",i/(fx-2) * 100))
write("%")
end
term.setCursorPos(1,y+2)
end
HorzLoadBar(15,35,"|")
term.clear()

Login:
Spoiler

print("PTOS V0.1.2")
write("Login: ")
lanswer = read()
if lanswer == "Pivotdude" then
write("Password: ")
else
os.reboot()
end
panswer = read("*")
if panswer == "1337" then
print("Welcome "..lanswer..".")
else
os.reboot()
end
term.clear()

System Clock:
Spoiler

print(textutils.formatTime(os.time(), false))

Windows style menu:
Spoiler

function VertMenu(tableofItems)
local selection = "";
local sIndex = 1;
while (selection == "") do
  term.clear();
  local xMax, yMax = term.getSize();
  local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
  y = y - #tableofItems;
  if y<1 then y = 1 end;
  if #tableofItems > yMax then
   print "ERROR: List does not fit on screen, reduce number of items in table";
   return "";
  end
  for i = 1, #tableofItems do
  if string.len(tableofItems[i]) >= xMax then
   term.clear();
   term.setCursorPos(1,1);
   print (("ERROR: List item # "..i).." is too long please reduce");
   return "";
  end
   local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);

   if i == sIndex then x = x-1 end;
   term.setCursorPos(x,y);
   if i == sIndex then term.write("[") end;
   term.write(tableofItems[i]);

   if i == sIndex then term.write("]") end;

   y = y+1;
  end
  local r,s = os.pullEvent("key");
  if s == 208 then sIndex = sIndex +1 end;
  if s == 200 then sIndex = sIndex -1 end;
  if sIndex > #tableofItems then sIndex = 1 end;
  if sIndex < 1 then sIndex = #tableofItems end;
  if s == 28 then
		term.clear();
		term.setCursorPos(1,1);
		return tableofItems[sIndex]
  end
end
end

Startup:
Spoiler

shell.run("loadbar")
shell.run("login")
term.clear()
shell.run("menu")
shell.run("systemtime")

could someone please point out what I'm doing wrong


Changelog of fixes

> * fixed the term.clear at startup
Kingdaro #2
Posted 04 June 2013 - 04:03 AM
I notice right off the bat that, under your startup script, () is missing after term.clear. I also notice that the VertMenu function you define in the menu script is never called, therefore nothing would happen when running the script.

Also, I don't want to be rude or impose, but may I make a suggestion? It's usually good to not have a loading bar if you don't have anything to actually load; it doesn't really look professional as some may think, and it's usually more annoying than it is a novelty.
pivotdude #3
Posted 04 June 2013 - 07:07 AM
Thank you , I am new to computercraft so i would appreciate if you could show me how to call the vertmenu script also , the load bar just looks pretty nothing more but i understand what you mean
pivotdude #4
Posted 04 June 2013 - 12:05 PM
I have updated the post with tags and a fix to term.clear to ensure i dont get the same things getting pointed at to me
Kingdaro #5
Posted 04 June 2013 - 02:58 PM
For your situation, I would just remove the line
function VertMenu(tableofItems)
and the last "end", so it's not a function.
pivotdude #6
Posted 05 June 2013 - 06:21 AM
okay will try that


Edit: error menu:6: attempt to get length of nil
Edit II: OMG i used the wrong menu code it is actually


function menuBar(items)
itemIndex = 0;
while(true) do --forever
  --clear and reset terminal
  term.clear();
  term.setCursorPos(1,1);
  term.setCursorBlink(false);
  --Print the menu titles
  for i = 1, #items do
   term.write((items[i])[1] .. "  ");
  end
  --listen for mouse
  e,mb,x,y = os.pullEvent("mouse_click");
  --if they clicked on the menu
  if (y==1) then
   --stopValue is the graphical location at the end of the menu title the user will click on
   stopValue = 0;
 
   --loop through the items
   for i = 1,#items do
	    --set the stop value to the end of the title string
	    stopValue = stopValue + string.len( (items[i])[1] );
	    --if it is between the stop value minus the word length and the stop value, then the user has clicked on a word
	    if (x<=stopValue and x> (stopValue - string.len( (items[i])[1] ))) then
		 --set the index to what the user clicked on
		 itemIndex = i;
		 --break out the loop
		 break;
	    end
	    --allow for the double spacing bewteen items
	    stopValue = stopValue +2;
   end
 
   --if the item index is greater than zero than the user has clicked on something
   if (itemIndex > 0) then
 
	    --loop through the sub table
	    for i = 2,#(items[itemIndex]) do
		 --line up to top item
		 term.setCursorPos((stopValue - string.len(items[itemIndex][1] )+1),i+1);
		 --print the sub menu
		 term.write(items[itemIndex][i]);
	   
	    end
	    --listen for mouse again
	    e2,mb2,x2,y2 = os.pullEvent("mouse_click");
 
	    --if they clicked on something (and not in empty space on Y axis)
 
	    if (y2 <= #(items[itemIndex]) + 2 and y2 ~= 1 and y2 ~= 2) then
	   
		 --and not in empty space on X axis
		 if (x2 >= (stopValue-3) and x2 <= stopValue-3+(string.len(items[itemIndex][y2-1]))) then
		  --tidy up the screen and return the two items they clicked on.
		  term.clear();
		  term.setCursorPos(1,1);
		  return({items[itemIndex][1],items[itemIndex][y2-1]});
		 end
	    end
   end
  end--if
end--while
end--function
n = menuBar({
{"Menu1","This","is","awesome"},
{"Menu2","This2","is2","awesome2"},
{"Menu3","This3","is3","awesome3"}
});
for i = 1,#n do
print(n[i]);
end

However it still behaves the same , can execute manually but not at startup
Bomb Bloke #7
Posted 05 June 2013 - 11:17 AM
Works for me on startup.
twig1233 #8
Posted 05 June 2013 - 11:48 AM
I am pretty sure you dont need the ; at the end of
term.clear()
and
term.setCursorPos()
theoriginalbit #9
Posted 05 June 2013 - 11:58 AM
I am pretty sure you dont need the ; at the end of
term.clear()
and
term.setCursorPos()
They actually have a `;` at the end of each line of code, while it is not required, it being there does not cause any problems with the code as it is also valid syntax.