Here are a couple of examples of what I'm 'drawing' (you'll notice the chosen color is highlighted in it's own color and there is a 'color line' on the right that also matches the chosen color).
<images removed upon resolution to save on my upload limit>
What do I think can be better? Well, in the 2nd attempt, I'm wondering if there is a way to use fewer tables and a different parsing method to handle everything. Also, is there any way to do the 'last part' without all the if/elseif/elseif/.../end?
First attempt (brute force - no tables)
Spoiler
local function drawColorList(gRating)
local trColor
drawElement(7,4,13,8,white,gray,"") -- menu body
drawElement(7,4,1,1,white,blue,"") -- B pip
if gRating == "B" then
drawElement(20,4,1,8,white,blue,"") -- B line
trColor = blue
else
trColor = lgray
end
drawElement(9,4,4,1,trColor,gray,"Blue")
drawElement(7,5,1,1,white,lblue,"") -- L pip
if gRating == "L" then
drawElement(20,4,1,8,white,lblue,"") -- L line
trColor = lblue
else
trColor = lgray
end
drawElement(9,5,10,1,trColor,gray,"Light Blue")
drawElement(7,6,1,1,white,brown,"") -- N pip
if gRating == "N" then
drawElement(20,4,1,8,white,brown,"") -- N line
trColor = brown
else
trColor = lgray
end
drawElement(9,6,5,1,trColor,gray,"Brown")
drawElement(7,7,1,1,white,purple,"") -- P pip
if gRating == "P" then
drawElement(20,4,1,8,white,purple,"") -- P line
trColor = purple
else
trColor = lgray
end
drawElement(9,7,6,1,trColor,gray,"Purple")
drawElement(7,8,1,1,white,green,"") -- G pip
if gRating == "G" then
drawElement(20,4,1,8,white,green,"") -- G line
trColor = green
else
trColor = lgray
end
drawElement(9,8,5,1,trColor,gray,"Green")
drawElement(7,9,1,1,white,orange,"") -- O pip
if gRating == "O" then
drawElement(20,4,1,8,white,orange,"") -- O line
trColor = orange
else
trColor = lgray
end
drawElement(9,9,6,1,trColor,gray,"Orange")
drawElement(7,10,1,1,white,red,"") -- R pip
if gRating == "R" then
drawElement(20,4,1,8,white,red,"") -- R line
trColor = red
else
trColor = lgray
end
drawElement(9,10,3,1,trColor,gray,"Red")
drawElement(7,11,1,1,white,lgray,"") -- Y pip
if gRating == "Y" then
drawElement(20,4,1,8,white,lgray,"") -- Y line
trColor = white
else
trColor = lgray
end
drawElement(9,11,10,1,trColor,gray,"Light Gray")
end
2nd attempt (trying to be 'more elegant')
Spoiler
local function drawColorList(gRating)
local i,k,v
local colorSpots = { blue,
lblue,
brown,
purple,
green,
orange,
red,
lgray
}
local colorWords = { "Blue",
"Light Blue",
"Brown",
"Purple",
"Green",
"Orange",
"Red",
"Light Gray",
}
local colorBurst = { B = blue,
L = lblue,
N = brown,
P = purple,
G = green,
O = orange,
R = red,
Y = lgray,
}
drawElement(7,4,13,8,white,gray,"") -- menu body
for i = 4,11,1 do
drawElement(7,i,1,1,white,colorSpots[i-3],"") -- color pips
drawElement(9,i,1,1,lgray,gray,colorWords[i-3]) -- color words
end
for k,v in pairs(colorBurst) do
if gRating == tostring(k) then
drawElement(20,4,1,8,white,v,"") -- color line
break
end
end
if gRating == "B" then
drawElement(9,4,4,1,blue,gray,"Blue")
elseif gRating == "L" then
drawElement(9,5,10,1,lblue,gray,"Light Blue")
elseif gRating == "N" then
drawElement(9,6,5,1,brown,gray,"Brown")
elseif gRating == "P" then
drawElement(9,7,6,1,purple,gray,"Purple")
elseif gRating == "G" then
drawElement(9,8,5,1,green,gray,"Green")
elseif gRating == "O" then
drawElement(9,9,6,1,orange,gray,"Orange")
elseif gRating == "R" then
drawElement(9,10,3,1,red,gray,"Red")
elseif gRating == "Y" then
drawElement(9,11,10,1,lgray,gray,"Light Gray")
end
end
drawElement (for reference)
Spoiler
local function drawElement(x,y,w,h,txColor,bgColor,text) -- x,y = cursor pos / w,h = width, height
local deText,i = tostring(text)
term.setCursorPos(x,y)
term.setBackgroundColor(bgColor)
if w > #deText or h > 1 then -- We're 'drawing' something more than text
for i = 1,h,1 do --
term.write(string.rep(" ",w)) -- Draw the 'element' (box/rectangle/line-seg)
term.setCursorPos(x,y+i) --
end
end
if deText ~= "" and deText ~= "nil" then
term.setTextColor(txColor)
if w < #deText then w = #deText end -- Ensure minimum length
local xW = (x + math.floor(w/2)) - math.floor(#deText/2) -- Center the text horizontally
local xH = y + math.floor(h/2) -- Center the text vertically
term.setCursorPos(xW,xH)
term.write(deText)
end
end
end
I've tried consolidating the code further without luck and I'm not sure how to proceed, or even if I should bother. Any ideas?