added bigfonts api for labels
Added bigfonts to labels just use :setFontSize(number) (1,2, 3 or 4)
This commit is contained in:
4529
basalt-source.lua
Normal file
4529
basalt-source.lua
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -156,7 +156,11 @@ local function Object(name)
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
getWidth = function(self)
|
getWidth = function(self)
|
||||||
return self.w
|
return self.width
|
||||||
|
end;
|
||||||
|
|
||||||
|
getSize = function(self)
|
||||||
|
return self.width, self.height
|
||||||
end;
|
end;
|
||||||
|
|
||||||
setBackground = function(self, color)
|
setBackground = function(self, color)
|
||||||
|
|||||||
137
source/project/lib/bigfont.lua
Normal file
137
source/project/lib/bigfont.lua
Normal file
File diff suppressed because one or more lines are too long
@@ -1,13 +1,13 @@
|
|||||||
local function getTextHorizontalAlign(text, width, textAlign)
|
local function getTextHorizontalAlign(text, width, textAlign, replaceChar)
|
||||||
text = string.sub(text, 1, width)
|
text = string.sub(text, 1, width)
|
||||||
local offset = width - string.len(text)
|
local offset = width - string.len(text)
|
||||||
if (textAlign == "right") then
|
if (textAlign == "right") then
|
||||||
text = string.rep(" ", offset) .. text
|
text = string.rep(replaceChar or " ", offset) .. text
|
||||||
elseif (textAlign == "center") then
|
elseif (textAlign == "center") then
|
||||||
text = string.rep(" ", math.floor(offset / 2)) .. text .. string.rep(" ", math.floor(offset / 2))
|
text = string.rep(replaceChar or " ", math.floor(offset / 2)) .. text .. string.rep(replaceChar or " ", math.floor(offset / 2))
|
||||||
text = text .. (string.len(text) < width and " " or "")
|
text = text .. (string.len(text) < width and (replaceChar or " ") or "")
|
||||||
else
|
else
|
||||||
text = text .. string.rep(" ", offset)
|
text = text .. string.rep(replaceChar or " ", offset)
|
||||||
end
|
end
|
||||||
return text
|
return text
|
||||||
end
|
end
|
||||||
@@ -34,4 +34,126 @@ local function rpairs(t)
|
|||||||
return i, t[i]
|
return i, t[i]
|
||||||
end
|
end
|
||||||
end, t, #t + 1
|
end, t, #t + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
local function shrink(bLittleData, bgColor)
|
||||||
|
|
||||||
|
-- shrinkSystem is copy pasted (and slightly changed) from blittle by Bomb Bloke: http://www.computercraft.info/forums2/index.php?/topic/25354-cc-176-blittle-api/
|
||||||
|
local relations = { [0] = { 8, 4, 3, 6, 5 }, { 4, 14, 8, 7 }, { 6, 10, 8, 7 }, { 9, 11, 8, 0 }, { 1, 14, 8, 0 }, { 13, 12, 8, 0 }, { 2, 10, 8, 0 }, { 15, 8, 10, 11, 12, 14 },
|
||||||
|
{ 0, 7, 1, 9, 2, 13 }, { 3, 11, 8, 7 }, { 2, 6, 7, 15 }, { 9, 3, 7, 15 }, { 13, 5, 7, 15 }, { 5, 12, 8, 7 }, { 1, 4, 7, 15 }, { 7, 10, 11, 12, 14 } }
|
||||||
|
|
||||||
|
local colourNum, exponents, colourChar = {}, {}, {}
|
||||||
|
for i = 0, 15 do
|
||||||
|
exponents[2 ^ i] = i
|
||||||
|
end
|
||||||
|
do
|
||||||
|
local hex = "0123456789abcdef"
|
||||||
|
for i = 1, 16 do
|
||||||
|
colourNum[hex:sub(i, i)] = i - 1
|
||||||
|
colourNum[i - 1] = hex:sub(i, i)
|
||||||
|
colourChar[hex:sub(i, i)] = 2 ^ (i - 1)
|
||||||
|
colourChar[2 ^ (i - 1)] = hex:sub(i, i)
|
||||||
|
|
||||||
|
local thisRel = relations[i - 1]
|
||||||
|
for i = 1, #thisRel do
|
||||||
|
thisRel[i] = 2 ^ thisRel[i]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function getBestColourMatch(usage)
|
||||||
|
local lastCol = relations[exponents[usage[#usage][1]]]
|
||||||
|
if(lastCol~=nil)then
|
||||||
|
for j = 1, #lastCol do
|
||||||
|
local thisRelation = lastCol[j]
|
||||||
|
for i = 1, #usage - 1 do
|
||||||
|
if usage[i][1] == thisRelation then
|
||||||
|
return i
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return 1
|
||||||
|
end
|
||||||
|
|
||||||
|
local function colsToChar(pattern, totals)
|
||||||
|
if not totals then
|
||||||
|
local newPattern = {}
|
||||||
|
totals = {}
|
||||||
|
for i = 1, 6 do
|
||||||
|
local thisVal = pattern[i]
|
||||||
|
local thisTot = totals[thisVal]
|
||||||
|
totals[thisVal], newPattern[i] = thisTot and (thisTot + 1) or 1, thisVal
|
||||||
|
end
|
||||||
|
pattern = newPattern
|
||||||
|
end
|
||||||
|
|
||||||
|
local usage = {}
|
||||||
|
for key, value in pairs(totals) do
|
||||||
|
usage[#usage + 1] = { key, value }
|
||||||
|
end
|
||||||
|
|
||||||
|
if #usage > 1 then
|
||||||
|
-- Reduce the chunk to two colours:
|
||||||
|
while #usage > 2 do
|
||||||
|
table.sort(usage, function(a, b)
|
||||||
|
return a[2] > b[2]
|
||||||
|
end)
|
||||||
|
local matchToInd, usageLen = getBestColourMatch(usage), #usage
|
||||||
|
local matchFrom, matchTo = usage[usageLen][1], usage[matchToInd][1]
|
||||||
|
for i = 1, 6 do
|
||||||
|
if pattern[i] == matchFrom then
|
||||||
|
pattern[i] = matchTo
|
||||||
|
usage[matchToInd][2] = usage[matchToInd][2] + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
usage[usageLen] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Convert to character. Adapted from oli414's function:
|
||||||
|
-- http://www.computercraft.info/forums2/index.php?/topic/25340-cc-176-easy-drawing-characters/
|
||||||
|
local data = 128
|
||||||
|
for i = 1, #pattern - 1 do
|
||||||
|
if pattern[i] ~= pattern[6] then
|
||||||
|
data = data + 2 ^ (i - 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return string.char(data), colourChar[usage[1][1] == pattern[6] and usage[2][1] or usage[1][1]], colourChar[pattern[6]]
|
||||||
|
else
|
||||||
|
-- Solid colour character:
|
||||||
|
return "\128", colourChar[pattern[1]], colourChar[pattern[1]]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local results, width, height, bgCol = { {}, {}, {} }, 0, #bLittleData + #bLittleData % 3, bgColor or colors.black
|
||||||
|
for i = 1, #bLittleData do
|
||||||
|
if #bLittleData[i] > width then
|
||||||
|
width = #bLittleData[i]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for y = 0, height - 1, 3 do
|
||||||
|
local cRow, tRow, bRow, counter = {}, {}, {}, 1
|
||||||
|
|
||||||
|
for x = 0, width - 1, 2 do
|
||||||
|
-- Grab a 2x3 chunk:
|
||||||
|
local pattern, totals = {}, {}
|
||||||
|
|
||||||
|
for yy = 1, 3 do
|
||||||
|
for xx = 1, 2 do
|
||||||
|
pattern[#pattern + 1] = (bLittleData[y + yy] and bLittleData[y + yy][x + xx]) and (bLittleData[y + yy][x + xx] == 0 and bgCol or bLittleData[y + yy][x + xx]) or bgCol
|
||||||
|
totals[pattern[#pattern]] = totals[pattern[#pattern]] and (totals[pattern[#pattern]] + 1) or 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
cRow[counter], tRow[counter], bRow[counter] = colsToChar(pattern, totals)
|
||||||
|
counter = counter + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
results[1][#results[1] + 1], results[2][#results[2] + 1], results[3][#results[3] + 1] = table.concat(cRow), table.concat(tRow), table.concat(bRow)
|
||||||
|
end
|
||||||
|
|
||||||
|
results.width, results.height = #results[1][1], #results[1]
|
||||||
|
|
||||||
|
return results
|
||||||
end
|
end
|
||||||
@@ -4,12 +4,15 @@ local function Label(name)
|
|||||||
local objectType = "Label"
|
local objectType = "Label"
|
||||||
|
|
||||||
base:setZIndex(3)
|
base:setZIndex(3)
|
||||||
|
base.fgColor = colors.white
|
||||||
|
base.bgcolor = colors.black
|
||||||
|
|
||||||
local autoWidth = true
|
local autoWidth = true
|
||||||
base:setValue("")
|
base:setValue("")
|
||||||
|
|
||||||
local textHorizontalAlign = "left"
|
local textHorizontalAlign = "left"
|
||||||
local textVerticalAlign = "top"
|
local textVerticalAlign = "top"
|
||||||
|
local fontsize = 0
|
||||||
|
|
||||||
local object = {
|
local object = {
|
||||||
getType = function(self)
|
getType = function(self)
|
||||||
@@ -31,6 +34,17 @@ local function Label(name)
|
|||||||
return self
|
return self
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
setFontSize = function(self, size)
|
||||||
|
if(size>0)and(size<=4)then
|
||||||
|
fontsize = size-1 or 0
|
||||||
|
end
|
||||||
|
return self
|
||||||
|
end;
|
||||||
|
|
||||||
|
getFontSize = function(self)
|
||||||
|
return fontsize+1
|
||||||
|
end;
|
||||||
|
|
||||||
setSize = function(self, width, height)
|
setSize = function(self, width, height)
|
||||||
base.setSize(self, width, height)
|
base.setSize(self, width, height)
|
||||||
autoWidth = false
|
autoWidth = false
|
||||||
@@ -46,9 +60,27 @@ local function Label(name)
|
|||||||
self.parent:drawBackgroundBox(obx, oby, self.width, self.height, self.bgColor)
|
self.parent:drawBackgroundBox(obx, oby, self.width, self.height, self.bgColor)
|
||||||
self.parent:drawForegroundBox(obx, oby, self.width, self.height, self.fgColor)
|
self.parent:drawForegroundBox(obx, oby, self.width, self.height, self.fgColor)
|
||||||
self.parent:drawTextBox(obx, oby, self.width, self.height, " ")
|
self.parent:drawTextBox(obx, oby, self.width, self.height, " ")
|
||||||
for n = 1, self.height do
|
if(fontsize==0)then
|
||||||
if (n == verticalAlign) then
|
for n = 1, self.height do
|
||||||
self.parent:writeText(obx, oby + (n - 1), getTextHorizontalAlign(self:getValue(), self.width, textHorizontalAlign), self.bgColor, self.fgColor)
|
if (n == verticalAlign) then
|
||||||
|
self.parent:writeText(obx, oby + (n - 1), getTextHorizontalAlign(self:getValue(), self.width, textHorizontalAlign), self.bgColor, self.fgColor)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local tData = makeText(fontsize, self:getValue(), self.fgColor, self.bgColor)
|
||||||
|
for n = 1, self.height do
|
||||||
|
if (n == verticalAlign) then
|
||||||
|
local oX, oY = self.parent:getSize()
|
||||||
|
local cX, cY = #tData[1][1], #tData[1]
|
||||||
|
obx = obx or math.floor((oX - cX) / 2) + 1
|
||||||
|
oby = oby or math.floor((oY - cY) / 2) + 1
|
||||||
|
|
||||||
|
for i = 1, cY do
|
||||||
|
self.parent:setFG(obx, oby + i + n - 2, getTextHorizontalAlign(tData[2][i], self.width, textHorizontalAlign))
|
||||||
|
self.parent:setBG(obx, oby + i + n - 2, getTextHorizontalAlign(tData[3][i], self.width, textHorizontalAlign, tHex[self.bgColor]))
|
||||||
|
self.parent:setText(obx, oby + i + n - 2, getTextHorizontalAlign(tData[1][i], self.width, textHorizontalAlign))
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ local function Example(name) -- you can call this function how you want, doesn't
|
|||||||
|
|
||||||
-- here you could set some default values, but its not necessary, it doesn't matter if you call the functions or change the values directly, maybe i should change that
|
-- here you could set some default values, but its not necessary, it doesn't matter if you call the functions or change the values directly, maybe i should change that
|
||||||
--i guess its better if you call functions base:setBackground, base:setSize and so on.
|
--i guess its better if you call functions base:setBackground, base:setSize and so on.
|
||||||
base.width = 3
|
base.width = 12
|
||||||
base.height = 1
|
base.height = 1
|
||||||
base.bgColor = colors.lightGray
|
base.bgColor = colors.lightGray
|
||||||
base.fgColor = colors.gray
|
base.fgColor = colors.gray
|
||||||
@@ -31,8 +31,8 @@ local function Example(name) -- you can call this function how you want, doesn't
|
|||||||
if (base.draw(self)) then
|
if (base.draw(self)) then
|
||||||
if (self.parent ~= nil) then
|
if (self.parent ~= nil) then
|
||||||
local obx, oby = self:getAnchorPosition()
|
local obx, oby = self:getAnchorPosition()
|
||||||
--self.parent:setBackground(obx, oby, self.width, self.height, self.bgColor) -- changes the background color of that object
|
--self.parent:drawBackgroundbox(obx, oby, self.width, self.height, self.bgColor) -- changes the background color of that object
|
||||||
--self.parent:setForeground(obx, oby, self.width, self.height, self.fgColor) -- changes the foreground (textcolor) color of that object
|
--self.parent:drawForegroundbox(obx, oby, self.width, self.height, self.fgColor) -- changes the foreground (textcolor) color of that object
|
||||||
--self.parent:writeText(obx, oby, "Some Text", self.bgColor, self.fgColor) -- writes something on the screen, also able to change its bgcolor and fgcolor
|
--self.parent:writeText(obx, oby, "Some Text", self.bgColor, self.fgColor) -- writes something on the screen, also able to change its bgcolor and fgcolor
|
||||||
|
|
||||||
--the draw functions always gets called after something got visually changed. I am always redrawing the entire screen, but only if something has changed.
|
--the draw functions always gets called after something got visually changed. I am always redrawing the entire screen, but only if something has changed.
|
||||||
|
|||||||
Reference in New Issue
Block a user