Graphic Object update
Reworked the graphic object The goal is to create a object people can use for drawing stuff while the program is running. It has to be done programatically - Added bimg lib (simple one) to make it easier for me to work with bimg - Small bugfix when using mouseclick/mouseup events
This commit is contained in:
@@ -565,6 +565,8 @@ return function(name)
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
activeEvents["mouse_click"] = true
|
||||
self.parent:addEvent("mouse_up", self)
|
||||
activeEvents["mouse_up"] = true
|
||||
end
|
||||
return self
|
||||
end;
|
||||
@@ -576,6 +578,8 @@ return function(name)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
activeEvents["mouse_click"] = true
|
||||
self.parent:addEvent("mouse_up", self)
|
||||
activeEvents["mouse_up"] = true
|
||||
end
|
||||
@@ -749,7 +753,7 @@ return function(name)
|
||||
isCoordsInObject = function(self, x, y)
|
||||
if(isVisible)and(isEnabled)then
|
||||
if(x==nil)or(y==nil)then return false end
|
||||
local objX, objY = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local w, h = self:getSize()
|
||||
if (objX <= x) and (objX + w > x) and (objY <= y) and (objY + h > y) then
|
||||
return true
|
||||
@@ -760,7 +764,8 @@ return function(name)
|
||||
|
||||
mouseHandler = function(self, button, x, y, isMon)
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
local val = eventSystem:sendEvent("mouse_click", self, "mouse_click", button, x, y, isMon)
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local val = eventSystem:sendEvent("mouse_click", self, "mouse_click", button, x - (objX-1), y - (objY-1), isMon)
|
||||
if(val==false)then return false end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:setFocusedObject(self)
|
||||
@@ -775,7 +780,8 @@ return function(name)
|
||||
mouseUpHandler = function(self, button, x, y)
|
||||
isDragging = false
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
local val = eventSystem:sendEvent("mouse_up", self, "mouse_up", button, x, y)
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local val = eventSystem:sendEvent("mouse_up", self, "mouse_up", button, x - (objX-1), y - (objY-1))
|
||||
if(val==false)then return false end
|
||||
return true
|
||||
end
|
||||
@@ -784,9 +790,8 @@ return function(name)
|
||||
|
||||
dragHandler = function(self, button, x, y)
|
||||
if(isDragging)then
|
||||
local objX, objY = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
local dX, dY = x - objX + 1, y - objY + 1
|
||||
local val = eventSystem:sendEvent("mouse_drag", self, "mouse_drag", button, dX, dY, dragStartX-x, dragStartY-y, x, y)
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local val = eventSystem:sendEvent("mouse_drag", self, "mouse_drag", button, x - (objX-1), y - (objY-1), dragStartX-x, dragStartY-y, x, y)
|
||||
dragStartX, dragStartY = x, y
|
||||
if(val~=nil)then return val end
|
||||
if(self.parent~=nil)then
|
||||
@@ -805,7 +810,8 @@ return function(name)
|
||||
|
||||
scrollHandler = function(self, dir, x, y)
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
local val = eventSystem:sendEvent("mouse_scroll", self, "mouse_scroll", dir, x, y)
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local val = eventSystem:sendEvent("mouse_scroll", self, "mouse_scroll", dir, x - (objX-1), y - (objY-1))
|
||||
if(val==false)then return false end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:setFocusedObject(self)
|
||||
|
||||
113
Basalt/libraries/bimgLibrary.lua
Normal file
113
Basalt/libraries/bimgLibrary.lua
Normal file
@@ -0,0 +1,113 @@
|
||||
local sub,rep = string.sub,string.rep
|
||||
|
||||
return function()
|
||||
local w, h = 0,0
|
||||
local t,fg,bg = {}, {}, {}
|
||||
local x, y = 1,1
|
||||
|
||||
local function recalculateSize()
|
||||
for y=1,h do
|
||||
if(t[y]==nil)then
|
||||
t[y] = rep(" ", w)
|
||||
else
|
||||
t[y] = t[y]..rep(" ", w-#t[y])
|
||||
end
|
||||
if(fg[y]==nil)then
|
||||
fg[y] = rep("0", w)
|
||||
else
|
||||
fg[y] = fg[y]..rep("0", w-#fg[y])
|
||||
end
|
||||
if(bg[y]==nil)then
|
||||
bg[y] = rep("f", w)
|
||||
else
|
||||
bg[y] = bg[y]..rep("f", w-#bg[y])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local addText = function(text, _x, _y)
|
||||
x = _x or x
|
||||
y = _y or y
|
||||
if(t[y]==nil)then
|
||||
t[y] = rep(" ", x-1)..text..rep(" ", w-(#text+x))
|
||||
else
|
||||
t[y] = sub(t[y], 1, x-1)..rep(" ", x-#t[y])..text..sub(t[y], x+#text, w)
|
||||
end
|
||||
if(#t[y]>w)then w = #t[y] end
|
||||
if(y > h)then h = y end
|
||||
recalculateSize()
|
||||
end
|
||||
|
||||
local addBg = function(b, _x, _y)
|
||||
x = _x or x
|
||||
y = _y or y
|
||||
if(bg[y]==nil)then
|
||||
bg[y] = rep("f", x-1)..b..rep("f", w-(#b+x))
|
||||
else
|
||||
bg[y] = sub(bg[y], 1, x-1)..rep("f", x-#bg[y])..b..sub(bg[y], x+#b, w)
|
||||
end
|
||||
if(#bg[y]>w)then w = #bg[y] end
|
||||
if(y > h)then h = y end
|
||||
recalculateSize()
|
||||
end
|
||||
|
||||
local addFg = function(f, _x, _y)
|
||||
x = _x or x
|
||||
y = _y or y
|
||||
if(fg[y]==nil)then
|
||||
fg[y] = rep("0", x-1)..f..rep("0", w-(#f+x))
|
||||
else
|
||||
fg[y] = sub(fg[y], 1, x-1)..rep("0", x-#fg[y])..f..sub(fg[y], x+#f, w)
|
||||
end
|
||||
if(#fg[y]>w)then w = #fg[y] end
|
||||
if(y > h)then h = y end
|
||||
recalculateSize()
|
||||
end
|
||||
|
||||
local public = {
|
||||
blit = function(text, fgCol, bgCol, x, y)
|
||||
addText(text, x, y)
|
||||
addFg(fgCol, x, y)
|
||||
addBg(bgCol, x, y)
|
||||
end,
|
||||
text = addText,
|
||||
fg = addFg,
|
||||
bg = addBg,
|
||||
|
||||
getSize = function()
|
||||
return w, h
|
||||
end,
|
||||
|
||||
setSize = function(_w, _h)
|
||||
local nt,nfg,nbg = {}, {}, {}
|
||||
for _y=1,_h do
|
||||
if(t[_y]~=nil)then
|
||||
nt[_y] = sub(t[_y], 1, _w)..rep(" ", _w - w)
|
||||
else
|
||||
nt[_y] = rep(" ", _w)
|
||||
end
|
||||
if(fg[_y]~=nil)then
|
||||
nfg[_y] = sub(fg[_y], 1, _w)..rep("0", _w - w)
|
||||
else
|
||||
nfg[_y] = rep("0", _w)
|
||||
end
|
||||
if(bg[_y]~=nil)then
|
||||
nbg[_y] = sub(bg[_y], 1, _w)..rep("f", _w - w)
|
||||
else
|
||||
nbg[_y] = rep("f", _w)
|
||||
end
|
||||
end
|
||||
t, fg, bg = nt, nfg, nbg
|
||||
w, h = _w, _h
|
||||
end,
|
||||
|
||||
getBimgData = function()
|
||||
local data = {}
|
||||
for k,v in pairs(t)do
|
||||
data[k] = {t[k], fg[k], bg[k]}
|
||||
end
|
||||
return {[1]=data,creator="Basalt Graphic 1.0",version="1.0.0"}
|
||||
end
|
||||
}
|
||||
return public
|
||||
end
|
||||
@@ -29,6 +29,39 @@ local function stop()
|
||||
baseTerm.setCursorPos(1, 1)
|
||||
end
|
||||
|
||||
local basaltError = function(errMsg)
|
||||
baseTerm.clear()
|
||||
baseTerm.setBackgroundColor(colors.black)
|
||||
baseTerm.setTextColor(colors.red)
|
||||
local w,h = baseTerm.getSize()
|
||||
if(basalt.logging)then
|
||||
log(errMsg, "Error")
|
||||
end
|
||||
|
||||
local text = createText("Basalt error: "..errMsg, w)
|
||||
local yPos = 1
|
||||
for k,v in pairs(text)do
|
||||
baseTerm.setCursorPos(1,yPos)
|
||||
baseTerm.write(v)
|
||||
yPos = yPos + 1
|
||||
end
|
||||
baseTerm.setCursorPos(1,yPos+1)
|
||||
updaterActive = false
|
||||
end
|
||||
|
||||
local function schedule(f)
|
||||
assert(f~="function", "Schedule needs a function in order to work!")
|
||||
return function(...)
|
||||
local co = coroutine.create(f)
|
||||
local ok, result = coroutine.resume(co, ...)
|
||||
if(ok)then
|
||||
table.insert(schedules, co)
|
||||
else
|
||||
basaltError(result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local setVariable = function(name, var)
|
||||
variables[name] = var
|
||||
end
|
||||
@@ -94,6 +127,8 @@ local bInstance = {
|
||||
return baseTerm
|
||||
end,
|
||||
|
||||
schedule = schedule,
|
||||
|
||||
stop = stop,
|
||||
newFrame = Frame,
|
||||
|
||||
@@ -102,26 +137,6 @@ local bInstance = {
|
||||
end
|
||||
}
|
||||
|
||||
local basaltError = function(errMsg)
|
||||
baseTerm.clear()
|
||||
baseTerm.setBackgroundColor(colors.black)
|
||||
baseTerm.setTextColor(colors.red)
|
||||
local w,h = baseTerm.getSize()
|
||||
if(basalt.logging)then
|
||||
log(errMsg, "Error")
|
||||
end
|
||||
|
||||
local text = createText("Basalt error: "..errMsg, w)
|
||||
local yPos = 1
|
||||
for k,v in pairs(text)do
|
||||
baseTerm.setCursorPos(1,yPos)
|
||||
baseTerm.write(v)
|
||||
yPos = yPos + 1
|
||||
end
|
||||
baseTerm.setCursorPos(1,yPos+1)
|
||||
updaterActive = false
|
||||
end
|
||||
|
||||
local function handleSchedules(event, p1, p2, p3, p4)
|
||||
if(#schedules>0)then
|
||||
local finished = {}
|
||||
@@ -175,20 +190,24 @@ local function moveHandlerTimer()
|
||||
end
|
||||
|
||||
local btn, dragX, dragY = nil, nil, nil
|
||||
local dragTimer = nil
|
||||
local function mouseDragEvent(b, x, y)
|
||||
btn, dragX, dragY = b, x, y
|
||||
if(dragTimer==nil)then
|
||||
dragTimer = os.startTimer(dragThrottle/1000)
|
||||
end
|
||||
end
|
||||
|
||||
local function dragHandlerTimer()
|
||||
dragTimer = nil
|
||||
mainFrame:dragHandler(btn, dragX, dragY)
|
||||
activeFrame = mainFrame
|
||||
end
|
||||
|
||||
local dragTimer = nil
|
||||
local function mouseDragEvent(b, x, y)
|
||||
btn, dragX, dragY = b, x, y
|
||||
if(dragThrottle<50)then
|
||||
dragHandlerTimer()
|
||||
else
|
||||
if(dragTimer==nil)then
|
||||
dragTimer = os.startTimer(dragThrottle/1000)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function basaltUpdateEvent(event, p1, p2, p3, p4)
|
||||
if(basaltEvent:sendEvent("basaltEventCycle", event, p1, p2, p3, p4)==false)then return end
|
||||
if(mainFrame~=nil)then
|
||||
@@ -296,8 +315,8 @@ basalt = {
|
||||
end,
|
||||
|
||||
setMouseDragThrottle = function(amount)
|
||||
if(amount<50)then
|
||||
dragThrottle = 50
|
||||
if(amount<0)then
|
||||
dragThrottle = 0
|
||||
else
|
||||
dragThrottle = amount
|
||||
end
|
||||
@@ -365,18 +384,7 @@ basalt = {
|
||||
end
|
||||
end,
|
||||
|
||||
schedule = function(f)
|
||||
assert(f~="function", "Schedule needs a function in order to work!")
|
||||
return function(...)
|
||||
local co = coroutine.create(f)
|
||||
local ok, result = coroutine.resume(co, ...)
|
||||
if(ok)then
|
||||
table.insert(schedules, co)
|
||||
else
|
||||
basaltError(result)
|
||||
end
|
||||
end
|
||||
end,
|
||||
schedule = schedule,
|
||||
|
||||
createFrame = function(name)
|
||||
name = name or uuid()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
local Object = require("Object")
|
||||
local geometric = require("geometricPoints")
|
||||
local tHex = require("tHex")
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
local bimgLib = require("bimgLibrary")
|
||||
|
||||
local sub,len,max,min = string.sub,string.len,math.max,math.min
|
||||
|
||||
@@ -9,420 +9,95 @@ return function(name)
|
||||
-- Graphic
|
||||
local base = Object(name)
|
||||
local objectType = "Graphic"
|
||||
base:setZIndex(2)
|
||||
local imgData = bimgLib()
|
||||
local bimg
|
||||
base:setZIndex(5)
|
||||
|
||||
local graphicObjects = {}
|
||||
local graphic = {}
|
||||
local shrinkedGraphic = {}
|
||||
local isGraphicShrinked = false
|
||||
local xOffset, yOffset = 0, 0
|
||||
local dragable = false
|
||||
local xMouse,yMouse
|
||||
local w, h = 40, 15
|
||||
local canvasSizeChanged = false
|
||||
|
||||
local tColourLookup = {}
|
||||
for n=1,16 do
|
||||
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
|
||||
end
|
||||
|
||||
local function stringToTable(str)
|
||||
local t = {}
|
||||
for i = 1, #str do
|
||||
t[i] = str:sub(i, i)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
local function setBG(x, y, width, height, colorStr)
|
||||
if (y >= 1) and (y <= height) then
|
||||
if (x + len(colorStr) > 0) and (x <= width) then
|
||||
local oldCache = graphic[y]
|
||||
local newCache
|
||||
local nEnd = x + #colorStr - 1
|
||||
|
||||
if (x < 1) then
|
||||
colorStr = sub(colorStr, 1 - x + 1, width - x + 1)
|
||||
elseif (nEnd > width) then
|
||||
colorStr = sub(colorStr, 1, width - x + 1)
|
||||
end
|
||||
|
||||
if (x > 1) then
|
||||
newCache = sub(oldCache, 1, x - 1) .. colorStr
|
||||
else
|
||||
newCache = colorStr
|
||||
end
|
||||
if nEnd < width then
|
||||
newCache = newCache .. sub(oldCache, nEnd + 1, width)
|
||||
end
|
||||
graphic[y] = newCache
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function redrawCanvasSize()
|
||||
local w,h = w,h
|
||||
if(isGraphicShrinked)then w = w*2 h = h*3 end
|
||||
for y=1,h do
|
||||
if(graphic[y]~=nil)then
|
||||
if(w>graphic[y]:len())then
|
||||
graphic[y] = graphic[y]..(tHex[base.bgColor]):rep(w-graphic[y]:len())
|
||||
else
|
||||
graphic[y] = graphic[y]:sub(1,w)
|
||||
end
|
||||
else
|
||||
graphic[y] = (tHex[base.bgColor]):rep(w)
|
||||
end
|
||||
end
|
||||
end
|
||||
redrawCanvasSize()
|
||||
|
||||
|
||||
local function shrink()
|
||||
local function parseLine( tImageArg, sLine )
|
||||
local tLine = {}
|
||||
for x=1,sLine:len() do
|
||||
tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
|
||||
end
|
||||
table.insert( tImageArg, tLine )
|
||||
end
|
||||
function parseImage( sRawData )
|
||||
if type( sRawData ) ~= "string" then
|
||||
error( "bad argument #1 (expected string, got " .. type( sRawData ) .. ")" )
|
||||
end
|
||||
local tImage = {}
|
||||
for sLine in ( sRawData .. "\n" ):gmatch( "(.-)\n" ) do
|
||||
parseLine( tImage, sLine )
|
||||
end
|
||||
return tImage
|
||||
end
|
||||
|
||||
local rawImg = ""
|
||||
for y=1,#graphic do
|
||||
if(y==#graphic)then
|
||||
rawImg = rawImg..graphic[y]
|
||||
else
|
||||
rawImg = rawImg..graphic[y].."\n"
|
||||
end
|
||||
end
|
||||
local img = parseImage(rawImg)
|
||||
-- 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]]]
|
||||
|
||||
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
|
||||
|
||||
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, #img + #img % 3, base.bgColor or colors.black
|
||||
for i = 1, #img do
|
||||
if #img[i] > width then
|
||||
width = #img[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] = (img[y + yy] and img[y + yy][x + xx]) and (img[y + yy][x + xx] == 0 and bgCol or img[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]
|
||||
|
||||
shrinkedGraphic = results
|
||||
end
|
||||
|
||||
local function redraw()
|
||||
local w,h = w,h
|
||||
if(isGraphicShrinked)then w = w*2 h = h*3 end
|
||||
for k,v in pairs(graphicObjects)do
|
||||
for a,b in pairs(v[1])do
|
||||
setBG(b.x, b.y, w, h, v[2])
|
||||
end
|
||||
end
|
||||
if(isGraphicShrinked)then
|
||||
shrink()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local object = {
|
||||
init = function(self)
|
||||
self.bgColor = self.parent:getTheme("GraphicBG")
|
||||
end,
|
||||
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
|
||||
setSize = function(self, width, height, rel)
|
||||
base.setSize(self, width, height, rel)
|
||||
if not(canvasSizeChanged)then
|
||||
w = width
|
||||
h = height
|
||||
redrawCanvasSize()
|
||||
setOffset = function(self, _x, _y, rel)
|
||||
if(rel)then
|
||||
xOffset = xOffset + _x or 0
|
||||
yOffset = yOffset + _y or 0
|
||||
else
|
||||
xOffset = _x or xOffset
|
||||
yOffset = _y or yOffset
|
||||
end
|
||||
redraw()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setOffset = function(self, x, y)
|
||||
xOffset = x or xOffset
|
||||
yOffset = y or yOffset
|
||||
return self
|
||||
end,
|
||||
|
||||
setCanvasSize = function(self, width, height)
|
||||
w,h = width,height
|
||||
canvasSizeChanged = true
|
||||
redrawCanvasSize()
|
||||
return self
|
||||
end,
|
||||
|
||||
clearCanvas = function(self)
|
||||
graphicObjects = {}
|
||||
graphic = {}
|
||||
redrawCanvasSize()
|
||||
end,
|
||||
|
||||
getOffset = function(self)
|
||||
return xOffset,yOffset
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("text", data)~=nil)then self:setText(xmlValue("text", data)) end
|
||||
if(xmlValue("xOffset", data)~=nil)then self:setOffset(xmlValue("xOffset", data), yOffset) end
|
||||
if(xmlValue("yOffset", data)~=nil)then self:setOffset(xOffset, xmlValue("yOffset", data)) end
|
||||
if(xmlValue("wCanvas", data)~=nil)then w = xmlValue("wCanvas", data) end
|
||||
if(xmlValue("hCanvas", data)~=nil)then h = xmlValue("hCanvas", data) end
|
||||
if(xmlValue("shrink", data)~=nil)then if(xmlValue("shrink", data))then self:shrink() end end
|
||||
if(xmlValue("dragable", data)~=nil)then if(xmlValue("dragable", data))then dragable = true end end
|
||||
if(data["ellipse"]~=nil)then
|
||||
local tab = data["ellipse"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
local col = colors[xmlValue("color", v)]
|
||||
local rad1 = xmlValue("radius", v)
|
||||
local rad2 = xmlValue("radius2", v)
|
||||
local x = xmlValue("x", v)
|
||||
local y = xmlValue("y", v)
|
||||
local filled = xmlValue("filled", v)
|
||||
self:addEllipse(col, rad1, rad2, x, y, filled)
|
||||
end
|
||||
end
|
||||
if(data["circle"]~=nil)then
|
||||
local tab = data["circle"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
local col = colors[xmlValue("color", v)]
|
||||
local rad = tonumber(xmlValue("radius", v))
|
||||
local x = tonumber(xmlValue("x", v))
|
||||
local y = tonumber(xmlValue("y", v))
|
||||
local filled = xmlValue("filled", v)
|
||||
self:addCircle(col, rad, x, y, filled)
|
||||
end
|
||||
end
|
||||
if(data["line"]~=nil)then
|
||||
local tab = data["line"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
local col = colors[xmlValue("color", v)]
|
||||
local x = tonumber(xmlValue("x", v))
|
||||
local x2 = tonumber(xmlValue("x2", v))
|
||||
local y = tonumber(xmlValue("y", v))
|
||||
local y2 = tonumber(xmlValue("y2", v))
|
||||
self:addLine(col, x, y, x2, y2)
|
||||
end
|
||||
end
|
||||
|
||||
if(data["rectangle"]~=nil)then
|
||||
local tab = data["rectangle"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
local col = colors[xmlValue("color", v)]
|
||||
local x = tonumber(xmlValue("x", v))
|
||||
local x2 = tonumber(xmlValue("x2", v))
|
||||
local y = tonumber(xmlValue("y", v))
|
||||
local y2 = tonumber(xmlValue("y2", v))
|
||||
local filled = xmlValue("filled", v)=="true" and true or false
|
||||
self:addRectangle(col, x, y, x2, y2, filled)
|
||||
end
|
||||
end
|
||||
if(data["triangle"]~=nil)then
|
||||
local tab = data["triangle"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
local col = colors[xmlValue("color", v)]
|
||||
local x = tonumber(xmlValue("x", v))
|
||||
local x2 = tonumber(xmlValue("x2", v))
|
||||
local x3 = tonumber(xmlValue("x2", v))
|
||||
local y = tonumber(xmlValue("y", v))
|
||||
local y2 = tonumber(xmlValue("y2", v))
|
||||
local y3 = tonumber(xmlValue("y3", v))
|
||||
local filled = xmlValue("filled", v)
|
||||
self:addTriangle(col, x, y, x2, y2, x3, y3, filled)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return self
|
||||
end,
|
||||
|
||||
addCircle = function(self, color, rad, x, y, filled)
|
||||
local col = tHex[color]
|
||||
table.insert(graphicObjects, {geometric.circle(x or 1, y or 1, rad, filled), tHex[color]})
|
||||
redraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
addEllipse = function(self, color, rad, rad2, x, y, filled)
|
||||
table.insert(graphicObjects, {geometric.ellipse(x or 1, y or 1, rad, rad2, filled), tHex[color]})
|
||||
redraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
addLine = function(self, color, x1, y1, x2, y2)
|
||||
table.insert(graphicObjects, {geometric.line(x1 or 1, y1 or 1, x2 or 1, y2 or 1), tHex[color]})
|
||||
redraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
addTriangle = function(self, color, x1, y1, x2, y2, x3, y3, filled)
|
||||
table.insert(graphicObjects, {geometric.triangle(x1 or 1, y1 or 1, x2 or 1, y2 or 1, x3 or 1, y3 or 1, filled), tHex[color]})
|
||||
redraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
addRectangle = function(self, color, x1, y1, x2, y2, filled)
|
||||
table.insert(graphicObjects, {geometric.rectangle(x1 or 1, y1 or 1, x2 or 1, y2 or 1, filled), tHex[color]})
|
||||
redraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
shrink = function(self)
|
||||
isGraphicShrinked = true
|
||||
redrawCanvasSize()
|
||||
shrink()
|
||||
setPixel = function(self, text, fg, bg, _x, _y)
|
||||
x = _x or x
|
||||
y = _y or y
|
||||
imgData.blit(text, fg, bg, x, y)
|
||||
bimg = imgData.getBimgData()[1]
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setDragable = function(self, drag)
|
||||
dragable = drag == true and true or false
|
||||
setText = function(self, text, _x, _y)
|
||||
x = _x or x
|
||||
y = _y or y
|
||||
imgData.text(text, x, y)
|
||||
bimg = imgData.getBimgData()[1]
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, event, button, x, y)
|
||||
if(base.mouseHandler(self, event, button, x, y))then
|
||||
if(dragable)then
|
||||
if(event=="mouse_click")then
|
||||
xMouse,yMouse = x,y
|
||||
end
|
||||
setBg = function(self, bg, _x, _y)
|
||||
x = _x or x
|
||||
y = _y or y
|
||||
imgData.bg(bg, x, y)
|
||||
bimg = imgData.getBimgData()[1]
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
if(event=="mouse_drag")then
|
||||
if(xMouse~=nil)and(yMouse~=nil)then
|
||||
xOffset = max(min(xOffset+xMouse-x, w-self:getWidth()),0)
|
||||
xMouse = x
|
||||
yOffset = max(min(yOffset+yMouse-y, h-self:getHeight()),0)
|
||||
yMouse = y
|
||||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
return false
|
||||
setFg = function(self, fg, _x, _y)
|
||||
x = _x or x
|
||||
y = _y or y
|
||||
imgData.fg(fg, x, y)
|
||||
bimg = imgData.getBimgData()[1]
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getImageSize = function(self)
|
||||
return imgData.getSize()
|
||||
end,
|
||||
|
||||
setImageSize = function(self, w, h)
|
||||
imgData.setSize(w, h)
|
||||
bimg = imgData.getBimgData()[1]
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
clear = function(self)
|
||||
imgData = bimgLib()
|
||||
bimg = nil
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getImage = function(self)
|
||||
return imgData.getBimgData()
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
@@ -430,41 +105,20 @@ return function(name)
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local w,h = self:getSize()
|
||||
if(self.bgColor~=false)then
|
||||
self.parent:drawBackgroundBox(obx, oby, w, h, self.bgColor)
|
||||
end
|
||||
if (isGraphicShrinked) then
|
||||
-- this 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 t, tC, bC = shrinkedGraphic[1], shrinkedGraphic[2], shrinkedGraphic[3]
|
||||
for i = 1, shrinkedGraphic.height do
|
||||
local x, y = obx+xOffset, oby + i - 1 + yOffset
|
||||
if(y>oby-1)and(y<=oby+h-1)and(x<=w+obx)then
|
||||
local tI = t[i]
|
||||
local xpos,substart,subend = max(x, obx), max(1 - x + 1, 1), min(w - (x-obx), w)
|
||||
if type(tI) == "string" then
|
||||
self.parent:setText(xpos, y, sub(tI, substart, subend))
|
||||
self.parent:setFG(xpos, y, sub(tC[i], substart, subend))
|
||||
self.parent:setBG(xpos, y, sub(bC[i], substart, subend))
|
||||
elseif type(tI) == "table" then
|
||||
self.parent:setText(xpos, y, sub(tI[2], substart, subend))
|
||||
self.parent:setFG(xpos, y, sub(tC[i], substart, subend))
|
||||
self.parent:setBG(xpos, y, sub(bC[i], substart, subend))
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
for i = 1, #graphic do
|
||||
local x, y = obx+xOffset, oby + i - 1 + yOffset
|
||||
if(y>oby-1)and(y<=oby+h-1)and(x<=w+obx)then
|
||||
local xpos,substart,subend = max(x, obx), max(1 - x + 1, 1), min(w - (x-obx), w)
|
||||
self.parent:setBG(xpos, y, sub(graphic[i],substart,subend))
|
||||
if(bimg~=nil)then
|
||||
for k,v in pairs(bimg)do
|
||||
if(k<=h-yOffset)and(k+yOffset>=1)then
|
||||
self.parent:blit(obx+xOffset, oby+k-1+yOffset, v[1], v[2], v[3])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
self:setVisualChanged(false)
|
||||
end
|
||||
end;
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
self.bgColor = self.parent:getTheme("GraphicBG")
|
||||
end,
|
||||
}
|
||||
|
||||
return setmetatable(object, base)
|
||||
|
||||
Reference in New Issue
Block a user