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

Derkoch's Ccgui-api, An Oop Attempt

Started by DerKoch, 14 October 2013 - 04:21 PM
DerKoch #1
Posted 14 October 2013 - 06:21 PM
Here is it my first API for CC.

About:
A couple of weeks ago I started with Advancd Computers and Monitors in my FTB Unleashed and I was using DireWolf20's Button API, but I did not quite like his API, manually putting buttons on the screen is simply not my style and I also had to do some minor tweaks to his code. So finally I started to do my own thing and day after day new things were added to it until now where I think it's more or less usable by other people.
This is my first real Lua-project and also my first attempt OOP-style Lua. I'm quite shocked that everything seems to work fine.

Features:
  • Grid-like layout for the monitor. Specify a number of rows and column and add elements to each cell or to combined cells
  • Different elements for the GUI: Button, Label and a "Picker" (the term I gave it, anyone got a better name?)
  • Need a bunch of Buttons that should behave like RadioButtons? My API can do that!
  • Setting colors for text, background and the different button-states (active, not-active)
  • Setting position of text on a Label or Button, form top-left to bottom-right, your choice!
  • Executing functions with specified parameters when clicking on a Button
  • A Picker that allows you to "scroll" through a table with values, either your own values or a generated table with your desired range of number values)
  • Full API documentation with LuaDoc
  • .. I think that's it.
Screenshot:
Spoiler

Getting started:
  1. Download my API:
  2. pastebin get SaCnHG5y rbbupdater
  3. Execute rbbupdater. The updater automatically downloads the newest version of my API to your Computer.
  4. Load my APIs:
  5. os.loadAPI("RBB/helper")
    	os.loadAPI("RBB/guiElements")
    	os.loadAPI("RBB/screen")
  6. Download the API documentation and find the stuff you need: https://www.dropbox....igk/API-Doc.zip
If you like to do the stuff manually:
Spoiler
  1. Create a folder on your computer named "RBB".
  2. Download the files:
  3. pastebin get 5qVa8CRF RBB/screen
    	pastebin get ZvUtcSv8 RBB/guiElements
    	pastebin get g1Z0JAW1 RBB/helper
    	pastebin get mK8ZHaZk RBB/config
  4. Load my APIs:
  5. os.loadAPI("RBB/helper")
    	os.loadAPI("RBB/guiElements")
    	os.loadAPI("RBB/screen")
  6. Download the API documentation: https://www.dropbox....igk/API-Doc.zip



Example:
This is the code I used in the screenshot above with some minor changes, also added a lot of of comments. Pastebin: LijScy3g
Spoiler

-- load ALL the APIs!
os.loadAPI("RBB/helper")
os.loadAPI("RBB/guiElements")
os.loadAPI("RBB/screen")
-- Function for the Buttons, just prints the parameter
function fancyFunction(p1, p2)
print(tostring(p1) .. " , " .. tostring(p2))
end
-- Function for Picker, just prints the parameter
function incdecFunction(param)
print("incdec: " .. tostring(param))
end
-- Create a new Screen. The Monitor is on the "right" and will have a 4 by 4 grid
local rows = 4
local cols = 4
local thisScreen = screen.Screen.create("right", rows, cols)
-- Lets make some buttons
-- For more detailed info, look into the documentation ;)/>/>/>/>/>/>/>/>/>
local btn = guiElements.Button.create("1", "Button", false, fancyFunction, {"Button", "derp"})
local btn2 = guiElements.Button.create("2", "Button2", true, fancyFunction, {"Button2", "derp"})
local btn3 = guiElements.Button.create("3", "Button3", false, fancyFunction, {"Button3", "derp"})
local btn4 = guiElements.Button.create("4", "Button4", false, fancyFunction, {"Button4", "derp"})
-- Lets make some RadioButtons:
-- The radioIndex can be a string or a number, it's just there to group the buttons
btn3:setRadioIndex("herpderp")
btn4:setRadioIndex("herpderp")
-- And now a label
local lbl = guiElements.Label.create("123","Down here!",{h = "center", v = "center"})
-- and when where at it, lets turn the background of the label blue!
lbl:setBGColor(colors.blue)
lbl:setTextPosition({h = "right", v = "bottom"})
-- Now we create a "Picker". This one's a bit complicated
-- First two parameters are like button and label, but the 3rd is the starting index for the table in the 4th parameter
-- The index is 2 so we start with "def"
local picker = guiElements.Picker.create("picky", "Picker1", 2, {"abc", "def", "ghi", "jkl"})
-- Set the function that is called when the "-" is clicked
picker:setDecFunction(incdecFunction)
-- Set the function that is called when the "+" is clicked
picker:setIncFunction(incdecFunction)

