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

Lwccgl 1.0.1 - Lightweight Computercraft Graphics Library

Started by Tiin57, 29 July 2013 - 06:30 PM
Tiin57 #1
Posted 29 July 2013 - 08:30 PM
So, after almost 6 months of complete absence from actual Computercraft-related Lua programming, I have returned.
No, it's not incredible. No, it's not almighty.
However, it is quite nifty, and I like it.

Now, anyways. This thing has a terrible name, yes?
Well, it stands for "Light-Weight ComputerCraft Graphics Library". Yes, I got the inspiration from LWJGL. No, you may not judge me.

I know that it's not impressive, but it works.
Example (with the API saved as 'lwccgl'):

os.loadAPI('lwccgl')

local circle = lwccgl.add_circle(8, 8, 7, colors.blue)
circle:draw()

local button = lwccgl.add_button(4, 4, 5, 2, colors.yellow, "Test")
button:draw()
button:input()
print("You pressed the button!")

Now for documentation.

THIS IS AN OBJECT-ORIENTED API.

Base API functions:

-- Creates a button object and returns it.
-- The color and text parameters can be left out.
button = lwccgl.add_button(x, y, width, height, color, text)

-- Creates a circle object and returns it.
-- The color parameter can be left out.
circle = lwccgl.add_circle(centerX, centerY, radius, color)

Button object:

-- Renders the button.
draw()

-- Waits for the button to be clicked.
input()

Circle object:

-- Renders the circle.
draw()

By the way, I do have a small license:
You may use this API however the hell you want.
I'd like it if you told me about your use of it, though. :D/>

Pastebin:
http://pastebin.com/4C3L6QFi

Get the API:
pastebin get 4C3L6QFi lwccgl

Changelog:
Spoiler1.0.1: Circles added
1.0.0: Buttons added

Don't expect this to be all.

Also:
PLEASE, FOR THE LOVE OF ALL DIVINE THINGS, REPORT YOUR BUGS!
Yevano #2
Posted 29 July 2013 - 11:39 PM
Just a bit of criticism: It is currently not possible to wait for input for multiple buttons without using coroutines. You could fix this by switching over to callbacks or making input waiting global for all buttons.

Other than that, I'm glad to see OO design.
theoriginalbit #3
Posted 29 July 2013 - 11:40 PM
Also, you know that LWJGL is "Lightweight Java Game Library" right? xD
Yevano #4
Posted 29 July 2013 - 11:44 PM
Also, you know that LWJGL is "Lightweight Java Game Library" right? xD

I was thinking the exact same thing. :P/>

Also I edited this out of my first post, but to change the wording a little: Is this going to have actual drawing features later on, or is this more of a gui lib than a graphics lib?
Tiin57 #5
Posted 29 July 2013 - 11:47 PM
It is currently not possible to wait for input for multiple buttons without using coroutines. You could fix this by switching over to callbacks or making input waiting global for all buttons.
Right, still trying to figure that one out.

Also, you know that LWJGL is "Lightweight Java Game Library" right? xD
Hence the allusion to LWJGL in the OP. Trust me, I know :D/>

Is this going to have actual drawing features later on, or is this more of a gui lib than a graphics lib?
It will have graphical and gui features.
jesusthekiller #6
Posted 30 July 2013 - 07:59 AM
Since LoadAPI just runs file and you have to run initialize() after loading file can't you just add "initialize()" at the end of API? :)/>
Tiin57 #7
Posted 30 July 2013 - 09:00 AM
Right, updated OP and pastebin with that change. Thanks.
Tiin57 #8
Posted 30 July 2013 - 10:46 AM
Moving towards graphics with add_circle()!
jesusthekiller #9
Posted 30 July 2013 - 11:20 AM
Cool :)/>
ardera #10
Posted 02 August 2013 - 06:13 AM
some gui features like lists, panels, windows, checkboxes etc. would be nice :)/>
Tiin57 #11
Posted 02 August 2013 - 07:23 AM
Yeah, everything is on pause during Modjam. :D/>
Kamefrede #12
Posted 03 August 2013 - 01:03 PM
Nice api i like it might use it for something later on
FPJarva #13
Posted 08 August 2013 - 09:44 AM
Wouldn't it be better to add the input() as an object oriented function?

