Advertisement
Oeed

How to make a Object Oriented and Graphical Operating System

Apr 28th, 2013
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. local _w, _h = term.getSize()
  2.  
  3. CCInterfaceEntities = {
  4. List = {},
  5.  
  6. Add = function(_entity)
  7. table.insert(CCInterfaceEntities.List, _entity)
  8. end
  9. }
  10.  
  11. CCDrawing = {
  12.  
  13. Screen = {
  14. Width = _w,
  15. Height = _h
  16. },
  17.  
  18. DrawCharacters = function (_x, _y, _string, _textColour, _backgroundColour)
  19. term.setBackgroundColour(_backgroundColour)
  20. term.setTextColour(_textColour)
  21. term.setCursorPos(_x, _y)
  22. term.write(_string)
  23. end,
  24.  
  25. DrawArea = function (_x, _y, _w, _h, _character, _textColour, _bgColour)
  26. --width and height must be greater than 1, other wise we get a stack overflow
  27. if _w < 0 then
  28. _w = _w * -1
  29. elseif _w == 0 then
  30. _w = 1
  31. end
  32.  
  33. if _h < 0 then
  34. _h = _h * -1
  35. elseif _h == 0 then
  36. _h = 1
  37. end
  38.  
  39. local sRow = ""
  40. for ix = 1, _w do
  41. sRow = sRow .. _character
  42. end
  43.  
  44. for iy = 1, _h do
  45. local currY = _y + iy - 1
  46. CCDrawing.DrawCharacters(_x, currY, sRow, _textColour, _bgColour)
  47. end
  48. end
  49.  
  50. }
  51.  
  52. OSObject = {
  53. Type = "OSObject",
  54. ID = 0
  55. }
  56.  
  57. CCEntity = {
  58. __index = OSObject,
  59.  
  60. X = 0,
  61. Y = 0,
  62. Width = 0,
  63. Height = 0,
  64. Title = "",
  65. }
  66.  
  67. CCControl = {
  68. __index = CCEntity,
  69. Action = nil,
  70. Enabled = true
  71. }
  72.  
  73. CCButton = {
  74. __index = CCControl,
  75. Type = "CCButton",
  76. TextColour = colours.white,
  77. BackgroundColour = colours.grey,
  78. Height = 1,
  79. New = function(self, _x, _y, _title, _action)
  80. local new = {} -- the new instance
  81. setmetatable( new, {__index = CCButton} )
  82. new.Width = string.len(_title)+2
  83. new.X = _x
  84. new.Y = _y
  85. new.Title = _title
  86. new.Action = _action
  87. new.Enabled = true
  88. return new
  89. end,
  90. Draw = function(self)
  91. CCDrawing.DrawCharacters(self.X,self.Y, " "..self.Title.." ", self.TextColour, self.BackgroundColour)
  92. end
  93. }
  94.  
  95. function DrawScreen()
  96. for _,entity in ipairs(CCInterfaceEntities.List) do
  97. entity:Draw()
  98. end
  99. end
  100.  
  101. function EventHandler()
  102. while true do
  103. local event, arg, x, y = os.pullEventRaw()
  104.  
  105. if event == "mouse_click" then
  106. for _,entity in pairs(CCInterfaceEntities.List) do
  107. --check if the click overlaps an entities
  108. if x >= entity.X and x <=entity.X + entity.Width - 1 and y >= entity.Y and y <=entity.Y + entity.Height then
  109. if entity.Action then
  110. entity:Action()
  111. break
  112. end
  113. end
  114. end
  115. end
  116. end
  117. end
  118.  
  119. function init()
  120. CCDrawing.DrawArea(1, 1, CCDrawing.Screen.Width, CCDrawing.Screen.Height, " ", colours.white, colours.lightGrey)
  121. local ourButton = CCButton:New(19, 10, "Hello Again!", function() print("hi") end)
  122. CCInterfaceEntities.Add(ourButton)
  123. DrawScreen()
  124. EventHandler()
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement