Jump to content




TouchScreen button menu


  • You cannot reply to this topic
8 replies to this topic

#1 men8

  • Members
  • 3 posts
  • LocationWarsaw

Posted 11 February 2014 - 04:51 PM

Ok, so I'm working on my little project, TouchScreen Button menu to operate my machines from one place. I'm using Direwolf20 buttonAPI which I edit a little bit to my personal use so now i can set different colors for all my buttons. I figured how to change pages with buttons on my monitor, but I want to add function to code that let me set two different functions. One when button is on, and one when button is off. So, this is the code I've got :
Spoiler

I don't know is this posible but i think i must change function setTable and screen to this:
Spoiler

This is the one thing left to do in my project so if anybody can help me with my problem, I'll be really happy. I'm really sorry if this post is in non "english" form, but it's not my main language. I also search for any results on this forum and on google and youtube and i can't find anything what can help me in my problem

#2 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 11 February 2014 - 07:04 PM

Your syntax is a bit off. Look at what I mean:

function setTable(name, func1, func2, xmin, xmax, ymin, ymax, btnOff, btnOn)
   button.name = {}
   button.name.func1 = func1
   button.name.func2 = func2
   button.name.active = false
   button.name.xmin = xmin
   button.name.ymin = ymin
   button.name.xmax = xmax
   button.name.ymax = ymax
   button.name.btnOff = btnOff
   button.name.btnOn  = btnOn
end


#3 men8

  • Members
  • 3 posts
  • LocationWarsaw

Posted 11 February 2014 - 08:00 PM

Yes @surferpup, I know, but it's working in this form. The thing is I don't know how to add secound functions to one button, so if I activate the button first function should work and if I deactivate it the secound function should work. I can stop editing code because it's working, but to start and end one thing i must use two buttons. This is the code of program that use this API.
Spoiler

This is not auto size screen so w=4 and h=3 monitors

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 11 February 2014 - 08:35 PM

View Postsurferpup, on 11 February 2014 - 07:04 PM, said:

Your syntax is a bit off. Look at what I mean:

function setTable(name, func1, func2, xmin, xmax, ymin, ymax, btnOff, btnOn)
   button.name = {}
   button.name.func1 = func1
   button.name.func2 = func2
   button.name.active = false
   button.name.xmin = xmin
   button.name.ymin = ymin
   button.name.xmax = xmax
   button.name.ymax = ymax
   button.name.btnOff = btnOff
   button.name.btnOn  = btnOn
end

I'm really not sure what you're trying to say. This code you've posted is wrong, but what are you trying to say is wrong with his?

#5 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 11 February 2014 - 10:31 PM

I see, I was a bit quick there. I was trying to show that instead of using this syntax:

button.name.["func"] = func

He could do:

button.name.func = func

What I missed was that he does use the contents of name as a key, so it should be:

button[name].func = func

That is cleaner and easier to troubleshoot.

#6 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 11 February 2014 - 11:51 PM

So, now that I have actually looked over your code and more carefully read your question (sorry about that), this is what I am actually covering in Part II and Part III of my Monitor Button and Control Tutorial. Parts I and II are done, and Part III covers exactly what you are trying to do (it is in draft). Your approach is slightly different than mine.

Your approach is basically fine.

It appears that you want to use the fill() function to execute the action. The only thing you need to do then is modify your function prototype for fill and then call the function you pass.

So I'd suggest changing your fill prototype to

function fill(text, color, bData, func)

I put func on the end so you don't need to worry about changing all of your calls.

Then in the fill function, call it (note that I put some error checking in it).

if func and type(func) == "function" -- for error checking
  func()
end

Make sure to fix all of your function calls as well.

Edited by surferpup, 12 February 2014 - 12:03 AM.


#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 12 February 2014 - 01:01 AM

Just to be clear, this:

button.name.func = func

... won't work as desired under any circumstance. It'll create a new key called "name" inside the "button" table, ignoring the "name" variable passed to the "setTable" function. If a key called "name" already exists in that table (which WILL be the case after the first call to "setTable"), then it'll simply be overwritten.

This:

button.name.["func"] = func

... is also invalid, for the exact same reason and also due to that second period (which shouldn't be there).

The OP's code is indeed fine as-is, but this would be ever-so-slightly faster to execute:

button[name].func = func

In terms of human readability, it makes next to no difference. That comes down to taste.

Edited by Bomb Bloke, 12 February 2014 - 01:02 AM.


#8 men8

  • Members
  • 3 posts
  • LocationWarsaw

Posted 12 February 2014 - 06:36 AM

So, anybody can help with my problem, because I know my code is working but when I tried to edit buttonAPI in this functions

View Postsurferpup, on 11 February 2014 - 11:51 PM, said:

So, now that I have actually looked over your code and more carefully read your question (sorry about that), this is what I am actually covering in Part II and Part III of my Monitor Button and Control Tutorial. Parts I and II are done, and Part III covers exactly what you are trying to do (it is in draft). Your approach is slightly different than mine.

Your approach is basically fine.

It appears that you want to use the fill() function to execute the action. The only thing you need to do then is modify your function prototype for fill and then call the function you pass.

So I'd suggest changing your fill prototype to

function fill(text, color, bData, func)

I put func on the end so you don't need to worry about changing all of your calls.

Then in the fill function, call it (note that I put some error checking in it).

if func and type(func) == "function" -- for error checking
  func()
end

Make sure to fix all of your function calls as well.
I'm sorry but I think I don't understand this correcty. This code is for two different functions assigned to one button or what?

#9 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 13 February 2014 - 12:24 PM

You suggested doing the following to change functions:

function screen()
   local currColor
   local currFunc
   for name,data in pairs(button) do
		  local on = data["active"]
		  if on == true then
				 currColor = data["btnOn"] and currFunc = data["func1"]
		  else
				 currColor = data["btnOff"] and currFunc = data["func2"]
		  end
		  fill(name, currColor, currFunc, data)
   end
end

Your if ... then code is slightly off:

if on == true then
  currColor = data["btnOn"]
  currFunc = data["func1"]
else
  currColor = data["btnOff"]
  currFunc = data["func2"]
end

currFunc will be assigned one of the two functions you store in data.func1 or data.func2

As long as data.func1 or data.func2 are functions, a call to currFunct(...) will succeed.

When I said your approach is correct for the most part, I am referring to your idea here.

Edited by surferpup, 13 February 2014 - 12:25 PM.






3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users