Jump to content




Touchpoint API


277 replies to this topic

#21 Crucidal

  • Members
  • 33 posts
  • LocationThe Netherlands

Posted 08 November 2013 - 09:13 AM

Hey,

Could you help me out?

I've got a single monitor attached to my adv. computer. The goal is to show just one button which can be pressed to open some piston door I made.
The reason I want to use a computer is to be able to keep it open for a longer period of time without having to build a big rs construction.

The code I used can be found here:

http://pastebin.com/1tszfhZ8

comment: I deleted the "right" button

I tried two options:

t:add("left", nil, 2, 4, 2 , 4, colors.red, colors.lime)

leading to error: touchpoint:13:vm error:Java.lang.NegativeArraySizeException

and

t:add("left", pulse(), 2, 4, 2 , 4, colors.red, colors.lime)

leading to error: "Touchpoint:104: attempt to index ? (a nil value)

I've got no clue why this code, which is so similar to your example generates errors in the API itself :(

thanks in advance.

Edited by Crucidal, 08 November 2013 - 09:13 AM.


#22 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 08 November 2013 - 11:07 AM

Well, part of the problem is that the coordinates are xMin, yMin, xMax, yMax, so the coordinates you have there would create a single-pixel button. Here's a modified version of the code:

-- load the touchpoint API
os.loadAPI("touchpoint" )

-- initialize a new button set on the top monitor
local t = touchpoint.new("top")

--ouput rs signal for 10 seconds.
function pulse()
t:toggleButton("door")
shell.run("redpulse", "right", "1", "10")
t:toggleButton("door")
end


-- use coordinates that make a reasonable sized button.
-- do not use parentheses when passing function values, pulse instead of pulse()
t:add("door", pulse, 2, 2, 7, 4, colors.red, colors.lime)
-- coordinates are minX, minY, maxX, maxY.
-- uses red for inactive color and green for active color.

-- since we only want to react to button presses and we have a click function for our button, we can just use this:
t:run()

Give that a shot and let me know if it resolves the problem. :)

#23 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 10 November 2013 - 03:53 PM

Hello. Just dropping by to post modified version of this api allowing it to make buttons in terminal instead of on monitor.

Code can be find in here: TNB10BJU

Modyfied Methods:
--# create a new instance of buttons on the top monitor (assumes API loaded as "touchpoint").
t = touchpoint.new("top")
--#If no side is provided makes buttons in terminal instead.

--#All other methods are same as in Standard Varsion

Thats all from me. :)

Note that this modification is only 3 lines. Nothing fancy :D

Edited by wojbie, 13 December 2013 - 01:56 AM.


#24 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 11 November 2013 - 07:16 PM

That modification gave me an idea: why not pass in a redirection object instead of just a side or nil? It'll allow you to pass in a monitor object, the "term" api itself, and other redirection objects, like Gopher's buffer redirect objects.

Edited by Kingdaro, 11 November 2013 - 07:17 PM.


#25 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 11 November 2013 - 08:12 PM

The trouble with accepting redirect objects (which I have considered) is that it becomes more difficult to decide which events to care about. With a monitor side argument, the API can wrap that monitor when creating a new instance, and can reference the side name itself in order to check incoming monitor_touch events. Accepting arbitrary redirect means that we then must require at least one more argument (the monitor side, if any) and need some way to properly validate incoming events. I feel that the use case for Touchpoint (relatively simple but powerful monitor-based touchscreen buttons) is covered quite adequately by the current code, and I don't really think that the added complexity to support arbitrary redirect targets falls within the purview of the Touchpoint API. I may at some point create an "advanced" API similar to Touchpoint with a few more features that would include support for arbitrary redirects.

#26 adencraft2000

  • Members
  • 43 posts

Posted 10 March 2014 - 04:04 AM

