Jump to content




Triggering A Function On Mouseclick, Doesnt Run The Function


11 replies to this topic

#1 makerimages

  • Members
  • 236 posts

Posted 15 September 2013 - 06:43 AM

term.clear()
os.loadAPI("Apis/DesignUtil")
os.loadAPI("Apis/AccountSytem")
DesignUtil.setDesign("whiteLightGray")
local backGround,topBar=DesignUtil.getDesign()
local drawO1=false;
term.setTextColor(colors.black)
local O1B=
{
text="O1|";
x=1;
y=1;
W=math.ceil(3);
H=math.ceil(1);
}
local O1Menu =
{
line1text="About this device";
line1x=1;
line1y=2;
line1W=18;
line1H=1
}
function drawO1Menu()

while drawO1==true do
term.setCursorPos(O1Menu.line1x,O1Menu.line1y)
print(O1Menu.line1text)
sleep(0.15)
end

end
function registerClick(x,y)
if x > O1B.x and x < O1B.x + O1B.w - 1 and y > O1B.y and y <  O1B.y + O1B.h then
  drawO1=not drawO1
  end
end

while true do
paintutils.drawImage(backGround,1,1)
paintutils.drawImage(topBar,1,1)
term.setCursorPos(1,1)
print(O1B.text)
event, button, xPos, yPos = os.pullEvent("mouse_click")
print(x..y)
registerClick(xPos,yPos)
drawO1Menu()
sleep(0.15)
end



heres the code, the problem is that registerClick OR drawO1Menu are not working somehow, when I click on the text, the menu wont be drawn. What is the issue & how to fix?

#2 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 15 September 2013 - 07:42 AM

2 tips:

W=math.ceil(3);
H=math.ceil(1);

--//It is the same as:
W=3
H=1

--//math.ceil(x) returns highest nearest integer or x if x is already an integer

print(x..y)
--//This will error because x and y are not set

Your problem is here:

function registerClick (x, y)
  if x > O1B.x and x < O1B.x + O1B.w - 1 and y > O1B.y and y <  O1B.y + O1B.h then
    drawO1=not drawO1
  end
end

--//When you check y position you check if you clicked BELOW your button (y > O1B.y). In fact you do same thing with x and other checks. What you should do is change all ">" to ">=" and "<" to "<=":

function registerClick (x, y)
  if x >= O1B.x and x <= O1B.x + O1B.w - 1 and y >= O1B.y and y <=  O1B.y + O1B.h then
    drawO1=not drawO1
  end
end

--//This is because you want to check if y is BELOW OR EQUAL to your button's start y position; same with all other checks.


#3 makerimages

  • Members
  • 236 posts

Posted 15 September 2013 - 08:24 AM