local button = lwccgl.add_button(4, 4, 5, 2, colors.yellow, "Test")
button:draw()
function button:input()
    print("You clicked the button!")
end
It just makes more sense, then you can have the button do multiple things upon being clicked rather than waiting for it to be clicked.
star_trekguy #14
Posted 14 August 2013 - 10:48 PM
This is a useful little api. I did actually use it for an advanced monitor touch screen key-code entry system. Here is a picture:

Spoiler

I had to add a function after your 'input' function to allow one loop to check all the buttons, instead of blocking for each button:


button['checkClick'] = function(self, x, y)

	  
	 if not self.drawn then
	  return false
	 end
	  
	 if ( x >= self.startX ) and ( x <= self.startX + self.width ) and
	  ( y >= self.startY ) and ( y <= self.startY + self.height ) then
	  return true
	 end
	  
	 return false
	  
	end

Here is the code to make the buttons work:

Spoiler

function drawMonitor (buttTable, mon)

term.redirect(mon)
term.clear()
for key, value in pairs(buttTable) do
  value.drawn = false
  value:draw()
end
term.redirect(term.native)

end
os.loadAPI("lwccgl")
redstone.setOutput("back", true)
local mon = peripheral.wrap("monitor_1")
local buttons = {}
term.redirect(mon)
mon.setTextScale(.5)
local width, height = term.getSize()
term.redirect(term.native)
term.redirect(term.native)
print("Width: " .. width .. " Height: " .. height)
term.redirect(mon)
table.insert(buttons, lwccgl.add_button(2, 1, 2, 2, colors.red, '1'))
table.insert(buttons, lwccgl.add_button(5, 1, 2, 2, colors.green, '2'))
table.insert(buttons, lwccgl.add_button(8, 1, 2, 2, colors.blue, '3'))
table.insert(buttons, lwccgl.add_button(2, 4, 2, 2, colors.orange, '4'))
table.insert(buttons, lwccgl.add_button(5, 4, 2, 2, colors.yellow, '5'))
table.insert(buttons, lwccgl.add_button(8, 4, 2, 2, colors.purple, '6'))
table.insert(buttons, lwccgl.add_button(2, 7, 2, 2, colors.cyan, '7'))
table.insert(buttons, lwccgl.add_button(5, 7, 2, 2, colors.gray, '8'))
table.insert(buttons, lwccgl.add_button(8, 7, 2, 2, colors.brown, '9'))
drawMonitor(buttons, mon)
local swidth, sheight = term.getSize()
term.clear()
screenButt = lwccgl.add_button(3, 3, swidth-6, sheight-6, colors.green, "					OPEN")
screenButt:draw()
local passcode = {5, 2, 7, 3}
local input	= {0, 0, 0, 0}
local inputCount = 1
local passTextX = 12
local passTextY = 3
while true do
event, side, x, y = os.pullEvent()

buttonPressed = false
if event == "monitor_touch" then
  --term.write(x .. ", " .. y)
  for key, value in pairs(buttons) do
   if value:checkClick(x, y) then
	input[inputCount] = key
  
	term.redirect(mon)
	term.setCursorPos(passTextX, passTextY + inputCount)
	term.setTextColor(colors.white)
	print(key)
	term.redirect(term.native)
  
	inputCount = inputCount + 1
  
	break
   end
  end

  if inputCount == 5 then
   buttonPressed = true
   for key, value in pairs(passcode) do
	if input[key] ~= value then
	 buttonPressed = false
	 break
	end
   end
  
   os.sleep(.5)
   drawMonitor(buttons, mon)
   inputCount = 1
  
  end



elseif event == "mouse_click" and side == 1 then
  if screenButt:checkClick(x, y) then
   buttonPressed = true
  end
end
if buttonPressed then
  redstone.setOutput("back", false)
  os.sleep(5)
  redstone.setOutput("back", true)
end
end

If you want to use it, just change the monitor name and redstone outputs to what you need.
LeB0ucEtMistere #15
Posted 15 August 2013 - 03:21 PM
Mhhh that's totally the kind of API I love :D/> easy to use, usefull and full of sens :P/> i'll use it for my buttons panel, with a little bit of parallels, it will be perfect for holding several buttons at the same time :)/>
thanks for your work :)/> keep it up