Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
MoparDan

Button API now for OC! - Updated 9-6-2014

Recommended Posts

Changelog:

Changelog:

9-6-2014..Take 2: Added in a global variable in the buttonAPI.lua file to be able to get the button's
current state and be able to pass it on to the 'demo' script. I also updated the demo script with notes
in it on how to use it. both links have been updated.

9-6-2014: Updated the buttonAPI with improved button drawing on the screen with a great amount of help
from Wuerfel_21, many, many thanks! You can use the same buttonAPI pastebin link below to get it.

9-3-2014: Fixed a bug causing the screen not redrawing itself after a MP server restart (might have also
existed in SP worlds). The bug was fixed in the demo file. The fix is after waiting 1 second for a touch
event, it would write a period in the lower-right corner on the screen then remove it right away. This fix
will work on any size/resolution of screen. Please re-grab the demo file using the link near the bottom
of this post.

9-2-2014: Updated both the API and the demo file. Thanks to Wobbo for pointing out my flaw, the API now
has the functions properly set in a table and also fixed an error with setting a label(not the heading,
but a label) and included it in the demo script. The demo script has been updated to work with the
changes in the API file. Also removed 2 unneeded functions from the buttonAPI script.
Links for the corrected scripts are in the post below!

First and foremost, yes, this is a port of DW20's button API from ComputerCraft. Why do his? Because his is a rather simple API and it provided a challenge, at least for me, to learn more about OC.

 

When I did the port, it is more or less a direct port, so any limitations, or bugs (if any) were present in it, are still there. The only change I made to his api is that I made the button flash delay configurable from your script so you shouldn't have to edit the API itself to change it.

 

Now, for some of the issues I noticed from this API running on OC:

 

1. It does fully work, but due to how OpenComputers draws to the screen, its not a smooth transition (you can see the button being redrawn).  Does not affect how it works, but it causes a minor delay, as noted below, and does not look very...pretty, if you care about that sort of thing. This has been corrected!

2. For things like switching the toggle-able button or while the flash button is...well...flashing, you can't press any other button until that button has finished its redrawing process. Again, not a huge deal, just a minor annoyance. That is why I made the flash delay easily configurable from your main script so you do not have to tinker around inside of the API itself. With the optimization that Molinko provided, this is no longer a major issue, but the button flash delay is still configurable to suite your personal tastes.

3. Do to how OC handles bundled cables, if you use this for working with redstone, be sure to include a short delay between switching multiple redstone outputs, or else it WILL become very upset, take your grandmother hostage, set fire to your house and kick your pets...in other words, it will crash in a spectacular fashion. Again, thanks to Molinko's help,

this no longer seems to be the case. It is possible that it could vomit all over itself if the buttons are 'mashed' as fast as possible, but currently it seems unlikely (note, unlikely does not mean impossible, so use caution just to be sure!).

4. This is more or less a direct port of his API to OC. The only change I made was the flash delay. Yes, I am sounding like a broken record, but thanks to the help of Wuerfel_21

and Molinko, some optimizations have been added and some code changed to make it more efficient in OpenComputers.

 

With that said, here is an 'old' demo of it in action (the button drawing issue has been fixed since this video was made):

 

 

Here is the pastebin link for the 'main' demo script: http://pastebin.com/tW1AfDbA  (save it as 'demo')  NO LONGER RECOMMENDED!

here is the pastebin link for the buttonAPI.lua script: http://pastebin.com/dP0bj3ck  (save it as 'buttonAPI.lua') NO LONGER RECOMMENDED!

 

Pastebin link for the corrected 'buttonAPI.lua' script: http://pastebin.com/YUPjgQmd (save it as 'buttonAPI.lua')

Pastebin link for the corrected 'main' demo script: http://pastebin.com/N7ggD2CN (save it as 'demo')

 

save them both and run demo to test it.

Link to post
Share on other sites

I am all for feedback, hints, tips, you name it, thank you! To be honest, the reason why I used pastebin instead is because they can simply use the pastbin get command to easily download them from within the game itself. Granted, it is just as easy to open up the folder in single player and drop them in, but on a MP server, its not quite as easy.

 