Probably me not thinking but... Line 46:
add = function(self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor)
to me looks like it takes MORE parameters than just
t:add("label", function() doStuffButNotRequiredForEventsMode() end, 2, 2, 28, 11, colors.red, colors.lime)
(Comparison)
self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor
"label", function() doStuffButNotRequiredForEventsMode() end, 2, 2, 28, 11, colors.red, colors.lime

Just not sure... Probably me being stupid

#27 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 10 March 2014 - 04:42 AM

View Postadencraft2000, on 10 March 2014 - 04:04 AM, said:

-snip-
the extra argument self is added by use of the : when invoking the function.

You showed the usage of this
t:add("label", function() doStuffButNotRequiredForEventsMode() end, 2, 2, 28, 11, colors.red, colors.lime)
well that is the same as this
t.add(t, "label", function() doStuffButNotRequiredForEventsMode() end, 2, 2, 28, 11, colors.red, colors.lime)
note how when you don't use the : syntax and instead use dot notation you must also pass a reference of the table to the function.

Edited by theoriginalbit, 10 March 2014 - 04:44 AM.


#28 ByteZz

  • Members
  • 23 posts
  • LocationThe United States

Posted 12 March 2014 - 08:20 PM

I need utter help. I am trying to create a touchscreen program using this API that will send redstone signals to the back, right and left of the computer. I have done most of it but I am still confused because it gives me an 'attempt to index ? nil' on line 47 everytime I try. The buttons show up but the rest doesn't. My task is (in more detail) is for me to have a button that can be enabled and disabled. Whent he button is on, the redstone signal will be sent. When off, the redstone signal should also be off. Moreover, I need it in such a way that all three of the redstone buttons can be enabled simultaneously. Is that too hard? Here's my current code: http://pastebin.com/d6pNsrYg. Please check it out and let me know how this works. I think there should be more tutorials for this API as it looks very good but there isn't enough in-depth information into how powerful the API actually along with its functions that display different things you can do. Thanks everyone and well done Lyqyd.

#29 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 12 March 2014 - 09:21 PM

One issue is that when you declare your page of buttons (t), you've declared it as local inside a function! You can't access it from outside that function when you do that. Here's a modified version of your script:


loadup = function()
        shell.run("pastebin", "get", "pFHeia96", "touchpoint")
        os.loadAPI("touchpoint")
        print("Touchpoint API Loaded")
        sleep(1)
        term.clear()
        term.setCursorPos(1,1)
end

loadup()

--make t be local to the whole program, not just the loading function.
local t = touchpoint.new("top")

--rs.getOutput is more reliable for toggling outputs.
function leftAction()
        t:toggleButton("Wither")
        if rs.getOutput("left") == true then
                rs.setOutput("left", false)
        else
                rs.setOutput("left", true)
        end
end

function backAction()
        t:toggleButton("Blaze")
        if rs.getOutput("back") == true then
                rs.setOutput("back", false)
        else
                rs.setOutput("back", true)
        end
end

function rightAction()
        t:toggleButton("Spider")
        if rs.getOutput("right") == true then
                rs.setOutput("right", false)
        else
                rs.setOutput("right", true)
        end
end

--this function must be below the three other functions.
buttonLoads = function()
        t:add("Wither", leftAction, 2, 2, 14, 5, colors.orange, colors.red)
        t:add("Blaze",  backAction, 9, 8, 21, 11, colors.gray, colors.red)
        t:add("Spider", rightAction, 16, 2, 28, 5, colors.blue, colors.red)
        t:draw()
end

buttonLoads()
t:run()


#30 ByteZz

  • Members
  • 23 posts
  • LocationThe United States

Posted 13 March 2014 - 12:06 PM

Thank you for the script but if you don't mind me asking for future reference, why does by buttonLoads() function need to be below my action functions?

#31 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 13 March 2014 - 03:19 PM

Well, since you have everything declared as globals, it might not matter as much, but it is always a good idea to have functions declared before they are referenced or called. The function that sets up your buttons references your three action functions, one for each button, so it is good to have those functions above it.

#32 ByteZz

  • Members
  • 23 posts
  • LocationThe United States