Did that and it sure didn`t work...

#4 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 15 September 2013 - 08:36 AM

You never invoke a function
I thought you meant something else..

Technically it should work, but this is useless crap Im giving you

#5 makerimages

  • Members
  • 236 posts

Posted 15 September 2013 - 08:39 AM

the function registerClick is supposed to set a variable that is used by the drawing function that is constantly invoked by the while true do loop.. apparently manually setting tha value aint drawingit either....

edit: it is, current code:

term.clear()
os.loadAPI("Apis/DesignUtil")
os.loadAPI("Apis/AccountSytem")
DesignUtil.setDesign("whiteLightGray")
local backGround,topBar=DesignUtil.getDesign()
local drawO1=true;
term.setTextColor(colors.black)
local O1B=
{
text="O1|";
x=1;
y=1;
W=3;
H=1;
}
local O1Menu =
{
line1text="About this device";
line1x=1;
line1y=2;
line1W=18;
line1H=1
}
function drawO1Menu()

while drawO1 do
term.setCursorPos(O1Menu.line1x,O1Menu.line1y)
print(O1Menu.line1text)
sleep(0.15)
end

end
function registerClick (x, y)
  if x >= O1B.x and x <= O1B.x + O1B.w - 1 and y >= O1B.y and y <=  O1B.y + O1B.h then
    drawO1= not drawO1
  end
end
while true do

paintutils.drawImage(backGround,1,1)
paintutils.drawImage(topBar,1,1)
term.setCursorPos(O1B.x,O1B.y)
print(O1B.text)
drawO1Menu()
  event, button, xPos, yPos = os.pullEvent("mouse_click")
registerClick(xPos,yPos)

sleep(0.15)
end


the clicking part still aint working

#6 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 15 September 2013 - 09:03 AM

Found another issue:

O1B.y + O1B.h
O1B.x + O1B.w

--//Should be:

O1B.y + O1B.H
O1B.x + O1B.W

EDIT:
Did you get any error?

#7 makerimages

  • Members
  • 236 posts

Posted 15 September 2013 - 09:22 AM

worked, however.. the menu is drawn and a few sec later its removed from screen.... need to undo that and my own reclick on O1| is not removing the menu

current code

term.clear()
os.loadAPI("Apis/DesignUtil")
os.loadAPI("Apis/AccountSytem")
DesignUtil.setDesign("whiteLightGray")
local backGround,topBar=DesignUtil.getDesign()
local drawO1=false;
term.setTextColor(colors.black)
local O1B=
{
text="O1|";
x=1;
y=1;
W=3;
H=1;
}
local O1Menu =
{
line1text="About this device";
line1x=1;
line1y=2;
line1W=18;
line1H=1
}
function drawO1Menu()

while drawO1 do
term.setCursorPos(O1Menu.line1x,O1Menu.line1y)
print(O1Menu.line1text)

end

end
function registerClick (x, y)
  if x >= O1B.x and x <= O1B.x + O1B.W - 1 and y >= O1B.y and y <=  O1B.y + O1B.H then
  drawO1= not drawO1
  end
end
while true do

paintutils.drawImage(backGround,1,1)
paintutils.drawImage(topBar,1,1)
term.setCursorPos(O1B.x,O1B.y)
print(O1B.text)
drawO1Menu()
  event, button, xPos, yPos = os.pullEvent("mouse_click")
registerClick(xPos,yPos)

sleep(0.15)
end



#8 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 15 September 2013 - 10:46 AM

Please refrain from bumping if it's been less than a few days.

Why does your draw01menu function have a while loop in it? You should remove that while loop.

Change it to something like this instead:
function drawO1Menu()
  if draw01 then
    term.setCursorPos(O1Menu.line1x,O1Menu.line1y)
    print(O1Menu.line1text)
  end
end


#9 makerimages

  • Members
  • 236 posts

Posted 15 September 2013 - 10:52 AM

That just made the thing not work at all,

cureent code
term.clear()
os.loadAPI("Apis/DesignUtil")
os.loadAPI("Apis/AccountSytem")
DesignUtil.setDesign("whiteLightGray")
local backGround,topBar=DesignUtil.getDesign()
local drawO1=true;
print(window)
term.setTextColor(colors.black)
local O1B=
{
text="O1|";
x=1;
y=1;
W=3;
H=1;
}
local O1Menu =
{
line1text="About this device";
line1x=1;
line1y=2;
line1W=18;
line1H=1
}
function drawO1Menu()
  if draw01 then
    term.setCursorPos(O1Menu.line1x,O1Menu.line1y)
    print(O1Menu.line1text)
  end
end
function registerClick (x, y)
  if x >= O1B.x and x <= O1B.x + O1B.W - 1 and y >= O1B.y and y <=  O1B.y + O1B.H then
  drawO1=true
  end
end

while true do

paintutils.drawImage(backGround,1,1)
paintutils.drawImage(topBar,1,1)
term.setBackgroundColor(colors.lightGray)
term.setCursorPos(O1B.x,O1B.y)
print(O1B.text)


drawO1Menu()
  event, button, xPos, yPos = os.pullEvent("mouse_click")
   registerClick(xPos,yPos)

sleep(0.1)
end



I click-nothing happens

#10 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 15 September 2013 - 11:06 AM

You don't set draw01 to false, you set it to true in the registerClick function. It needs to change to false if you don't want the menu displayed.

Change that to this:
function registerClick (x, y)
  if x >= O1B.x and x <= O1B.x + O1B.W - 1 and y >= O1B.y and y <=  O1B.y + O1B.H then
    drawO1=false
  end
end

You should probably also clear the screen during every while loop unless those images take the entire screen. Otherwise it will look like the menu hasn't disappeared despite not being redrawn.

#11 CoderPuppy

  • Members
  • 121 posts

Posted 15 September 2013 - 11:06 AM

When you click on O1B it sets draw01 to true, which since draw01 is already true does nothing. I'm guessing what you want is:
draw01 = not draw01
Also you don't need a sleep in your main loop.

#12 makerimages

  • Members
  • 236 posts

Posted 15 September 2013 - 11:08 AM

all works now, final code

term.clear()
os.loadAPI("Apis/DesignUtil")
os.loadAPI("Apis/AccountSytem")
DesignUtil.setDesign("whiteLightGray")
local backGround,topBar=DesignUtil.getDesign()
local drawO1=false;
print(window)
term.setTextColor(colors.black)
local O1B=
{
text="O1|";
x=1;
y=1;
W=3;
H=1;
}
local O1Menu =
{
line1text="About this device";
line1x=1;
line1y=2;
line1W=18;
line1H=1
}
function drawO1Menu()
  if draw01 then
    term.setCursorPos(O1Menu.line1x,O1Menu.line1y)
    print(O1Menu.line1text)
  end
end
function registerClick (x, y)
  if x >= O1B.x and x <= O1B.x + O1B.W - 1 and y >= O1B.y and y <=  O1B.y + O1B.H then
draw01 = not draw01
  end
end
while true do

paintutils.drawImage(backGround,1,1)
paintutils.drawImage(topBar,1,1)
term.setBackgroundColor(colors.lightGray)
term.setCursorPos(O1B.x,O1B.y)
print(O1B.text)


drawO1Menu()
  event, button, xPos, yPos = os.pullEvent("mouse_click")
   registerClick(xPos,yPos)

sleep(0.1)
end








1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users