As for putting it on OpenPrograms, to be honest, I don't feel right about it. I mean, yes, I am sure quite a few people can make very good use of it from doing so, but on the other hand, I am not the original creator of this API, I just merely ported it, and to be completely honest, I know just enough LUA to be dangerous with it (I.E. I do ALOT of fumbling with hap-hazzard code to get things to somewhat work).

 

And finally, the reason why I did this port in the first place is because 1) I wanted bundled cables working with computers with buttons :D and 2) I did it as a challenge to myself to help better learn OC and also LUA a little bit more and finally 3) to help spark some (more) interest in not only OC but to also help point out just because OC works differently then CC doesn't mean that it can't be done on OC, it just needs to be done a little bit differently.

Link to post
Share on other sites

I could give you a few tips and critiques if you're up for it. First of good job. It needs some real work to be any good with open computers though. I understand the want for

a familiar api from computercraft venturing into OC, but it's just very poorly optimized for OC. For instance:

function API.fill(text, color, bData)
  mon.setBackground(color)
  local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  for j = bData["ymin"], bData["ymax"] do
    term.setCursor(bData["xmin"], j)
    if j == yspot then
      for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
         if k == xspot then
            term.write(text)
          else
            term.write(" ")
          end
       end
     else
       for i = bData["xmin"], bData["xmax"] do
         term.write(" ")
       end
     end
  end
  mon.setBackground(Black)
end
--------------------------------------------
-- ## A much faster and easier way to draw,
-- ## You'll notice the buttons will draw much much faster!!

function API.fill(text, color, bData)
  -- ## this part works just fine
  local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  -- ## save any previous color used before, not just black
  local oldColor = mon.setBackground(color)
    -- ## gpu.fill(x,y,width,height,char) char here = " " for a blank space of color
    mon.fill(xmin, ymin, (xmin+xmax-1), (ymin+ymax-1), " ")
    -- ## write your text here from the gpu.set function instead of io.
    mon.set(xspot, yspot, text)
    -- ## reset the old color
    mon.setBackground(oldColor)
end

Also, naming a table of functions in the api 'API' is awkward and could be misleading or confusing for others and newer users.

 

Check out this page for some more component functions like gpu.fill() and such.

 

I hope this helps. Feel free to add it into the existing code for a quicker drawing update! Any questions are welcome :)

Link to post
Share on other sites

Haha, "poorly optimized" is an understatement, and thank you for the feedback/input on it! It was a "quick and dirty" port just to get it working into OC. I am sure if you opened up the ComputerCraft version and compared it to this, you will see it is almost identical with 'just' enough changes to make it work.

 

I find it very interesting how that whole for loop was replaced by a simple mon.fill and mon.set (if I am understanding this correctly)...I never even thought about that. Like I stated before, I know 'just' enough LUA to be dangerous with it, but I don't fully know its 'ins and outs' if you will.

 

As for the "API" naming method, to be honest, I did that just to help myself out with what was being called from the API/Library and to help become more familiar with how tables work in general. (yea, my brain works....differently.... that way), not to mention that there is already a table in use called 'button' that gets used within the API itself.

 

I have been looking through the various component pages of the main site to try to get better educated with what each does and how each works, I just struggle with ways on how to better utilize them, like how you did with the fill function.

 

And finally, yes it has helped, alot. I will try testing/implementing this when I have a bit more free time (married, kids and 50~60 hr workweek leaves very little free time).

Link to post
Share on other sites

Haha, "poorly optimized" is an understatement, and thank you for the feedback/input on it! It was a "quick and dirty" port just to get it working into OC. I am sure if you opened up the ComputerCraft version and compared it to this, you will see it is almost identical with 'just' enough changes to make it work.

 

I find it very interesting how that whole for loop was replaced by a simple mon.fill and mon.set (if I am understanding this correctly)...I never even thought about that. Like I stated before, I know 'just' enough LUA to be dangerous with it, but I don't fully know its 'ins and outs' if you will.

 

As for the "API" naming method, to be honest, I did that just to help myself out with what was being called from the API/Library and to help become more familiar with how tables work in general. (yea, my brain works....differently.... that way), not to mention that there is already a table in use called 'button' that gets used within the API itself.

 

