Nathan1852

OSTest

Jul 30th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.28 KB | None | 0 0
  1. local _mon = peripheral.wrap("monitor_0")
  2. local _w, _h = _mon.getSize()
  3. local _back = peripheral.wrap("back")
  4. local _top = peripheral.wrap("top")
  5. local _front = peripheral.wrap("front")
  6. local _bottom = peripheral.wrap("bottom")
  7. local _right = peripheral.wrap("right")
  8. local _left = peripheral.wrap("left")
  9. local _Save = false
  10.  
  11. CCInterfaceEntities = {
  12.  
  13. List = {},
  14. ListSave = {},
  15.  
  16.  
  17. Add = function(_entity)
  18. table.insert(CCInterfaceEntities.List, _entity)
  19. end,
  20.  
  21. Remove = function(_entity)
  22. for k, v in pairs(CCInterfaceEntities.List) do
  23. if tostring(v) == tostring(_entity) then
  24. table.remove(CCInterfaceEntities.List, tonumber(k))
  25. break
  26. end
  27. end
  28. end,
  29.  
  30. Toggle = function(_entity)
  31. local remove = false
  32. for _l, _v in ipairs(CCInterfaceEntities.List) do
  33. if tostring(_v) == tostring(_entity) then
  34. table.remove(CCInterfaceEntities.List, _l)
  35. remove = true
  36. break
  37. end
  38. end
  39. if remove == false then
  40. table.insert(CCInterfaceEntities.List, _entity)
  41. end
  42. end,
  43.  
  44. RemoveAll = function(_List)
  45. local k = next(_List)
  46. while k do
  47. _List[k] = nil
  48. k = next(_List)
  49. end
  50. end,
  51.  
  52. Save = function()
  53. for k, v in pairs(CCInterfaceEntities.List) do
  54. table.insert(CCInterfaceEntities.ListSave, v)
  55. end
  56. CCInterfaceArea.RemoveAll(CCInterfaceEntities.List)
  57. _Save = true
  58. end,
  59.  
  60. Load = function()
  61. if _Save == true then
  62. CCInterfaceArea.RemoveAll(CCInterfaceEntities.List)
  63. for k, v in pairs(CCInterfaceEntities.ListSave) do
  64. table.insert(CCInterfaceEntities.List, v)
  65. end
  66. CCInterfaceArea.RemoveAll(CCInterfaceEntities.ListSave)
  67. end
  68. _Save = false
  69. end
  70.  
  71. }
  72.  
  73. CCInterfaceArea = {
  74.  
  75. List = {},
  76. ListSave = {},
  77.  
  78. Add = function(_area)
  79. table.insert(CCInterfaceArea.List, _area)
  80. end,
  81.  
  82. Remove = function(_area)
  83. for k, v in pairs(CCInterfaceArea.List) do
  84. if tostring(v) == tostring(_area) then
  85. table.remove(CCInterfaceArea.List, tonumber(k))
  86. break
  87. end
  88. end
  89. end,
  90.  
  91. Toggle = function(_area)
  92. local remove = true
  93. for _l, _v in pairs(CCInterfaceArea.List) do
  94. if tostring(_v) == tostring(_area) then
  95. table.remove(CCInterfaceArea.List, tonumber(_l))
  96. remove = false
  97. break
  98. end
  99. end
  100. if remove == true then
  101. table.insert(CCInterfaceArea.List, _entity)
  102. end
  103. end,
  104.  
  105. RemoveAll = function(_List)
  106. local k = next(_List)
  107. while k do
  108. _List[k] = nil
  109. k = next(_List)
  110. end
  111. end,
  112.  
  113. Save = function()
  114. for k, v in pairs(CCInterfaceArea.List) do
  115. table.insert(CCInterfaceArea.ListSave, v)
  116. end
  117. CCInterfaceArea.RemoveAll(CCInterfaceArea.List)
  118. end,
  119.  
  120. Load = function()
  121. CCInterfaceArea.RemoveAll(CCInterfaceArea.List)
  122. for k, v in pairs(CCInterfaceArea.ListSave) do
  123. table.insert(CCInterfaceArea.List, v)
  124. end
  125. CCInterfaceArea.RemoveAll(CCInterfaceArea.ListSave)
  126. end
  127. }
  128.  
  129. Screen = {
  130. Width = _w,
  131. Height = _h,
  132. }
  133.  
  134. Peripherals = {
  135. Back = _back,
  136. Front = _front,
  137. Right = _right,
  138. Left = _left,
  139. Top = _top,
  140. Bottom = _bottom,
  141. }
  142.  
  143. CCDrawing = {
  144.  
  145. DrawCharacters = function (_x,_y,_string,_textColour,_backgroundColour)
  146. _mon.setBackgroundColour(_backgroundColour)
  147. _mon.setTextColour(_textColour)
  148. _mon.setCursorPos(_x,_y)
  149. _mon.write(_string)
  150. end,
  151. DrawArea = function (_x,_y,_w,_h,_character,_textColour,_bgColour)
  152. --width must be greater than 1, other wise we get a stack overflow
  153. if _w > 0 then
  154. _w = _w * 1
  155. elseif _w == 0 then
  156. _w = 1
  157. end
  158.  
  159. if _h > 0 then
  160. _h = _h * 1
  161. elseif _h == 0 then
  162. _h = 1
  163. end
  164.  
  165. local sRow = " "
  166. for ix = 1, _w do
  167. sRow = sRow .. _character
  168. end
  169. for iy = 1, _h do
  170. local currY = _y + iy - 1
  171. CCDrawing.DrawCharacters(_x, currY, sRow, _textColour, _bgColour)
  172. end
  173. end
  174. }
  175.  
  176.  
  177. CCObject = {
  178. Type = "CCObject",
  179. ID = 0,
  180. }
  181.  
  182. CCEntity = {
  183. __index = CCObject,
  184.  
  185. X = 0,
  186. Y = 0,
  187. Width = 0,
  188. Height = 0,
  189. Title = " ";
  190. }
  191.  
  192. CCControl = {
  193. __index = CCEntity,
  194. Action = nil,
  195. Enabled = true,
  196. }
  197.  
  198. CCButton = {
  199. __index = CCControl,
  200. Type = "CCButton",
  201. Height = 1,
  202. New = function(self, _x, _y, _textColour, _bgColour, _title, _action)
  203. local new = {} --the new instance
  204. setmetatable( new, {__index = self} )
  205. new.Width = string.len(_title)
  206. new.X = _x
  207. new.Y = _y
  208. new.TextColour = _textColour
  209. new.BackgroundColour = _bgColour
  210. new.Title = _title
  211. new.Action = _action
  212. new.Enabled = true
  213. return new
  214. end,
  215. Draw = function(self)
  216. CCDrawing.DrawCharacters(self.X, self.Y,""..self.Title.."", self.TextColour, self.BackgroundColour)
  217. end
  218. }
  219.  
  220. CCArea = {
  221. __index = CCControl,
  222. Type = "CCArea",
  223. New = function(self, _x, _y,_w,_h, _character, _textColour, _bgColour)
  224. local new = {} --the new instance
  225. setmetatable( new, {__index = self} )
  226. new.X = _x
  227. new.Y = _y
  228. new.W = _w
  229. new.H = _h
  230. new.Character = _character
  231. new.TextColour = _textColour
  232. new.BackgroundColour = _bgColour
  233. new.Enabled = true
  234. return new
  235. end,
  236. Draw = function(self)
  237. CCDrawing.DrawArea(self.X, self.Y, self.W, self.H, self.Character, self.TextColour, self.BackgroundColour)
  238. end
  239. }
  240.  
  241.  
  242.  
  243.  
  244. CCPrintTable = function(_name)
  245. local X = 4
  246. local Y = 4
  247. for _, v in pairs(_name) do
  248. CCDrawing.DrawCharacters(4, Y, tostring(_), colors.black,colors.lightGray)
  249. CCDrawing.DrawCharacters(15, Y, tostring(v), colors.black, colors.lightGray)
  250. Y = Y+1
  251. end
  252. end
  253.  
  254.  
  255. CCMenuSite = {
  256.  
  257. New = function(self, _SiteName)
  258. local new = {} --the new instance
  259. new.Name = _SiteName
  260.  
  261. return new
  262. end,
  263.  
  264. AddButton = function(self, _x, _y, _CBack, _CText, _String, _Func)
  265. end
  266. }
  267.  
  268.  
  269.  
  270.  
  271.  
  272. CCDebug = function()
  273. for k, l in pairs(CCInterfaceEntities.List) do
  274. print("NR: ",k," Entity: ",textutils.tabulate(l))
  275. end
  276. end
  277.  
  278. CCCustomEvent = {
  279. __index = CCControl,
  280. Type = "CCCustomEvent",
  281. Call = function(_EventName, _Param1, _Param2, _Param3)
  282. os.queueEvent(_EventName, _Param1, _Param2, _Param3)
  283. end
  284. }
  285.  
  286. function DrawButtons()
  287. for _,entity in ipairs(CCInterfaceEntities.List) do
  288. entity:Draw()
  289. end
  290. end
  291.  
  292. function DrawScreens()
  293. for _,area in ipairs(CCInterfaceArea.List) do
  294. area:Draw()
  295. end
  296. end
  297.  
  298. function EventHandler()
  299. while true do
  300. local event, arg, x, y = os.pullEventRaw()
  301. if event == "mouse_click" or "monitor_touch" or "CustomEvent_touch" then
  302. for _,entity in pairs(CCInterfaceEntities.List) do
  303. --check if the click overlaps an entities
  304. --if x >= entity.X and x <=entity.X + entity.Width - 1 and y >= entity.Y and y <=entity.Y + entity.Height then
  305. if x >= entity.X and x <= entity.X + entity.Width and y >= entity.Y and y <= entity.Y + entity.Height then
  306. print("Button clicked: Name=",_," x=",x," y=",y," Event=",event)
  307. if entity.Action then
  308. entity:Action()
  309. break
  310. end
  311. end
  312. end
  313. end
  314. end
  315. end
  316.  
  317. function init()
  318. local Background1 = CCArea:New(1,1,Screen.Width,Screen.Height," ", colors.white,colors.cyan)
  319. local Background2 = CCArea:New(1,1,Screen.Width,1," ", colors.white,colors.lightGray)
  320. local Background3 = CCArea:New(1,Screen.Height,Screen.Width,1," ", colors.white,colors.lightGray)
  321. local Background4 = CCArea:New(0,1,1,1,"Test Bildschirm", colors.black,colors.lightGray)
  322. local Background5 = CCArea:New(0,1,1,1,"Items", colors.black,colors.lightGray)
  323. CCInterfaceArea.Add(Background1)
  324. CCInterfaceArea.Add(Background3)
  325. CCInterfaceArea.Add(Background2)
  326. CCInterfaceArea.Add(Background4)
  327. local Beenden = CCButton:New(Screen.Width, 1,colors.white,colors.red,"X", function() os.reboot() end)
  328. local Items = CCButton:New(1,Screen.Height-1,colors.black,colors.lightGray,"Items ", function() CCInterfaceArea.Remove(Background4) CCInterfaceArea.Add(Background5) CCCustomEvent.Call("CustomEvent_touch", 1, 1, Screen.Height) DrawScreens() DrawButtons() end)
  329. local Options = CCButton:New(1,Screen.Height-4,colors.black,colors.lightGray,"Options ",function() CCInterfaceEntities.Load() end)
  330. local Crafting = CCButton:New(1,Screen.Height-2,colors.black,colors.lightGray,"Crafting", function() end)
  331. local Energy = CCButton:New(1,Screen.Height-3,colors.black,colors.lightGray,"Energy ",CCPrintTable(CCInterfaceEntities.List) end)
  332. local Machines = CCButton:New(1,Screen.Height-5,colors.black,colors.lightGray,"Machines", function() CCInterfaceEntities.Save() CCInterfaceEntities.Add(Options) end)
  333. local Home = CCButton:New(1,Screen.Height,colors.black,colors.lightGray,"Home", function() CCInterfaceEntities.Toggle(Items) CCInterfaceEntities.Toggle(Options) CCInterfaceEntities.Toggle(Machines) CCInterfaceEntities.Toggle(Crafting) CCInterfaceEntities.Toggle(Energy) DrawScreens() DrawButtons() end)
  334. CCInterfaceEntities.Add(Beenden)
  335. CCInterfaceEntities.Add(Home)
  336. DrawScreens()
  337. DrawButtons()
  338. --CCDrawing.DrawArea(10, 7, 20, 7, " ", colors.white, colors.red)
  339. EventHandler()
  340. end
Advertisement
Add Comment
Please, Sign In to add comment