From 3f6060cbae16afa16d6855a91086a369a2c53f69 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 28 Jul 2023 10:07:14 -0400 Subject: [PATCH] Added install.lua and drawButton --- README.md | 8 +++++++- install.lua | 15 +++++++++++++++ simple-graphics.lua | 26 ++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 install.lua diff --git a/README.md b/README.md index 2ff1bf9..4408e2c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # cc-simple-graphics -A simple, unopinionated graphics API for Computercraft devices. +A simple, unopinionated graphics API for Computercraft devices. See the source +code for documentation on each function. + +Installation is simple: +```shell +wget run https://raw.githubusercontent.com/andrewlalis/cc-simple-graphics/main/install.lua +``` \ No newline at end of file diff --git a/install.lua b/install.lua new file mode 100644 index 0000000..7025730 --- /dev/null +++ b/install.lua @@ -0,0 +1,15 @@ +-- Quick installation script for simple-graphics. +if fs.exists("simple-graphics.lua") then + print("simple-graphics.lua already exists. Overwrite it? (y/n)") + local response = io.read() + if response == "y" or response == "Y" then + fs.delete("simple-graphics.lua") + print("Removed simple-graphics.lua.") + else + print("Quitting the installation.") + return + end +end + +shell.execute("wget", "https://raw.githubusercontent.com/andrewlalis/cc-simple-graphics/main/simple-graphics.lua", "simple-graphics.lua") +print("Installed simple-graphics.lua.") diff --git a/simple-graphics.lua b/simple-graphics.lua index db74c09..7a1d472 100644 --- a/simple-graphics.lua +++ b/simple-graphics.lua @@ -25,6 +25,17 @@ function lib.fillRect(m, x, y, w, h, c) end end +-- Draws the outline of 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 +-- color of the border is set to c. +function lib.drawRect(m, x, y, w, h, c) + if c ~= nil then m.setBackgroundColor(c) end + lib.drawXLine(x, x+w-1, y, c) + lib.drawXLine(x, x+w-1, y+h-1, c) + lib.drawYLine(y+1, y+h-2, x, c) + lib.drawYLine(y+1, y+h-2, x+w-1, c) +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 " " @@ -61,4 +72,19 @@ function lib.drawTextCenter(m, x, y, text, fg, bg) lib.drawText(m, x-(#text/2)+1, y, text, fg, bg) end +-- Draws a button as a plain rectangle with the given background color, and +-- centered text drawn with the foreground color. Returns a table containing +-- x1, y1, x2, y2 coordinates for handling the button's event later. +function lib.drawButton(m, x, y, w, h, text, fg, bg) + local x1 = x + local y1 = y + local x2 = x+w-1 + local y2 = y+h-1 + local midX = x+(w/2) + local midY = y+(h/2) + lib.fillRect(m, x, y, w, h, bg) + lib.drawText(m, midX, midY, text, fg) + return {x1=x1,y1=y1,x2=x2,y2=y2} +end + return lib \ No newline at end of file