Posted 14 March 2014 - 02:48 PM

View PostLyqyd, on 13 March 2014 - 03:19 PM, said:

Well, since you have everything declared as globals, it might not matter as much, but it is always a good idea to have functions declared before they are referenced or called. The function that sets up your buttons references your three action functions, one for each button, so it is good to have those functions above it.

Thank you sir for the great help. I am soon going to use your brilliant and simple API for other things later.

#33 frytekmk

  • New Members
  • 2 posts

Posted 01 May 2014 - 04:54 PM

I need your help. I'm trying make program that will send(with rednet) instructions for turtle when toggleButton is on and off, but i dont have idea how.

Simply:

Button green(state on) - rednet.send(4,"dig")

Button red(state off) - red.send(4,"take")

rest of job do turtle program.

#34 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 01 May 2014 - 05:37 PM

This may be something like what you're looking for:

--# Find an attached monitor.
local t, x, y
for _, side in ipairs(rs.getSides()) do
  if not t and peripheral.getType(side) == "monitor" then
    t = touchpoint.new(side)
    x, y = peripheral.call(side, "getSize")
  elseif not rednet.isOpen() then
    if peripheral.getType(side) == "modem" then
      rednet.open(side)
    end
  elseif t and rednet.isOpen() then
    break
  end
end

local function action()
  if t.buttonList.Dig then
    --# button currently says Dig.
    t:toggleButton("Dig")
    t:rename("Dig", "Take")
    rednet.send(4, "dig")
  elseif t.buttonList.Take then
    --# button currently says Take.
    t:toggleButton("Take")
    t:rename("Take", "Dig")
    rednet.send(4, "take")
  end
end

t:add("Dig", action, 2, 2, x - 1, y - 1, colors.red, colors.green)

t:run()


#35 frytekmk

  • New Members
  • 2 posts

Posted 01 May 2014 - 06:59 PM

Thank you.That is even more than I wanted but lines responsible for finding modem don't work properly. I had to remove that and simply open rednet. This is only for your know.

Edited by frytekmk, 02 May 2014 - 09:44 AM.


#36 UmbraCharon

  • Members
  • 7 posts

Posted 09 May 2014 - 03:52 PM

How could i get it so that i can have multiple buttons do different things things. I want to make an elevator with buttons for different floors and an if statement for each button controlling the amount of time a red stone signal is on, i got one floor working but i don't know how to do multiple button if statement.

#37 Joseph_Stalin

  • Members
  • 9 posts

Posted 20 May 2014 - 09:07 PM

Is there anyway that I can tell your buttons to execute a certain command when the button is active and a different command when the button is inactive?

#38 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 20 May 2014 - 09:22 PM

The easiest way is probably by using the handleEvents function and an event loop, but you could also use a function that knew the name of the button it was assigned to and checked whether it was active or inactive when it was called.

#39 Joseph_Stalin

  • Members
  • 9 posts

Posted 20 May 2014 - 10:50 PM

Would you mind explaining to me how I would go about doing this? Sorry, I am not very familiar with these "events".

#40 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 21 May 2014 - 12:17 AM

So, here's what the two options look like. First, the event handling way:

local t = touchpoint.new("top")

t:add("NameOfButton", nil, 2, 2, 28, 11, colors.red, colors.lime)

while true do
    local event = {t:handleEvents(os.pullEvent())}
    if event[1] == "button_click" then
        if event[2] == "NameOfButton" then
            if t.buttonList.NameOfButton.active then
                --do stuff when button is active and just got clicked
            else
                --do stuff when button is inactive and just got clicked
            end
        end
    end
end

Second, the button function way:

local t = touchpoint.new("top")

local doAction = function()
    if t.buttonList.NameOfButton.active then
        --do stuff when button is active
    else
        --do stuff when button is inactive
    end
end

t:add("NameOfButton", doAction, 2, 2, 28, 11, colors.red, colors.lime)

t:run()

Obviously, these are just examples and would need to be adapted to whatever your actual setup is.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users