Advertisement
Guest User

Simple UI API v4 - Example

a guest
Mar 18th, 2013
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. os.loadAPI('ui')
  2.  
  3.     --Pattern:
  4.     -- add(listUI, component name, X, Y, Text, FG, BG, button action, pages, output)
  5.     -- draw(listUI, pages, FG, BG)
  6.     -- mouse(listUI, x, y, pages)
  7.  
  8. function loadUI()
  9.     -- Create a list and adds components to it using the above pattern
  10.     myUI = {}
  11.    
  12.     ui.reset(myUI) -- give myUI the right format
  13.     ui.addUI(myUI, 'stop_button', 1,19, '[EXIT]', colors.black, -1, 'stop', {'all'}, {term})
  14.     ui.addUI(myUI, 'p2', 49,19, '[>]', colors.black, -1, '2', {'1'}, {term})
  15.     ui.addUI(myUI, 'p1', 46,19, '[<]', colors.black, -1, '1', {'2'}, {term})
  16.     ui.addUI(myUI, 'p2_dis', myUI.ui.p1[1],myUI.ui.p1[2], '[<]', colors.lightGray, -1, 'nil', {'1'}, {term})
  17.     ui.addUI(myUI, 'p1_dis', myUI.ui.p2[1],myUI.ui.p2[2], '[>]', colors.lightGray, -1, 'nil', {'2'}, {term})
  18.     ui.addUI(myUI, 'page1', 5,4, 'This displays on page one', colors.blue, -1, 'nil', {'1'}, {term})
  19.     ui.addUI(myUI, 'page2', 5,6, 'This displays on page two', colors.red, -1, 'nil', {'2'}, {term})
  20.     ui.addShape(myUI, 'outline1', 'outline', 1, 2, 51, 18, 0, colors.lime, {'all'}, {term})
  21.     ui.addShape(myUI, 'outline2', 'outline', 3, 4, 49, 13, 0, colors.purple, {'1'}, {term})
  22.     ui.addShape(myUI, 'outline3', 'outline', 3, 6, 49, 16, 0, colors.green, {'2'}, {term})
  23.     ui.addUI(myUI, 'mod1', 7,9, 'Use mod 1', colors.orange, colors.gray, 'mod1', {'1'}, {term})
  24.     ui.addShape(myUI, 'modButton', 'box', 6, 8, 16, 10, 0, colors.gray, {'1'}, {term})
  25.     ui.addUI(myUI, 'default', 7,11, 'Use default', colors.orange, -1, 'default', {'2'}, {term})
  26. end
  27.  
  28. loadUI() -- Load the UI(s)
  29.  pages = {'all','1'}
  30.  loop = true
  31.  action = 'none'
  32.  pos = '0' -- Loads the variables
  33.  
  34. while loop do
  35.  
  36.     -- ## Graphics part ## --
  37.     ui.color(term, colors.white, colors.yellow) -- Sets the colors
  38.     term.clear() -- Clears the screen
  39.     ui.drawSingle(1, 1, 'A: '..action..' P: '..pos, colors.white, colors.yellow, {term}) -- Draws a single comonent that is only for display
  40.     ui.drawSingle(2,2, 'Page '..pages[2], colors.black, colors.lime, {term})
  41.     ui.draw(myUI, pages, colors.white, colors.yellow) -- Uses the list myUI to draw it
  42.     -- ## Processing part ## --
  43.     event, button, xx, yy = os.pullEvent('mouse_click') --Catches mouse click events
  44.     action, key, pos = ui.mouse(myUI, xx, yy, pages, {term}) -- Finds out an action, key (in myUI) and pos on clicked text
  45.     if action == nil then
  46.         action = 'none'
  47.         if not(pos) then
  48.             pos = '0'
  49.         end
  50.     elseif action == 'stop' then
  51.         break
  52.     elseif action == 'mod1' then
  53.         ui.editUI(myUI, 'stop_button', 'bg', colors.red)
  54.     elseif action == 'default' then
  55.         ui.editUI(myUI, 'stop_button', 'bg', -1)
  56.     elseif ui.inTable({'1','2'},action) > 0 then
  57.         pages[2] = action
  58.     else error('something happened')
  59.     end
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement