Advertisement
Piorjade

fsEAPI example

Jan 3rd, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.70 KB | None | 0 0
  1. --let's say the API is in /fsEAPI
  2. os.loadAPI("/fsEAPI")
  3. --make a table for the items, this is necessary
  4. local items = {}
  5. local debug = fs.open("/debug", "w")
  6. --add a button to the screen, labeled 'myButton' --> items['myButton']
  7. fsEAPI.addButton(items, "myButton", "Button", 6, 2, 2)
  8.  
  9. --add a textbox to the screen, labeled 'myTextBox' --> items['myTextBox']
  10. fsEAPI.addTextBox(items, "myTextBox", "Edit me senpai", 20, 2, 4)
  11.  
  12. --add a scrollbox to the screen, labeled 'myScrollBox' --> items['myScrollBox']
  13. fsEAPI.addScrollBox(items, "myScrollBox", 20, 3, 2, 6)
  14. --add a label
  15. fsEAPI.addLabel(items, "myLabel", "Hello, World!", 20, 2, 10)
  16. --add 'Hello' into the scrollbox
  17. fsEAPI.addChild(items['myScrollBox'], "Hello")
  18. --add 'My' into the scrollbox
  19. fsEAPI.addChild(items['myScrollBox'], "My")
  20. --add 'Friend' into the scrollbox
  21. fsEAPI.addChild(items['myScrollBox'], "Friend")
  22. --add 'Lolz' into the scrollbox
  23. fsEAPI.addChild(items['myScrollBox'], "Lolz")
  24. --add a context menu for the scrollbox, containing "Open" and "Delete"
  25. --'bg' is the backgroundcolor and 'fg' is the textcolor
  26. local list = {
  27.     {
  28.         text = "Open",
  29.         bg = colors.white,
  30.         fg = colors.blue,
  31.     },
  32.     {
  33.         text = "Delete",
  34.         bg = colors.white,
  35.         fg = colors.red,
  36.     },
  37. }
  38. --now create a context menu for that scrollbox giving the made list
  39. local ok, err = fsEAPI.addContextMenu(items['myScrollBox'], list)
  40. --preselect an item in myScrollBox, 2 should be 'My'
  41. items['myScrollBox'].childs[2].selected = true
  42.  
  43. --clear the screen and draw every child
  44. fsEAPI.updateAll(items, colors.black)
  45. --Everything should be drawn on a black screen now
  46.  
  47. --Now create a coroutine for reading the events so you can still process the information, you don't have to do that tho, you can use parallel too for example
  48.  
  49. local c = coroutine.create(fsEAPI.run)
  50. --the way my API is made, it works by just resuming the coroutine with the table for "initializing"
  51. coroutine.resume(c, items)
  52. --this coroutine has your table now loaded and returns events just for that table
  53. --now start reading events and process the output
  54. local file = fs.open("/log", "w")
  55. --for testing purposes, we'll log the events
  56. --create a table for events (e.g. mouseclicks)
  57. local evt = {}
  58. --initialize a loop
  59. while true do
  60.   coroutine.resume(c, unpack(evt))
  61.   evt = {os.pullEvent()}
  62.   --now check if your coroutine returned any button clicks
  63.   if evt[1] == "button_press" then
  64.     --if a button was pressed
  65.     --log that and the name (key) of the button
  66.     file.writeLine("Button: "..evt[3].." was pressed.")
  67.     file.flush()
  68.   elseif evt[1] == "scrollBox_select" then
  69.     --if an item in the myScrollBox was selected
  70.     --log that and the name (table.text) of the item
  71.     file.writeLine("Item: "..evt[3]..": child: "..items[evt[3]].childs[evt[4]].text.." was selected.")
  72.     file.flush()
  73.     --edit the text of myLabel to the selected item and update
  74.     items["myLabel"].text = items[evt[3]].childs[evt[4]].text
  75.     fsEAPI.update(items.myLabel)
  76.   elseif evt[1] == "context_select" and evt[2] == "myScrollBox" then
  77.     --if something inside the context window (specificly from myScrollBox) is clicked, log that
  78.     file.writeLine("Item: "..evt[2]..": child: "..items.myScrollBox.childs[evt[3]].text.." context: "..evt[4].." was clicked.")
  79.     file.flush()
  80.     if evt[4] == "Delete" then
  81.         fsEAPI.removeChild(items.myScrollBox, evt[3])
  82.         fsEAPI.update(items.myScrollBox)
  83.     end
  84.   elseif evt[1] == "textBox_input" and evt[3] == "myTextBox" then
  85.     --if something was written in the textbox, log it and add a new child to the scrollbox
  86.     file.writeLine("Item: "..evt[3].." was edited.")
  87.     file.flush()
  88.     fsEAPI.addChild(items.myScrollBox, items.myTextBox.text)
  89.     fsEAPI.update(items.myScrollBox)
  90.   end
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement