Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local _w, _h = term.getSize()
- CCInterfaceEntities = {
- List = {},
- Add = function(_entity)
- table.insert(CCInterfaceEntities.List, _entity)
- end
- }
- CCDrawing = {
- Screen = {
- Width = _w,
- Height = _h
- },
- DrawCharacters = function (_x, _y, _string, _textColour, _backgroundColour)
- term.setBackgroundColour(_backgroundColour)
- term.setTextColour(_textColour)
- term.setCursorPos(_x, _y)
- term.write(_string)
- end,
- DrawArea = function (_x, _y, _w, _h, _character, _textColour, _bgColour)
- --width and height must be greater than 1, other wise we get a stack overflow
- if _w < 0 then
- _w = _w * -1
- elseif _w == 0 then
- _w = 1
- end
- if _h < 0 then
- _h = _h * -1
- elseif _h == 0 then
- _h = 1
- end
- local sRow = ""
- for ix = 1, _w do
- sRow = sRow .. _character
- end
- for iy = 1, _h do
- local currY = _y + iy - 1
- CCDrawing.DrawCharacters(_x, currY, sRow, _textColour, _bgColour)
- end
- end
- }
- OSObject = {
- Type = "OSObject",
- ID = 0
- }
- CCEntity = {
- __index = OSObject,
- X = 0,
- Y = 0,
- Width = 0,
- Height = 0,
- Title = "",
- }
- CCControl = {
- __index = CCEntity,
- Action = nil,
- Enabled = true
- }
- CCButton = {
- __index = CCControl,
- Type = "CCButton",
- TextColour = colours.white,
- BackgroundColour = colours.grey,
- Height = 1,
- New = function(self, _x, _y, _title, _action)
- local new = {} -- the new instance
- setmetatable( new, {__index = CCButton} )
- new.Width = string.len(_title)+2
- new.X = _x
- new.Y = _y
- new.Title = _title
- new.Action = _action
- new.Enabled = true
- return new
- end,
- Draw = function(self)
- CCDrawing.DrawCharacters(self.X,self.Y, " "..self.Title.." ", self.TextColour, self.BackgroundColour)
- end
- }
- function DrawScreen()
- for _,entity in ipairs(CCInterfaceEntities.List) do
- entity:Draw()
- end
- end
- function EventHandler()
- while true do
- local event, arg, x, y = os.pullEventRaw()
- if event == "mouse_click" then
- for _,entity in pairs(CCInterfaceEntities.List) do
- --check if the click overlaps an entities
- if x >= entity.X and x <=entity.X + entity.Width - 1 and y >= entity.Y and y <=entity.Y + entity.Height then
- if entity.Action then
- entity:Action()
- break
- end
- end
- end
- end
- end
- end
- function init()
- CCDrawing.DrawArea(1, 1, CCDrawing.Screen.Width, CCDrawing.Screen.Height, " ", colours.white, colours.lightGrey)
- local ourButton = CCButton:New(19, 10, "Hello Again!", function() print("hi") end)
- CCInterfaceEntities.Add(ourButton)
- DrawScreen()
- EventHandler()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement