Jump to content

[Computercraft] LUA Button-API V1.0


Corona
 Share

Recommended Posts

Hallo alle zusammen, ob ihr's nun wollt oder nicht: hier bin ich wieder. Nachdem ich mal etwas mehr Zeit im Alltag habe, habe ich wieder mal entschlossen Minecraft zu spielen, besonders Computercraft, und wollte endlich eine eigene API haben, mit der man Buttons ganz einfach und schnell erstellen und benutzen kann. Das hier ist dabei rausgekommen und naja, ich hoffe es kann von anderen auch genutzt werden ^^

	    --(C) Corona 2013 PLEASE DO NOT STEAL THIS CODE!    function draw(button,side)      xStart=button["x1"]      xEnd=button["x2"]      yStart=button["y1"]      yEnd=button["y2"]      fillColor=button["color"]      x=xEnd-xStart      y=yEnd-yStart      textCentery=math.ceil(yStart+y/2)      textCenterx=math.ceil((xStart+(x/2))-(string.len(button["text"])/2))      print("")      print("Putting Text at y-Coordinates:"..textCentery)      print("Putting Text at x-Coordinates:"..textCenterx)      print("Drawing x:"..x)      print("Drawing y:"..y)      print("Draw Color:"..button["color"])      monitor=peripheral.wrap(side)      monitor.setCursorPos(xStart,yStart)      monitor.setBackgroundColor(fillColor)      for j=1,y do        for i=1,x do            monitor.write(" ")        end        monitor.setCursorPos(xStart,yStart+j)      end      monitor.setCursorPos(textCenterx,textCentery)      monitor.write(button["text"])      monitor.setBackgroundColor(colors.black)    end         function cls(side)      local monitor=peripheral.wrap(side)      monitor.setBackgroundColor(colors.black)      monitor.clear()    end         function pressed(button,x,y)      if (button["x1"]<=x) and (button["x2"]>=x) and (button["y1"]<=y) and (button["y2"]>=y) then        return true      else        return false      end    end         function changeColor(button,color)      button["color"] = color      draw(button)      return button    end         function moveButton(button,newx1,newx2,newy1,newy2)      button["x1"] = newx1      button["x2"] = newx2      button["y1"] = newy1      button["y2"] = newy2      draw(button)      return button    end

und hier der Pastebin zu diesem Code, damit man es direkt in Computercraft mit pastebin get runterladen kann: P5kM0hgh

Hier noch 2 Bilder im Anhang was ich damit gemacht habe :D

Und der Code zu dem, was auf den Bildern zu sehen ist:

	    os.loadAPI("buttonAPI")    mon = peripheral.wrap("right")    maxx,maxy = mon.getSize()    local button1 = {x1 = 3,x2 = 23,y1 = 2,y2 = 16,state = false,rs = colors.white,text = "Boiler 1",color = colors.red}    local button2 = {x1 = maxx-22,x2 = maxx-2,y1 = 2,y2 = 16,state = false,rs = colors.blue,text = "Boiler 2",color = colors.red}    local button3 = {x1 = 3,x2 = 23,y1 = maxy-15,y2 = maxy-1,state = false,rs = colors.lime,text="Boiler 3",color = colors.red}    local button4 = {x1 = maxx-22,x2 = maxx-2,y1 = maxy-15,y2 = maxy-1,state = false,rs = colors.red,text = "Boiler 4",color = colors.red}    local buttons = {button1,button2,button3,button4}    local outputs = {0,0,0,0}    while true do      buttonAPI.cls("right")      for i=1,#buttons do        buttonAPI.draw(buttons[i],"right")      end      rs.setBundledOutput("bottom",outputs[1]+outputs[2]+outputs[3]+outputs[4])      local ev,btn,x,y = os.pullEvent("monitor_touch")      print(x)      print(y)      for i=1,#buttons do        if buttonAPI.pressed(buttons[i],x,y) then          print("triggered")          if buttons[i]["state"] then            outputs[i] = 0            buttons[i]["state"] = false            buttons[i]["color"] = colors.red            --button1 = buttonAPI.moveButton(button1,5,6,5,6)          else            outputs[i] = buttons[i]["rs"]            buttons[i]["state"] = true            buttons[i]["color"] = colors.lime          end        end      end    end

post-673-0-73054500-1385904229_thumb.png

post-673-0-11960800-1385904280_thumb.png

Edited by Corona
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...
B
B