Jump to content




Mouse Clicks In A While True Do


  • You cannot reply to this topic
9 replies to this topic

#1 makerimages

  • Members
  • 236 posts

Posted 04 August 2013 - 10: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?

#2 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 04 August 2013 - 11:10 AM

View Postmakerimages, on 04 August 2013 - 10:40 AM, said:

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.

#3 makerimages

  • Members
  • 236 posts

Posted 04 August 2013 - 11:34 AM

Doesn`t work

#4 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 04 August 2013 - 12:18 PM

View Postmakerimages, on 04 August 2013 - 11:34 AM, said:

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.

#5 albrat

  • Members
  • 162 posts
  • LocationA Chair

Posted 04 August 2013 - 01: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 by albrat, 04 August 2013 - 01:20 PM.


#6 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 04 August 2013 - 01:56 PM

View Postalbrat, on 04 August 2013 - 01:17 PM, said:

   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

#7 makerimages

  • Members
  • 236 posts

Posted 05 August 2013 - 01: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


#8 makerimages

  • Members
  • 236 posts

Posted 05 August 2013 - 11:59 AM

anyone?

#9 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 05 August 2013 - 12: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.

#10 makerimages

  • Members
  • 236 posts

Posted 06 August 2013 - 12:51 AM

Well, guess what-All i had to do was to add math.ceil() to the x and y of the button...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users