I have been looking through the various component pages of the main site to try to get better educated with what each does and how each works, I just struggle with ways on how to better utilize them, like how you did with the fill function.

 

And finally, yes it has helped, alot. I will try testing/implementing this when I have a bit more free time (married, kids and 50~60 hr workweek leaves very little free time).

 

Ah don't be harsh on yourself. It was a good little exercise for you to port im sure. Its a great way to improve your understanding of lua. A few months back and that woulda confused the crap out of me too. I would recommend looking into the lua pil (programming in lua). If you need any more help or have questions just ask.

Link to post
Share on other sites

Nah, I don't think I am being harsh, just realistic.

 

As for for the lua pil, do you recommend the red one or the blue one? :P

 

But seriously, is there a good reference (other then the one linked on the main site that is) that provides a more "tutorial" method of learning more into lua? I know some argue that "copy-pasta" from a tutorial doesn't teach you anything, but for myself the best teaching aid (personally that is) is to 'learn by example'.

Link to post
Share on other sites

UPDATE
 
Molinko, thank you soooooo much! That helped out ALOT on how it works, and it also pointed out a different flaw (or more like a difference between how OC and CC work).
 
I had to do some tweaks to it to get it to work properly within its script, but it helped out a ton, thank you!

 

function API.fill(text, color, bData)
  local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  -- # had to ADD xmin and xmax, or else the button text ended off on the left/off the screen
  local xspot = math.floor((bData["xmax"] + bData["xmin"] - string.len(text)) /2)+1
  local oldColor = mon.setBackground(color)
  -- # and here I had to call the table info directly, or else it threw a nil error.
  mon.fill(bData["xmin"], bData["ymin"], (bData["xmax"]-bData["xmin"]+1), (bData["ymax"]-bData["ymin"]+1), " ")
  mon.set(xspot, yspot, text)
  mon.setBackground(oldColor)
end

 

I will update the first post 'soonish' with this change.

Link to post
Share on other sites

Seems simplistic. A button API would be quite simple to make since all you're doing is this little bit of math:

 

if (mouse.x >= button.x and mouse.x <= button.x+button.w) and (mouse.y >= button.y and mouse.y <= button.y+button.h) then
  return true
else
  return false
end

 

Honestly, if you want to make this improved, you could go with better Button graphics. Such as background-image, text-color ( you might already have this ), border-color, border-size, also if mouse.x is able to be checked without a click event, you could do Button highlights as people move their mouse over them.

Link to post
Share on other sites

Unfortunately, there is no way to know where is your mouse cursor, before you click or drag.

Dang, that'd be a nice additional feature for making graphics. I use that in Love2D a lot for my Button APIs.

Isn't there a Love2D OC thing out or something? I remember reading something about that.

Link to post
Share on other sites

if you want to use a more complex,object-oriented GUI-API you can use mine: http://oc.cil.li/index.php?/topic/580-gui-api-064beta/

I know it's more complex, has a lot of features and  more files but it also has buttons in it, well kind of as you can make anything a button, e.g.:

gui=require"GUI"
gui.initialize()
button=gui.labelbox(x,y,rx,ry,layer,fore-color,back-color,clickEvent,nil,text)

x,y is the Position, rx&ry length from starting point, layer can be nil (automatically done then)

clickEvent is the function you want to have executed when clicking on it.

you can change everything by commands like: button.setText(text), button.move(dx,dy), button.setFCol(color), button.setBCol(color),...

 

Hope this helps you, but if you search something very simple and short then use this Button-API.

main command seems to be this one:

API.setTable("text",function,x,y,rx,ry)
Link to post
Share on other sites

This button work great, like DW20 buttons.

But I have a question, how can I operate with two and more gpu's with such code ?

I try to change "local mon = component.gpu " on

function gpuBind(gpuA,scrA)
 mon = component.proxy(component.get(gpuAddress))
 scr = component.get(screenAddress)
 gpu.bind(scr)
 return mon
end

local mon = gpuBind("00b","213")

 

So, How can I call two variables (gpuAddress , screenAddress)in function from other program ?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.