From d47607632cf698291cc529f42bcce948f9c59e3a Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 28 Jul 2023 09:46:06 -0400 Subject: [PATCH] Added simple-graphics.lua and added documentation. --- README.md | 1 + simple-graphics.lua | 64 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 simple-graphics.lua diff --git a/README.md b/README.md index 63c05bc..2ff1bf9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # cc-simple-graphics + A simple, unopinionated graphics API for Computercraft devices. diff --git a/simple-graphics.lua b/simple-graphics.lua new file mode 100644 index 0000000..db74c09 --- /dev/null +++ b/simple-graphics.lua @@ -0,0 +1,64 @@ +--[[ +Simple Graphics: An unopinionated graphics API to make hand-made GUIs easy. +Author: Andrew Lalis +]]-- + +local lib = {} + +-- Clears a screen or terminal. If c is not nil then the background color will +-- be set prior to clearing. +function lib.clear(m, c) + if c ~= nil then m.setBackgroundColor(c) end + m.clear() +end + +-- Fills a rectangle from the given X and Y coordinates, with a width and +-- height given by w and h respectively. If c is not nil, then the background +-- color is set to c. +function lib.fillRect(m, x, y, w, h, c) + if c ~= nil then m.setBackgroundColor(c) end + for i = 1, w do + for j = 1, h do + m.setCursorPos(x+i-1, y+j-1) + m.write(" ") + end + end +end + +-- Draws a horizontal line from x1 to x2 on the given y level. +function lib.drawXLine(m, x1, x2, y, bg, fg, char) + char = char or " " + if bg ~= nil then m.setBackgroundColor(bg) end + if fg ~= nil then m.setTextColor(fg) end + for x = x1, x2 do + m.setCursorPos(x, y) + m.write(char) + end +end + +-- Draws a vertical line from y1 to y2, on the given x level. +function lib.drawYLine(m, y1, y2, x, bg, fg, char) + char = char or " " + if bg ~= nil then m.setBackgroundColor(bg) end + if fg ~= nil then m.setTextColor(fg) end + for y = y1, y2 do + m.setCursorPos(x, y) + m.write(char) + end +end + +-- Draws text starting at the given x y position. +function lib.drawText(m, x, y, text, fg, bg) + if fg ~= nil then m.setTextColor(fg) end + if bg ~= nil then m.setBackgroundColor(bg) end + m.setCursorPos(x, y) + m.write(text) +end + +-- Draws text such that the center of the text is aligned with the given +-- x and y coordinates. +function lib.drawTextCenter(m, x, y, text, fg, bg) + lib.drawText(m, x-(#text/2)+1, y, text, fg, bg) +end + +return lib \ No newline at end of file