CodeCrafter

Gui creator

Mar 7th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. local oldPull = os.pullEvent
  2. os.pullEvent = os.pullEventRaw
  3.  
  4. term.clear = function(c) if c then term.setBackgroundColor(c) end term.native.clear() term.setCursorPos(1,1) end
  5. setColors = function(b,t) if b then term.setBackgroundColor(b) end if t then term.setTextColor(t) end end
  6. clickedIn = function(mx,my,x,y,w,h) return (mx >= x and mx <= x + w and my >= y and my <= y + h) end
  7. startsWith = function(str, prefix) prefix = tostring( prefix ) return str:sub( 1, prefix:len()) == prefix end
  8.  
  9. local localGui = {
  10. ["New button"] = { x = 1, y = 1, w = 5, h = 6, action = new },
  11. ["Generate Code"] = { x = 1, y = 1, w = 5, h = 6, action = generate },
  12. ["Cancel"] = { x = 1, y = 1, w = 5, h = 6, action = function() os.pullEvent = oldPull error() end },
  13. }
  14.  
  15. local genericFuncs = [[local function checkButtonClicked(mouseX, mouseY)
  16. for _,v in pairs(buttons) do
  17. if mouseX >= v.x and mouseX <= v.x + v.w and mouseY >= v.y and mouseY <= v.y + v.h then
  18. v.action()
  19. return true
  20. end
  21. end
  22. return false
  23. end
  24.  
  25. local function drawButtons()
  26. for k, v in pairs(buttons) do
  27. term.setBackgroundColor(v.bgColor)
  28. term.setTextColor(v.txtColor)
  29. for i = 0, v.h - 1 do
  30. term.setCursorPos(v.x,v.y+i)
  31. write(string.rep(" ", v.w))
  32. end
  33. term.setCursorPos(v.x,v.y)
  34. write(k)
  35. end
  36. end
  37.  
  38. -- example main loop
  39. while true do
  40. drawButtons()
  41.  
  42. local event = { os.pullEvent() }
  43.  
  44. if event[1] == "mouse_click" then
  45. if event[2] == 1 then -- left click
  46. checkButtonClicked(event[3]. event[4])
  47. end
  48. end
  49. end
  50. ]]
  51.  
  52. local progButtons = {}
  53. local progFuncs = {}
  54.  
  55. local function getInput(text, validationFunc, compareVal)
  56. local output
  57. while true do
  58. write(text)
  59. local input = read()
  60. local ok, val, err = validationFunc(input, compareVal)
  61. if ok then
  62. if err then
  63. if startsWith(err, "Error:") then
  64. term.setTextColor(colors.red)
  65. elseif startsWith(err, "Warning:") then
  66. term.setTextColor(colors.yellow)
  67. end
  68. print(err)
  69. term.setTextColor(colors.white)
  70. write("Continue Anyway? (y/n) ")
  71. local con = read()
  72. if con:lower() == "y" or con:lower() == "yes" then
  73. output = val
  74. break
  75. end
  76. else
  77. output = val
  78. break
  79. end
  80. else
  81. if startsWith(err, "Error:") then
  82. term.setTextColor(colors.red)
  83. elseif startsWith(err, "Warning:") then
  84. term.setTextColor(colors.yellow)
  85. end
  86. print(err)
  87. term.setTextColor(colors.white)
  88. end
  89. end
  90. return output
  91. end
  92.  
  93. local function validateColor(icol, notused)
  94. local col = loadstring("return colors."..icol)
  95. col = col()
  96. if not col then
  97. return false, nil, "Error: "..icol.." is not a valid color."
  98. end
  99. return true, col, nil
  100. end
  101.  
  102. local function new()
  103. local bText = getInput("Button Text: ", function(text)
  104. if progButtons[text] then
  105. return true, text, "Error: Button with that text already exists"
  106. end
  107. return true, text, nil
  108. end, nil)
  109.  
  110. local bWidth = getInput("Button Width: ", function(num, text)
  111. local sw = term.getSize()
  112. num = tonumber(num)
  113. if not num then
  114. return false, nil, "Error: Not a number"
  115. else
  116. if num > sw then return true, num, "Warning: Button width is wider than current screens width" end
  117. if num < 0 then return false, nil, "Error: Button width cannot be less than 1" end
  118. if num < #text then return false, nil, "Error: Button width must be greater than text width" end
  119. return true, num, nil
  120. end
  121. end, bText)
  122.  
  123. local bHeight = getInput("Button Height: ", function(num, notused)
  124. local sw,sh = term.getSize()
  125. num = tonumber(num)
  126. if not num then
  127. return false, nil, "Error: Not a number"
  128. else
  129. if num > sh then return true, num, "Warning: Button height is taller than current screens height" end
  130. if num < 0 then return false, nil, "Error: Button height cannot be less than 1" end
  131. return true, num, nil
  132. end
  133. end, nil)
  134.  
  135. local bx = getInput("Button X: ", function(num, compare)
  136. local sw,sh = term.getSize()
  137. num = tonumber(num)
  138. if not num then
  139. return false, nil, "Error: Not a number"
  140. else
  141. if num > sw then return true, num, "Warning: Button X position would be off the current screens width" end
  142. if num + compare > sw then
  143. return true, num, "Warning: Button X position and width would make part of the button off the current screen"
  144. end
  145. return true, num, nil
  146. end
  147. end, bWidth)
  148.  
  149. local by = getInput("Button Y: ", function(num, compare)
  150. local sw,sh = term.getSize()
  151. num = tonumber(num)
  152. if not num then
  153. return false, nil, "Error: Not a number"
  154. else
  155. if num > sh then return true, num, "Warning: Button Y position would be off the current screens height" end
  156. if num + compare > sh then
  157. return true, num, "Warning: Button Y position and height would make part of the button off the current screen"
  158. end
  159. return true, num, nil
  160. end
  161. end, bHeight)
  162.  
  163. local bTextCol = getInput("Text Color (eg white, red, etc.): ", validateColor, nil)
  164.  
  165. local bBgCol = getInput("Button Color (eg white, red, etc.): ", validateColor, nil)
  166.  
  167. local bAction = getInput("Function name to call when button clicked:\n", function(text, notused) return true, text, nil end, nil) -- allow anything to be entered
  168.  
  169. progButtons[bText] = { x = bx, y = by, w = bWidth, h = bHeight, txtColor = bTextCol, bgColor = bBgCol, action = bAction }
  170. progFuncs[#progFuncs+1] = bAction
  171. end
  172.  
  173. local function generate( progname )
  174. local file = fs.open(progname,"w")
  175. file.write("local buttons = {\n")
  176. for text,buttoninfo in pairs(progButtons) do
  177. file.write("\t[\""..text.."\"] = {")
  178. for k,v in pairs(buttoninfo) do
  179. file.write(" "..k.." = "..v..",")
  180. end
  181. file.write("},\n")
  182. end
  183. file.write("}\n\n")
  184.  
  185. for k,v in pairs(progFuncs) do
  186. if v ~= "error" and v ~= "os.shutdown" and v ~= "os.reboot" then
  187. file.write("local function "..v.."()\n\t\nend\n\n" )
  188. end
  189. end
  190. file.write(genericFuncs)
  191. file.close()
  192. end
  193.  
  194. term.clear()
  195. while true do
  196. new()
  197. write("File name: ")
  198. generate( read() )
  199. localGui.Cancel.action()
  200. end
Add Comment
Please, Sign In to add comment