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

Mouse Clicks In A While True Do

Started by makerimages, 04 August 2013 - 09:40 AM
makerimages #1
Posted 04 August 2013 - 11:40 AM
Here is the code

--Developed by Makerimages
os.loadAPI("/apis/gameutils");--Load the animation api by NitrogenFingers
term.setTextColor(colors.lightGray)
local topBar=gameutils.loadSprite("/graphics/TopBar.nfp",1,1);--top bar
local w, h=term.getSize();
local loadBar=gameutils.loadAnimation("/graphics/LoadingBar.nfa",math.ceil(w/2-3),math.ceil(h/2));--loading bar
local backGround=gameutils.loadSprite("/graphics/b.nfp",1,1)--background
local addAccountButton=
{
text="+ Add an account";
x=w/2-5;
y=h/2;
w=16;
h=1;
}
loadBar.currentFrame=1;

gameutils.initializeBuffer();
gameutils.writeToBuffer(backGround)
gameutils.writeToBuffer(topBar);
gameutils.writeToBuffer(loadBar);
gameutils.drawBuffer();
while loadBar.currentFrame<=10 do
loadBar:next();
sleep(0.15)
gameutils.writeToBuffer(loadBar);
gameutils.drawBuffer();
term.setCursorPos(math.ceil(w/2-3),math.ceil(h/2)-1)
print("OS One is booting...")
end
if fs.exists("userData/user1.uData") then
shell.run("/OSCore/loginScreen.lua")
else
gameutils.clearBuffer()
gameutils.writeToBuffer(backGround)
gameutils.writeToBuffer(topBar);
while true do
gameutils.drawBuffer();
term.setCursorPos(1,math.ceil(h/2-5))
print("OS One has identified that you do not have an admininstrative user Account set up yet. Please do so now.")
term.setCursorPos(addAccountButton.x, addAccountButton.y)
write("|")write(addAccountButton.text)write("|")
  event, button, x, y = os.pullEvent("mouse_click")
   if x >= addAccountButton.x and x <= addAccountButton.x + addAccountButton.w - 1 -- minus 1 because of how the coordinate system works
  and y >= addAccountButton.y and y <=  addAccountButton.y + addAccountButton.h - 1 then
   shell.run("id")
  end
sleep(0.15)
end
end

the problem lies in the lower part-the while true loop, basically my mouse clicks do not work. Any ideas?
GamerNebulae #2
Posted 04 August 2013 - 12:10 PM
Here is the code

--Developed by Makerimages
os.loadAPI("/apis/gameutils");--Load the animation api by NitrogenFingers
term.setTextColor(colors.lightGray)
local topBar=gameutils.loadSprite("/graphics/TopBar.nfp",1,1);--top bar
local w, h=term.getSize();
local loadBar=gameutils.loadAnimation("/graphics/LoadingBar.nfa",math.ceil(w/2-3),math.ceil(h/2));--loading bar
local backGround=gameutils.loadSprite("/graphics/b.nfp",1,1)--background
local addAccountButton=
{
text="+ Add an account";
x=w/2-5;
y=h/2;
w=16;
h=1;
}
loadBar.currentFrame=1;

gameutils.initializeBuffer();
gameutils.writeToBuffer(backGround)
gameutils.writeToBuffer(topBar);
gameutils.writeToBuffer(loadBar);
gameutils.drawBuffer();
while loadBar.currentFrame<=10 do
loadBar:next();
sleep(0.15)
gameutils.writeToBuffer(loadBar);
gameutils.drawBuffer();
term.setCursorPos(math.ceil(w/2-3),math.ceil(h/2)-1)
print("OS One is booting...")
end
if fs.exists("userData/user1.uData") then
shell.run("/OSCore/loginScreen.lua")
else
gameutils.clearBuffer()
gameutils.writeToBuffer(backGround)
gameutils.writeToBuffer(topBar);
while true do
gameutils.drawBuffer();
term.setCursorPos(1,math.ceil(h/2-5))
print("OS One has identified that you do not have an admininstrative user Account set up yet. Please do so now.")
term.setCursorPos(addAccountButton.x, addAccountButton.y)
write("|")write(addAccountButton.text)write("|")
  event, button, x, y = os.pullEvent("mouse_click")
   if x >= addAccountButton.x and x <= addAccountButton.x + addAccountButton.w - 1 -- minus 1 because of how the coordinate system works
  and y >= addAccountButton.y and y <=  addAccountButton.y + addAccountButton.h - 1 then
   shell.run("id")
  end
sleep(0.15)
end
end

the problem lies in the lower part-the while true loop, basically my mouse clicks do not work. Any ideas?

I had this problem with my Monitor API, try to put the if-statement that checks if the click was on the right coordinates in brackets.
It would look like this:


