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

Triggering A Function On Mouseclick, Doesnt Run The Function

Started by makerimages, 15 September 2013 - 05:43 AM
makerimages #1
Posted 15 September 2013 - 07: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 &amp; how to fix?
MKlegoman357 #2
Posted 15 September 2013 - 08: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.
makerimages #3
Posted 15 September 2013 - 09:24 AM
Did that and it sure didn`t work…
Engineer #4
Posted 15 September 2013 - 09: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
makerimages #5
Posted 15 September 2013 - 09: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
MKlegoman357 #6
Posted 15 September 2013 - 10: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?
makerimages #7
Posted 15 September 2013 - 10: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

Bubba #8
Posted 15 September 2013 - 11: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
makerimages #9
Posted 15 September 2013 - 11: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
Bubba #10
Posted 15 September 2013 - 12:06 PM
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.
CoderPuppy #11
Posted 15 September 2013 - 12:06 PM
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.
makerimages #12
Posted 15 September 2013 - 12:08 PM
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