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

Touchscreen

Started by grand_mind1, 02 February 2013 - 03:47 PM
grand_mind1 #1
Posted 02 February 2013 - 04:47 PM
I recently discovered the touchscreen functionality of advanced monitors. I am trying to learn how to use them but I am having a lot of difficulty. I am trying to make a simple button that you can hit and it will do whatever.


mon = peripheral.wrap("right")
mon.clear()
while true do
  xMax,xMin,yMax,yMin = os.pullEvent("monitor_touch")
  if xMax <= 9 and xMin >= 1 and yMax <= 3 and yMin >= 1 then
    mon.write("Hello World!")
  end
end
When I run this I get this error:

touch2:6: attempt to compare number with string, got number
I have no idea what I've done wrong and thought that this would work fine. Could someone please tell me what I am doing wrong?
Help is appreciated!
Thanks! :D/>
raineth #2
Posted 02 February 2013 - 05:03 PM
  xMax,xMin,yMax,yMin = os.pullEvent("monitor_touch")

The first value returned from os.pullEvent() is the event type. You need to account for that even if you don't want to use it.

Try this instead:
  _,xMax,xMin,yMax,yMin = os.pullEvent("monitor_touch")
grand_mind1 #3
Posted 02 February 2013 - 05:16 PM
Thank you for telling me this. I am sure this will help me in the future, however this doesn't seem to fix the error. I get the same error after trying to touch monitor.
Kingdaro #4
Posted 02 February 2013 - 05:26 PM
It's still wrong. The variables returned from the event are the monitor side, the mouse x and the mouse y. It doesn't return any range.

http://computercraft.info/wiki/Monitor_touch_(event)
grand_mind1 #5
Posted 02 February 2013 - 05:43 PM
Thank you. I am starting to understand this. I am now able to make a button where ever on the screen I want! Do you know how to make it a visible button? Like have it be a box that you can click.
Skullblade #6
Posted 02 February 2013 - 05:44 PM
It's still wrong. The variables returned from the event are the monitor side, the mouse x and the mouse y. It doesn't return any range.

http://computercraft...r_touch_(event)
ding ding ding we have a winner

Edit: also grand to make a button just print stuff were the computer accepts the button values
grand_mind1 #7
Posted 02 February 2013 - 05:54 PM
It's still wrong. The variables returned from the event are the monitor side, the mouse x and the mouse y. It doesn't return any range.

http://computercraft...r_touch_(event)
ding ding ding we have a winner

Edit: also grand to make a button just print stuff were the computer accepts the button values
Yes but I mean like making a background color for the area of the button.
Skullblade #8
Posted 02 February 2013 - 06:09 PM
well you could always do something like this(untested)

mon=peripherial.wrap("side")
mon.setBackgroundColor(colors.black)
mon.clear()
mon.setBackgroundColor(colors.lime)--or other color
mon.setCursorPos(1,1)
mon.print("This is a button")

while true do
event,side,x,y=os.pullEvent("monitor_touch")
if x>0 and x<17 and y==1 then
--do button stuff
end
end
Its very l8 here hope this helps im going 2 bed
remiX #9
Posted 03 February 2013 - 12:50 AM
If the touchscreen will have multiple buttons, I would suggest using a table:


--[[ Declare a table with the buttons that will be printed,
      with it's own Text Colour and Background Colour and
      unique x and y values.
t = {
    {text = "Button 1", x = 1, y = 1, txtCol = colours.red, bgCol = colours.lime},
    {text = "Button 2", x = 1, y = 2, txtCol = colours.red, bgCol = colours.lime},
    {text = "Button 3", x = 1, y = 3, txtCol = colours.red, bgCol = colours.lime}
}


mon = peripherial.wrap("side")

function writeButtons(_table)
    for i, v in pairs(_table) do
        term.setCursorPos(v.x, v.y)
        term.setTextColour(v.txtCol)
        term.setBackgroundColour(v.bgCol)
        write(v.text)
    end
end

function isValidClick(_table, mx, my)

    for i, v in pairs(_table) do
        if mx >= v.x and mx <= (v.x + #v.text) and my == v.y then
             return true, v.text
        end
    end
    return false, nil
end

writeButtons(t)

while true do
    _, but, x, y = os.pullEvent("monitor_touch")
    bClick, option = isValidClick(t, x, y)
    if bClick then
    -- Yes, it's a valid click. Now let's do something with the returned text 'option'
    if option == "Button 1" then -- button 1...

    elseif option == "Button 2" then

    elseif option == "Button 3" then

    end
end

Something like that would be the easiest way. If you need me to explain anything, feel free to ask :)/>
grand_mind1 #10
Posted 03 February 2013 - 06:04 AM
I am very thankful for your help however I do have some questions about the code, if you don't mind.
First:

for i, v in pairs(_table) do
I see this a lot in different programs. Could you explain what "for i, v in pairs" will do? Also, I see "(_table)" a lot in the code however I don't see where you defined it or if you even have to define it. I think I understand most of the code besides those two things and I will mess around and test in-game.
remiX #11
Posted 03 February 2013 - 06:25 AM

for i, v in pairs(_table) do

All this code is doing is looping (iterating) through the table (_table) - it's using the name _table because that's the argument for the function.


function isValidClick(_table, mx, my)

    for i, v in pairs(_table) do
        if mx >= v.x and mx <= (v.x + #v.text) and my == v.y then
             return true, v.text
        end
    end
    return false, nil
end

What this is doing is going through each index of the table checking if the clicked X and Y value is equal to one of the values within the table and if it is it returns true and which text is clicked.