if (....) then

For some reason it doesn't handle long if-statements that well, in C# for example you ALWAYS put if-statements in brackets.
makerimages #3
Posted 04 August 2013 - 12:34 PM
Doesn`t work
GamerNebulae #4
Posted 04 August 2013 - 01:18 PM
Doesn`t work

And if you try to put the equations in brackets (those within the equations)? That's the only thing I can come up with. Try to put in some diagnostic features like the x and y of the button and the spot where you clicked. Maybe that can help you.
albrat #5
Posted 04 August 2013 - 02:17 PM

   if x >= addAccountButton.x and x <= addAccountButton.x + addAccountButton.w - 1 -- minus 1 because of how the coordinate system works
  and y >= addAccountButton.y and y <=  addAccountButton.y + addAccountButton.h - 1 then

I hope you don't have the comment in the middle of your code like that as standard… as that may cause a problem. ( not sure though ).

since your account button is only 1 high…. you are checking against a 0 height…. This will always fail. remove the -1 from "+ addAccountButton.h -1" and you may find that the button will work…

Future note : Buttons of height 1 are not a good idea. ( always make them 2 high or 3 high to work well with co-ords )
Edited on 04 August 2013 - 12:20 PM
GamerNebulae #6
Posted 04 August 2013 - 02:56 PM

   if x >= addAccountButton.x and x <= addAccountButton.x + addAccountButton.w - 1 -- minus 1 because of how the coordinate system works
  and y >= addAccountButton.y and y <=  addAccountButton.y + addAccountButton.h - 1 then

I hope you don't have the comment in the middle of your code like that as standard… as that may cause a problem. ( not sure though ).

since your account button is only 1 high…. you are checking against a 0 height…. This will always fail. remove the -1 from "+ addAccountButton.h -1" and you may find that the button will work…

Future note : Buttons of height 1 are not a good idea. ( always make them 2 high or 3 high to work well with co-ords )

That would actually be so stupid if that actually was the problem xD
makerimages #7
Posted 05 August 2013 - 02:23 AM
That was the problem, the height. However, now I find myself with the issue that the button`s active area is 1 row below the button. How to fix that?
Code:

--Developed by Makerimages
os.loadAPI("/apis/gameutils");--Load the animation api by NitrogenFingers

term.setTextColor(colors.lightGray)
local topBar=gameutils.loadSprite("/graphics/TopBar.nfp",1,1);--top bar
local w, h=term.getSize();
local loadBar=gameutils.loadAnimation("/graphics/LoadingBar.nfa",math.ceil(w/2-3),math.ceil(h/2));--loading bar
local backGround=gameutils.loadSprite("/graphics/b.nfp",1,1)--background
local addAccountButton=
{
text="+ Add an account";
x=w/2-5;
y=h/2;
w=16;
h=1;
}
loadBar.currentFrame=1;

gameutils.initializeBuffer();
gameutils.writeToBuffer(backGround)
gameutils.writeToBuffer(topBar);
gameutils.writeToBuffer(loadBar);
gameutils.drawBuffer();
while loadBar.currentFrame<=10 do
loadBar:next();
sleep(0.15)
gameutils.writeToBuffer(loadBar);
gameutils.drawBuffer();
term.setCursorPos(math.ceil(w/2-3),math.ceil(h/2)-1)
print("OS One is booting...")
end
if fs.exists("userData/user1.uData") then
shell.run("/OSCore/loginScreen.lua")
else
gameutils.clearBuffer()
gameutils.writeToBuffer(backGround)
gameutils.writeToBuffer(topBar);
while true do
gameutils.drawBuffer();
term.setCursorPos(1,math.ceil(h/2-5))
print("OS One has identified that you do not have an admininstrative user Account set up yet. Please do so now.")
term.setCursorPos(addAccountButton.x, addAccountButton.y)
write("|")write(addAccountButton.text)write("|")
  event, button, x, y = os.pullEvent("mouse_click")
   if x >= addAccountButton.x and x <= addAccountButton.x + addAccountButton.w - 1 and y >= addAccountButton.y and y <=  addAccountButton.y + addAccountButton.h then
   os.reboot()
  end
sleep(0.15)
end
end
makerimages #8
Posted 05 August 2013 - 12:59 PM
anyone?
Kingdaro #9
Posted 05 August 2013 - 01:14 PM
Here:
y <=  addAccountButton.y + addAccountButton.h

Change <= to <
y < addAccountButton.y + addAccountButton.h

And for the record, you can split up that ridiculously long if statement into two lines; the OP's area checking code is based off of code written by and tested by myself.
makerimages #10
Posted 06 August 2013 - 01:51 AM
Well, guess what-All i had to do was to add math.ceil() to the x and y of the button…