-- Add ALL the elements to the grid
thisScreen:addElementToGrid(picker,2,3)
-- This ones is different! the buttons will have a 2x2 size
thisScreen:addElementToGrid(btn,{1,2},{1,2})
thisScreen:addElementToGrid(btn2,1,3)
thisScreen:addElementToGrid(btn3,3,2)
thisScreen:addElementToGrid(btn4,4,1)
thisScreen:addElementToGrid(lbl,4,{2,3,4})
-- This has to be called after adding all the elements, it just sets up each element...the automatic stuff ;)/>/>/>/>/>/>/>/>/>
thisScreen:initGrid()
-- Initially draws everything, only needs to be called once
thisScreen:draw()

-- Basic loop to get the touch events from the monitor and check them on the screen
while true do
event,side,x,y = os.pullEvent("monitor_touch")
thisScreen:checkTouchEvent(event,side,x,y)
end

Pastebins/GitHub:
http://pastebin.com/5qVa8CRF
http://pastebin.com/ZvUtcSv8
http://pastebin.com/g1Z0JAW1
http://pastebin.com/mK8ZHaZk

http://pastebin.com/LijScy3g

https://github.com/RoyalBingBong/ccGUI-API


Changelog:
Spoiler

v1.0
-Initial release
jay5476 #2
Posted 23 October 2013 - 05:55 AM
looks nice havnt go a chance to test it yet but maybe you could make an installer so people do not have to go through so much trouble to get this
Wojbie #3
Posted 23 October 2013 - 06:41 AM
Looks at his code…
Looks at This beauty….
Looks at his code…
Trashes his code…
Steals beauty
Runs away giggling madly…
DerKoch #4
Posted 27 October 2013 - 12:12 PM
^
waaaat? Thanks I guess.. :o/>
To be honest, I'm surprised the inheritance stuff actually worked out.^^

looks nice havnt go a chance to test it yet but maybe you could make an installer so people do not have to go through so much trouble to get this
I seriously thought about that too, but I somehow distracted myself with other things. Also haven't started minecraft in a week or so… :wacko:/>
Edit: Just added an installer/updater.
Also what's with the editor being so weird and the thread name being so fugly with caps? (Is This A Thing Now?)
Dave-ee Jones #5
Posted 04 November 2013 - 02:25 AM
HOW DID YOU DRAW THE PENTAGON?!! IT'S…out of place. :P/>

Nice little neat thingo. Don't the label buttons and buttons do the same thing or what? I like the pickers…I'm assuming they can scroll through each choice by using the + and - buttons?

Neat little API.


EDIT: What texture pack is that? I tried downloading a Sphax one once but it didn't work…
Bomb Bloke #6
Posted 04 November 2013 - 03:24 AM
Looks like the Sphax texture pack, which just so happens to have a pentagon for it's pointer shape (DerKoch didn't draw that on the monitor; he just happened to be looking at the monitor, so you see his pointer superimposed over it).

Last I looked, Sphax requires you to download both its "base" pack and a supplementary pack for whatever modpack you're using.
sens #7
Posted 04 November 2013 - 06:03 AM
Nice - in a way it reminds me of Java's Swing API. But speaking of the screenshot, shouldn't the computer with the Commodore logo say "READY." as its prompt?
DerKoch #8
Posted 06 November 2013 - 02:42 PM
Don't the label buttons and buttons do the same thing or what? I like the pickers…I'm assuming they can scroll through each choice by using the + and - buttons?
Well, yes Buttons and Label are somewhat the same. Except the Label is not clickable or has any kind of attribute in this direction. In this screenshot the label doesn't have the default background color, that might be confusing.
And you're right on the point with the pickers. :)/>
The reason for the picker was to control the Auto-Spawner in the "cage" above/behind the monitor. I select a type of mob and a turtle puts a monster net into the spawner. Didn't finish that project though. :D/>

EDIT: What texture pack is that? I tried downloading a Sphax one once but it didn't work…
Like Bomb Bloke said, it's Sphax PureBDCraft with the applied patches (in my case the patch for FTB Unleashed). Also the pentagon is from this TP. It actually would be cool to draw pixel-wise on monitors and not with those chunky big things.^^


Nice - in a way it reminds me of Java's Swing API. But speaking of the screenshot, shouldn't the computer with the Commodore logo say "READY." as its prompt?
Never had a Commodore, I grew up with Atari ST.^^