Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Computercraft UI API by ThePH]]
- -- functions comonly used, suing targets (being a monitor or term)
- function cur(target, x, y) target.setCursorPos(x, y) end
- function color(target,fg,bg) if fg > -1 then target.setTextColor(fg) end if bg > -1 then target.setBackgroundColor(bg) end end
- function drawPixel(target, x, y, nColour) if nColour then term.setBackgroundColor( nColour ) end cur(target, x, y) v.write(' ') end
- --Function to check if item is contained in tbl and return en key or 0 if not in tbl
- function inTable(tbl, item)
- if type(item) == 'table' then
- for i, v in pairs(item) do
- for key,value in pairs(tbl) do
- if value == v then return key end
- end
- end
- elseif type(item) == 'string' then
- for key,value in pairs(tbl) do
- if value == item then return key end
- end
- end
- return 0
- end
- -- Reset the the UI list to make it fit the 'format'
- function reset(list)
- list.ui = {}
- list.shape = {}
- return list
- end
- -- Function to create and edit the lists
- -- Adding a UI element
- function addUI(uiList, keyName, x, y, text, fg, bg, action, pages, output, enabled, visible)
- if enabled == nil then enabled = true end
- if visible == nil then visible = true end
- uiList.ui[keyName] = {x, y, text, fg, bg, action, pages, output, enabled, visible}
- return uiList
- end
- -- Adding a shape (p3 is not used yet, but its here just in case)
- function addShape(uiList, keyName, shape, x, y, p1, p2, p3, color, pages, output, enabled)
- if enabled == nil then enabled = true end
- uiList.shape[keyName] = {shape, x, y, p1, p2, p3, color, pages, output, enabled}
- return uiList
- end
- -- Editing a UI element
- function editUI(uiList, key, id, value)
- local ids = {'x','y','text','fg','bg','action','pages','output', 'enabled', 'visible'}
- local index = inTable(ids, id)
- uiList.ui[key][index] = value
- return uiList
- end
- -- Draws the UI and shapes (shapes before)
- function draw(uiList, pageList, dFG, dBG)
- for index, value in pairs(uiList.shape) do
- local shape, x, y, p1, p2, p3, color, pagesIn, output, enabled = unpack(value)
- local pageOk = false
- if inTable(pagesIn, pageList) > 0 then
- pageOk = true
- end
- if pageOk and enabled then
- if shape == 'line' then
- drawLine(x, y, p1, p2, output, color)
- elseif shape == 'outline' then
- drawOutline(x, y, p1, p2, output, color)
- elseif shape == 'box' then
- drawBox(x, y, p1, p2, output, color)
- end
- end
- end
- for index, value in pairs(uiList.ui) do
- local x, y, text, fg, bg, action, pagesIn, output, enabled, visible = unpack(value)
- local pageOk = false
- if inTable(pagesIn, pageList) > 0 then
- pageOk = true
- end
- if pageOk and visible and enabled then
- for i,v in pairs(output) do
- color(v, dFG, dBG)
- cur(v, x, y)
- color(v, fg, bg)
- v.write(text)
- end
- end
- end
- end
- -- Function to draw something not used for interaction (captions etc)
- function drawSingle(x, y, text, fg, bg, output)
- for i, v in pairs(output) do
- cur(v, x, y)
- color(v, fg, bg)
- v.write(text)
- end
- end
- -- Checks mouse function (using the ui list, x, y pages and (optional, the targets))
- -- returns the action, the key in the ui list and the position on the string
- function mouse(uiList, mx, my, pageList, target)
- for index, value in pairs(uiList.ui) do
- local x, y, text, fg, bg, action, pagesIn, output, enabled, visible = unpack(value)
- pageOk = false
- if inTable(pagesIn, pageList) > 0 then pageOk = true end
- if (not(target) or inTable(output, target) > 0) and not(action == 'nil') and pageOk and enabled then
- for i, v in pairs(pagesIn) do
- if inTable(pageList, v) then
- if my == y then
- if mx >= x and mx <= (x + text:len() - 1) then
- return action, index, (mx - x + 1)
- end
- end
- break
- end
- end
- end
- end
- return nil, nil, nil
- end
- -- function the draw an empty box on target
- function drawOutline(x1,y1, x2, y2, target, c)
- drawLine(x1,y1, x1, y2, target, c)
- drawLine(x1,y2, x2, y2, target, c)
- drawLine(x2,y1, x2, y2, target, c)
- drawLine(x1,y1, x2, y1, target, c)
- end
- -- function from paintutils ported for using targets
- local function drawPixelInternal( xPos, yPos, target )
- target.setCursorPos(xPos, yPos)
- target.write(" ")
- end
- -- function to draw line using targets
- function drawLine(startX, startY, endX, endY, target, nColour)
- for i, v in pairs(target) do
- line( startX, startY, endX, endY, v, nColour )
- end
- end
- -- function to draw a filled box on target
- function drawBox(startX, startY, endX, endY, target, nColour)
- if startY <= endY then s = 1 else s= -1 end
- for index=startY, endY, s do
- drawLine( startX, index, endX, index, target, nColour )
- end
- end
- -- function from paintutils ported for using targets
- function line( startX, startY, endX, endY, v, nColour )
- if nColour then
- v.setBackgroundColor( nColour )
- end
- startX = math.floor(startX)
- startY = math.floor(startY)
- endX = math.floor(endX)
- endY = math.floor(endY)
- if startX == endX and startY == endY then
- drawPixelInternal( startX, startY, v )
- return
- end
- local minX = math.min( startX, endX )
- if minX == startX then
- minY = startY
- maxX = endX
- maxY = endY
- else
- minY = endY
- maxX = startX
- maxY = startY
- end
- local xDiff = maxX - minX
- local yDiff = maxY - minY
- if xDiff > math.abs(yDiff) then
- local y = minY
- local dy = yDiff / xDiff
- for x=minX,maxX do
- drawPixelInternal( x, math.floor( y + 0.5 ), v )
- y = y + dy
- end
- else
- local x = minX
- local dx = xDiff / yDiff
- if maxY >= minY then
- for y=minY,maxY do
- drawPixelInternal( math.floor( x + 0.5 ), y, v )
- x = x + dx
- end
- else
- for y=minY,maxY,-1 do
- drawPixelInternal( math.floor( x + 0.5 ), y, v )
- x = x - dx
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement