Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ DrawingTools v0.2 ]]
- --[[ by Siasur | Mischa ]]
- local version = 0.2
- local _mt = { }
- _mt.__index = _mt
- -- [[ HELPERS ]]
- -- Clamps a number between a min and a max value
- local function --[[ Number ]] clamp(--[[ Number ]] value, --[[ Number ]] min, --[[ Number ]] max )
- if value < min then return min end
- if value > max then return max end
- return value
- end
- local function --[[ String ]] getPaintColor( --[[ Color ]] color )
- local shiftCount = 0
- while bit.band(0x1, color) ~= 0x1 do
- color = bit.brshift(color, 1)
- shiftCount = shiftCount +1
- end
- return string.format("%x", shiftCount)
- end
- local lightColorMask = 0x7e
- local darkColorMask = 0x7e00
- local function --[[ Color ]] resolveColor( --[[ Color ]] color , --[[ bool ]] enableColor )
- if not color then
- error("Can't resolve nil to color", 2)
- end
- if 1 > color or color > 0x8000 then
- error("Color is out of bounds", 3)
- end
- if not enableColor then
- if bit.band(lightColorMask, color) ~= 0 then return 0x100 end
- if bit.band(darkColorMask, color) ~= 0 then return 0x80 end
- end
- return color
- end
- -- [[ /HELPERS ]]
- -- Clears the Buffer
- function --[[ void ]] _mt.clear( --[[ Canvas ]] self )
- for x = 1, self.width, 1 do
- self.buffer[x] = self.buffer[x] or { }
- for y = 1, self.height, 1 do
- self.buffer[x][y] = self.buffer[x][y] or { }
- self.buffer[x][y].background = self.bgColor
- self.buffer[x][y].foreground = self.fgColor
- self.buffer[x][y].char = " "
- end
- end
- end
- -- Sets a new default background color
- function --[[ void ]] _mt.setBackgroundColor( --[[ Canvas ]] self, --[[ Color ]] color )
- self.bgColor = resolveColor(color, self.isColor)
- end
- -- Sets a new default foreground color
- function --[[ void ]] _mt.setForegroundColor( --[[ Canvas ]] self, --[[ Color ]] color )
- self.fgColor = resolveColor(color, self.isColor)
- end
- -- Clears the text in the given area
- function --[[ void ]] _mt.eraseText( --[[ Canvas ]] self, --[[ Number ]] posX, --[[ Number ]] posY, --[[ Number ]] width, --[[ Number ]] height )
- for x = clamp(posX, 1, self.width), clamp(width + posX - 1, 1, self.width), 1 do
- for y = clamp(posY, 1, self.height), clamp(height + posY - 1, 1, self.height), 1 do
- self.buffer[x][y].char = " "
- end
- end
- end
- -- Draws a box in the given size at the given location
- function --[[ void ]] _mt.drawBox( --[[ Canvas ]] self, --[[ Number ]] posX, --[[ Number ]] posY, --[[ Number ]] width, --[[ Number ]] height, --[[ Color ]] color)
- for x = clamp(posX, 1, self.width), clamp(width + posX - 1, 1, self.width), 1 do
- for y = clamp(posY, 1, self.height), clamp(height + posY - 1, 1, self.height), 1 do
- self.buffer[x][y].background = resolveColor(color, self.isColor)
- end
- end
- end
- -- Writes some text starting at the specific location
- -- Returns true and and empty string if successful, false and the remaining string if it was too long
- function --[[ bool, String ]] _mt.write( --[[ Canvas ]] self, --[[ Number ]] posX, --[[ Number ]] posY, --[[ String ]] text, --[[ Color? ]] color )
- if 1 > posY or posY > self.height then
- error("Can't write out of bounds! (X: " .. posX .. " Y: " .. posY .. ")", 2)
- return false, text
- end
- local remaining = string.format("%s", text)
- local step = posX
- local color = color and resolveColor(color, self.isColor) or self.fgColor
- while #remaining > 0 do
- if step > self.width then
- return false, remaining
- end
- local current = remaining:sub(1,1)
- remaining = remaining:sub(2)
- if step >= 1 then
- self.buffer[step][posY].char = current
- self.buffer[step][posY].foreground = color
- end
- step = step+1
- end
- return true, ""
- end
- -- Renders the Canvas to the screen
- function --[[ void ]] _mt.render( --[[ Canvas ]] self )
- for y = 1, self.height, 1 do
- local lineText = ""
- local lineColorFG = ""
- local lineColorBG = ""
- for x = 1, self.width, 1 do
- lineColorBG = lineColorBG .. getPaintColor(self.buffer[x][y].background)
- lineColorFG = lineColorFG .. getPaintColor(self.buffer[x][y].foreground)
- lineText = lineText .. self.buffer[x][y].char or " "
- end
- self.screen.setCursorPos(1, y)
- self.screen.blit(lineText, lineColorFG, lineColorBG)
- end
- end
- -- Creates a new canvas
- function --[[ Canvas ]] createCanvas( --[[ display ]] display )
- local isMonitor = display.seTextScale and true or false
- local isColor = display.isColor()
- local sizeX, sizeY = display.getSize()
- display.setCursorBlink(false)
- local canvas = {
- screen = display,
- height = sizeY,
- width = sizeX,
- isColor = isColor,
- bgColor = colors.black,
- fgColor = colors.white,
- buffer = {}
- }
- _mt.clear(canvas)
- return setmetatable(canvas, _mt)
- end
- -- Returns the current version number
- function --[[ Number ]] getVersion()
- return version
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement