Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --2D Graphics API for Computercraft Computers
- --Author: Andrew Lalis, andrewlalisofficial@gmail.com
- --Current peripheral to draw to
- local m
- --Determines if the peripheral is a monitor or computer screen.
- local isMonitor
- local buttonLoopActive = false
- --Holds all registered buttons.
- local buttons = {}
- --Clears the screen.
- function clear(bc)
- m.setBackgroundColor(bc)
- m.clear()
- end
- --Sets the current peripheral.
- function setTarget(target,isMon)
- m = target
- isMonitor = isMon
- end
- --Wraps text
- function wrap(str, limit)
- limit = limit or 72
- local here = 1
- local buf = ""
- local t = {}
- str:gsub("(%s*)()(%S+)()",
- function(sp, st, word, fi)
- if fi-here > limit then
- --# Break the line
- here = st
- table.insert(t, buf)
- buf = word
- else
- buf = buf..sp..word --# Append
- end
- end)
- --# Tack on any leftovers
- if(buf ~= "") then
- table.insert(t, buf)
- end
- return t
- end
- --Fills a box with a color
- function fillBox(x,y,width,height,bc)
- m.setBackgroundColor(bc)
- for i=x,width+x-1 do
- for k=y,height+y-1 do
- m.setCursorPos(i,k)
- m.write(" ")
- end
- end
- end
- --Draws a horizontal line.
- function drawLineH(x1,width,y,bc)
- m.setBackgroundColor(bc)
- for x=x1,width+x1-1 do
- m.setCursorPos(x,y)
- m.write(" ")
- end
- end
- --Draws a vertical line.
- function drawLineV(x,y1,height,bc)
- m.setBackgroundColor(bc)
- for y=y1,height+y1-1 do
- m.setCursorPos(x,y)
- m.write(" ")
- end
- end
- --Draws border box.
- function drawBorder(x1,y1,width,height,bc,fillColor)
- fillBox(x1+1,y1+1,width-2,height-2,fillColor)
- drawLineH(x1,width,y1,bc)
- drawLineH(x1,width,y1+height-1,bc)
- drawLineV(x1,y1,height,bc)
- drawLineV(x1+width-1,y1,height,bc)
- end
- --Creates a text box with a title and border.
- function drawTextBox(title,title_tc,title_bc,body,body_tc,body_bc,x,y,width,height)
- if (height == nil) then
- height = #(wrap(body,width)) + 2
- end
- drawBorder(x,y,width,height,title_bc,body_bc)
- writeText(title,x+1,y,title_tc,title_bc)
- writeTextWrapped(body,x+1,y+1,width-2,body_tc,body_bc)
- end
- --Writes a block of text wrapped in an area.
- function writeTextWrapped(text,x,y,width,tc,bc)
- local wrapped = wrap(text, width)
- fillBox(x,y,width,#wrapped,bc)
- m.setTextColor(tc)
- for i=1,#wrapped do
- m.setCursorPos(x,y+i-1)
- m.write(wrapped[i])
- end
- end
- --Writes text starting at given coordinates.
- function writeText(text,x,y,tc,bc)
- m.setCursorPos(x,y)
- m.setTextColor(tc)
- m.setBackgroundColor(bc)
- m.write(text)
- end
- --Writes text centered at a given point.
- function writeTextCentered(text,x,y,tc,bc)
- m.setCursorPos(x-((#text)/2),y)
- m.setTextColor(tc)
- m.setBackgroundColor(bc)
- m.write(text)
- end
- --Gets the horizontal and vertical size of the drawing area.
- function getSize()
- return m.getSize()
- end
- --Creates a button with a name, location, text, and function.
- function createButton(name,x,y,width,height,tc,bc,bcPressed,func)
- buttons[name] = {name=name,x=x,y=y,width=width,height=height,tc=tc,bc=bc,bcPressed=bcPressed,func=func}
- end
- --Removes a button with a specific name.
- function removeButton(name)
- table.remove(buttons, name)
- end
- --Draws a button on the target screen.
- local function drawButton(bTable)
- local effectiveBc = bTable.bc
- if (bTable.timer ~= nil) then
- effectiveBc = bTable.bcPressed
- end
- fillBox(bTable.x,bTable.y,bTable.width,bTable.height,effectiveBc)
- writeTextCentered(bTable.name,bTable.x + bTable.width/2,bTable.y + bTable.height/2,bTable.tc,effectiveBc)
- end
- --Checks if a coordinate is within a button's x and y values.
- local function buttonContains(bTable,x,y)
- return (x >= bTable.x and x < bTable.x+bTable.width and y >= bTable.y and y < bTable.y+bTable.height)
- end
- --Draws all buttons onto the screen.
- function drawButtons()
- for key,button in pairs(buttons) do
- drawButton(button)
- end
- end
- --What to do when the user touches the monitor.
- local function onMonitorTouch(side,x,y)
- for key,button in pairs(buttons) do
- if (buttonContains(button,x,y)) then
- button.timer = os.startTimer(0.25)
- drawButton(button)
- button.func(side,x,y)
- end
- end
- end
- --What to do when a timer is up. Set the timer to nil and redraw.
- local function onTimerEvent(tId)
- for key,button in pairs(buttons) do
- if (button.timer ~= nil and button.timer == tId) then
- button.timer = nil
- drawButton(button)
- end
- end
- end
- --Begins an event handler for monitor and terminal touch events, and executes any buttons the user clicks on.
- function buttonLoop()
- buttonLoopActive = true
- drawButtons()
- while (buttonLoopActive) do
- local event,p1,p2,p3,p4,p5 = os.pullEvent()
- if (isMonitor and event == "monitor_touch") then
- onMonitorTouch(p1,p2,p3)
- elseif (event == "mouse_click") then
- --Do the same thing for computer screens. The user changes the behaviour.
- onMonitorTouch(p1,p2,p3)
- elseif (event == "timer") then
- onTimerEvent(p1)
- end
- end
- end
- --Ends the button loop.
- function endButtonLoop()
- buttonLoopActive = false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement