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

Touchpoint in a window

Started by Dreadstone, 17 August 2015 - 02:50 PM
Dreadstone #1
Posted 17 August 2015 - 04:50 PM
Hello, first time poster here.

I'm trying to adapt the example program that's one the Touchpoint API thread to draw the buttons in a window that's parented by the main monitor. I'm able to get the buttons to draw sucessfully where I want them, but for some reason I can't get them to toggle when they are place on the monitor.

I know I can't term.redirect directly to the window, so I've been trying to redirect to the window first, then leaving touchpoint.new() blank so it uses the current terminal, however, this doesn't seem to be working.

Spoiler–# load the touchpoint API
os.loadAPI("touchpoint")
mon = peripheral.wrap("left")
mon.clear()

win = window.create(mon, 4, 4, 20, 20, true)
term.redirect(win)

–# intialize a new button set
local t = touchpoint.new()

–# add two buttons
t:add("left", nil, 3, 3, 9, 8, colors.blue, colors.lime)
t:add("right", nil, 12, 3, 18, 8, colors.blue, colors.lime)
–# draw the buttons
t:draw()

while true do
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)

end
end

If someone could point out what's wrong with my code or provide an alternate solution I'd be most grateful. I've spent more time than I'd like to admit experiment and searching for solutions online but have not been sucessful. Being relatively new to Lua doesn't help either. =/

In case anyone is wondering, I'm using window term objects because I'm trying to use the buttons to intgrate with a background image (representing my reactor and turbines) for a control program, if that narrows down solutions any. Thanks in advanced.
Lyqyd #2
Posted 17 August 2015 - 06:29 PM
When you don't give the API the monitor side name, it will look for mouse_click events rather than monitor_touch events. You'd need to write a function to take the monitor_touch events and translate them to mouse_click events, changing the x and y coordinates if necessary.

A better solution would be to get rid of the window, as it likely won't let you do what you want anyway, then copy the draw function out of the API and add a line to draw the background image between the clear call and the buttons being drawn. Then simply override the draw function on your touchpoint instance with the modified copy of the draw function and it will draw the background with the buttons.


local image = paintutils.loadImage("myImg")

local function newDraw(self)
  --# copy existing draw function from API, then insert this line between the clear and the button drawing:
  paintutils.drawImage(image, 1, 1)
  --# the buttons draw
end

t.draw = newDraw
Dreadstone #3
Posted 17 August 2015 - 06:51 PM
Thanks for the quick response! I'll let you know how it goes.
Dreadstone #4
Posted 18 August 2015 - 10:26 PM
Got it working. Thanks again. Took me a while to figure out where to put the "t.draw = newDraw" line, but it works beautifully now.