Advertisement
mad1231999

GUI Util 1.0 Example #1

Jan 31st, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. local guiElements = {}
  2. local loginScreen = {}
  3.  
  4. local function initLoginScreen()
  5.     local bg = gui_rect.new()
  6.     bg:init(0, 0, S_WIDTH + 1, S_HEIGHT + 1)
  7.     bg.bgColor = colors.cyan
  8.  
  9.     local userLabel = gui_label.new()
  10.     userLabel:init("Username: ", S_WIDTH / 2 - 10, S_HEIGHT / 2 + 2, 10, 1)
  11.     userLabel.bgColor = bg.bgColor
  12.  
  13.     local userField = gui_textfield.new()
  14.     userField:init("", S_WIDTH / 2 + 1, S_HEIGHT / 2 + 2, 15, 1)
  15.  
  16.     local passLabel = gui_label.new()
  17.     passLabel:init("Password: ", S_WIDTH / 2 - 10, S_HEIGHT / 2 + 2 + 2, 10, 1)
  18.     passLabel.bgColor = bg.bgColor
  19.  
  20.     local passField = gui_textfield.new()
  21.     passField:init("", S_WIDTH / 2 + 1, S_HEIGHT / 2 + 2 + 2, 15, 1)
  22.     passField.replaceChar = "*"
  23.  
  24.     table.insert(loginScreen, bg)
  25.     table.insert(loginScreen, userLabel)
  26.     table.insert(loginScreen, userField)
  27.     table.insert(loginScreen, passLabel)
  28.     table.insert(loginScreen, passField)
  29.  
  30.     local cancelButton = gui_button.new()
  31.     cancelButton:init("Cancel", S_WIDTH / 2 - 10, S_HEIGHT / 2 + 2 + 2 + 2, 8, 3)
  32.     cancelButton.onClick = function(self, x, y)
  33.         cleanExit()
  34.     end
  35.     cancelButton.textColor = bg.bgColor
  36.     table.insert(loginScreen, cancelButton)
  37.  
  38.     local loginButton = gui_button.new()
  39.     loginButton:init("Log in", S_WIDTH / 2 + 8, S_HEIGHT / 2 + 2 + 2 + 2, 8, 3)
  40.     loginButton.onClick = function(loginB, x, y)
  41.         -- PERFORM LOGIN STUFF
  42.  
  43.         if success then
  44.             -- Perform success stuff
  45.         else
  46.             userField.editable = false
  47.             passField.editable = false
  48.             loginButton.clickable = false
  49.             cancelButton.clickable = false
  50.  
  51.             local msg = createMessageBox("Failure", errorMessage, "error")
  52.             msg.msgBoxQuit.onClick = function(self, x, y)
  53.                 userField.editable = true
  54.                 passField.editable = true
  55.                 loginButton.clickable = true
  56.                 cancelButton.clickable = true
  57.  
  58.                 for _, el in pairs(msg) do
  59.                     el.visible = false
  60.                 end
  61.             end
  62.  
  63.             for _, el in pairs(msg) do
  64.                 table.insert(loginScreen, el)
  65.             end
  66.         end
  67.     end
  68.     loginButton.textColor = bg.bgColor
  69.     table.insert(loginScreen, loginButton)
  70. end
  71.  
  72. initLoginScreen()
  73. guiElements = loginScreen
  74.  
  75. for _, el in ipairs(guiElements) do
  76.     el:draw()
  77. end
  78.  
  79.  
  80.  
  81. -------------------------------------------------------------
  82. -------------------------------------------------------------
  83. --                   GUI Control loop                      --
  84. -------------------------------------------------------------
  85. -------------------------------------------------------------
  86. while true do
  87.     local ev, p1, p2, p3, p4 = os.pullEvent()
  88.  
  89.     local gui_ev = gui_event.new()
  90.  
  91.     if ev == "mouse_click" then
  92.         gui_ev.button = p1
  93.         if p1 == 3 then
  94.             gui_ev.button = BUTTON_W
  95.         end
  96.         gui_ev.x = p2
  97.         gui_ev.y = p3
  98.        
  99.         for _, el in ipairs(guiElements) do
  100.             el:onMouseEvent(gui_ev)
  101.         end    
  102.     elseif ev == "mouse_scroll" then
  103.         gui_ev.button = p1 == 1 and BUTTON_W_DOWN or BUTTON_W_UP
  104.         gui_ev.x = p2
  105.         gui_ev.y = p3
  106.        
  107.         for _, el in ipairs(guiElements) do
  108.             el:onMouseEvent(gui_ev)
  109.         end
  110.     elseif ev == "key" then
  111.         gui_ev.key = p1
  112.  
  113.         for _, el in ipairs(guiElements) do
  114.             el:onKeyEvent(gui_ev)
  115.         end
  116.     end
  117.    
  118.     for _, el in ipairs(guiElements) do
  119.         el:draw()
  120.     end
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement