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

Help using Touchpoint Api buttons with other text

Started by Rasmon, 10 November 2015 - 04:42 AM
Rasmon #1
Posted 10 November 2015 - 05:42 AM
I am fairly new to CC and I am trying to create my own adaptation of the Direwolf20 reactor/turbine control program and wanted to use Touchpoint API. I am struggling a bit with getting the text to update on the screen while using this API. I basically took the example code for Touchpoint API, shrunk the buttons and moved them a bit, removed the monitor.clear from the API (as it was erasing my other text), and added details above the buttons. My next plan was to make those buttons do some other things, but when I tested my progress I noticed that the reactor/turbine information was not updating unless I clicked one of the buttons on the screen. I have tried moving the location of the t:draw() inside the while true do loop, since it seems I need to refresh that after clearing the screen for the reactor/turbine status, but even when it's outside the loop I cannot get the status to update.

I am sure I am just missing something here, but not sure what. Below is the code I have thus far.

Code: http://pastebin.com/W0gJV9NL

Any chance someone can give me a suggestion on how I should be incorporating my other text onto the same monitor with the buttons generated from Touchpoint API?
Lyqyd #2
Posted 10 November 2015 - 06:03 AM
If you want the information displayed on the screen to update periodically, you'd need to use a timer event to update the screen. You'd set a timer with os.startTimer(seconds), then watch for timer events. It's best to capture the timer ID (the return value from your os.startTimer call) and check that to ensure you only update the screen on your own timer events. When handling the timer event, you'd then start a new timer and capture the new ID.

Also, it's probably easier to override the draw function for the touchpoint instance to include the extra stuff you'd like it to do than to modify the API, but that's up to you. It'd look something like this:


local t = touchpoint.new("top")

local oldDraw = t.draw

function t.draw(self)
  oldDraw(self)

  --# other drawing stuff, basically everything in your current display function.
end
Rasmon #3
Posted 12 November 2015 - 12:16 AM
Hi Lua Liquidator! Thanks for the response and suggestions. I finally got a chance to try it and thought I had it, but it appears I am still missing something. Below is where I am at:

I really like the suggestion of overriding the function. For now I did the following, which seems to work for the first draw, but I will likely clean it up later.


  -- Override the touchpoint API draw function
  local oldDraw = t.draw
  function t.draw(self)
    oldDraw(self)
    display(self)
  end

I also added the timer and moved the t:draw() back outside the main loop.


  --# draw the buttons
  timerCode = os.startTimer(3)
  t:draw()

I tried adding a separate event IF statement and when I couldn't get that to work I put an elseif at the end of the original button_click event. I also added some print statements, which show the trigger is firing. I guess I am just not sure what to call in order to refresh though. I am currently using t:draw(self), but that's not doing it.


-- Main Loop
while true do
  --# handleEvents will convert monitor_touch events to button_click if it was on a button
  local event, p1 = t:handleEvents(os.pullEvent())
  if event == "button_click" then
    --# p1 will be "left" or "right", since those are the button labels
    --# toggle the button that was clicked.
    t:toggleButton(p1)
    --# and toggle the redstone output on that side.
    rs.setOutput(p1, not rs.getOutput(p1))
  -- refresh screen based on timer
  elseif event == "timer" and p1 == timerCode then
    print('timerCode: '..timerCode)
    print('event: '..event..' : p1'..p1)
    timerCode = os.startTimer(3)
    print('New timerCode: '..timerCode)
    t:draw(self)
  end
end

Any more tips you can share?
Rasmon #4
Posted 12 November 2015 - 12:37 AM
Please disregard the above… I figured it out! I forgot to add the check() into the correct place. It's working great. Now I need to work on getting those buttons to do something else and clean things up a bit… :)/>

Thanks again!