Compare commits
277 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14c0630c1e | ||
|
|
64cb0d15e8 | ||
|
|
322ddb6158 | ||
|
|
c71557feb7 | ||
|
|
98b4191504 | ||
|
|
8dd8e63d21 | ||
|
|
28d35559c8 | ||
|
|
f3eb0accf8 | ||
|
|
599edf55dd | ||
|
|
303d0e65ff | ||
|
|
12e981a0e2 | ||
|
|
ca2fd64302 | ||
|
|
4fefe5734c | ||
|
|
8168fa4465 | ||
|
|
33753f6d8f | ||
|
|
1c7558d133 | ||
|
|
8d4e96a128 | ||
|
|
42aa1059cf | ||
|
|
a3886d34b0 | ||
|
|
5f8cbc90f3 | ||
|
|
82dcceda05 | ||
|
|
8ebede1430 | ||
|
|
31cda3d672 | ||
|
|
b794612017 | ||
|
|
9aa3663f90 | ||
|
|
71b4d6e01a | ||
|
|
2970451fc6 | ||
|
|
193616c20d | ||
|
|
df40fb496c | ||
|
|
9dc7449ace | ||
|
|
24b31a091a | ||
|
|
45f4d99db1 | ||
|
|
fe9d1f44a1 | ||
|
|
958b962ada | ||
|
|
cbe67c3919 | ||
|
|
63f88f8184 | ||
|
|
cc43817f34 | ||
|
|
c583eb666d | ||
|
|
f16e172fb1 | ||
|
|
4d2aad4155 | ||
|
|
dd7c26d4c3 | ||
|
|
8f370a0106 | ||
|
|
26567debd6 | ||
|
|
af14c7b867 | ||
|
|
1339337e6e | ||
|
|
84096eeca5 | ||
|
|
5bbed5be5c | ||
|
|
c945931c3c | ||
|
|
716debd419 | ||
|
|
f7e55c9f52 | ||
|
|
555ab6217c | ||
|
|
5373dab682 | ||
|
|
de94f8266f | ||
|
|
d3db685573 | ||
|
|
cfb4a197cd | ||
|
|
4feeaa7942 | ||
|
|
42972f3c2c | ||
|
|
e67945fccf | ||
|
|
1b7f61323b | ||
|
|
2293aacdec | ||
|
|
2d9efbbb5a | ||
|
|
9b0af8fcb3 | ||
|
|
87105c4856 | ||
|
|
a5e3191fde | ||
|
|
afe6639200 | ||
|
|
01a8226721 | ||
|
|
86972e21ab | ||
|
|
ad7a15af7c | ||
|
|
e22c2dfcd5 | ||
|
|
1f9734ef5f | ||
|
|
f4c469d64e | ||
|
|
cfec78f6f7 | ||
|
|
67a408a4c8 | ||
|
|
cbc8ca5c5b | ||
|
|
631eef525c | ||
|
|
930d2ad0c8 | ||
|
|
df1569af39 | ||
|
|
7884ec4ee0 | ||
|
|
b6ff31d77c | ||
|
|
d7a6885af6 | ||
|
|
b6114ca8da | ||
|
|
89d1eff6e1 | ||
|
|
059759d1c0 | ||
|
|
47a4706a14 | ||
|
|
a8d08c865e | ||
|
|
062b6b0342 | ||
|
|
b7de9c5c93 | ||
|
|
a0a717367e | ||
|
|
2e5255bee4 | ||
|
|
5d682d1a76 | ||
|
|
6b5bbfb54b | ||
|
|
d6642ad855 | ||
|
|
eb80f2daac | ||
|
|
c0d670b57d | ||
|
|
bb3bcfba2c | ||
|
|
8689858745 | ||
|
|
bdc241575b | ||
|
|
015666ad14 | ||
|
|
325f1c1fb0 | ||
|
|
cc602c0749 | ||
|
|
7b0ad7294f | ||
|
|
e8748638db | ||
|
|
37e5be8a6a | ||
|
|
35113e2a7d | ||
|
|
741f7cf4ec | ||
|
|
8d3eaf21bc | ||
|
|
398f800270 | ||
|
|
2295581f37 | ||
|
|
c549403988 | ||
|
|
e210939988 | ||
|
|
e84f65654d | ||
|
|
dcaeb04b2a | ||
|
|
45fdaa3da5 | ||
|
|
b2e5476890 | ||
|
|
ede6c87c88 | ||
|
|
5593b3bbfb | ||
|
|
bfe5163956 | ||
|
|
588fe4f5cf | ||
|
|
5f53e7c7cd | ||
|
|
bd0f22929d | ||
|
|
2266f6cc52 | ||
|
|
1fdf569ba8 | ||
|
|
1ff1a6a0a4 | ||
|
|
ba81a2a092 | ||
|
|
6fad764537 | ||
|
|
674955508d | ||
|
|
6895c6883f | ||
|
|
a6789fb53a | ||
|
|
905a1117e1 | ||
|
|
960badacc5 | ||
|
|
73c6e27653 | ||
|
|
a44f40851d | ||
|
|
3c18ce6cd9 | ||
|
|
99836ee75a | ||
|
|
4d44016058 | ||
|
|
5326008a2f | ||
|
|
24087454fc | ||
|
|
32b98f089b | ||
|
|
e35708902c | ||
|
|
d2f4717500 | ||
|
|
e14922f6c0 | ||
|
|
926dd7703d | ||
|
|
0086229e38 | ||
|
|
4a5eaf324a | ||
|
|
dc4584eb60 | ||
|
|
32e972e103 | ||
|
|
560c99195d | ||
|
|
794f7c258b | ||
|
|
71c02d4115 | ||
|
|
4966384d0c | ||
|
|
e4de37e8c2 | ||
|
|
f2e7f9d962 | ||
|
|
166a234bd2 | ||
|
|
3de3fbecb3 | ||
|
|
6cd8e0ebb0 | ||
|
|
c93d6bf91f | ||
|
|
e2723161a0 | ||
|
|
9b932704e6 | ||
|
|
3e019021ae | ||
|
|
87ca529c46 | ||
|
|
0b7d5d5bb7 | ||
|
|
43ebd3fc3c | ||
|
|
f57616205c | ||
|
|
d18776e116 | ||
|
|
2fb0f1c8ce | ||
|
|
c9ff68dd9e | ||
|
|
88d137d8ba | ||
|
|
19d7fa9a61 | ||
|
|
e2bead28cb | ||
|
|
48ad4ad88e | ||
|
|
a4b53b7cd0 | ||
|
|
2f09e12ec2 | ||
|
|
c084e9e5ad | ||
|
|
deb2a004c1 | ||
|
|
2e65d14538 | ||
|
|
5604925e27 | ||
|
|
e3de424f42 | ||
|
|
1eddd209f0 | ||
|
|
8b6cf6bdb8 | ||
|
|
c9a74dfbd4 | ||
|
|
f3f8105459 | ||
|
|
60615f103a | ||
|
|
2b2de1cddf | ||
|
|
2931a95c20 | ||
|
|
e87557aba9 | ||
|
|
dda224bb53 | ||
|
|
6e00661011 | ||
|
|
2d6188d119 | ||
|
|
419cfad357 | ||
|
|
ba1f602965 | ||
|
|
4caffa585e | ||
|
|
fe590dceab | ||
|
|
f86694df6e | ||
|
|
a4f19511d3 | ||
|
|
6176bb0bf2 | ||
|
|
5b47bd3daa | ||
|
|
a97254ddf7 | ||
|
|
cbce567e59 | ||
|
|
f90c2e3878 | ||
|
|
710106e125 | ||
|
|
4f0d154b31 | ||
|
|
4247277d88 | ||
|
|
4b2d417d75 | ||
|
|
6c235b04df | ||
|
|
d4c72514ef | ||
|
|
92b93a3862 | ||
|
|
6bab574e8a | ||
|
|
f2eacf4a82 | ||
|
|
fbdc1fa3b0 | ||
|
|
33397b0871 | ||
|
|
a12717935e | ||
|
|
a8a8aef7be | ||
|
|
43e1d086dc | ||
|
|
25701a75d9 | ||
|
|
4f46c55f24 | ||
|
|
79a8ec5f3a | ||
|
|
999a563a3e | ||
|
|
7c1f94015a | ||
|
|
c35d766889 | ||
|
|
bb1b1beb79 | ||
|
|
e086c1abb2 | ||
|
|
075903f9a9 | ||
|
|
d6931412c8 | ||
|
|
73b43358ff | ||
|
|
df7be99fdd | ||
|
|
6f0ddd6bf6 | ||
|
|
a821abed1f | ||
|
|
50dce1c1e7 | ||
|
|
94fccf9211 | ||
|
|
a4d0492d17 | ||
|
|
5ebb5fe436 | ||
|
|
bb921c57cd | ||
|
|
2b85ab9746 | ||
|
|
29ebd7d4d7 | ||
|
|
8ab7bc4bde | ||
|
|
1b8f62ce41 | ||
|
|
61e38d89e8 | ||
|
|
a0db5ecb61 | ||
|
|
3ef7aa91bf | ||
|
|
09e8589938 | ||
|
|
e9aba2498b | ||
|
|
20ceb6b56f | ||
|
|
6d2956f3bf | ||
|
|
91e42e096c | ||
|
|
5f00dfe401 | ||
|
|
2c4c059a70 | ||
|
|
923b570d48 | ||
|
|
5499057e7e | ||
|
|
d6091b312c | ||
|
|
f19c0ebb49 | ||
|
|
01b83822b1 | ||
|
|
c81f24cca2 | ||
|
|
ed894cb390 | ||
|
|
355db9a82c | ||
|
|
8112378c70 | ||
|
|
a181496052 | ||
|
|
636ed0001f | ||
|
|
0e3da7eda5 | ||
|
|
3cf5adaef1 | ||
|
|
c0fab23cef | ||
|
|
7609708507 | ||
|
|
e2f2a2871d | ||
|
|
37dde881ad | ||
|
|
8f2e191fe3 | ||
|
|
15d8cb3781 | ||
|
|
8acb673f74 | ||
|
|
cfdca639db | ||
|
|
b34cdd8383 | ||
|
|
a74b8a3d9f | ||
|
|
ef480c3998 | ||
|
|
c3e97ae516 | ||
|
|
d78bb4d141 | ||
|
|
ab72f244ed | ||
|
|
4352d36831 | ||
|
|
04ad97ccbe | ||
|
|
6b2433e0a1 | ||
|
|
bf1b008084 |
1195
Basalt/Frame.lua
@@ -1,947 +0,0 @@
|
||||
local basaltEvent = require("basaltEvent")
|
||||
local utils = require("utils")
|
||||
local split = utils.splitString
|
||||
local numberFromString = utils.numberFromString
|
||||
local xmlValue = utils.getValueFromXML
|
||||
|
||||
return function(name)
|
||||
-- Base object
|
||||
local objectType = "Object" -- not changeable
|
||||
local object = {}
|
||||
local zIndex = 1
|
||||
local value
|
||||
local anchor = "topLeft"
|
||||
local ignOffset = false
|
||||
local isVisible = true
|
||||
local initialized = false
|
||||
local isHovered = false
|
||||
local isClicked = false
|
||||
|
||||
local shadow = false
|
||||
local borderColors = {
|
||||
left = false,
|
||||
right = false,
|
||||
top = false,
|
||||
bottom = false
|
||||
}
|
||||
|
||||
local shadowColor = colors.black
|
||||
local isEnabled = true
|
||||
local isDragging = false
|
||||
local dragStartX, dragStartY, dragXOffset, dragYOffset = 0, 0, 0, 0
|
||||
|
||||
local draw = true
|
||||
local activeEvents = {}
|
||||
|
||||
local eventSystem = basaltEvent()
|
||||
|
||||
object = {
|
||||
x = 1,
|
||||
y = 1,
|
||||
width = 1,
|
||||
height = 1,
|
||||
bgColor = colors.black,
|
||||
bgSymbol = " ",
|
||||
bgSymbolColor = colors.black,
|
||||
fgColor = colors.white,
|
||||
transparentColor = false,
|
||||
name = name or "Object",
|
||||
parent = nil,
|
||||
|
||||
show = function(self)
|
||||
isVisible = true
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
hide = function(self)
|
||||
isVisible = false
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
enable = function(self)
|
||||
isEnabled = true
|
||||
return self
|
||||
end,
|
||||
|
||||
disable = function(self)
|
||||
isEnabled = false
|
||||
return self
|
||||
end,
|
||||
|
||||
isEnabled = function(self)
|
||||
return isEnabled
|
||||
end,
|
||||
|
||||
generateXMLEventFunction = function(self, func, val)
|
||||
local createF = function(str)
|
||||
if(str:sub(1,1)=="#")then
|
||||
local o = self:getBaseFrame():getDeepObject(str:sub(2,str:len()))
|
||||
if(o~=nil)and(o.internalObjetCall~=nil)then
|
||||
func(self,function()o:internalObjetCall()end)
|
||||
end
|
||||
else
|
||||
func(self,self:getBaseFrame():getVariable(str))
|
||||
end
|
||||
end
|
||||
if(type(val)=="string")then
|
||||
createF(val)
|
||||
elseif(type(val)=="table")then
|
||||
for k,v in pairs(val)do
|
||||
createF(v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
local baseFrame = self:getBaseFrame()
|
||||
if(xmlValue("x", data)~=nil)then self:setPosition(xmlValue("x", data), self.y) end
|
||||
if(xmlValue("y", data)~=nil)then self:setPosition(self.x, xmlValue("y", data)) end
|
||||
if(xmlValue("width", data)~=nil)then self:setSize(xmlValue("width", data), self.height) end
|
||||
if(xmlValue("height", data)~=nil)then self:setSize(self.width, xmlValue("height", data)) end
|
||||
if(xmlValue("bg", data)~=nil)then self:setBackground(colors[xmlValue("bg", data)]) end
|
||||
if(xmlValue("fg", data)~=nil)then self:setForeground(colors[xmlValue("fg", data)]) end
|
||||
if(xmlValue("value", data)~=nil)then self:setValue(colors[xmlValue("value", data)]) end
|
||||
if(xmlValue("visible", data)~=nil)then if(xmlValue("visible", data))then self:show() else self:hide() end end
|
||||
if(xmlValue("enabled", data)~=nil)then if(xmlValue("enabled", data))then self:enable() else self:disable() end end
|
||||
if(xmlValue("zIndex", data)~=nil)then self:setZIndex(xmlValue("zIndex", data)) end
|
||||
if(xmlValue("anchor", data)~=nil)then self:setAnchor(xmlValue("anchor", data)) end
|
||||
if(xmlValue("shadowColor", data)~=nil)then self:setShadow(colors[xmlValue("shadowColor", data)]) end
|
||||
if(xmlValue("border", data)~=nil)then self:setBorder(colors[xmlValue("border", data)]) end
|
||||
if(xmlValue("borderLeft", data)~=nil)then borderColors["left"] = xmlValue("borderLeft", data) end
|
||||
if(xmlValue("borderTop", data)~=nil)then borderColors["top"] = xmlValue("borderTop", data) end
|
||||
if(xmlValue("borderRight", data)~=nil)then borderColors["right"] = xmlValue("borderRight", data) end
|
||||
if(xmlValue("borderBottom", data)~=nil)then borderColors["bottom"] = xmlValue("borderBottom", data) end
|
||||
if(xmlValue("borderColor", data)~=nil)then self:setBorder(colors[xmlValue("borderColor", data)]) end
|
||||
if(xmlValue("ignoreOffset", data)~=nil)then if(xmlValue("ignoreOffset", data))then self:ignoreOffset(true) end end
|
||||
if(xmlValue("onClick", data)~=nil)then self:generateXMLEventFunction(self.onClick, xmlValue("onClick", data)) end
|
||||
if(xmlValue("onClickUp", data)~=nil)then self:generateXMLEventFunction(self.onClickUp, xmlValue("onClickUp", data)) end
|
||||
if(xmlValue("onScroll", data)~=nil)then self:generateXMLEventFunction(self.onScroll, xmlValue("onScroll", data)) end
|
||||
if(xmlValue("onDrag", data)~=nil)then self:generateXMLEventFunction(self.onDrag, xmlValue("onDrag", data)) end
|
||||
if(xmlValue("onHover", data)~=nil)then self:generateXMLEventFunction(self.onHover, xmlValue("onHover", data)) end
|
||||
if(xmlValue("onLeave", data)~=nil)then self:generateXMLEventFunction(self.onLeave, xmlValue("onLeave", data)) end
|
||||
if(xmlValue("onKey", data)~=nil)then self:generateXMLEventFunction(self.onKey, xmlValue("onKey", data)) end
|
||||
if(xmlValue("onKeyUp", data)~=nil)then self:generateXMLEventFunction(self.onKeyUp, xmlValue("onKeyUp", data)) end
|
||||
if(xmlValue("onChange", data)~=nil)then self:generateXMLEventFunction(self.onChange, xmlValue("onChange", data)) end
|
||||
if(xmlValue("onResize", data)~=nil)then self:generateXMLEventFunction(self.onResize, xmlValue("onResize", data)) end
|
||||
if(xmlValue("onReposition", data)~=nil)then self:generateXMLEventFunction(self.onReposition, xmlValue("onReposition", data)) end
|
||||
if(xmlValue("onEvent", data)~=nil)then self:generateXMLEventFunction(self.onEvent, xmlValue("onEvent", data)) end
|
||||
if(xmlValue("onGetFocus", data)~=nil)then self:generateXMLEventFunction(self.onGetFocus, xmlValue("onGetFocus", data)) end
|
||||
if(xmlValue("onLoseFocus", data)~=nil)then self:generateXMLEventFunction(self.onLoseFocus, xmlValue("onLoseFocus", data)) end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
isVisible = function(self)
|
||||
return isVisible
|
||||
end;
|
||||
|
||||
setFocus = function(self)
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:setFocusedObject(self)
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
setZIndex = function(self, index)
|
||||
zIndex = index
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:removeObject(self)
|
||||
self.parent:addObject(self)
|
||||
self:updateEventHandlers()
|
||||
end
|
||||
|
||||
return self
|
||||
end,
|
||||
|
||||
updateEventHandlers = function(self)
|
||||
for k,v in pairs(activeEvents)do
|
||||
if(v)then
|
||||
self.parent:addEvent(k, self)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
getZIndex = function(self)
|
||||
return zIndex;
|
||||
end;
|
||||
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
|
||||
getName = function(self)
|
||||
return self.name
|
||||
end;
|
||||
|
||||
remove = function(self)
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:removeObject(self)
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setParent = function(self, frame)
|
||||
if (frame.getType ~= nil and frame:getType() == "Frame") then
|
||||
self:remove()
|
||||
frame:addObject(self)
|
||||
if (self.draw) then
|
||||
self:show()
|
||||
end
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
setValue = function(self, _value)
|
||||
if (value ~= _value) then
|
||||
value = _value
|
||||
self:updateDraw()
|
||||
self:valueChangedHandler()
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
getValue = function(self)
|
||||
return value
|
||||
end;
|
||||
|
||||
getDraw = function(self)
|
||||
return draw
|
||||
end;
|
||||
|
||||
updateDraw = function(self, change)
|
||||
draw = change
|
||||
if(change == nil)then draw = true end
|
||||
if(draw)then if(self.parent~=nil)then self.parent:updateDraw() end end
|
||||
return self
|
||||
end;
|
||||
|
||||
|
||||
getEventSystem = function(self)
|
||||
return eventSystem
|
||||
end;
|
||||
|
||||
|
||||
getParent = function(self)
|
||||
return self.parent
|
||||
end;
|
||||
|
||||
setPosition = function(self, xPos, yPos, rel)
|
||||
if(type(xPos)=="number")then
|
||||
self.x = rel and self:getX()+xPos or xPos
|
||||
end
|
||||
if(type(yPos)=="number")then
|
||||
self.y = rel and self:getY()+yPos or yPos
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
if(type(xPos)=="string")then
|
||||
self.x = self.parent:newDynamicValue(self, xPos)
|
||||
end
|
||||
if(type(yPos)=="string")then
|
||||
self.y = self.parent:newDynamicValue(self, yPos)
|
||||
end
|
||||
self.parent:recalculateDynamicValues()
|
||||
end
|
||||
eventSystem:sendEvent("basalt_reposition", self)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getX = function(self)
|
||||
return type(self.x) == "number" and self.x or math.floor(self.x[1]+0.5)
|
||||
end;
|
||||
|
||||
getY = function(self)
|
||||
return type(self.y) == "number" and self.y or math.floor(self.y[1]+0.5)
|
||||
end;
|
||||
|
||||
getPosition = function(self)
|
||||
return self:getX(), self:getY()
|
||||
end;
|
||||
|
||||
getVisibility = function(self)
|
||||
return isVisible
|
||||
end;
|
||||
|
||||
setVisibility = function(self, _isVisible)
|
||||
isVisible = _isVisible or not isVisible
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setSize = function(self, width, height, rel)
|
||||
if(type(width)=="number")then
|
||||
self.width = rel and self:getWidth()+width or width
|
||||
end
|
||||
if(type(height)=="number")then
|
||||
self.height = rel and self:getHeight()+height or height
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
if(type(width)=="string")then
|
||||
self.width = self.parent:newDynamicValue(self, width)
|
||||
end
|
||||
if(type(height)=="string")then
|
||||
self.height = self.parent:newDynamicValue(self, height)
|
||||
end
|
||||
self.parent:recalculateDynamicValues()
|
||||
end
|
||||
eventSystem:sendEvent("basalt_resize", self)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getHeight = function(self)
|
||||
return type(self.height) == "number" and self.height or math.floor(self.height[1]+0.5)
|
||||
end;
|
||||
|
||||
getWidth = function(self)
|
||||
return type(self.width) == "number" and self.width or math.floor(self.width[1]+0.5)
|
||||
end;
|
||||
|
||||
getSize = function(self)
|
||||
return self:getWidth(), self:getHeight()
|
||||
end;
|
||||
|
||||
calculateDynamicValues = function(self)
|
||||
if(type(self.width)=="table")then self.width:calculate() end
|
||||
if(type(self.height)=="table")then self.height:calculate() end
|
||||
if(type(self.x)=="table")then self.x:calculate() end
|
||||
if(type(self.y)=="table")then self.y:calculate() end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setBackground = function(self, color, symbol, symbolCol)
|
||||
self.bgColor = color or false
|
||||
self.bgSymbol = symbol or (self.bgColor~=false and self.bgSymbol or false)
|
||||
self.bgSymbolColor = symbolCol or self.bgSymbolColor
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setTransparent = function(self, color)
|
||||
self.transparentColor = color or false
|
||||
self.bgSymbol = false
|
||||
self.bgSymbolColor = false
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getBackground = function(self)
|
||||
return self.bgColor
|
||||
end;
|
||||
|
||||
setForeground = function(self, color)
|
||||
self.fgColor = color or false
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getForeground = function(self)
|
||||
return self.fgColor
|
||||
end;
|
||||
|
||||
setShadow = function(self, color)
|
||||
if(color==false)then
|
||||
shadow = false
|
||||
else
|
||||
shadowColor = color
|
||||
shadow = true
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
isShadowActive = function(self)
|
||||
return shadow;
|
||||
end;
|
||||
|
||||
setBorder = function(self, ...)
|
||||
if(...~=nil)then
|
||||
local t = {...}
|
||||
for k,v in pairs(t)do
|
||||
if(v=="left")or(#t==1)then
|
||||
borderColors["left"] = t[1]
|
||||
end
|
||||
if(v=="top")or(#t==1)then
|
||||
borderColors["top"] = t[1]
|
||||
end
|
||||
if(v=="right")or(#t==1)then
|
||||
borderColors["right"] = t[1]
|
||||
end
|
||||
if(v=="bottom")or(#t==1)then
|
||||
borderColors["bottom"] = t[1]
|
||||
end
|
||||
end
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getBorder = function(self, side)
|
||||
if(side=="left")then
|
||||
return borderLeft;
|
||||
end
|
||||
if(side=="top")then
|
||||
return borderTop;
|
||||
end
|
||||
if(side=="right")then
|
||||
return borderRight;
|
||||
end
|
||||
if(side=="bottom")then
|
||||
return borderBottom;
|
||||
end
|
||||
end;
|
||||
|
||||
draw = function(self)
|
||||
if (isVisible)then
|
||||
if(self.parent~=nil)then
|
||||
local x, y = self:getAnchorPosition()
|
||||
local w,h = self:getSize()
|
||||
local wP,hP = self.parent:getSize()
|
||||
if(x+w<1)or(x>wP)or(y+h<1)or(y>hP)then return false end
|
||||
if(self.transparentColor~=false)then
|
||||
self.parent:drawForegroundBox(x, y, w, h, self.transparentColor)
|
||||
end
|
||||
if(self.bgColor~=false)then
|
||||
self.parent:drawBackgroundBox(x, y, w, h, self.bgColor)
|
||||
end
|
||||
if(self.bgSymbol~=false)then
|
||||
self.parent:drawTextBox(x, y, w, h, self.bgSymbol)
|
||||
if(self.bgSymbol~=" ")then
|
||||
self.parent:drawForegroundBox(x, y, w, h, self.bgSymbolColor)
|
||||
end
|
||||
end
|
||||
if(shadow)then
|
||||
self.parent:drawBackgroundBox(x+1, y+h, w, 1, shadowColor)
|
||||
self.parent:drawBackgroundBox(x+w, y+1, 1, h, shadowColor)
|
||||
self.parent:drawForegroundBox(x+1, y+h, w, 1, shadowColor)
|
||||
self.parent:drawForegroundBox(x+w, y+1, 1, h, shadowColor)
|
||||
end
|
||||
|
||||
local bgCol = self.bgColor
|
||||
if(borderColors["left"]~=false)then
|
||||
self.parent:drawTextBox(x, y, 1, h, "\149")
|
||||
if(bgCol~=false)then self.parent:drawBackgroundBox(x, y, 1, h, self.bgColor) end
|
||||
self.parent:drawForegroundBox(x, y, 1, h, borderColors["left"])
|
||||
end
|
||||
if(borderColors["top"]~=false)then
|
||||
|
||||
self.parent:drawTextBox(x, y, w, 1, "\131")
|
||||
if(bgCol~=false)then self.parent:drawBackgroundBox(x, y, w, 1, self.bgColor) end
|
||||
self.parent:drawForegroundBox(x, y, w, 1, borderColors["top"])
|
||||
end
|
||||
if(borderColors["left"]~=false)and(borderColors["top"]~=false)then
|
||||
self.parent:drawTextBox(x, y, 1, 1, "\151")
|
||||
if(bgCol~=false)then self.parent:drawBackgroundBox(x, y, 1, 1, self.bgColor) end
|
||||
self.parent:drawForegroundBox(x, y, 1, 1, borderColors["left"])
|
||||
end
|
||||
if(borderColors["right"]~=false)then
|
||||
self.parent:drawTextBox(x+w-1, y, 1, h, "\149")
|
||||
if(bgCol~=false)then self.parent:drawForegroundBox(x+w-1, y, 1, h, self.bgColor) end
|
||||
self.parent:drawBackgroundBox(x+w-1, y, 1, h, borderColors["right"])
|
||||
end
|
||||
if(borderColors["bottom"]~=false)then
|
||||
self.parent:drawTextBox(x, y+h-1, w, 1, "\143")
|
||||
if(bgCol~=false)then self.parent:drawForegroundBox(x, y+h-1, w, 1, self.bgColor) end
|
||||
self.parent:drawBackgroundBox(x, y+h-1, w, 1, borderColors["bottom"])
|
||||
end
|
||||
if(borderColors["top"]~=false)and(borderColors["right"]~=false)then
|
||||
self.parent:drawTextBox(x+w-1, y, 1, 1, "\148")
|
||||
if(bgCol~=false)then self.parent:drawForegroundBox(x+w-1, y, 1, 1, self.bgColor) end
|
||||
self.parent:drawBackgroundBox(x+w-1, y, 1, 1, borderColors["right"])
|
||||
end
|
||||
if(borderColors["right"]~=false)and(borderColors["bottom"]~=false)then
|
||||
self.parent:drawTextBox(x+w-1, y+h-1, 1, 1, "\133")
|
||||
if(bgCol~=false)then self.parent:drawForegroundBox(x+w-1, y+h-1, 1, 1, self.bgColor) end
|
||||
self.parent:drawBackgroundBox(x+w-1, y+h-1, 1, 1, borderColors["right"])
|
||||
end
|
||||
if(borderColors["bottom"]~=false)and(borderColors["left"]~=false)then
|
||||
self.parent:drawTextBox(x, y+h-1, 1, 1, "\138")
|
||||
if(bgCol~=false)then self.parent:drawForegroundBox(x-1, y+h-1, 1, 1, self.bgColor) end
|
||||
self.parent:drawBackgroundBox(x, y+h-1, 1, 1, borderColors["left"])
|
||||
end
|
||||
end
|
||||
draw = false
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end;
|
||||
|
||||
|
||||
getAbsolutePosition = function(self, x, y)
|
||||
-- relative position to absolute position
|
||||
if (x == nil) or (y == nil) then
|
||||
x, y = self:getAnchorPosition()
|
||||
end
|
||||
|
||||
if (self.parent ~= nil) then
|
||||
local fx, fy = self.parent:getAbsolutePosition()
|
||||
x = fx + x - 1
|
||||
y = fy + y - 1
|
||||
end
|
||||
return x, y
|
||||
end;
|
||||
|
||||
getAnchorPosition = function(self, x, y, ignOff)
|
||||
if (x == nil) then
|
||||
x = self:getX()
|
||||
end
|
||||
if (y == nil) then
|
||||
y = self:getY()
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
local pw,ph = self.parent:getSize()
|
||||
if (anchor == "top") then
|
||||
x = math.floor(pw/2) + x - 1
|
||||
elseif(anchor == "topRight") then
|
||||
x = pw + x - 1
|
||||
elseif(anchor == "right") then
|
||||
x = pw + x - 1
|
||||
y = math.floor(ph/2) + y - 1
|
||||
elseif(anchor == "bottomRight") then
|
||||
x = pw + x - 1
|
||||
y = ph + y - 1
|
||||
elseif(anchor == "bottom") then
|
||||
x = math.floor(pw/2) + x - 1
|
||||
y = ph + y - 1
|
||||
elseif(anchor == "bottomLeft") then
|
||||
y = ph + y - 1
|
||||
elseif(anchor == "left") then
|
||||
y = math.floor(ph/2) + y - 1
|
||||
elseif(anchor == "center") then
|
||||
x = math.floor(pw/2) + x - 1
|
||||
y = math.floor(ph/2) + y - 1
|
||||
end
|
||||
|
||||
local xO, yO = self.parent:getOffsetInternal()
|
||||
if not(ignOffset or ignOff) then
|
||||
return x+xO, y+yO
|
||||
end
|
||||
end
|
||||
return x, y
|
||||
end;
|
||||
|
||||
ignoreOffset = function(self, ignore)
|
||||
ignOffset = ignore
|
||||
if(ignore==nil)then ignOffset = true end
|
||||
return self
|
||||
end;
|
||||
|
||||
getBaseFrame = function(self)
|
||||
if(self.parent~=nil)then
|
||||
return self.parent:getBaseFrame()
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
setAnchor = function(self, newAnchor)
|
||||
anchor = newAnchor
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getAnchor = function(self)
|
||||
return anchor
|
||||
end;
|
||||
|
||||
onChange = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("value_changed", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onClick = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_click", v)
|
||||
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
|
||||
return self
|
||||
end;
|
||||
|
||||
onClickUp = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_up", v)
|
||||
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
|
||||
return self
|
||||
end;
|
||||
|
||||
onRelease = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_release", v)
|
||||
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
|
||||
return self
|
||||
end;
|
||||
|
||||
onScroll = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_scroll", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("mouse_scroll", self)
|
||||
activeEvents["mouse_scroll"] = true
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onHover = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_hover", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("mouse_move", self)
|
||||
activeEvents["mouse_move"] = true
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onLeave = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_leave", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("mouse_move", self)
|
||||
activeEvents["mouse_move"] = true
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onDrag = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_drag", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("mouse_drag", self)
|
||||
activeEvents["mouse_drag"] = true
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
activeEvents["mouse_click"] = true
|
||||
self.parent:addEvent("mouse_up", self)
|
||||
activeEvents["mouse_up"] = true
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onEvent = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("other_event", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("other_event", self)
|
||||
activeEvents["other_event"] = true
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onKey = function(self, ...)
|
||||
if(isEnabled)then
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("key", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("key", self)
|
||||
activeEvents["key"] = true
|
||||
end
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onChar = function(self, ...)
|
||||
if(isEnabled)then
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("char", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("char", self)
|
||||
activeEvents["char"] = true
|
||||
end
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onResize = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("basalt_resize", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onReposition = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("basalt_reposition", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onKeyUp = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("key_up", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("key_up", self)
|
||||
activeEvents["key_up"] = true
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
isFocused = function(self)
|
||||
if (self.parent ~= nil) then
|
||||
return self.parent:getFocusedObject() == self
|
||||
end
|
||||
return false
|
||||
end;
|
||||
|
||||
onGetFocus = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("get_focus", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
activeEvents["mouse_click"] = true
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onLoseFocus = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("lose_focus", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
activeEvents["mouse_click"] = true
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
registerEvent = function(self, event, func)
|
||||
return eventSystem:registerEvent(event, func)
|
||||
end;
|
||||
|
||||
removeEvent = function(self, event, index)
|
||||
return eventSystem:removeEvent(event, index)
|
||||
end;
|
||||
|
||||
sendEvent = function(self, event, ...)
|
||||
return eventSystem:sendEvent(event, self, ...)
|
||||
end;
|
||||
|
||||
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()
|
||||
local w, h = self:getSize()
|
||||
if (objX <= x) and (objX + w > x) and (objY <= y) and (objY + h > y) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y, isMon)
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
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)
|
||||
end
|
||||
isClicked = true
|
||||
isDragging = true
|
||||
dragStartX, dragStartY = x, y
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
mouseUpHandler = function(self, button, x, y)
|
||||
isDragging = false
|
||||
if(isClicked)then
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local val = eventSystem:sendEvent("mouse_release", self, "mouse_release", button, x - (objX-1), y - (objY-1))
|
||||
isClicked = false
|
||||
end
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
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
|
||||
return false
|
||||
end,
|
||||
|
||||
dragHandler = function(self, button, x, y)
|
||||
if(isDragging)then
|
||||
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
|
||||
self.parent:setFocusedObject(self)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
local objX, objY = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
dragStartX, dragStartY = x, y
|
||||
dragXOffset, dragYOffset = objX - x, objY - y
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
scrollHandler = function(self, dir, x, y)
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
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)
|
||||
end
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
hoverHandler = function(self, x, y, stopped)
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
local val = eventSystem:sendEvent("mouse_hover", self, "mouse_hover", x, y, stopped)
|
||||
if(val==false)then return false end
|
||||
isHovered = true
|
||||
return true
|
||||
end
|
||||
if(isHovered)then
|
||||
local val = eventSystem:sendEvent("mouse_leave", self, "mouse_leave", x, y, stopped)
|
||||
if(val==false)then return false end
|
||||
isHovered = false
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
keyHandler = function(self, key, isHolding)
|
||||
if(isEnabled)and(isVisible)then
|
||||
if (self:isFocused()) then
|
||||
local val = eventSystem:sendEvent("key", self, "key", key, isHolding)
|
||||
if(val==false)then return false end
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end;
|
||||
|
||||
keyUpHandler = function(self, key)
|
||||
if(isEnabled)and(isVisible)then
|
||||
if (self:isFocused()) then
|
||||
local val = eventSystem:sendEvent("key_up", self, "key_up", key)
|
||||
if(val==false)then return false end
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end;
|
||||
|
||||
charHandler = function(self, char)
|
||||
if(isEnabled)and(isVisible)then
|
||||
if (self:isFocused()) then
|
||||
local val = eventSystem:sendEvent("char", self, "char", char)
|
||||
if(val==false)then return false end
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
valueChangedHandler = function(self)
|
||||
eventSystem:sendEvent("value_changed", self, value)
|
||||
end;
|
||||
|
||||
eventHandler = function(self, event, p1, p2, p3, p4)
|
||||
local val = eventSystem:sendEvent("other_event", self, event, p1, p2, p3, p4)
|
||||
if(val~=nil)then return val end
|
||||
return true
|
||||
end;
|
||||
|
||||
getFocusHandler = function(self)
|
||||
local val = eventSystem:sendEvent("get_focus", self)
|
||||
if(val~=nil)then return val end
|
||||
return true
|
||||
end;
|
||||
|
||||
loseFocusHandler = function(self)
|
||||
isDragging = false
|
||||
local val = eventSystem:sendEvent("lose_focus", self)
|
||||
if(val~=nil)then return val end
|
||||
return true
|
||||
end;
|
||||
|
||||
init = function(self)
|
||||
if(self.parent~=nil)then
|
||||
for k,v in pairs(activeEvents)do
|
||||
if(v)then
|
||||
self.parent:addEvent(k, self)
|
||||
end
|
||||
end
|
||||
end
|
||||
if not(initialized)then
|
||||
initialized = true
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return object
|
||||
end
|
||||
@@ -1,9 +1,17 @@
|
||||
local curDir = fs.getDir(table.pack(...)[2]) or ""
|
||||
|
||||
local defaultPath = package.path
|
||||
local format = "%s;/%s/?.lua;/%s/?/init.lua"
|
||||
package.path = string.format(format, package.path, curDir,curDir)..string.format(format, package.path, curDir.."/libraries",curDir.."/libraries")..string.format(format, package.path, curDir.."/objects",curDir.."/objects")
|
||||
if not(packed)then
|
||||
local format = "path;/path/?.lua;/path/?/init.lua;"
|
||||
|
||||
local main = format:gsub("path", curDir)
|
||||
local objFolder = format:gsub("path", curDir.."/objects")
|
||||
local plugFolder = format:gsub("path", curDir.."/plugins")
|
||||
local libFolder = format:gsub("path", curDir.."/libraries")
|
||||
|
||||
|
||||
package.path = main..objFolder..plugFolder..libFolder..defaultPath
|
||||
end
|
||||
local Basalt = require("main")
|
||||
package.path = defaultPath
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
local tHex = require("tHex")
|
||||
local utils = require("utils")
|
||||
local split = utils.splitString
|
||||
local sub,rep = string.sub,string.rep
|
||||
|
||||
return function(drawTerm)
|
||||
@@ -9,13 +11,9 @@ return function(drawTerm)
|
||||
local cacheBG = {}
|
||||
local cacheFG = {}
|
||||
|
||||
local _cacheT = {}
|
||||
local _cacheBG = {}
|
||||
local _cacheFG = {}
|
||||
|
||||
local emptySpaceLine
|
||||
local emptyColorLines = {}
|
||||
|
||||
|
||||
local function createEmptyLines()
|
||||
emptySpaceLine = rep(" ", width)
|
||||
for n = 0, 15 do
|
||||
@@ -40,92 +38,138 @@ return function(drawTerm)
|
||||
end
|
||||
recreateWindowArray()
|
||||
|
||||
local function setText(x, y, text)
|
||||
if (y >= 1) and (y <= height) then
|
||||
if (x + text:len() > 0) and (x <= width) then
|
||||
local oldCache = cacheT[y]
|
||||
local newCache
|
||||
local nEnd = x + #text - 1
|
||||
|
||||
if (x < 1) then
|
||||
local startN = 1 - x + 1
|
||||
local endN = width - x + 1
|
||||
text = sub(text, startN, endN)
|
||||
elseif (nEnd > width) then
|
||||
local endN = width - x + 1
|
||||
text = sub(text, 1, endN)
|
||||
local function blit(x, y, t, fg, bg)
|
||||
if #t == #fg and #t == #bg then
|
||||
if y >= 1 and y <= height then
|
||||
if x + #t > 0 and x <= width then
|
||||
local newCacheT, newCacheFG, newCacheBG
|
||||
local oldCacheT, oldCacheFG, oldCacheBG = cacheT[y], cacheFG[y], cacheBG[y]
|
||||
local startN, endN = 1, #t
|
||||
|
||||
if x < 1 then
|
||||
startN = 1 - x + 1
|
||||
endN = width - x + 1
|
||||
elseif x + #t > width then
|
||||
endN = width - x + 1
|
||||
end
|
||||
|
||||
newCacheT = sub(oldCacheT, 1, x - 1) .. sub(t, startN, endN)
|
||||
newCacheFG = sub(oldCacheFG, 1, x - 1) .. sub(fg, startN, endN)
|
||||
newCacheBG = sub(oldCacheBG, 1, x - 1) .. sub(bg, startN, endN)
|
||||
|
||||
if x + #t <= width then
|
||||
newCacheT = newCacheT .. sub(oldCacheT, x + #t, width)
|
||||
newCacheFG = newCacheFG .. sub(oldCacheFG, x + #t, width)
|
||||
newCacheBG = newCacheBG .. sub(oldCacheBG, x + #t, width)
|
||||
end
|
||||
|
||||
cacheT[y], cacheFG[y], cacheBG[y] = newCacheT,newCacheFG,newCacheBG
|
||||
end
|
||||
|
||||
if (x > 1) then
|
||||
local endN = x - 1
|
||||
newCache = sub(oldCache, 1, endN) .. text
|
||||
else
|
||||
newCache = text
|
||||
end
|
||||
if nEnd < width then
|
||||
newCache = newCache .. sub(oldCache, nEnd + 1, width)
|
||||
end
|
||||
cacheT[y] = newCache
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function setBG(x, y, colorStr)
|
||||
if (y >= 1) and (y <= height) then
|
||||
if (x + colorStr:len() > 0) and (x <= width) then
|
||||
local oldCache = cacheBG[y]
|
||||
local newCache
|
||||
local nEnd = x + #colorStr - 1
|
||||
local function setText(x, y, t)
|
||||
if y >= 1 and y <= height then
|
||||
if x + #t > 0 and x <= width then
|
||||
local newCacheT
|
||||
local oldCacheT = cacheT[y]
|
||||
local startN, endN = 1, #t
|
||||
|
||||
if (x < 1) then
|
||||
colorStr = sub(colorStr, 1 - x + 1, width - x + 1)
|
||||
elseif (nEnd > width) then
|
||||
colorStr = sub(colorStr, 1, width - x + 1)
|
||||
if x < 1 then
|
||||
startN = 1 - x + 1
|
||||
endN = width - x + 1
|
||||
elseif x + #t > width then
|
||||
endN = width - x + 1
|
||||
end
|
||||
|
||||
if (x > 1) then
|
||||
newCache = sub(oldCache, 1, x - 1) .. colorStr
|
||||
else
|
||||
newCache = colorStr
|
||||
newCacheT = sub(oldCacheT, 1, x - 1) .. sub(t, startN, endN)
|
||||
|
||||
if x + #t <= width then
|
||||
newCacheT = newCacheT .. sub(oldCacheT, x + #t, width)
|
||||
end
|
||||
if nEnd < width then
|
||||
newCache = newCache .. sub(oldCache, nEnd + 1, width)
|
||||
end
|
||||
cacheBG[y] = newCache
|
||||
|
||||
cacheT[y] = newCacheT
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function setBG(x, y, bg)
|
||||
if y >= 1 and y <= height then
|
||||
if x + #bg > 0 and x <= width then
|
||||
local newCacheBG
|
||||
local oldCacheBG = cacheBG[y]
|
||||
local startN, endN = 1, #bg
|
||||
|
||||
if x < 1 then
|
||||
startN = 1 - x + 1
|
||||
endN = width - x + 1
|
||||
elseif x + #bg > width then
|
||||
endN = width - x + 1
|
||||
end
|
||||
|
||||
newCacheBG = sub(oldCacheBG, 1, x - 1) .. sub(bg, startN, endN)
|
||||
|
||||
if x + #bg <= width then
|
||||
newCacheBG = newCacheBG .. sub(oldCacheBG, x + #bg, width)
|
||||
end
|
||||
|
||||
cacheBG[y] = newCacheBG
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function setFG(x, y, fg)
|
||||
if y >= 1 and y <= height then
|
||||
if x + #fg > 0 and x <= width then
|
||||
local newCacheFG
|
||||
local oldCacheFG = cacheFG[y]
|
||||
local startN, endN = 1, #fg
|
||||
|
||||
if x < 1 then
|
||||
startN = 1 - x + 1
|
||||
endN = width - x + 1
|
||||
elseif x + #fg > width then
|
||||
endN = width - x + 1
|
||||
end
|
||||
|
||||
newCacheFG = sub(oldCacheFG, 1, x - 1) .. sub(fg, startN, endN)
|
||||
|
||||
if x + #fg <= width then
|
||||
newCacheFG = newCacheFG .. sub(oldCacheFG, x + #fg, width)
|
||||
end
|
||||
|
||||
cacheFG[y] = newCacheFG
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
local function setText(x, y, text)
|
||||
if (y >= 1) and (y <= height) then
|
||||
local emptyLine = rep(" ", #text)
|
||||
blit(x, y, text, emptyLine, emptyLine)
|
||||
end
|
||||
end
|
||||
|
||||
local function setFG(x, y, colorStr)
|
||||
if (y >= 1) and (y <= height) then
|
||||
if (x + colorStr:len() > 0) and (x <= width) then
|
||||
local oldCache = cacheFG[y]
|
||||
local newCache
|
||||
local nEnd = x + #colorStr - 1
|
||||
|
||||
if (x < 1) then
|
||||
local startN = 1 - x + 1
|
||||
local endN = width - x + 1
|
||||
colorStr = sub(colorStr, startN, endN)
|
||||
elseif (nEnd > width) then
|
||||
local endN = width - x + 1
|
||||
colorStr = sub(colorStr, 1, endN)
|
||||
end
|
||||
|
||||
if (x > 1) then
|
||||
local endN = x - 1
|
||||
newCache = sub(oldCache, 1, endN) .. colorStr
|
||||
else
|
||||
newCache = colorStr
|
||||
end
|
||||
if nEnd < width then
|
||||
newCache = newCache .. sub(oldCache, nEnd + 1, width)
|
||||
end
|
||||
cacheFG[y] = newCache
|
||||
end
|
||||
local w = #colorStr
|
||||
local emptyLine = rep(" ", w)
|
||||
local text = sub(cacheT[y], x, w)
|
||||
blit(x, y, text, colorStr, emptyLine)
|
||||
end
|
||||
end
|
||||
|
||||
local function setBG(x, y, colorStr)
|
||||
if (y >= 1) and (y <= height) then
|
||||
local w = #colorStr
|
||||
local emptyLine = rep(" ", w)
|
||||
local text = sub(cacheT[y], x, w)
|
||||
blit(x, y, text, emptyLine, colorStr)
|
||||
end
|
||||
end]]
|
||||
|
||||
local drawHelper = {
|
||||
setSize = function(w, h)
|
||||
width, height = w, h
|
||||
@@ -135,44 +179,41 @@ return function(drawTerm)
|
||||
setMirror = function(mirror)
|
||||
mirrorTerm = mirror
|
||||
end,
|
||||
|
||||
setBG = function(x, y, colorStr)
|
||||
setBG(x, y, colorStr)
|
||||
end;
|
||||
end,
|
||||
|
||||
setText = function(x, y, text)
|
||||
setText(x, y, text)
|
||||
end;
|
||||
end,
|
||||
|
||||
setFG = function(x, y, colorStr)
|
||||
setFG(x, y, colorStr)
|
||||
end;
|
||||
|
||||
blit = function(x, y, t, fg, bg)
|
||||
blit(x, y, t, fg, bg)
|
||||
end,
|
||||
|
||||
drawBackgroundBox = function(x, y, width, height, bgCol)
|
||||
local colorStr = rep(tHex[bgCol], width)
|
||||
for n = 1, height do
|
||||
setBG(x, y + (n - 1), rep(tHex[bgCol], width))
|
||||
setBG(x, y + (n - 1), colorStr)
|
||||
end
|
||||
end;
|
||||
end,
|
||||
drawForegroundBox = function(x, y, width, height, fgCol)
|
||||
local colorStr = rep(tHex[fgCol], width)
|
||||
for n = 1, height do
|
||||
setFG(x, y + (n - 1) ,rep(tHex[fgCol], width))
|
||||
setFG(x, y + (n - 1), colorStr)
|
||||
end
|
||||
end;
|
||||
end,
|
||||
drawTextBox = function(x, y, width, height, symbol)
|
||||
local textStr = rep(symbol, width)
|
||||
for n = 1, height do
|
||||
setText(x, y + (n - 1), rep(symbol, width))
|
||||
setText(x, y + (n - 1), textStr)
|
||||
end
|
||||
end;
|
||||
writeText = function(x, y, text, bgCol, fgCol)
|
||||
if(text~=nil)then
|
||||
setText(x, y, text)
|
||||
if(bgCol~=nil)and(bgCol~=false)then
|
||||
setBG(x, y, rep(tHex[bgCol], text:len()))
|
||||
end
|
||||
if(fgCol~=nil)and(fgCol~=false)then
|
||||
setFG(x, y, rep(tHex[fgCol], text:len()))
|
||||
end
|
||||
end
|
||||
end;
|
||||
end,
|
||||
|
||||
update = function()
|
||||
local xC, yC = terminal.getCursorPos()
|
||||
@@ -199,11 +240,11 @@ return function(drawTerm)
|
||||
mirrorTerm.setCursorPos(xC, yC)
|
||||
end
|
||||
|
||||
end;
|
||||
end,
|
||||
|
||||
setTerm = function(newTerm)
|
||||
terminal = newTerm;
|
||||
end;
|
||||
terminal = newTerm
|
||||
end,
|
||||
}
|
||||
return drawHelper
|
||||
end
|
||||
@@ -1,21 +1,41 @@
|
||||
return function()
|
||||
local events = {}
|
||||
local index = {}
|
||||
|
||||
local event = {
|
||||
registerEvent = function(self, _event, func)
|
||||
if (events[_event] == nil) then
|
||||
events[_event] = {}
|
||||
index[_event] = 1
|
||||
end
|
||||
events[_event][index[_event]] = func
|
||||
index[_event] = index[_event] + 1
|
||||
return index[_event] - 1
|
||||
end;
|
||||
table.insert(events[_event], func)
|
||||
end,
|
||||
|
||||
removeEvent = function(self, _event, index)
|
||||
events[_event][index[_event]] = nil
|
||||
end;
|
||||
end,
|
||||
|
||||
hasEvent = function(self, _event)
|
||||
return events[_event]~=nil
|
||||
end,
|
||||
|
||||
getEventCount = function(self, _event)
|
||||
return events[_event]~=nil and #events[_event] or 0
|
||||
end,
|
||||
|
||||
getEvents = function(self)
|
||||
local t = {}
|
||||
for k,v in pairs(events)do
|
||||
table.insert(t, k)
|
||||
end
|
||||
return t
|
||||
end,
|
||||
|
||||
clearEvent = function(self, _event)
|
||||
events[_event] = nil
|
||||
end,
|
||||
|
||||
clear = function(self, _event)
|
||||
events = {}
|
||||
end,
|
||||
|
||||
sendEvent = function(self, _event, ...)
|
||||
local returnValue
|
||||
@@ -28,7 +48,7 @@ return function()
|
||||
end
|
||||
end
|
||||
return returnValue
|
||||
end;
|
||||
end,
|
||||
}
|
||||
event.__index = event
|
||||
return event
|
||||
|
||||
@@ -10,7 +10,7 @@ local mt = {
|
||||
if(text==nil)then return end
|
||||
local dirStr = logDir~="" and logDir.."/"..logFileName or logFileName
|
||||
local handle = fs.open(dirStr, fs.exists(dirStr) and "a" or "w")
|
||||
handle.writeLine("[Basalt]["..(typ and typ or defaultLogType).."]: "..tostring(text))
|
||||
handle.writeLine("[Basalt]["..os.date("%Y-%m-%d %H:%M:%S").."]["..(typ and typ or defaultLogType).."]: "..tostring(text))
|
||||
handle.close()
|
||||
end,
|
||||
}
|
||||
|
||||
390
Basalt/libraries/bimg.lua
Normal file
@@ -0,0 +1,390 @@
|
||||
local sub,rep = string.sub,string.rep
|
||||
|
||||
local function frame(base, manager)
|
||||
local w, h = 0, 0
|
||||
local t,fg,bg = {}, {}, {}
|
||||
local x, y = 1,1
|
||||
|
||||
local data = {}
|
||||
|
||||
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
|
||||
manager.updateSize(w, h)
|
||||
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
|
||||
manager.updateSize(w, h)
|
||||
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
|
||||
manager.updateSize(w, h)
|
||||
end
|
||||
|
||||
local function setFrame(frm)
|
||||
data = {}
|
||||
t, fg, bg = {}, {}, {}
|
||||
for k,v in pairs(base)do
|
||||
if(type(k)=="string")then
|
||||
data[k] = v
|
||||
else
|
||||
t[k], fg[k], bg[k] = v[1], v[2], v[3]
|
||||
end
|
||||
end
|
||||
manager.updateSize(w, h)
|
||||
end
|
||||
|
||||
if(base~=nil)then
|
||||
if(#base>0)then
|
||||
w = #base[1][1]
|
||||
h = #base
|
||||
setFrame(base)
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
recalculateSize = recalculateSize,
|
||||
setFrame = setFrame,
|
||||
|
||||
getFrame = function()
|
||||
local f = {}
|
||||
|
||||
for k,v in pairs(t)do
|
||||
table.insert(f, {v, fg[k], bg[k]})
|
||||
end
|
||||
|
||||
for k,v in pairs(data)do
|
||||
f[k] = v
|
||||
end
|
||||
|
||||
return f, w, h
|
||||
end,
|
||||
|
||||
getImage = function()
|
||||
local i = {}
|
||||
for k,v in pairs(t)do
|
||||
table.insert(i, {v, fg[k], bg[k]})
|
||||
end
|
||||
return i
|
||||
end,
|
||||
|
||||
setFrameData = function(key, value)
|
||||
if(value~=nil)then
|
||||
data[key] = value
|
||||
else
|
||||
if(type(key)=="table")then
|
||||
data = key
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
setFrameImage = function(imgData)
|
||||
for k,v in pairs(imgData.t)do
|
||||
t[k] = imgData.t[k]
|
||||
fg[k] = imgData.fg[k]
|
||||
bg[k] = imgData.bg[k]
|
||||
end
|
||||
end,
|
||||
|
||||
getFrameImage = function()
|
||||
return {t = t, fg = fg, bg = bg}
|
||||
end,
|
||||
|
||||
getFrameData = function(key)
|
||||
if(key~=nil)then
|
||||
return data[key]
|
||||
else
|
||||
return data
|
||||
end
|
||||
end,
|
||||
|
||||
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,
|
||||
}
|
||||
end
|
||||
|
||||
return function(img)
|
||||
local frames = {}
|
||||
local metadata = {creator="Bimg Library by NyoriE", date=os.date("!%Y-%m-%dT%TZ")}
|
||||
local width,height = 0, 0
|
||||
|
||||
if(img~=nil)then
|
||||
if(img[1][1][1]~=nil)then
|
||||
width,height = metadata.width or #img[1][1][1], metadata.height or #img[1]
|
||||
end
|
||||
end
|
||||
|
||||
local manager = {}
|
||||
|
||||
local function addFrame(id, data)
|
||||
id = id or #frames+1
|
||||
local f = frame(data, manager)
|
||||
table.insert(frames, id, f)
|
||||
if(data==nil)then
|
||||
frames[id].setSize(width, height)
|
||||
end
|
||||
return f
|
||||
end
|
||||
|
||||
local function removeFrame(id)
|
||||
table.remove(frames, id or #frames)
|
||||
end
|
||||
|
||||
local function moveFrame(id, dir)
|
||||
local f = frames[id]
|
||||
if(f~=nil)then
|
||||
local newId = id+dir
|
||||
if(newId>=1)and(newId<=#frames)then
|
||||
table.remove(frames, id)
|
||||
table.insert(frames, newId, f)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
manager = {
|
||||
updateSize = function(w, h, force)
|
||||
local changed = force==true and true or false
|
||||
if(w > width)then changed = true width = w end
|
||||
if(h > height)then changed = true height = h end
|
||||
if(changed)then
|
||||
for k,v in pairs(frames)do
|
||||
v.setSize(width, height)
|
||||
v.recalculateSize()
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
text = function(frame, text, x, y)
|
||||
local f = frames[frame]
|
||||
if(f==nil)then
|
||||
f = addFrame(frame)
|
||||
end
|
||||
f.text(text, x, y)
|
||||
end,
|
||||
|
||||
fg = function(frame, fg, x, y)
|
||||
local f = frames[frame]
|
||||
if(f==nil)then
|
||||
f = addFrame(frame)
|
||||
end
|
||||
f.fg(fg, x, y)
|
||||
end,
|
||||
|
||||
bg = function(frame, bg, x, y)
|
||||
local f = frames[frame]
|
||||
if(f==nil)then
|
||||
f = addFrame(frame)
|
||||
end
|
||||
f.bg(bg, x, y)
|
||||
end,
|
||||
|
||||
blit = function(frame, text, fg, bg, x, y)
|
||||
local f = frames[frame]
|
||||
if(f==nil)then
|
||||
f = addFrame(frame)
|
||||
end
|
||||
f.blit(text, fg, bg, x, y)
|
||||
end,
|
||||
|
||||
setSize = function(w, h)
|
||||
width = w
|
||||
height = h
|
||||
for k,v in pairs(frames)do
|
||||
v.setSize(w, h)
|
||||
end
|
||||
end,
|
||||
|
||||
getFrame = function(id)
|
||||
if(frames[id]~=nil)then
|
||||
return frames[id].getFrame()
|
||||
end
|
||||
end,
|
||||
|
||||
getFrameObjects = function()
|
||||
return frames
|
||||
end,
|
||||
|
||||
getFrames = function()
|
||||
local f = {}
|
||||
for k,v in pairs(frames)do
|
||||
local frame = v.getFrame()
|
||||
table.insert(f, frame)
|
||||
end
|
||||
return f
|
||||
end,
|
||||
|
||||
getFrameObject = function(id)
|
||||
return frames[id]
|
||||
end,
|
||||
|
||||
addFrame = function(id)
|
||||
if(#frames<=1)then
|
||||
if(metadata.animated==nil)then
|
||||
metadata.animated = true
|
||||
end
|
||||
if(metadata.secondsPerFrame==nil)then
|
||||
metadata.secondsPerFrame = 0.2
|
||||
end
|
||||
end
|
||||
return addFrame(id)
|
||||
end,
|
||||
|
||||
removeFrame = removeFrame,
|
||||
|
||||
moveFrame = moveFrame,
|
||||
|
||||
setFrameData = function(id, key, value)
|
||||
if(frames[id]~=nil)then
|
||||
frames[id].setFrameData(key, value)
|
||||
end
|
||||
end,
|
||||
|
||||
getFrameData = function(id, key)
|
||||
if(frames[id]~=nil)then
|
||||
return frames[id].getFrameData(key)
|
||||
end
|
||||
end,
|
||||
|
||||
getSize = function()
|
||||
return width, height
|
||||
end,
|
||||
|
||||
setAnimation = function(anim)
|
||||
metadata.animation = anim
|
||||
end,
|
||||
|
||||
setMetadata = function(key, val)
|
||||
if(val~=nil)then
|
||||
metadata[key] = val
|
||||
else
|
||||
if(type(key)=="table")then
|
||||
metadata = key
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
getMetadata = function(key)
|
||||
if(key~=nil)then
|
||||
return metadata[key]
|
||||
else
|
||||
return metadata
|
||||
end
|
||||
end,
|
||||
|
||||
createBimg = function()
|
||||
local bimg = {}
|
||||
for k,v in pairs(frames)do
|
||||
local f = v.getFrame()
|
||||
table.insert(bimg, f)
|
||||
end
|
||||
for k,v in pairs(metadata)do
|
||||
bimg[k] = v
|
||||
end
|
||||
bimg.width = width
|
||||
bimg.height = height
|
||||
return bimg
|
||||
end,
|
||||
}
|
||||
|
||||
if(img~=nil)then
|
||||
for k,v in pairs(img)do
|
||||
if(type(k)=="string")then
|
||||
metadata[k] = v
|
||||
end
|
||||
end
|
||||
if(metadata.width==nil)or(metadata.height==nil)then
|
||||
width = metadata.width or #img[1][1][1]
|
||||
height = metadata.height or #img[1]
|
||||
manager.updateSize(width, height, true)
|
||||
end
|
||||
|
||||
for k,v in pairs(img)do
|
||||
if(type(k)=="number")then
|
||||
addFrame(k, v)
|
||||
end
|
||||
end
|
||||
else
|
||||
addFrame(1)
|
||||
end
|
||||
|
||||
return manager
|
||||
end
|
||||
87
Basalt/libraries/images.lua
Normal file
@@ -0,0 +1,87 @@
|
||||
local sub,floor = string.sub,math.floor
|
||||
|
||||
local function loadNFPAsBimg(path)
|
||||
return {[1]={{}, {}, paintutils.loadImage(path)}}, "bimg"
|
||||
end
|
||||
|
||||
local function loadNFP(path)
|
||||
return paintutils.loadImage(path), "nfp"
|
||||
end
|
||||
|
||||
local function loadBIMG(path, binaryMode)
|
||||
local f = fs.open(path, binaryMode and "rb" or "r")
|
||||
if(f==nil)then error("Path - "..path.." doesn't exist!") end
|
||||
local content = textutils.unserialize(f.readAll())
|
||||
|
||||
f.close()
|
||||
if(content~=nil)then
|
||||
return content, "bimg"
|
||||
end
|
||||
end
|
||||
|
||||
local function loadBBF(path)
|
||||
|
||||
end
|
||||
|
||||
local function loadBBFAsBimg(path)
|
||||
|
||||
end
|
||||
|
||||
local function loadImage(path, f, binaryMode)
|
||||
if(sub(path, -4) == ".bimg")then
|
||||
return loadBIMG(path, binaryMode)
|
||||
elseif(sub(path, -3) == ".bbf")then
|
||||
return loadBBF(path, binaryMode)
|
||||
else
|
||||
return loadNFP(path, binaryMode)
|
||||
end
|
||||
-- ...
|
||||
end
|
||||
|
||||
local function loadImageAsBimg(path)
|
||||
if(path:find(".bimg"))then
|
||||
return loadBIMG(path)
|
||||
elseif(path:find(".bbf"))then
|
||||
return loadBBFAsBimg(path)
|
||||
else
|
||||
return loadNFPAsBimg(path)
|
||||
end
|
||||
end
|
||||
|
||||
local function resizeBIMG(source, w, h)
|
||||
local oW, oH = source.width or #source[1][1][1], source.height or #source[1]
|
||||
local newImg = {}
|
||||
for k,v in pairs(source)do
|
||||
if(type(k)=="number")then
|
||||
local frame = {}
|
||||
for y=1, h do
|
||||
local xT,xFG,xBG = "","",""
|
||||
local yR = floor(y / h * oH + 0.5)
|
||||
if(v[yR]~=nil)then
|
||||
for x=1, w do
|
||||
local xR = floor(x / w * oW + 0.5)
|
||||
xT = xT..sub(v[yR][1], xR,xR)
|
||||
xFG = xFG..sub(v[yR][2], xR,xR)
|
||||
xBG = xBG..sub(v[yR][3], xR,xR)
|
||||
end
|
||||
table.insert(frame, {xT, xFG, xBG})
|
||||
end
|
||||
end
|
||||
table.insert(newImg, k, frame)
|
||||
else
|
||||
newImg[k] = v
|
||||
end
|
||||
end
|
||||
newImg.width = w
|
||||
newImg.height = h
|
||||
return newImg
|
||||
end
|
||||
|
||||
return {
|
||||
loadNFP = loadNFP,
|
||||
loadBIMG = loadBIMG,
|
||||
loadImage = loadImage,
|
||||
resizeBIMG = resizeBIMG,
|
||||
loadImageAsBimg = loadImageAsBimg,
|
||||
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
local function newNode(name)
|
||||
local node = {}
|
||||
node.___value = nil
|
||||
node.___name = name
|
||||
node.___children = {}
|
||||
node.___props = {}
|
||||
|
||||
function node:value() return self.___value end
|
||||
function node:setValue(val) self.___value = val end
|
||||
function node:name() return self.___name end
|
||||
function node:setName(name) self.___name = name end
|
||||
function node:children() return self.___children end
|
||||
function node:numChildren() return #self.___children end
|
||||
function node:addChild(child)
|
||||
if self[child:name()] ~= nil then
|
||||
if type(self[child:name()].name) == "function" then
|
||||
local tempTable = {}
|
||||
table.insert(tempTable, self[child:name()])
|
||||
self[child:name()] = tempTable
|
||||
end
|
||||
table.insert(self[child:name()], child)
|
||||
else
|
||||
self[child:name()] = child
|
||||
end
|
||||
table.insert(self.___children, child)
|
||||
end
|
||||
|
||||
function node:properties() return self.___props end
|
||||
function node:numProperties() return #self.___props end
|
||||
function node:addProperty(name, value)
|
||||
local lName = "@" .. name
|
||||
if self[lName] ~= nil then
|
||||
if type(self[lName]) == "string" then
|
||||
local tempTable = {}
|
||||
table.insert(tempTable, self[lName])
|
||||
self[lName] = tempTable
|
||||
end
|
||||
table.insert(self[lName], value)
|
||||
else
|
||||
self[lName] = value
|
||||
end
|
||||
table.insert(self.___props, { name = name, value = self[name] })
|
||||
end
|
||||
|
||||
return node
|
||||
end
|
||||
|
||||
local XmlParser = {};
|
||||
|
||||
function XmlParser:ToXmlString(value)
|
||||
value = string.gsub(value, "&", "&"); -- '&' -> "&"
|
||||
value = string.gsub(value, "<", "<"); -- '<' -> "<"
|
||||
value = string.gsub(value, ">", ">"); -- '>' -> ">"
|
||||
value = string.gsub(value, "\"", """); -- '"' -> """
|
||||
value = string.gsub(value, "([^%w%&%;%p%\t% ])",
|
||||
function(c)
|
||||
return string.format("&#x%X;", string.byte(c))
|
||||
end);
|
||||
return value;
|
||||
end
|
||||
|
||||
function XmlParser:FromXmlString(value)
|
||||
value = string.gsub(value, "&#x([%x]+)%;",
|
||||
function(h)
|
||||
return string.char(tonumber(h, 16))
|
||||
end);
|
||||
value = string.gsub(value, "&#([0-9]+)%;",
|
||||
function(h)
|
||||
return string.char(tonumber(h, 10))
|
||||
end);
|
||||
value = string.gsub(value, """, "\"");
|
||||
value = string.gsub(value, "'", "'");
|
||||
value = string.gsub(value, ">", ">");
|
||||
value = string.gsub(value, "<", "<");
|
||||
value = string.gsub(value, "&", "&");
|
||||
return value;
|
||||
end
|
||||
|
||||
function XmlParser:ParseArgs(node, s)
|
||||
string.gsub(s, "(%w+)=([\"'])(.-)%2", function(w, _, a)
|
||||
node:addProperty(w, self:FromXmlString(a))
|
||||
end)
|
||||
end
|
||||
|
||||
function XmlParser:ParseXmlText(xmlText)
|
||||
local stack = {}
|
||||
local top = newNode()
|
||||
table.insert(stack, top)
|
||||
local ni, c, label, xarg, empty
|
||||
local i, j = 1, 1
|
||||
while true do
|
||||
ni, j, c, label, xarg, empty = string.find(xmlText, "<(%/?)([%w_:]+)(.-)(%/?)>", i)
|
||||
if not ni then break end
|
||||
local text = string.sub(xmlText, i, ni - 1);
|
||||
if not string.find(text, "^%s*$") then
|
||||
local lVal = (top:value() or "") .. self:FromXmlString(text)
|
||||
stack[#stack]:setValue(lVal)
|
||||
end
|
||||
if empty == "/" then -- empty element tag
|
||||
local lNode = newNode(label)
|
||||
self:ParseArgs(lNode, xarg)
|
||||
top:addChild(lNode)
|
||||
elseif c == "" then -- start tag
|
||||
local lNode = newNode(label)
|
||||
self:ParseArgs(lNode, xarg)
|
||||
table.insert(stack, lNode)
|
||||
top = lNode
|
||||
else -- end tag
|
||||
local toclose = table.remove(stack) -- remove top
|
||||
|
||||
top = stack[#stack]
|
||||
if #stack < 1 then
|
||||
error("XmlParser: nothing to close with " .. label)
|
||||
end
|
||||
if toclose:name() ~= label then
|
||||
error("XmlParser: trying to close " .. toclose.name .. " with " .. label)
|
||||
end
|
||||
top:addChild(toclose)
|
||||
end
|
||||
i = j + 1
|
||||
end
|
||||
local text = string.sub(xmlText, i);
|
||||
if #stack > 1 then
|
||||
error("XmlParser: unclosed " .. stack[#stack]:name())
|
||||
end
|
||||
return top
|
||||
end
|
||||
|
||||
function XmlParser:loadFile(xmlFilename, base)
|
||||
if not base then
|
||||
base = system.ResourceDirectory
|
||||
end
|
||||
|
||||
local path = system.pathForFile(xmlFilename, base)
|
||||
local hFile, err = io.open(path, "r");
|
||||
|
||||
if hFile and not err then
|
||||
local xmlText = hFile:read("*a"); -- read file content
|
||||
io.close(hFile);
|
||||
return self:ParseXmlText(xmlText), nil;
|
||||
else
|
||||
print(err)
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
return XmlParser
|
||||
@@ -1,4 +0,0 @@
|
||||
return function(path)
|
||||
local exists, content = pcall(require, path)
|
||||
return exists and content or nil
|
||||
end
|
||||
@@ -2,14 +2,35 @@ local processes = {}
|
||||
local process = {}
|
||||
local processId = 0
|
||||
|
||||
function process:new(path, window, ...)
|
||||
local newPackage = dofile("rom/modules/main/cc/require.lua").make
|
||||
|
||||
function process:new(path, window, newEnv, ...)
|
||||
local args = {...}
|
||||
local newP = setmetatable({ path = path }, { __index = self })
|
||||
newP.window = window
|
||||
window.current = term.current
|
||||
window.redirect = term.redirect
|
||||
newP.processId = processId
|
||||
if(type(path)=="string")then
|
||||
newP.coroutine = coroutine.create(function()
|
||||
shell.execute(path, table.unpack(args))
|
||||
local pPath = shell.resolveProgram(path)
|
||||
local env = setmetatable(newEnv, {__index=_ENV})
|
||||
env.shell = shell
|
||||
env.basaltProgram=true
|
||||
env.arg = {[0]=path, table.unpack(args)}
|
||||
if(pPath==nil)then
|
||||
error("The path "..path.." does not exist!")
|
||||
end
|
||||
env.require, env.package = newPackage(env, fs.getDir(pPath))
|
||||
if(fs.exists(pPath))then
|
||||
local file = fs.open(pPath, "r")
|
||||
local content = file.readAll()
|
||||
file.close()
|
||||
local program = load(content, path, "bt", env)
|
||||
if(program~=nil)then
|
||||
return program()
|
||||
end
|
||||
end
|
||||
end)
|
||||
elseif(type(path)=="function")then
|
||||
newP.coroutine = coroutine.create(function()
|
||||
@@ -24,17 +45,21 @@ function process:new(path, window, ...)
|
||||
end
|
||||
|
||||
function process:resume(event, ...)
|
||||
local cur = term.current()
|
||||
term.redirect(self.window)
|
||||
if(self.filter~=nil)then
|
||||
if(event~=self.filter)then return end
|
||||
self.filter=nil
|
||||
end
|
||||
local ok, result = coroutine.resume(self.coroutine, event, ...)
|
||||
|
||||
if ok then
|
||||
self.filter = result
|
||||
else
|
||||
error(result)
|
||||
printError(result)
|
||||
end
|
||||
term.redirect(cur)
|
||||
return ok, result
|
||||
end
|
||||
|
||||
function process:isDead()
|
||||
|
||||
165
Basalt/libraries/reactivePrimitives.lua
Normal file
@@ -0,0 +1,165 @@
|
||||
local NodeStatus = {
|
||||
CURRENT = 0,
|
||||
STALE = 1,
|
||||
MAYBE_STALE = 2
|
||||
}
|
||||
|
||||
local Node = {}
|
||||
|
||||
Node.new = function()
|
||||
return {
|
||||
fn = nil,
|
||||
value = nil,
|
||||
status = NodeStatus.STALE,
|
||||
parents = {},
|
||||
children = {},
|
||||
|
||||
cleanup = function(self)
|
||||
for _, parentNode in ipairs(self.parents) do
|
||||
for index, childNode in ipairs(parentNode.children) do
|
||||
if (childNode == self) then
|
||||
table.remove(parentNode.children, index)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
self.parents = {}
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
local ReactiveState = {
|
||||
listeningNode = nil,
|
||||
sourceNodes = {},
|
||||
effectNodes = {},
|
||||
transaction = false
|
||||
}
|
||||
|
||||
local Reactive = {}
|
||||
|
||||
Reactive.pushUpdates = function()
|
||||
for _, sourceNode in ipairs(ReactiveState.sourceNodes) do
|
||||
Reactive.pushSourceNodeUpdate(sourceNode)
|
||||
end
|
||||
Reactive.pullUpdates()
|
||||
end
|
||||
|
||||
Reactive.pushSourceNodeUpdate = function(sourceNode)
|
||||
if (sourceNode.status == NodeStatus.CURRENT) then
|
||||
return
|
||||
end
|
||||
Reactive.pushNodeUpdate(sourceNode)
|
||||
for _, childNode in ipairs(sourceNode.children) do
|
||||
childNode.status = NodeStatus.STALE
|
||||
end
|
||||
sourceNode.status = NodeStatus.CURRENT
|
||||
end
|
||||
|
||||
Reactive.pushNodeUpdate = function(node)
|
||||
if (node == nil) then
|
||||
return
|
||||
end
|
||||
node.status = NodeStatus.MAYBE_STALE
|
||||
for _, childNode in ipairs(node.children) do
|
||||
Reactive.pushNodeUpdate(childNode)
|
||||
end
|
||||
end
|
||||
|
||||
Reactive.pullUpdates = function()
|
||||
for _, effectNode in ipairs(ReactiveState.effectNodes) do
|
||||
Reactive.pullNodeUpdates(effectNode)
|
||||
end
|
||||
end
|
||||
|
||||
Reactive.pullNodeUpdates = function(node)
|
||||
if (node.status == NodeStatus.CURRENT) then
|
||||
return
|
||||
end
|
||||
if (node.status == NodeStatus.MAYBE_STALE) then
|
||||
for _, parentNode in ipairs(node.parents) do
|
||||
Reactive.pullNodeUpdates(parentNode)
|
||||
end
|
||||
end
|
||||
if (node.status == NodeStatus.STALE) then
|
||||
node:cleanup()
|
||||
local prevListeningNode = ReactiveState.listeningNode
|
||||
ReactiveState.listeningNode = node
|
||||
local oldValue = node.value
|
||||
node.value = node.fn()
|
||||
ReactiveState.listeningNode = prevListeningNode
|
||||
for _, childNode in ipairs(node.children) do
|
||||
if (oldValue == node.value) then
|
||||
childNode.status = NodeStatus.CURRENT
|
||||
else
|
||||
childNode.status = NodeStatus.STALE
|
||||
end
|
||||
end
|
||||
end
|
||||
node.status = NodeStatus.CURRENT
|
||||
end
|
||||
|
||||
Reactive.subscribe = function(node)
|
||||
local listeningNode = ReactiveState.listeningNode
|
||||
if (listeningNode ~= nil) then
|
||||
table.insert(node.children, listeningNode)
|
||||
table.insert(listeningNode.parents, node)
|
||||
end
|
||||
end
|
||||
|
||||
Reactive.observable = function(initialValue)
|
||||
local node = Node.new()
|
||||
node.value = initialValue
|
||||
node.status = NodeStatus.CURRENT
|
||||
local get = function()
|
||||
Reactive.subscribe(node)
|
||||
return node.value
|
||||
end
|
||||
local set = function(newValue)
|
||||
if (node.value == newValue) then
|
||||
return
|
||||
end
|
||||
node.value = newValue
|
||||
node.status = ReactiveState.STALE
|
||||
if (not ReactiveState.transaction) then
|
||||
Reactive.pushUpdates()
|
||||
end
|
||||
end
|
||||
table.insert(ReactiveState.sourceNodes, node)
|
||||
return get, set
|
||||
end
|
||||
|
||||
Reactive.derived = function(fn)
|
||||
local node = Node.new()
|
||||
node.fn = fn
|
||||
return function()
|
||||
if (node.status ~= NodeStatus.CURRENT) then
|
||||
Reactive.pullNodeUpdates(node)
|
||||
end
|
||||
Reactive.subscribe(node)
|
||||
return node.value
|
||||
end
|
||||
end
|
||||
|
||||
Reactive.effect = function(fn)
|
||||
local node = Node.new()
|
||||
node.fn = fn
|
||||
table.insert(ReactiveState.effectNodes, node)
|
||||
Reactive.pushUpdates()
|
||||
end
|
||||
|
||||
Reactive.transaction = function(fn)
|
||||
ReactiveState.transaction = true
|
||||
fn()
|
||||
ReactiveState.transaction = false
|
||||
Reactive.pushUpdates()
|
||||
end
|
||||
|
||||
Reactive.untracked = function(fn)
|
||||
local prevListeningNode = ReactiveState.listeningNode
|
||||
ReactiveState.listeningNode = nil
|
||||
local value = fn()
|
||||
ReactiveState.listeningNode = prevListeningNode
|
||||
return value
|
||||
end
|
||||
|
||||
return Reactive
|
||||
@@ -1,18 +1,6 @@
|
||||
return { -- copy paste is a very important feature
|
||||
[colors.white] = "0",
|
||||
[colors.orange] = "1",
|
||||
[colors.magenta] = "2",
|
||||
[colors.lightBlue] = "3",
|
||||
[colors.yellow] = "4",
|
||||
[colors.lime] = "5",
|
||||
[colors.pink] = "6",
|
||||
[colors.gray] = "7",
|
||||
[colors.lightGray] = "8",
|
||||
[colors.cyan] = "9",
|
||||
[colors.purple] = "a",
|
||||
[colors.blue] = "b",
|
||||
[colors.brown] = "c",
|
||||
[colors.green] = "d",
|
||||
[colors.red] = "e",
|
||||
[colors.black] = "f",
|
||||
}
|
||||
local cols = {}
|
||||
|
||||
for i = 0, 15 do
|
||||
cols[2^i] = ("%x"):format(i)
|
||||
end
|
||||
return cols
|
||||
@@ -1,25 +1,227 @@
|
||||
local splitString = function(str, sep)
|
||||
if sep == nil then
|
||||
sep = "%s"
|
||||
local tHex = require("tHex")
|
||||
local sub,find,reverse,rep,insert,len = string.sub,string.find,string.reverse,string.rep,table.insert,string.len
|
||||
|
||||
local function splitString(str, delimiter)
|
||||
local results = {}
|
||||
if str == "" or delimiter == "" then
|
||||
return results
|
||||
end
|
||||
local t={}
|
||||
for v in string.gmatch(str, "([^"..sep.."]+)") do
|
||||
table.insert(t, v)
|
||||
end
|
||||
return t
|
||||
local start = 1
|
||||
local delim_start, delim_end = find(str, delimiter, start)
|
||||
while delim_start do
|
||||
insert(results, sub(str, start, delim_start - 1))
|
||||
start = delim_end + 1
|
||||
delim_start, delim_end = find(str, delimiter, start)
|
||||
end
|
||||
insert(results, sub(str, start))
|
||||
return results
|
||||
end
|
||||
|
||||
local function removeTags(input)
|
||||
return input:gsub("{[^}]+}", "")
|
||||
end
|
||||
|
||||
|
||||
local function wrapText(str, width)
|
||||
str = removeTags(str)
|
||||
if(str=="")or(width==0)then
|
||||
return {""}
|
||||
end
|
||||
local uniqueLines = splitString(str, "\n")
|
||||
local result = {}
|
||||
for k, v in pairs(uniqueLines) do
|
||||
if #v == 0 then
|
||||
table.insert(result, "")
|
||||
else
|
||||
while #v > width do
|
||||
local last_space = width
|
||||
for i = width, 1, -1 do
|
||||
if sub(v, i, i) == " " then
|
||||
last_space = i
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if last_space == width then
|
||||
local line = sub(v, 1, last_space - 1) .. "-"
|
||||
table.insert(result, line)
|
||||
v = sub(v, last_space)
|
||||
else
|
||||
local line = sub(v, 1, last_space - 1)
|
||||
table.insert(result, line)
|
||||
v = sub(v, last_space + 1)
|
||||
end
|
||||
|
||||
if #v <= width then
|
||||
break
|
||||
end
|
||||
end
|
||||
if #v > 0 then
|
||||
table.insert(result, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
--- Coonverts a string with special tags to a table with colors and text
|
||||
-- @param input The string to convert
|
||||
-- @return A table with the following structure: { {text = "Hello", color = colors.red}, {text = "World", color = colors.blue} }
|
||||
local function convertRichText(input)
|
||||
local parsedResult = {}
|
||||
local currentPosition = 1
|
||||
local rawPosition = 1
|
||||
|
||||
while currentPosition <= #input do
|
||||
local closestColor, closestBgColor
|
||||
local color, bgColor
|
||||
local colorEnd, bgColorEnd
|
||||
|
||||
for colorName, _ in pairs(colors) do
|
||||
local fgPattern = "{fg:" .. colorName.."}"
|
||||
local bgColorPattern = "{bg:" .. colorName.."}"
|
||||
local colorStart, colorEndCandidate = input:find(fgPattern, currentPosition)
|
||||
local bgColorStart, bgColorEndCandidate = input:find(bgColorPattern, currentPosition)
|
||||
|
||||
if colorStart and (not closestColor or colorStart < closestColor) then
|
||||
closestColor = colorStart
|
||||
color = colorName
|
||||
colorEnd = colorEndCandidate
|
||||
end
|
||||
|
||||
if bgColorStart and (not closestBgColor or bgColorStart < closestBgColor) then
|
||||
closestBgColor = bgColorStart
|
||||
bgColor = colorName
|
||||
bgColorEnd = bgColorEndCandidate
|
||||
end
|
||||
end
|
||||
|
||||
local nextPosition
|
||||
if closestColor and (not closestBgColor or closestColor < closestBgColor) then
|
||||
nextPosition = closestColor
|
||||
elseif closestBgColor then
|
||||
nextPosition = closestBgColor
|
||||
else
|
||||
nextPosition = #input + 1
|
||||
end
|
||||
|
||||
local text = input:sub(currentPosition, nextPosition - 1)
|
||||
if #text > 0 then
|
||||
table.insert(parsedResult, {
|
||||
color = nil,
|
||||
bgColor = nil,
|
||||
text = text,
|
||||
position = rawPosition
|
||||
})
|
||||
rawPosition = rawPosition + #text
|
||||
currentPosition = currentPosition + #text
|
||||
end
|
||||
|
||||
if closestColor and (not closestBgColor or closestColor < closestBgColor) then
|
||||
table.insert(parsedResult, {
|
||||
color = color,
|
||||
bgColor = nil,
|
||||
text = "",
|
||||
position = rawPosition,
|
||||
})
|
||||
currentPosition = colorEnd + 1
|
||||
elseif closestBgColor then
|
||||
table.insert(parsedResult, {
|
||||
color = nil,
|
||||
bgColor = bgColor,
|
||||
text = "",
|
||||
position = rawPosition,
|
||||
})
|
||||
currentPosition = bgColorEnd + 1
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
return parsedResult
|
||||
end
|
||||
|
||||
--- Wrapts text with special color tags, like {fg:red} or {bg:blue} to multiple lines
|
||||
--- @param text string Text to wrap
|
||||
--- @param width number Width of the line
|
||||
--- @return table Table of lines
|
||||
local function wrapRichText(text, width)
|
||||
local colorData = convertRichText(text)
|
||||
local formattedLines = {}
|
||||
local x, y = 1, 1
|
||||
local currentColor, currentBgColor
|
||||
|
||||
local function addFormattedEntry(entry)
|
||||
table.insert(formattedLines, {
|
||||
x = x,
|
||||
y = y,
|
||||
text = entry.text,
|
||||
color = entry.color or currentColor,
|
||||
bgColor = entry.bgColor or currentBgColor
|
||||
})
|
||||
end
|
||||
|
||||
for _, entry in ipairs(colorData) do
|
||||
if entry.color then
|
||||
currentColor = entry.color
|
||||
elseif entry.bgColor then
|
||||
currentBgColor = entry.bgColor
|
||||
else
|
||||
local words = splitString(entry.text, " ")
|
||||
|
||||
for i, word in ipairs(words) do
|
||||
local wordLength = #word
|
||||
|
||||
if i > 1 then
|
||||
if x + 1 + wordLength <= width then
|
||||
addFormattedEntry({ text = " " })
|
||||
x = x + 1
|
||||
else
|
||||
x = 1
|
||||
y = y + 1
|
||||
end
|
||||
end
|
||||
|
||||
while wordLength > 0 do
|
||||
local line = word:sub(1, width - x + 1)
|
||||
word = word:sub(width - x + 2)
|
||||
wordLength = #word
|
||||
|
||||
addFormattedEntry({ text = line })
|
||||
|
||||
if wordLength > 0 then
|
||||
x = 1
|
||||
y = y + 1
|
||||
else
|
||||
x = x + #line
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if x > width then
|
||||
x = 1
|
||||
y = y + 1
|
||||
end
|
||||
end
|
||||
|
||||
return formattedLines
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
return {
|
||||
getTextHorizontalAlign = function(text, width, textAlign, replaceChar)
|
||||
text = string.sub(text, 1, width)
|
||||
local offset = width - string.len(text)
|
||||
text = sub(text, 1, width)
|
||||
local offset = width - len(text)
|
||||
if (textAlign == "right") then
|
||||
text = string.rep(replaceChar or " ", offset) .. text
|
||||
text = rep(replaceChar or " ", offset) .. text
|
||||
elseif (textAlign == "center") then
|
||||
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 (replaceChar or " ") or "")
|
||||
text = rep(replaceChar or " ", math.floor(offset / 2)) .. text .. rep(replaceChar or " ", math.floor(offset / 2))
|
||||
text = text .. (len(text) < width and (replaceChar or " ") or "")
|
||||
else
|
||||
text = text .. string.rep(replaceChar or " ", offset)
|
||||
text = text .. rep(replaceChar or " ", offset)
|
||||
end
|
||||
return text
|
||||
end,
|
||||
@@ -39,6 +241,14 @@ getTextVerticalAlign = function(h, textAlign)
|
||||
return offset
|
||||
end,
|
||||
|
||||
orderedTable = function(t)
|
||||
local newTable = {}
|
||||
for _, v in pairs(t) do
|
||||
newTable[#newTable+1] = v
|
||||
end
|
||||
return newTable
|
||||
end,
|
||||
|
||||
rpairs = function(t)
|
||||
return function(t, i)
|
||||
i = i - 1
|
||||
@@ -59,62 +269,75 @@ tableCount = function(t)
|
||||
end,
|
||||
|
||||
splitString = splitString,
|
||||
removeTags = removeTags,
|
||||
|
||||
createText = function(str, width)
|
||||
local uniqueLines = splitString(str, "\n")
|
||||
local lines = {}
|
||||
for k,v in pairs(uniqueLines)do
|
||||
local line = ""
|
||||
local words = splitString(v, " ")
|
||||
for a,b in pairs(words)do
|
||||
if(#line+#b <= width)then
|
||||
line = line=="" and b or line.." "..b
|
||||
if(a==#words)then table.insert(lines, line) end
|
||||
else
|
||||
table.insert(lines, line)
|
||||
line = b:sub(1,width)
|
||||
if(a==#words)then table.insert(lines, line) end
|
||||
wrapText = wrapText,
|
||||
|
||||
convertRichText = convertRichText,
|
||||
|
||||
--- Writes text with special color tags
|
||||
--- @param obj object The object to write to
|
||||
--- @param x number X-Position
|
||||
--- @param y number Y-Position
|
||||
--- @param text string The text to write
|
||||
writeRichText = function(obj, x, y, text)
|
||||
local richText = convertRichText(text)
|
||||
if(#richText==0)then
|
||||
obj:addText(x, y, text)
|
||||
return
|
||||
end
|
||||
|
||||
local defaultFG, defaultBG = obj:getForeground(), obj:getBackground()
|
||||
for _,v in pairs(richText)do
|
||||
obj:addText(x+v.position-1, y, v.text)
|
||||
if(v.color~=nil)then
|
||||
obj:addFG(x+v.position-1, y, tHex[colors[v.color] ]:rep(#v.text))
|
||||
defaultFG = colors[v.color]
|
||||
else
|
||||
obj:addFG(x+v.position-1, y, tHex[defaultFG]:rep(#v.text))
|
||||
end
|
||||
if(v.bgColor~=nil)then
|
||||
obj:addBG(x+v.position-1, y, tHex[colors[v.bgColor] ]:rep(#v.text))
|
||||
defaultBG = colors[v.bgColor]
|
||||
else
|
||||
if(defaultBG~=false)then
|
||||
obj:addBG(x+v.position-1, y, tHex[defaultBG]:rep(#v.text))
|
||||
end
|
||||
end
|
||||
end
|
||||
return lines
|
||||
end,
|
||||
|
||||
getValueFromXML = function(name, tab)
|
||||
local var
|
||||
if(type(tab)~="table")then return end
|
||||
if(tab[name]~=nil)then
|
||||
if(type(tab[name])=="table")then
|
||||
if(tab[name].value~=nil)then
|
||||
var = tab[name]:value()
|
||||
end
|
||||
wrapRichText = wrapRichText,
|
||||
|
||||
--- Writes wrapped Text with special tags.
|
||||
--- @param obj object The object to write to
|
||||
--- @param x number X-Position
|
||||
--- @param y number Y-Position
|
||||
--- @param text string Text
|
||||
--- @param width number Width
|
||||
--- @param height number Height
|
||||
writeWrappedText = function(obj, x, y, text, width, height)
|
||||
local wrapped = wrapRichText(text, width)
|
||||
for _,v in pairs(wrapped)do
|
||||
if(v.y>height)then
|
||||
break
|
||||
end
|
||||
if(v.text~=nil)then
|
||||
obj:addText(x+v.x-1, y+v.y-1, v.text)
|
||||
end
|
||||
if(v.color~=nil)then
|
||||
obj:addFG(x+v.x-1, y+v.y-1, tHex[colors[v.color] ]:rep(#v.text))
|
||||
end
|
||||
if(v.bgColor~=nil)then
|
||||
obj:addBG(x+v.x-1, y+v.y-1, tHex[colors[v.bgColor] ]:rep(#v.text))
|
||||
end
|
||||
end
|
||||
if(var==nil)then var = tab["@"..name] end
|
||||
|
||||
if(var=="true")then
|
||||
var = true
|
||||
elseif(var=="false")then
|
||||
var = false
|
||||
elseif(tonumber(var)~=nil)then
|
||||
var = tonumber(var)
|
||||
end
|
||||
return var
|
||||
end,
|
||||
|
||||
numberFromString = function(str)
|
||||
return load("return " .. str)()
|
||||
end,
|
||||
|
||||
--- Returns a random UUID.
|
||||
--- @return string UUID.
|
||||
uuid = function()
|
||||
local random = math.random
|
||||
local function uuid()
|
||||
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
|
||||
return string.gsub(template, '[xy]', function (c)
|
||||
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
|
||||
return string.format('%x', v)
|
||||
end)
|
||||
end
|
||||
return uuid()
|
||||
end,
|
||||
return string.gsub(string.format('%x-%x-%x-%x-%x', math.random(0, 0xffff), math.random(0, 0xffff), math.random(0, 0xffff), math.random(0, 0x0fff) + 0x4000, math.random(0, 0x3fff) + 0x8000), ' ', '0')
|
||||
end
|
||||
|
||||
}
|
||||
77
Basalt/libraries/xmlParser.lua
Normal file
@@ -0,0 +1,77 @@
|
||||
local XMLNode = {
|
||||
new = function(tag)
|
||||
return {
|
||||
tag = tag,
|
||||
value = nil,
|
||||
attributes = {},
|
||||
children = {},
|
||||
|
||||
addChild = function(self, child)
|
||||
table.insert(self.children, child)
|
||||
end,
|
||||
|
||||
addAttribute = function(self, tag, value)
|
||||
self.attributes[tag] = value
|
||||
end
|
||||
}
|
||||
end
|
||||
}
|
||||
|
||||
local parseAttributes = function(node, s)
|
||||
-- Parse "" style string attributes
|
||||
local _, _ = string.gsub(s, "(%w+)=([\"'])(.-)%2", function(attribute, _, value)
|
||||
node:addAttribute(attribute, "\"" .. value .. "\"")
|
||||
end)
|
||||
-- Parse {} style computed attributes
|
||||
local _, _ = string.gsub(s, "(%w+)={(.-)}", function(attribute, expression)
|
||||
node:addAttribute(attribute, expression)
|
||||
end)
|
||||
end
|
||||
|
||||
local XMLParser = {
|
||||
parseText = function(xmlText)
|
||||
local stack = {}
|
||||
local top = XMLNode.new()
|
||||
table.insert(stack, top)
|
||||
local ni, c, label, xarg, empty
|
||||
local i, j = 1, 1
|
||||
while true do
|
||||
ni, j, c, label, xarg, empty = string.find(xmlText, "<(%/?)([%w_:]+)(.-)(%/?)>", i)
|
||||
if not ni then break end
|
||||
local text = string.sub(xmlText, i, ni - 1);
|
||||
if not string.find(text, "^%s*$") then
|
||||
local lVal = (top.value or "") .. text
|
||||
stack[#stack].value = lVal
|
||||
end
|
||||
if empty == "/" then -- empty element tag
|
||||
local lNode = XMLNode.new(label)
|
||||
parseAttributes(lNode, xarg)
|
||||
top:addChild(lNode)
|
||||
elseif c == "" then -- start tag
|
||||
local lNode = XMLNode.new(label)
|
||||
parseAttributes(lNode, xarg)
|
||||
table.insert(stack, lNode)
|
||||
top = lNode
|
||||
else -- end tag
|
||||
local toclose = table.remove(stack) -- remove top
|
||||
|
||||
top = stack[#stack]
|
||||
if #stack < 1 then
|
||||
error("XMLParser: nothing to close with " .. label)
|
||||
end
|
||||
if toclose.tag ~= label then
|
||||
error("XMLParser: trying to close " .. toclose.tag .. " with " .. label)
|
||||
end
|
||||
top:addChild(toclose)
|
||||
end
|
||||
i = j + 1
|
||||
end
|
||||
local text = string.sub(xmlText, i);
|
||||
if #stack > 1 then
|
||||
error("XMLParser: unclosed " .. stack[#stack].tag)
|
||||
end
|
||||
return top.children
|
||||
end
|
||||
}
|
||||
|
||||
return XMLParser
|
||||
@@ -1,4 +1,5 @@
|
||||
local _OBJECTS = {}
|
||||
|
||||
if(packaged)then
|
||||
for k,v in pairs(getProject("objects"))do
|
||||
_OBJECTS[k] = v()
|
||||
@@ -13,7 +14,7 @@ if(dir==nil)then
|
||||
end
|
||||
|
||||
for _,v in pairs(fs.list(fs.combine(dir, "objects")))do
|
||||
if(v~="example.lua")then
|
||||
if(v~="example.lua")and not(v:find(".disabled"))then
|
||||
local name = v:gsub(".lua", "")
|
||||
_OBJECTS[name] = require(name)
|
||||
end
|
||||
|
||||
460
Basalt/main.lua
@@ -1,20 +1,23 @@
|
||||
local basaltEvent = require("basaltEvent")()
|
||||
local Frame = require("Frame")
|
||||
local theme = require("theme")
|
||||
local baseObjects = require("loadObjects")
|
||||
local moddedObjects
|
||||
local pluginSystem = require("plugin")
|
||||
local utils = require("utils")
|
||||
local log = require("basaltLogs")
|
||||
local uuid = utils.uuid
|
||||
local createText = utils.createText
|
||||
local wrapText = utils.wrapText
|
||||
local count = utils.tableCount
|
||||
local moveThrottle = 300
|
||||
local dragThrottle = 50
|
||||
local dragThrottle = 0
|
||||
local renderingThrottle = 0
|
||||
local newObjects = {}
|
||||
|
||||
local baseTerm = term.current()
|
||||
local version = "1.6.2"
|
||||
local version = "1.7.0"
|
||||
|
||||
local projectDirectory = fs.getDir(table.pack(...)[2] or "")
|
||||
|
||||
local activeKey, frames, monFrames, monGroups, variables, schedules = {}, {}, {}, {}, {}, {}
|
||||
local activeKey, frames, monFrames, variables, schedules = {}, {}, {}, {}, {}
|
||||
local mainFrame, activeFrame, focusedObject, updaterActive
|
||||
|
||||
local basalt = {}
|
||||
@@ -23,30 +26,23 @@ if not term.isColor or not term.isColor() then
|
||||
error('Basalt requires an advanced (golden) computer to run.', 0)
|
||||
end
|
||||
|
||||
local defaultColors = {}
|
||||
for k,v in pairs(colors)do
|
||||
if(type(v)=="number")then
|
||||
defaultColors[k] = {baseTerm.getPaletteColor(v)}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local function stop()
|
||||
updaterActive = false
|
||||
baseTerm.clear()
|
||||
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")
|
||||
for k,v in pairs(colors)do
|
||||
if(type(v)=="number")then
|
||||
baseTerm.setPaletteColor(v, colors.packRGB(table.unpack(defaultColors[k])))
|
||||
end
|
||||
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)
|
||||
@@ -57,11 +53,15 @@ return function(...)
|
||||
if(ok)then
|
||||
table.insert(schedules, co)
|
||||
else
|
||||
basaltError(result)
|
||||
basalt.basaltError(result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
basalt.log = function(...)
|
||||
log(...)
|
||||
end
|
||||
|
||||
local setVariable = function(name, var)
|
||||
variables[name] = var
|
||||
end
|
||||
@@ -70,22 +70,29 @@ local getVariable = function(name)
|
||||
return variables[name]
|
||||
end
|
||||
|
||||
local setTheme = function(_theme)
|
||||
theme = _theme
|
||||
local getObjects = function()
|
||||
return moddedObjects
|
||||
end
|
||||
|
||||
local getTheme = function(name)
|
||||
return theme[name]
|
||||
local getObject = function(objectName)
|
||||
return getObjects()[objectName]
|
||||
end
|
||||
|
||||
local createObject = function(objectName, id)
|
||||
return getObject(objectName)(id, basalt)
|
||||
end
|
||||
|
||||
local bInstance = {
|
||||
getDynamicValueEventSetting = function()
|
||||
return basalt.dynamicValueEvents
|
||||
end,
|
||||
|
||||
getMainFrame = function()
|
||||
return mainFrame
|
||||
end,
|
||||
|
||||
setVariable = setVariable,
|
||||
getVariable = getVariable,
|
||||
getTheme = getTheme,
|
||||
|
||||
setMainFrame = function(mFrame)
|
||||
mainFrame = mFrame
|
||||
@@ -123,20 +130,47 @@ local bInstance = {
|
||||
end
|
||||
end,
|
||||
|
||||
getBaseTerm = function()
|
||||
getTerm = function()
|
||||
return baseTerm
|
||||
end,
|
||||
|
||||
schedule = schedule,
|
||||
|
||||
stop = stop,
|
||||
newFrame = Frame,
|
||||
debug = basalt.debug,
|
||||
log = basalt.log,
|
||||
|
||||
getObjects = getObjects,
|
||||
|
||||
getObject = getObject,
|
||||
|
||||
createObject = createObject,
|
||||
|
||||
getDirectory = function()
|
||||
return projectDirectory
|
||||
end
|
||||
}
|
||||
|
||||
local function defaultErrorHandler(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 = wrapText("Basalt error: "..errMsg, w)
|
||||
local yPos = 1
|
||||
for _,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 = {}
|
||||
@@ -145,7 +179,7 @@ local function handleSchedules(event, p1, p2, p3, p4)
|
||||
if (coroutine.status(schedules[n]) == "suspended")then
|
||||
local ok, result = coroutine.resume(schedules[n], event, p1, p2, p3, p4)
|
||||
if not(ok)then
|
||||
basaltError(result)
|
||||
basalt.basaltError(result)
|
||||
end
|
||||
else
|
||||
table.insert(finished, n)
|
||||
@@ -161,23 +195,19 @@ end
|
||||
local function drawFrames()
|
||||
if(updaterActive==false)then return end
|
||||
if(mainFrame~=nil)then
|
||||
mainFrame:draw()
|
||||
mainFrame:render()
|
||||
mainFrame:updateTerm()
|
||||
end
|
||||
for _,v in pairs(monFrames)do
|
||||
v:draw()
|
||||
v:render()
|
||||
v:updateTerm()
|
||||
end
|
||||
for _,v in pairs(monGroups)do
|
||||
v[1]:draw()
|
||||
v[1]:updateTerm()
|
||||
end
|
||||
end
|
||||
|
||||
local stopped, moveX, moveY = nil, nil, nil
|
||||
local moveTimer = nil
|
||||
local function mouseMoveEvent(stp, x, y)
|
||||
stopped, moveX, moveY = stopped, x, y
|
||||
local function mouseMoveEvent(_, stp, x, y)
|
||||
stopped, moveX, moveY = stp, x, y
|
||||
if(moveTimer==nil)then
|
||||
moveTimer = os.startTimer(moveThrottle/1000)
|
||||
end
|
||||
@@ -197,7 +227,7 @@ local function dragHandlerTimer()
|
||||
activeFrame = mainFrame
|
||||
end
|
||||
|
||||
local function mouseDragEvent(b, x, y)
|
||||
local function mouseDragEvent(_, b, x, y)
|
||||
btn, dragX, dragY = b, x, y
|
||||
if(dragThrottle<50)then
|
||||
dragHandlerTimer()
|
||||
@@ -208,95 +238,220 @@ local function mouseDragEvent(b, x, y)
|
||||
end
|
||||
end
|
||||
|
||||
local function basaltUpdateEvent(event, p1, p2, p3, p4)
|
||||
if(basaltEvent:sendEvent("basaltEventCycle", event, p1, p2, p3, p4)==false)then return end
|
||||
|
||||
local renderingTimer = nil
|
||||
local function renderingUpdateTimer()
|
||||
renderingTimer = nil
|
||||
drawFrames()
|
||||
end
|
||||
|
||||
local function renderingUpdateEvent(timer)
|
||||
if(renderingThrottle<50)then
|
||||
drawFrames()
|
||||
else
|
||||
if(renderingTimer==nil)then
|
||||
renderingTimer = os.startTimer(renderingThrottle/1000)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function basaltUpdateEvent(event, ...)
|
||||
local a = {...}
|
||||
if(basaltEvent:sendEvent("basaltEventCycle", event, ...)==false)then return end
|
||||
if(event=="terminate")then basalt.stop() end
|
||||
if(mainFrame~=nil)then
|
||||
if (event == "mouse_click") then
|
||||
mainFrame:mouseHandler(p1, p2, p3, false)
|
||||
activeFrame = mainFrame
|
||||
elseif (event == "mouse_drag") then
|
||||
mouseDragEvent(p1, p2, p3)
|
||||
elseif (event == "mouse_up") then
|
||||
mainFrame:mouseUpHandler(p1, p2, p3, p4)
|
||||
activeFrame = mainFrame
|
||||
elseif (event == "mouse_scroll") then
|
||||
mainFrame:scrollHandler(p1, p2, p3, p4)
|
||||
activeFrame = mainFrame
|
||||
elseif (event == "mouse_move") then
|
||||
mouseMoveEvent(p1, p2, p3)
|
||||
local mouseEvents = {
|
||||
mouse_click = mainFrame.mouseHandler,
|
||||
mouse_up = mainFrame.mouseUpHandler,
|
||||
mouse_scroll = mainFrame.scrollHandler,
|
||||
mouse_drag = mouseDragEvent,
|
||||
mouse_move = mouseMoveEvent,
|
||||
}
|
||||
local mouseEvent = mouseEvents[event]
|
||||
if(mouseEvent~=nil)then
|
||||
mouseEvent(mainFrame, ...)
|
||||
handleSchedules(event, ...)
|
||||
renderingUpdateEvent()
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if(event == "monitor_touch") then
|
||||
if(monFrames[p1]~=nil)then
|
||||
monFrames[p1]:mouseHandler(1, p2, p3, true)
|
||||
activeFrame = monFrames[p1]
|
||||
end
|
||||
if(count(monGroups)>0)then
|
||||
for k,v in pairs(monGroups)do
|
||||
v[1]:mouseHandler(1, p2, p3, true, p1)
|
||||
for k,v in pairs(monFrames)do
|
||||
if(v:mouseHandler(1, a[2], a[3], true, a[1]))then
|
||||
activeFrame = v
|
||||
end
|
||||
end
|
||||
handleSchedules(event, ...)
|
||||
renderingUpdateEvent()
|
||||
return
|
||||
end
|
||||
|
||||
if(activeFrame~=nil)then
|
||||
local keyEvents = {
|
||||
char = activeFrame.charHandler,
|
||||
key = activeFrame.keyHandler,
|
||||
key_up = activeFrame.keyUpHandler,
|
||||
}
|
||||
local keyEvent = keyEvents[event]
|
||||
if(keyEvent~=nil)then
|
||||
if(event == "key")then
|
||||
activeKey[a[1]] = true
|
||||
elseif(event == "key_up")then
|
||||
activeKey[a[1]] = false
|
||||
end
|
||||
keyEvent(activeFrame, ...)
|
||||
handleSchedules(event, ...)
|
||||
renderingUpdateEvent()
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if(event=="timer")and(a[1]==moveTimer)then
|
||||
moveHandlerTimer()
|
||||
elseif(event=="timer")and(a[1]==dragTimer)then
|
||||
dragHandlerTimer()
|
||||
elseif(event=="timer")and(a[1]==renderingTimer)then
|
||||
renderingUpdateTimer()
|
||||
else
|
||||
for _, v in pairs(frames) do
|
||||
v:eventHandler(event, ...)
|
||||
end
|
||||
for _, v in pairs(monFrames) do
|
||||
v:eventHandler(event, ...)
|
||||
end
|
||||
handleSchedules(event, ...)
|
||||
renderingUpdateEvent()
|
||||
end
|
||||
end
|
||||
|
||||
if(event == "char")then
|
||||
if(activeFrame~=nil)then
|
||||
activeFrame:charHandler(p1)
|
||||
end
|
||||
end
|
||||
if(event == "key_up")then
|
||||
if(activeFrame~=nil)then
|
||||
activeFrame:keyUpHandler(p1)
|
||||
end
|
||||
activeKey[p1] = false
|
||||
end
|
||||
if(event == "key")then
|
||||
if(activeFrame~=nil)then
|
||||
activeFrame:keyHandler(p1, p2)
|
||||
end
|
||||
activeKey[p1] = true
|
||||
end
|
||||
if(event == "terminate")then
|
||||
if(activeFrame~=nil)then
|
||||
activeFrame:eventHandler(event)
|
||||
if(updaterActive==false)then return end
|
||||
end
|
||||
end
|
||||
if(event~="mouse_click")and(event~="mouse_up")and(event~="mouse_scroll")and(event~="mouse_drag")and(event~="mouse_move")and(event~="key")and(event~="key_up")and(event~="char")and(event~="terminate")then
|
||||
if(event=="timer")and(p1==moveTimer)then
|
||||
moveHandlerTimer()
|
||||
elseif(event=="timer")and(p1==dragTimer)then
|
||||
dragHandlerTimer()
|
||||
else
|
||||
for k, v in pairs(frames) do
|
||||
v:eventHandler(event, p1, p2, p3, p4)
|
||||
local loadedObjects = false
|
||||
local loadedPlugins = false
|
||||
local function InitializeBasalt()
|
||||
if not(loadedObjects)then
|
||||
for _,v in pairs(newObjects)do
|
||||
if(fs.exists(v))then
|
||||
if(fs.isDir(v))then
|
||||
local files = fs.list(v)
|
||||
for _,object in pairs(files)do
|
||||
if not(fs.isDir(v.."/"..object))then
|
||||
local name = object:gsub(".lua", "")
|
||||
if(name~="example.lua")and not(name:find(".disabled"))then
|
||||
if(baseObjects[name]==nil)then
|
||||
baseObjects[name] = require(v.."."..object:gsub(".lua", ""))
|
||||
else
|
||||
error("Duplicate object name: "..name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
local name = v:gsub(".lua", "")
|
||||
if(baseObjects[name]==nil)then
|
||||
baseObjects[name] = require(name)
|
||||
else
|
||||
error("Duplicate object name: "..name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
loadedObjects = true
|
||||
end
|
||||
handleSchedules(event, p1, p2, p3, p4)
|
||||
drawFrames()
|
||||
if not(loadedPlugins)then
|
||||
moddedObjects = pluginSystem.loadPlugins(baseObjects, bInstance)
|
||||
local basaltPlugins = pluginSystem.get("basalt")
|
||||
if(basaltPlugins~=nil)then
|
||||
for k,v in pairs(basaltPlugins)do
|
||||
for a,b in pairs(v(basalt))do
|
||||
basalt[a] = b
|
||||
bInstance[a] = b
|
||||
end
|
||||
end
|
||||
end
|
||||
local basaltPlugins = pluginSystem.get("basaltInternal")
|
||||
if(basaltPlugins~=nil)then
|
||||
for _,v in pairs(basaltPlugins)do
|
||||
for a,b in pairs(v(basalt))do
|
||||
bInstance[a] = b
|
||||
end
|
||||
end
|
||||
end
|
||||
loadedPlugins = true
|
||||
end
|
||||
end
|
||||
|
||||
local function createFrame(name)
|
||||
InitializeBasalt()
|
||||
for _, v in pairs(frames) do
|
||||
if (v:getName() == name) then
|
||||
return nil
|
||||
end
|
||||
end
|
||||
local newFrame = moddedObjects["BaseFrame"](name, bInstance)
|
||||
newFrame:init()
|
||||
newFrame:load()
|
||||
newFrame:draw()
|
||||
table.insert(frames, newFrame)
|
||||
if(mainFrame==nil)and(newFrame:getName()~="basaltDebuggingFrame")then
|
||||
newFrame:show()
|
||||
end
|
||||
return newFrame
|
||||
end
|
||||
|
||||
basalt = {
|
||||
basaltError = defaultErrorHandler,
|
||||
logging = false,
|
||||
setTheme = setTheme,
|
||||
getTheme = getTheme,
|
||||
dynamicValueEvents = false,
|
||||
drawFrames = drawFrames,
|
||||
log = log,
|
||||
getVersion = function()
|
||||
return version
|
||||
end,
|
||||
|
||||
memory = function()
|
||||
return math.floor(collectgarbage("count")+0.5).."KB"
|
||||
end,
|
||||
|
||||
addObject = function(path)
|
||||
if(fs.exists(path))then
|
||||
table.insert(newObjects, path)
|
||||
end
|
||||
end,
|
||||
|
||||
addPlugin = function(path)
|
||||
pluginSystem.addPlugin(path)
|
||||
end,
|
||||
|
||||
getAvailablePlugins = function()
|
||||
return pluginSystem.getAvailablePlugins()
|
||||
end,
|
||||
|
||||
getAvailableObjects = function()
|
||||
local objectNames = {}
|
||||
for k,_ in pairs(baseObjects)do
|
||||
table.insert(objectNames, k)
|
||||
end
|
||||
return objectNames
|
||||
end,
|
||||
|
||||
setVariable = setVariable,
|
||||
getVariable = getVariable,
|
||||
|
||||
getObjects = getObjects,
|
||||
getObject = getObject,
|
||||
|
||||
createObject = createObject,
|
||||
|
||||
setBaseTerm = function(_baseTerm)
|
||||
baseTerm = _baseTerm
|
||||
end,
|
||||
|
||||
log = function(...)
|
||||
log(...)
|
||||
resetPalette = function()
|
||||
for k,v in pairs(colors)do
|
||||
if(type(v)=="number")then
|
||||
--baseTerm.setPaletteColor(v, colors.packRGB(table.unpack(defaultColors[k])))
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
setMouseMoveThrottle = function(amount)
|
||||
@@ -312,6 +467,15 @@ basalt = {
|
||||
return false
|
||||
end,
|
||||
|
||||
setRenderingThrottle = function(amount)
|
||||
if(amount<=0)then
|
||||
renderingThrottle = 0
|
||||
else
|
||||
renderingTimer = nil
|
||||
renderingThrottle = amount
|
||||
end
|
||||
end,
|
||||
|
||||
setMouseDragThrottle = function(amount)
|
||||
if(amount<=0)then
|
||||
dragThrottle = 0
|
||||
@@ -330,18 +494,20 @@ basalt = {
|
||||
basaltUpdateEvent(os.pullEventRaw())
|
||||
end
|
||||
end
|
||||
local ok, err = xpcall(f, debug.traceback)
|
||||
if not(ok)then
|
||||
basaltError(err)
|
||||
return
|
||||
while updaterActive do
|
||||
local ok, err = xpcall(f, debug.traceback)
|
||||
if not(ok)then
|
||||
basalt.basaltError(err)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
update = function(event, p1, p2, p3, p4)
|
||||
update = function(event, ...)
|
||||
if (event ~= nil) then
|
||||
local ok, err = xpcall(basaltUpdateEvent, debug.traceback, event, p1, p2, p3, p4)
|
||||
local args = {...}
|
||||
local ok, err = xpcall(function() basaltUpdateEvent(event, table.unpack(args)) end, debug.traceback)
|
||||
if not(ok)then
|
||||
basaltError(err)
|
||||
basalt.basaltError(err)
|
||||
return
|
||||
end
|
||||
end
|
||||
@@ -349,6 +515,9 @@ basalt = {
|
||||
|
||||
stop = stop,
|
||||
stopUpdate = stop,
|
||||
getTerm = function()
|
||||
return baseTerm
|
||||
end,
|
||||
|
||||
isKeyDown = function(key)
|
||||
if(activeKey[key]==nil)then return false end
|
||||
@@ -368,12 +537,16 @@ basalt = {
|
||||
end,
|
||||
|
||||
setActiveFrame = function(frame)
|
||||
if (frame:getType() == "Frame") then
|
||||
if (frame:getType() == "Container") then
|
||||
activeFrame = frame
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
getMainFrame = function()
|
||||
return mainFrame
|
||||
end,
|
||||
|
||||
onEvent = function(...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
@@ -385,19 +558,21 @@ basalt = {
|
||||
|
||||
schedule = schedule,
|
||||
|
||||
createFrame = function(name)
|
||||
name = name or uuid()
|
||||
addFrame = createFrame,
|
||||
createFrame = createFrame,
|
||||
|
||||
addMonitor = function(name)
|
||||
InitializeBasalt()
|
||||
for _, v in pairs(frames) do
|
||||
if (v.name == name) then
|
||||
if (v:getName() == name) then
|
||||
return nil
|
||||
end
|
||||
end
|
||||
local newFrame = Frame(name,nil,nil,bInstance)
|
||||
local newFrame = moddedObjects["MonitorFrame"](name, bInstance)
|
||||
newFrame:init()
|
||||
table.insert(frames, newFrame)
|
||||
if(mainFrame==nil)and(newFrame:getName()~="basaltDebuggingFrame")then
|
||||
newFrame:show()
|
||||
end
|
||||
newFrame:load()
|
||||
newFrame:draw()
|
||||
table.insert(monFrames, newFrame)
|
||||
return newFrame
|
||||
end,
|
||||
|
||||
@@ -408,37 +583,24 @@ basalt = {
|
||||
setProjectDir = function(dir)
|
||||
projectDirectory = dir
|
||||
end,
|
||||
|
||||
debug = function(...)
|
||||
local args = { ... }
|
||||
if(mainFrame==nil)then print(...) return end
|
||||
if (mainFrame.name ~= "basaltDebuggingFrame") then
|
||||
if (mainFrame ~= basalt.debugFrame) then
|
||||
basalt.debugLabel:setParent(mainFrame)
|
||||
end
|
||||
end
|
||||
local str = ""
|
||||
for key, value in pairs(args) do
|
||||
str = str .. tostring(value) .. (#args ~= key and ", " or "")
|
||||
end
|
||||
basalt.debugLabel:setText("[Debug] " .. str)
|
||||
for k,v in pairs(createText(str, basalt.debugList:getWidth()))do
|
||||
basalt.debugList:addItem(v)
|
||||
end
|
||||
if (basalt.debugList:getItemCount() > 50) then
|
||||
basalt.debugList:removeItem(1)
|
||||
end
|
||||
basalt.debugList:setValue(basalt.debugList:getItem(basalt.debugList:getItemCount()))
|
||||
if(basalt.debugList.getItemCount() > basalt.debugList:getHeight())then
|
||||
basalt.debugList:setOffset(basalt.debugList:getItemCount() - basalt.debugList:getHeight())
|
||||
end
|
||||
basalt.debugLabel:show()
|
||||
end,
|
||||
}
|
||||
|
||||
basalt.debugFrame = basalt.createFrame("basaltDebuggingFrame"):showBar():setBackground(colors.lightGray):setBar("Debug", colors.black, colors.gray)
|
||||
basalt.debugFrame:addButton("back"):setAnchor("topRight"):setSize(1, 1):setText("\22"):onClick(function() if(basalt.oldFrame~=nil)then basalt.oldFrame:show() end end):setBackground(colors.red):show()
|
||||
basalt.debugList = basalt.debugFrame:addList("debugList"):setSize("parent.w - 2", "parent.h - 3"):setPosition(2, 3):setScrollable(true):show()
|
||||
basalt.debugLabel = basalt.debugFrame:addLabel("debugLabel"):onClick(function() basalt.oldFrame = mainFrame basalt.debugFrame:show() end):setBackground(colors.black):setForeground(colors.white):setAnchor("bottomLeft"):ignoreOffset():setZIndex(20):show()
|
||||
local basaltPlugins = pluginSystem.get("basalt")
|
||||
if(basaltPlugins~=nil)then
|
||||
for k,v in pairs(basaltPlugins)do
|
||||
for a,b in pairs(v(basalt))do
|
||||
basalt[a] = b
|
||||
bInstance[a] = b
|
||||
end
|
||||
end
|
||||
end
|
||||
local basaltPlugins = pluginSystem.get("basaltInternal")
|
||||
if(basaltPlugins~=nil)then
|
||||
for k,v in pairs(basaltPlugins)do
|
||||
for a,b in pairs(v(basalt))do
|
||||
bInstance[a] = b
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return basalt
|
||||
return basalt
|
||||
@@ -1,645 +0,0 @@
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
local basaltEvent = require("basaltEvent")
|
||||
|
||||
local floor,sin,cos,pi,sqrt,pow = math.floor,math.sin,math.cos,math.pi,math.sqrt,math.pow
|
||||
|
||||
-- You can find the easing curves here https://easings.net
|
||||
|
||||
local lerp = function(s, e, pct)
|
||||
return s + (e - s) * pct
|
||||
end
|
||||
|
||||
local linear = function (t)
|
||||
return t
|
||||
end
|
||||
|
||||
local flip = function (t)
|
||||
return 1 - t
|
||||
end
|
||||
|
||||
local easeIn = function (t)
|
||||
return t * t * t
|
||||
end
|
||||
|
||||
local easeOut = function(t)
|
||||
return flip(easeIn(flip(t)))
|
||||
end
|
||||
|
||||
local easeInOut = function(t)
|
||||
return lerp(easeIn(t), easeOut(t), t)
|
||||
end
|
||||
|
||||
local easeOutSine = function(t)
|
||||
return sin((t * pi) / 2);
|
||||
end
|
||||
|
||||
local easeInSine = function(t)
|
||||
return flip(cos((t * pi) / 2))
|
||||
end
|
||||
|
||||
local easeInOutSine = function(t)
|
||||
return -(cos(pi * x) - 1) / 2
|
||||
end
|
||||
|
||||
local easeInBack = function(t)
|
||||
local c1 = 1.70158;
|
||||
local c3 = c1 + 1
|
||||
return c3*t^3-c1*t^2
|
||||
end
|
||||
|
||||
local easeInCubic = function(t)
|
||||
return t^3
|
||||
end
|
||||
|
||||
local easeInElastic = function(t)
|
||||
local c4 = (2*pi)/3;
|
||||
return t == 0 and 0 or (t == 1 and 1 or (
|
||||
-2^(10*t-10)*sin((t*10-10.75)*c4)
|
||||
))
|
||||
end
|
||||
|
||||
local function easeInExpo(t)
|
||||
return t == 0 and 0 or 2^(10*t-10)
|
||||
end
|
||||
|
||||
local function easeInExpo(t)
|
||||
return t == 0 and 0 or 2^(10*t-10)
|
||||
end
|
||||
|
||||
local function easeInOutBack(t)
|
||||
local c1 = 1.70158;
|
||||
local c2 = c1 * 1.525;
|
||||
return t < 0.5 and ((2*t)^2*((c2+1)*2*t-c2))/2 or ((2*t-2)^2*((c2+1)*(t*2-2)+c2)+2)/2
|
||||
end
|
||||
|
||||
local function easeInOutCubic(t)
|
||||
return t < 0.5 and 4 * t^3 or 1-(-2*t+2)^3 / 2
|
||||
end
|
||||
|
||||
local function easeInOutElastic(t)
|
||||
local c5 = (2*pi) / 4.5
|
||||
return t==0 and 0 or (t == 1 and 1 or (t < 0.5 and -(2^(20*t-10) * sin((20*t - 11.125) * c5))/2 or (2^(-20*t+10) * sin((20*t - 11.125) * c5))/2 + 1))
|
||||
end
|
||||
|
||||
local function easeInOutExpo(t)
|
||||
return t == 0 and 0 or (t == 1 and 1 or (t < 0.5 and 2^(20*t-10)/2 or (2-2^(-20*t+10)) /2))
|
||||
end
|
||||
|
||||
local function easeInOutQuad(t)
|
||||
return t < 0.5 and 2*t^2 or 1-(-2*t+2)^2/2
|
||||
end
|
||||
|
||||
local function easeInOutQuart(t)
|
||||
return t < 0.5 and 8*t^4 or 1 - (-2*t+2)^4 / 2
|
||||
end
|
||||
|
||||
local function easeInOutQuint(t)
|
||||
return t < 0.5 and 16*t^5 or 1-(-2*t+2)^5 / 2
|
||||
end
|
||||
|
||||
local function easeInQuad(t)
|
||||
return t^2
|
||||
end
|
||||
|
||||
local function easeInQuart(t)
|
||||
return t^4
|
||||
end
|
||||
|
||||
local function easeInQuint(t)
|
||||
return t^5
|
||||
end
|
||||
|
||||
local function easeOutBack(t)
|
||||
local c1 = 1.70158;
|
||||
local c3 = c1 + 1
|
||||
return 1+c3*(t-1)^3+c1*(t-1)^2
|
||||
end
|
||||
|
||||
local function easeOutCubic(t)
|
||||
return 1 - (1-t)^3
|
||||
end
|
||||
|
||||
local function easeOutElastic(t)
|
||||
local c4 = (2*pi)/3;
|
||||
|
||||
return t == 0 and 0 or (t == 1 and 1 or (2^(-10*t)*sin((t*10-0.75)*c4)+1))
|
||||
end
|
||||
|
||||
local function easeOutExpo(t)
|
||||
return t == 1 and 1 or 1-2^(-10*t)
|
||||
end
|
||||
|
||||
local function easeOutQuad(t)
|
||||
return 1 - (1 - t) * (1 - t)
|
||||
end
|
||||
|
||||
local function easeOutQuart(t)
|
||||
return 1 - (1-t)^4
|
||||
end
|
||||
|
||||
local function easeOutQuint(t)
|
||||
return 1 - (1 - t)^5
|
||||
end
|
||||
|
||||
local function easeInCirc(t)
|
||||
return 1 - sqrt(1 - pow(t, 2))
|
||||
end
|
||||
|
||||
local function easeOutCirc(t)
|
||||
return sqrt(1 - pow(t - 1, 2))
|
||||
end
|
||||
|
||||
local function easeInOutCirc(t)
|
||||
return t < 0.5 and (1 - sqrt(1 - pow(2 * t, 2))) / 2 or (sqrt(1 - pow(-2 * t + 2, 2)) + 1) / 2;
|
||||
end
|
||||
|
||||
local function easeOutBounce(t)
|
||||
local n1 = 7.5625;
|
||||
local d1 = 2.75;
|
||||
|
||||
if (t < 1 / d1)then
|
||||
return n1 * t * t
|
||||
elseif (t < 2 / d1)then
|
||||
local a = t - 1.5 / d1
|
||||
return n1 * a * a + 0.75;
|
||||
elseif (t < 2.5 / d1)then
|
||||
local a = t - 2.25 / d1
|
||||
return n1 * a * a + 0.9375;
|
||||
else
|
||||
local a = t - 2.625 / d1
|
||||
return n1 * a * a + 0.984375;
|
||||
end
|
||||
end
|
||||
|
||||
local function easeInBounce(t)
|
||||
return 1 - easeOutBounce(1 - t)
|
||||
end
|
||||
|
||||
local function easeInOutBounce(t)
|
||||
return x < 0.5 and (1 - easeOutBounce(1 - 2 * t)) / 2 or (1 + easeOutBounce(2 * t - 1)) / 2;
|
||||
end
|
||||
|
||||
|
||||
|
||||
local lerp = {
|
||||
linear = linear,
|
||||
lerp = lerp,
|
||||
flip=flip,
|
||||
easeIn=easeIn,
|
||||
easeInSine = easeInSine,
|
||||
easeInBack=easeInBack,
|
||||
easeInCubic=easeInCubic,
|
||||
easeInElastic=easeInElastic,
|
||||
easeInExpo=easeInExpo,
|
||||
easeInQuad=easeInQuad,
|
||||
easeInQuart=easeInQuart,
|
||||
easeInQuint=easeInQuint,
|
||||
easeInCirc=easeInCirc,
|
||||
easeInBounce=easeInBounce,
|
||||
easeOut=easeOut,
|
||||
easeOutSine = easeOutSine,
|
||||
easeOutBack=easeOutBack,
|
||||
easeOutCubic=easeOutCubic,
|
||||
easeOutElastic=easeOutElastic,
|
||||
easeOutExpo=easeOutExpo,
|
||||
easeOutQuad=easeOutQuad,
|
||||
easeOutQuart=easeOutQuart,
|
||||
easeOutQuint=easeOutQuint,
|
||||
easeOutCirc=easeOutCirc,
|
||||
easeOutBounce=easeOutBounce,
|
||||
easeInOut=easeInOut,
|
||||
easeInOutSine = easeInOutSine,
|
||||
easeInOutBack=easeInOutBack,
|
||||
easeInOutCubic=easeInOutCubic,
|
||||
easeInOutElastic=easeInOutElastic,
|
||||
easeInOutExpo=easeInOutExpo,
|
||||
easeInOutQuad=easeInOutQuad,
|
||||
easeInOutQuart=easeInOutQuart,
|
||||
easeInOutQuint=easeInOutQuint,
|
||||
easeInOutCirc=easeInOutCirc,
|
||||
easeInOutBounce=easeInOutBounce,
|
||||
}
|
||||
|
||||
local activeAnimations = {}
|
||||
|
||||
return function(name)
|
||||
local object = {}
|
||||
local objectType = "Animation"
|
||||
|
||||
local timerObj
|
||||
|
||||
local animations = {}
|
||||
local animationTime = 0
|
||||
local animationActive = false
|
||||
local index = 1
|
||||
local infinitePlay = false
|
||||
|
||||
local eventSystem = basaltEvent()
|
||||
|
||||
local nextWaitTimer = 0
|
||||
local lastFunc
|
||||
local loop=false
|
||||
local autoDestroy = false
|
||||
local mode = "easeOut"
|
||||
|
||||
local _OBJ
|
||||
|
||||
local function call(tab)
|
||||
for k,v in pairs(tab)do
|
||||
v(object, animations[index].t, index)
|
||||
end
|
||||
end
|
||||
|
||||
local function onPlay(self)
|
||||
if(index==1)then self:animationStartHandler() end
|
||||
if (animations[index] ~= nil) then
|
||||
call(animations[index].f)
|
||||
animationTime = animations[index].t
|
||||
end
|
||||
index = index + 1
|
||||
if(animations[index]==nil)then
|
||||
if(infinitePlay)then
|
||||
index = 1
|
||||
animationTime = 0
|
||||
else
|
||||
self:animationDoneHandler()
|
||||
return
|
||||
end
|
||||
end
|
||||
if (animations[index].t > 0) then
|
||||
timerObj = os.startTimer(animations[index].t - animationTime)
|
||||
else
|
||||
onPlay(self)
|
||||
end
|
||||
end
|
||||
|
||||
local function addAnimationPart(time, f)
|
||||
for n=1,#animations do
|
||||
if(animations[n].t==time)then
|
||||
table.insert(animations[n].f, f)
|
||||
return
|
||||
end
|
||||
end
|
||||
for n=1,#animations do
|
||||
if(animations[n].t>time)then
|
||||
if(animations[n-1]~=nil)then
|
||||
if(animations[n-1].t<time)then
|
||||
table.insert(animations, n-1, {t=time, f={f}})
|
||||
return
|
||||
end
|
||||
else
|
||||
table.insert(animations, n, {t=time, f={f}})
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
if(#animations<=0)then
|
||||
table.insert(animations, 1, {t=time, f={f}})
|
||||
return
|
||||
elseif(animations[#animations].t<time)then
|
||||
table.insert(animations, {t=time, f={f}})
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local function predefinedLerp(v1,v2,d,t,get,set,typ,self)
|
||||
local obj = _OBJ
|
||||
local x,y
|
||||
local name = ""
|
||||
if(obj.parent~=nil)then name = obj.parent:getName() end
|
||||
name = name..obj:getName()
|
||||
addAnimationPart(t+0.05, function()
|
||||
if(typ~=nil)then
|
||||
if(activeAnimations[typ]==nil)then activeAnimations[typ] = {} end
|
||||
if(activeAnimations[typ][name]~=nil)then
|
||||
if(activeAnimations[typ][name]~=self)then
|
||||
activeAnimations[typ][name]:cancel()
|
||||
end
|
||||
end
|
||||
activeAnimations[typ][name] = self
|
||||
end
|
||||
x,y = get(obj)
|
||||
end)
|
||||
for n=0.05,d+0.01,0.05 do
|
||||
addAnimationPart(t+n, function()
|
||||
local _x = math.floor(lerp.lerp(x, v1, lerp[mode](n / d))+0.5)
|
||||
local _y = math.floor(lerp.lerp(y, v2, lerp[mode](n / d))+0.5)
|
||||
set(obj, _x,_y)
|
||||
if(typ~=nil)then
|
||||
if(n>=d-0.01)then
|
||||
if(activeAnimations[typ][name]==self)then
|
||||
activeAnimations[typ][name] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
end;
|
||||
|
||||
object = {
|
||||
name = name,
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
|
||||
getBaseFrame = function(self)
|
||||
if(self.parent~=nil)then
|
||||
return self.parent:getBaseFrame()
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
setMode = function(self, newMode)
|
||||
mode = newMode
|
||||
return self
|
||||
end,
|
||||
|
||||
addMode = function(self, modeId, modeF)
|
||||
lerp[modeId] = modeF
|
||||
return self
|
||||
end,
|
||||
|
||||
generateXMLEventFunction = function(self, func, val)
|
||||
local createF = function(str)
|
||||
if(str:sub(1,1)=="#")then
|
||||
local o = self:getBaseFrame():getDeepObject(str:sub(2,str:len()))
|
||||
if(o~=nil)and(o.internalObjetCall~=nil)then
|
||||
func(self,function()o:internalObjetCall()end)
|
||||
end
|
||||
else
|
||||
func(self,self:getBaseFrame():getVariable(str))
|
||||
end
|
||||
end
|
||||
if(type(val)=="string")then
|
||||
createF(val)
|
||||
elseif(type(val)=="table")then
|
||||
for k,v in pairs(val)do
|
||||
createF(v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
loop = xmlValue("loop", data)==true and true or false
|
||||
if(xmlValue("object", data)~=nil)then
|
||||
local o = self:getBaseFrame():getDeepObject(xmlValue("object", data))
|
||||
if(o==nil)then
|
||||
o = self:getBaseFrame():getVariable(xmlValue("object", data))
|
||||
end
|
||||
if(o~=nil)then
|
||||
self:setObject(o)
|
||||
end
|
||||
end
|
||||
if(data["move"]~=nil)then
|
||||
local x = xmlValue("x", data["move"])
|
||||
local y = xmlValue("y", data["move"])
|
||||
local duration = xmlValue("duration", data["move"])
|
||||
local time = xmlValue("time", data["move"])
|
||||
self:move(x, y, duration, time)
|
||||
end
|
||||
if(data["size"]~=nil)then
|
||||
local w = xmlValue("width", data["size"])
|
||||
local h = xmlValue("height", data["size"])
|
||||
local duration = xmlValue("duration", data["size"])
|
||||
local time = xmlValue("time", data["size"])
|
||||
self:size(w, h, duration, time)
|
||||
end
|
||||
if(data["offset"]~=nil)then
|
||||
local x = xmlValue("x", data["offset"])
|
||||
local y = xmlValue("y", data["offset"])
|
||||
local duration = xmlValue("duration", data["offset"])
|
||||
local time = xmlValue("time", data["offset"])
|
||||
self:offset(x, y, duration, time)
|
||||
end
|
||||
if(data["textColor"]~=nil)then
|
||||
local duration = xmlValue("duration", data["textColor"])
|
||||
local timer = xmlValue("time", data["textColor"])
|
||||
local t = {}
|
||||
local tab = data["textColor"]["color"]
|
||||
if(tab~=nil)then
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
table.insert(t, colors[v:value()])
|
||||
end
|
||||
end
|
||||
if(duration~=nil)and(#t>0)then
|
||||
self:changeTextColor(duration, timer or 0, table.unpack(t))
|
||||
end
|
||||
end
|
||||
if(data["background"]~=nil)then
|
||||
local duration = xmlValue("duration", data["background"])
|
||||
local timer = xmlValue("time", data["background"])
|
||||
local t = {}
|
||||
local tab = data["background"]["color"]
|
||||
if(tab~=nil)then
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
table.insert(t, colors[v:value()])
|
||||
end
|
||||
end
|
||||
if(duration~=nil)and(#t>0)then
|
||||
self:changeBackground(duration, timer or 0, table.unpack(t))
|
||||
end
|
||||
end
|
||||
if(data["text"]~=nil)then
|
||||
local duration = xmlValue("duration", data["text"])
|
||||
local timer = xmlValue("time", data["text"])
|
||||
local t = {}
|
||||
local tab = data["text"]["text"]
|
||||
if(tab~=nil)then
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
table.insert(t, v:value())
|
||||
end
|
||||
end
|
||||
if(duration~=nil)and(#t>0)then
|
||||
self:changeText(duration, timer or 0, table.unpack(t))
|
||||
end
|
||||
end
|
||||
if(xmlValue("onDone", data)~=nil)then self:generateXMLEventFunction(self.onDone, xmlValue("onDone", data)) end
|
||||
if(xmlValue("onStart", data)~=nil)then self:generateXMLEventFunction(self.onDone, xmlValue("onStart", data)) end
|
||||
if(xmlValue("autoDestroy", data)~=nil)then
|
||||
if(xmlValue("autoDestroy", data))then
|
||||
autoDestroy = true
|
||||
end
|
||||
end
|
||||
mode = xmlValue("mode", data) or mode
|
||||
if(xmlValue("play", data)~=nil)then if(xmlValue("play", data))then self:play(loop) end end
|
||||
return self
|
||||
end,
|
||||
|
||||
getZIndex = function(self)
|
||||
return 1
|
||||
end;
|
||||
|
||||
getName = function(self)
|
||||
return self.name
|
||||
end;
|
||||
|
||||
setObject = function(self, obj)
|
||||
_OBJ = obj
|
||||
return self
|
||||
end;
|
||||
|
||||
move = function(self, x, y, duration, timer, obj)
|
||||
_OBJ = obj or _OBJ
|
||||
predefinedLerp(x,y,duration,timer or 0,_OBJ.getPosition,_OBJ.setPosition, "position", self)
|
||||
return self
|
||||
end,
|
||||
|
||||
offset = function(self, x, y, duration, timer, obj)
|
||||
_OBJ = obj or _OBJ
|
||||
predefinedLerp(x,y,duration,timer or 0,_OBJ.getOffset,_OBJ.setOffset, "offset", self)
|
||||
return self
|
||||
end,
|
||||
|
||||
size = function(self, w, h, duration, timer, obj)
|
||||
_OBJ = obj or _OBJ
|
||||
predefinedLerp(w,h,duration,timer or 0,_OBJ.getSize,_OBJ.setSize, "size", self)
|
||||
return self
|
||||
end,
|
||||
|
||||
changeText = function(self, duration, timer, ...)
|
||||
local text = {...}
|
||||
timer = timer or 0
|
||||
_OBJ = obj or _OBJ
|
||||
for n=1,#text do
|
||||
addAnimationPart(timer+n*(duration/#text), function()
|
||||
_OBJ.setText(_OBJ, text[n])
|
||||
end)
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
changeBackground = function(self, duration, timer, ...)
|
||||
local colors = {...}
|
||||
timer = timer or 0
|
||||
_OBJ = obj or _OBJ
|
||||
for n=1,#colors do
|
||||
addAnimationPart(timer+n*(duration/#colors), function()
|
||||
_OBJ.setBackground(_OBJ, colors[n])
|
||||
end)
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
changeTextColor = function(self, duration, timer, ...)
|
||||
local colors = {...}
|
||||
timer = timer or 0
|
||||
_OBJ = obj or _OBJ
|
||||
for n=1,#colors do
|
||||
addAnimationPart(timer+n*(duration/#colors), function()
|
||||
_OBJ.setForeground(_OBJ, colors[n])
|
||||
end)
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
add = function(self, func, timer)
|
||||
lastFunc = func
|
||||
addAnimationPart((timer or nextWaitTimer) + (animations[#animations]~=nil and animations[#animations].t or 0), func)
|
||||
return self
|
||||
end;
|
||||
|
||||
wait = function(self, wait)
|
||||
nextWaitTimer = wait
|
||||
return self
|
||||
end;
|
||||
|
||||
rep = function(self, reps)
|
||||
if(lastFunc~=nil)then
|
||||
for n = 1, reps or 1 do
|
||||
addAnimationPart((wait or nextWaitTimer) + (animations[#animations]~=nil and animations[#animations].t or 0), lastFunc)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
onDone = function(self, f)
|
||||
eventSystem:registerEvent("animation_done", f)
|
||||
return self
|
||||
end,
|
||||
|
||||
onStart = function(self, f)
|
||||
eventSystem:registerEvent("animation_start", f)
|
||||
return self
|
||||
end,
|
||||
|
||||
setAutoDestroy = function(self, destroy)
|
||||
autoDestroy = destroy~=nil and destroy or true
|
||||
return self
|
||||
end,
|
||||
|
||||
animationDoneHandler = function(self)
|
||||
eventSystem:sendEvent("animation_done", self)
|
||||
self.parent:removeEvent("other_event", self)
|
||||
if(autoDestroy)then
|
||||
self.parent:removeObject(self)
|
||||
self = nil
|
||||
end
|
||||
end;
|
||||
|
||||
animationStartHandler = function(self)
|
||||
eventSystem:sendEvent("animation_start", self)
|
||||
end;
|
||||
|
||||
clear = function(self)
|
||||
animations = {}
|
||||
lastFunc = nil
|
||||
nextWaitTimer = 0
|
||||
index = 1
|
||||
animationTime = 0
|
||||
infinitePlay = false
|
||||
return self
|
||||
end;
|
||||
|
||||
play = function(self, infinite)
|
||||
self:cancel()
|
||||
animationActive = true
|
||||
infinitePlay = infinite and true or false
|
||||
index = 1
|
||||
animationTime = 0
|
||||
if (animations[index] ~= nil) then
|
||||
if (animations[index].t > 0) then
|
||||
timerObj = os.startTimer(animations[index].t)
|
||||
else
|
||||
onPlay(self)
|
||||
end
|
||||
else
|
||||
self:animationDoneHandler()
|
||||
end
|
||||
self.parent:addEvent("other_event", self)
|
||||
return self
|
||||
end;
|
||||
|
||||
cancel = function(self)
|
||||
if(timerObj~=nil)then
|
||||
os.cancelTimer(timerObj)
|
||||
infinitePlay = false
|
||||
end
|
||||
animationActive = false
|
||||
self.parent:removeEvent("other_event", self)
|
||||
return self
|
||||
end;
|
||||
|
||||
internalObjetCall = function(self)
|
||||
self:play(loop)
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, tObj)
|
||||
if(animationActive)then
|
||||
if (event == "timer") and (tObj == timerObj) then
|
||||
if (animations[index] ~= nil) then
|
||||
onPlay(self)
|
||||
else
|
||||
self:animationDoneHandler()
|
||||
end
|
||||
end
|
||||
end
|
||||
end;
|
||||
}
|
||||
object.__index = object
|
||||
|
||||
return object
|
||||
end
|
||||
231
Basalt/objects/BaseFrame.lua
Normal file
@@ -0,0 +1,231 @@
|
||||
local drawSystem = require("basaltDraw")
|
||||
local utils = require("utils")
|
||||
|
||||
local max,min,sub,rep = math.max,math.min,string.sub,string.rep
|
||||
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("Container")(name, basalt)
|
||||
local objectType = "BaseFrame"
|
||||
|
||||
local xOffset, yOffset = 0, 0
|
||||
|
||||
local colorTheme = {}
|
||||
|
||||
local updateRender = true
|
||||
|
||||
local termObject = basalt.getTerm()
|
||||
local basaltDraw = drawSystem(termObject)
|
||||
|
||||
local xCursor, yCursor, cursorBlink, cursorColor = 1, 1, false, colors.white
|
||||
|
||||
local object = {
|
||||
getType = function()
|
||||
return objectType
|
||||
end,
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
getOffset = function(self)
|
||||
return xOffset, yOffset
|
||||
end,
|
||||
|
||||
setOffset = function(self, xOff, yOff)
|
||||
xOffset = xOff or xOffset
|
||||
yOffset = yOff or yOffset
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getXOffset = function(self)
|
||||
return xOffset
|
||||
end,
|
||||
|
||||
setXOffset = function(self, newXOffset)
|
||||
return self:setOffset(newXOffset, nil)
|
||||
end,
|
||||
|
||||
getYOffset = function(self)
|
||||
return yOffset
|
||||
end,
|
||||
|
||||
setYOffset = function(self, newYOffset)
|
||||
return self:setOffset(nil, newYOffset)
|
||||
end,
|
||||
|
||||
setPalette = function(self, col, ...)
|
||||
if(self==basalt.getActiveFrame())then
|
||||
if(type(col)=="string")then
|
||||
colorTheme[col] = ...
|
||||
termObject.setPaletteColor(type(col)=="number" and col or colors[col], ...)
|
||||
elseif(type(col)=="table")then
|
||||
for k,v in pairs(col)do
|
||||
colorTheme[k] = v
|
||||
if(type(v)=="number")then
|
||||
termObject.setPaletteColor(type(k)=="number" and k or colors[k], v)
|
||||
else
|
||||
local r,g,b = table.unpack(v)
|
||||
termObject.setPaletteColor(type(k)=="number" and k or colors[k], r,g,b)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
setSize = function(self, ...)
|
||||
base.setSize(self, ...)
|
||||
basaltDraw = drawSystem(termObject)
|
||||
return self
|
||||
end,
|
||||
|
||||
getSize = function()
|
||||
return termObject.getSize()
|
||||
end,
|
||||
|
||||
getWidth = function(self)
|
||||
return ({termObject.getSize()})[1]
|
||||
end,
|
||||
|
||||
getHeight = function(self)
|
||||
return ({termObject.getSize()})[2]
|
||||
end,
|
||||
|
||||
show = function(self)
|
||||
base.show(self)
|
||||
basalt.setActiveFrame(self)
|
||||
for k,v in pairs(colors)do
|
||||
if(type(v)=="number")then
|
||||
termObject.setPaletteColor(v, colors.packRGB(term.nativePaletteColor((v))))
|
||||
end
|
||||
end
|
||||
for k,v in pairs(colorTheme)do
|
||||
if(type(v)=="number")then
|
||||
termObject.setPaletteColor(type(k)=="number" and k or colors[k], v)
|
||||
else
|
||||
local r,g,b = table.unpack(v)
|
||||
termObject.setPaletteColor(type(k)=="number" and k or colors[k], r,g,b)
|
||||
end
|
||||
end
|
||||
basalt.setMainFrame(self)
|
||||
return self
|
||||
end,
|
||||
|
||||
render = function(self)
|
||||
if(base.render~=nil)then
|
||||
if(self:isVisible())then
|
||||
if(updateRender)then
|
||||
base.render(self)
|
||||
local children = self:getChildren()
|
||||
for _, child in ipairs(children) do
|
||||
if (child.element.render ~= nil) then
|
||||
child.element:render()
|
||||
end
|
||||
end
|
||||
updateRender = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
updateDraw = function(self)
|
||||
updateRender = true
|
||||
return self
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, ...)
|
||||
base.eventHandler(self, event, ...)
|
||||
if(event=="term_resize")then
|
||||
self:setSize(termObject.getSize())
|
||||
end
|
||||
end,
|
||||
|
||||
updateTerm = function(self)
|
||||
if(basaltDraw~=nil)then
|
||||
basaltDraw.update()
|
||||
end
|
||||
end,
|
||||
|
||||
setTerm = function(self, newTerm)
|
||||
termObject = newTerm
|
||||
if(newTerm==nil)then
|
||||
basaltDraw = nil
|
||||
else
|
||||
basaltDraw = drawSystem(termObject)
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
getTerm = function()
|
||||
return termObject
|
||||
end,
|
||||
|
||||
blit = function (self, x, y, t, f, b)
|
||||
local obx, oby = self:getPosition()
|
||||
local w, h = self:getSize()
|
||||
if y >= 1 and y <= h then
|
||||
local t_visible = sub(t, max(1 - x + 1, 1), max(w - x + 1, 1))
|
||||
local f_visible = sub(f, max(1 - x + 1, 1), max(w - x + 1, 1))
|
||||
local b_visible = sub(b, max(1 - x + 1, 1), max(w - x + 1, 1))
|
||||
basaltDraw.blit(max(x + (obx - 1), obx), oby + y - 1, t_visible, f_visible, b_visible)
|
||||
end
|
||||
end,
|
||||
|
||||
setCursor = function(self, _blink, _xCursor, _yCursor, color)
|
||||
local obx, oby = self:getAbsolutePosition()
|
||||
local xO, yO = self:getOffset()
|
||||
cursorBlink = _blink or false
|
||||
if (_xCursor ~= nil) then
|
||||
xCursor = obx + _xCursor - 1 - xO
|
||||
end
|
||||
if (_yCursor ~= nil) then
|
||||
yCursor = oby + _yCursor - 1 - yO
|
||||
end
|
||||
cursorColor = color or cursorColor
|
||||
if (cursorBlink) then
|
||||
termObject.setTextColor(cursorColor)
|
||||
termObject.setCursorPos(xCursor, yCursor)
|
||||
termObject.setCursorBlink(cursorBlink)
|
||||
else
|
||||
termObject.setCursorBlink(false)
|
||||
end
|
||||
return self
|
||||
end,
|
||||
}
|
||||
|
||||
for k,v in pairs({mouse_click={"mouseHandler", true},mouse_up={"mouseUpHandler", false},mouse_drag={"dragHandler", false},mouse_scroll={"scrollHandler", true},mouse_hover={"hoverHandler", false}})do
|
||||
object[v[1]] = function(self, btn, x, y, ...)
|
||||
if(base[v[1]](self, btn, x, y, ...))then
|
||||
basalt.setActiveFrame(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for k,v in pairs({"drawBackgroundBox", "drawForegroundBox", "drawTextBox"})do
|
||||
object[v] = function(self, x, y, width, height, symbol)
|
||||
local obx, oby = self:getPosition()
|
||||
local w, h = self:getSize()
|
||||
height = (y < 1 and (height + y > self:getHeight() and self:getHeight() or height + y - 1) or (height + y > self:getHeight() and self:getHeight() - y + 1 or height))
|
||||
width = (x < 1 and (width + x > self:getWidth() and self:getWidth() or width + x - 1) or (width + x > self:getWidth() and self:getWidth() - x + 1 or width))
|
||||
basaltDraw[v](max(x + (obx - 1), obx), max(y + (oby - 1), oby), width, height, symbol)
|
||||
end
|
||||
end
|
||||
|
||||
for k,v in pairs({"setBG", "setFG", "setText"}) do
|
||||
object[v] = function(self, x, y, str)
|
||||
local obx, oby = self:getPosition()
|
||||
local w, h = self:getSize()
|
||||
if (y >= 1) and (y <= h) then
|
||||
basaltDraw[v](max(x + (obx - 1), obx), oby + y - 1, sub(str, max(1 - x + 1, 1), max(w - x + 1,1)))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,73 +1,77 @@
|
||||
local Object = require("Object")
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.getValueFromXML
|
||||
local tHex = require("tHex")
|
||||
|
||||
return function(name)
|
||||
return function(name, basalt)
|
||||
-- Button
|
||||
local base = Object(name)
|
||||
local base = basalt.getObject("VisualObject")(name, basalt)
|
||||
local objectType = "Button"
|
||||
local textHorizontalAlign = "center"
|
||||
local textVerticalAlign = "center"
|
||||
|
||||
local text = "Button"
|
||||
|
||||
base:setSize(12, 3)
|
||||
base:setZIndex(5)
|
||||
base:setValue("Button")
|
||||
base.width = 12
|
||||
base.height = 3
|
||||
|
||||
local object = {
|
||||
init = function(self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("ButtonBG")
|
||||
self.fgColor = self.parent:getTheme("ButtonText")
|
||||
end
|
||||
end,
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
end,
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
getHorizontalAlign = function(self)
|
||||
return textHorizontalAlign
|
||||
end,
|
||||
|
||||
setHorizontalAlign = function(self, pos)
|
||||
textHorizontalAlign = pos
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getVerticalAlign = function(self)
|
||||
return textVerticalAlign
|
||||
end,
|
||||
|
||||
setVerticalAlign = function(self, pos)
|
||||
textVerticalAlign = pos
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setText = function(self, text)
|
||||
base:setValue(text)
|
||||
getText = function(self)
|
||||
return text
|
||||
end,
|
||||
|
||||
setText = function(self, newText)
|
||||
text = newText
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("text", data)~=nil)then self:setText(xmlValue("text", data)) end
|
||||
if(xmlValue("horizontalAlign", data)~=nil)then textHorizontalAlign = xmlValue("horizontalAlign", data) end
|
||||
if(xmlValue("verticalAlign", data)~=nil)then textVerticalAlign = xmlValue("verticalAlign", data) end
|
||||
return self
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local w,h = self:getSize()
|
||||
local verticalAlign = utils.getTextVerticalAlign(h, textVerticalAlign)
|
||||
|
||||
for n = 1, h do
|
||||
if (n == verticalAlign) then
|
||||
self.parent:setText(obx, oby + (n - 1), utils.getTextHorizontalAlign(self:getValue(), w, textHorizontalAlign))
|
||||
self.parent:setFG(obx, oby + (n - 1), utils.getTextHorizontalAlign(tHex[self.fgColor]:rep(self:getValue():len()), w, textHorizontalAlign))
|
||||
end
|
||||
end
|
||||
base.draw(self)
|
||||
self:addDraw("button", function()
|
||||
local w,h = self:getSize()
|
||||
local verticalAlign = utils.getTextVerticalAlign(h, textVerticalAlign)
|
||||
local xOffset
|
||||
if(textHorizontalAlign=="center")then
|
||||
xOffset = math.floor((w - text:len()) / 2)
|
||||
elseif(textHorizontalAlign=="right")then
|
||||
xOffset = w - text:len()
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
self:addText(xOffset + 1, verticalAlign, text)
|
||||
self:addFG(xOffset + 1, verticalAlign, tHex[self:getForeground() or colors.white]:rep(text:len()))
|
||||
end)
|
||||
end,
|
||||
}
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
40
Basalt/objects/ChangeableObject.lua
Normal file
@@ -0,0 +1,40 @@
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("VisualObject")(name, basalt)
|
||||
-- Base object
|
||||
local objectType = "ChangeableObject"
|
||||
|
||||
local value
|
||||
|
||||
local object = {
|
||||
setValue = function(self, _value, valueChangedHandler)
|
||||
if (value ~= _value) then
|
||||
value = _value
|
||||
self:updateDraw()
|
||||
if(valueChangedHandler~=false)then
|
||||
self:valueChangedHandler()
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
getValue = function(self)
|
||||
return value
|
||||
end,
|
||||
|
||||
onChange = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("value_changed", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
valueChangedHandler = function(self)
|
||||
self:sendEvent("value_changed", value)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,31 +1,79 @@
|
||||
local Object = require("Object")
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.getValueFromXML
|
||||
local tHex = require("tHex")
|
||||
|
||||
return function(name)
|
||||
return function(name, basalt)
|
||||
-- Checkbox
|
||||
local base = Object(name)
|
||||
local base = basalt.getObject("ChangeableObject")(name, basalt)
|
||||
local objectType = "Checkbox"
|
||||
|
||||
base:setZIndex(5)
|
||||
base:setValue(false)
|
||||
base.width = 1
|
||||
base.height = 1
|
||||
base:setSize(1, 1)
|
||||
|
||||
local symbol = "\42"
|
||||
local symbol,inactiveSymbol,text,textPos = "\42"," ","","right"
|
||||
|
||||
local object = {
|
||||
load = function(self)
|
||||
self:listenEvent("mouse_click", self)
|
||||
self:listenEvent("mouse_up", self)
|
||||
end,
|
||||
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
end,
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
setSymbol = function(self, sym)
|
||||
symbol = sym
|
||||
setSymbol = function(self, sym, inactive)
|
||||
symbol = sym or symbol
|
||||
inactiveSymbol = inactive or inactiveSymbol
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setActiveSymbol = function(self, sym)
|
||||
return self:setSymbol(sym, nil)
|
||||
end,
|
||||
|
||||
setInactiveSymbol = function(self, inactive)
|
||||
return self:setSymbol(nil, inactive)
|
||||
end,
|
||||
|
||||
getSymbol = function(self)
|
||||
return symbol, inactiveSymbol
|
||||
end,
|
||||
|
||||
getActiveSymbol = function(self)
|
||||
return symbol
|
||||
end,
|
||||
|
||||
getInactiveSymbol = function(self)
|
||||
return inactiveSymbol
|
||||
end,
|
||||
|
||||
setText = function(self, _text)
|
||||
text = _text
|
||||
return self
|
||||
end,
|
||||
|
||||
getText = function(self)
|
||||
return text
|
||||
end,
|
||||
|
||||
setTextPosition = function(self, pos)
|
||||
textPos = pos or textPos
|
||||
return self
|
||||
end,
|
||||
|
||||
getTextPosition = function(self)
|
||||
return textPos
|
||||
end,
|
||||
|
||||
setChecked = base.setValue,
|
||||
|
||||
getChecked = base.getValue,
|
||||
|
||||
mouseHandler = function(self, button, x, y)
|
||||
if (base.mouseHandler(self, button, x, y)) then
|
||||
if(button == 1)then
|
||||
@@ -41,45 +89,26 @@ return function(name)
|
||||
return false
|
||||
end,
|
||||
|
||||
touchHandler = function(self, x, y)
|
||||
return self:mouseHandler(1, x, y)
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("checked", data)~=nil)then if(xmlValue("checked", data))then self:setValue(true) else self:setValue(false) end end
|
||||
return self
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local w,h = self:getSize()
|
||||
local verticalAlign = utils.getTextVerticalAlign(h, "center")
|
||||
if(self.bgColor~=false)then self.parent:drawBackgroundBox(obx, oby, w, h, self.bgColor) end
|
||||
for n = 1, h do
|
||||
if (n == verticalAlign) then
|
||||
if (self:getValue() == true) then
|
||||
self.parent:writeText(obx, oby + (n - 1), utils.getTextHorizontalAlign(symbol, w, "center"), self.bgColor, self.fgColor)
|
||||
else
|
||||
self.parent:writeText(obx, oby + (n - 1), utils.getTextHorizontalAlign(" ", w, "center"), self.bgColor, self.fgColor)
|
||||
end
|
||||
end
|
||||
end
|
||||
base.draw(self)
|
||||
self:addDraw("checkbox", function()
|
||||
local obx, oby = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
local verticalAlign = utils.getTextVerticalAlign(h, "center")
|
||||
local bg,fg = self:getBackground(), self:getForeground()
|
||||
if (self:getValue()) then
|
||||
self:addBlit(1, verticalAlign, utils.getTextHorizontalAlign(symbol, w, "center"), tHex[fg], tHex[bg])
|
||||
else
|
||||
self:addBlit(1, verticalAlign, utils.getTextHorizontalAlign(inactiveSymbol, w, "center"), tHex[fg], tHex[bg])
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
self.parent:addEvent("mouse_up", self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("CheckboxBG")
|
||||
self.fgColor = self.parent:getTheme("CheckboxText")
|
||||
end
|
||||
if(text~="")then
|
||||
local align = textPos=="left" and -text:len() or 3
|
||||
self:addText(align, verticalAlign, text)
|
||||
end
|
||||
end)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
428
Basalt/objects/Container.lua
Normal file
@@ -0,0 +1,428 @@
|
||||
local utils = require("utils")
|
||||
local tableCount = utils.tableCount
|
||||
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("VisualObject")(name, basalt)
|
||||
local objectType = "Container"
|
||||
|
||||
local children = {}
|
||||
|
||||
local events = {}
|
||||
|
||||
local container = {}
|
||||
|
||||
local focusedChild
|
||||
local sorted = true
|
||||
local objId, evId = 0, 0
|
||||
|
||||
local objSort = function(a, b)
|
||||
if a.zIndex == b.zIndex then
|
||||
return a.objId < b.objId
|
||||
else
|
||||
return a.zIndex < b.zIndex
|
||||
end
|
||||
end
|
||||
local evSort = function(a, b)
|
||||
if a.zIndex == b.zIndex then
|
||||
return a.evId > b.evId
|
||||
else
|
||||
return a.zIndex > b.zIndex
|
||||
end
|
||||
end
|
||||
|
||||
local function getChildren(self)
|
||||
self:sortChildren()
|
||||
return children
|
||||
end
|
||||
|
||||
local function getChild(self, name)
|
||||
for _, v in ipairs(children) do
|
||||
if v.element:getName() == name then
|
||||
return v.element
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function getDeepChild(self, name)
|
||||
local maybeChild = self:getChild(name)
|
||||
if (maybeChild ~= nil) then
|
||||
return maybeChild
|
||||
end
|
||||
for _, child in ipairs(children) do
|
||||
if (child:getType() == "Container") then
|
||||
local maybeDeepChild = child:getDeepChild(name)
|
||||
if (maybeDeepChild ~= nil) then
|
||||
return maybeDeepChild
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function addChild(self, element)
|
||||
if (getChild(element:getName()) ~= nil) then
|
||||
return
|
||||
end
|
||||
objId = objId + 1
|
||||
local zIndex = element:getZIndex()
|
||||
table.insert(children, {element = element, zIndex = zIndex, objId = objId})
|
||||
sorted = false
|
||||
element:setParent(self, true)
|
||||
for event, _ in pairs(element:getRegisteredEvents()) do
|
||||
self:addEvent(event, element)
|
||||
end
|
||||
if (element.init~=nil) then
|
||||
element:init()
|
||||
end
|
||||
if (element.load~=nil) then
|
||||
element:load()
|
||||
end
|
||||
if (element.draw~=nil) then
|
||||
element:draw()
|
||||
end
|
||||
return element
|
||||
end
|
||||
|
||||
local function removeChild(self, element)
|
||||
if (type(element)=="string") then
|
||||
element = self:getChild(element)
|
||||
end
|
||||
if (element==nil) then
|
||||
return
|
||||
end
|
||||
for i, v in ipairs(children) do
|
||||
if v.element:getName() == element:getName() then
|
||||
table.remove(children, i)
|
||||
self:removeEvents(element)
|
||||
sorted = false
|
||||
self:updateDraw()
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function removeChildren(self)
|
||||
local parent = self:getParent()
|
||||
children = {}
|
||||
events = {}
|
||||
sorted = false
|
||||
objId = 0
|
||||
evId = 0
|
||||
focusedChild = nil
|
||||
parent:removeEvents(self)
|
||||
end
|
||||
|
||||
local function updateZIndex(self, element, newZ)
|
||||
objId = objId + 1
|
||||
evId = evId + 1
|
||||
for _,v in pairs(children)do
|
||||
if(v.element==element)then
|
||||
v.zIndex = newZ
|
||||
v.objId = objId
|
||||
break
|
||||
end
|
||||
end
|
||||
for _,v in pairs(events)do
|
||||
for a,b in pairs(v)do
|
||||
if(b.element==element)then
|
||||
b.zIndex = newZ
|
||||
b.evId = evId
|
||||
end
|
||||
end
|
||||
end
|
||||
sorted = false
|
||||
self:updateDraw()
|
||||
end
|
||||
|
||||
local function removeEvents(self, element)
|
||||
local parent = self:getParent()
|
||||
for a, b in pairs(events) do
|
||||
for c, d in pairs(b) do
|
||||
if(d.element == element)then
|
||||
table.remove(events[a], c)
|
||||
end
|
||||
end
|
||||
if(tableCount(events[a])<=0)then
|
||||
if(parent~=nil)then
|
||||
parent:removeEvent(a, self)
|
||||
end
|
||||
end
|
||||
end
|
||||
sorted = false
|
||||
end
|
||||
|
||||
local function getEvent(self, event, name)
|
||||
if(type(name)=="table")then name = name:getName() end
|
||||
if(events[event]~=nil)then
|
||||
for _, obj in pairs(events[event]) do
|
||||
if (obj.element:getName() == name) then
|
||||
return obj
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function addEvent(self, event, element)
|
||||
if (getEvent(self, event, element:getName()) ~= nil) then
|
||||
return
|
||||
end
|
||||
local zIndex = element:getZIndex()
|
||||
evId = evId + 1
|
||||
if(events[event]==nil)then events[event] = {} end
|
||||
table.insert(events[event], {element = element, zIndex = zIndex, evId = evId})
|
||||
sorted = false
|
||||
self:listenEvent(event)
|
||||
return element
|
||||
end
|
||||
|
||||
local function removeEvent(self, event, element)
|
||||
if(events[event]~=nil)then
|
||||
for a, b in pairs(events[event]) do
|
||||
if(b.element == element)then
|
||||
table.remove(events[event], a)
|
||||
end
|
||||
end
|
||||
if(tableCount(events[event])<=0)then
|
||||
self:listenEvent(event, false)
|
||||
end
|
||||
end
|
||||
sorted = false
|
||||
end
|
||||
|
||||
local function getEvents(self, event)
|
||||
return event~=nil and events[event] or events
|
||||
end
|
||||
|
||||
container = {
|
||||
getType = function()
|
||||
return objectType
|
||||
end,
|
||||
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
setSize = function(self, ...)
|
||||
base.setSize(self, ...)
|
||||
self:customEventHandler("basalt_FrameResize")
|
||||
return self
|
||||
end,
|
||||
|
||||
setPosition = function(self, ...)
|
||||
base.setPosition(self, ...)
|
||||
self:customEventHandler("basalt_FrameReposition")
|
||||
return self
|
||||
end,
|
||||
|
||||
searchChildren = function(self, name)
|
||||
local results = {}
|
||||
for _, child in pairs(children) do
|
||||
if (string.find(child.element:getName(), name)) then
|
||||
table.insert(results, child)
|
||||
end
|
||||
end
|
||||
return results
|
||||
end,
|
||||
|
||||
getChildrenByType = function(self, type)
|
||||
local results = {}
|
||||
for _, child in pairs(children) do
|
||||
if (child.element:isType(type)) then
|
||||
table.insert(results, child)
|
||||
end
|
||||
end
|
||||
return results
|
||||
end,
|
||||
|
||||
setImportant = function(self, element)
|
||||
objId = objId + 1
|
||||
evId = evId + 1
|
||||
for a, b in pairs(events) do
|
||||
for c, d in pairs(b) do
|
||||
if(d.element == element)then
|
||||
d.evId = evId
|
||||
table.remove(events[a], c)
|
||||
table.insert(events[a], d)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
for i, v in ipairs(children) do
|
||||
if v.element == element then
|
||||
v.objId = objId
|
||||
table.remove(children, i)
|
||||
table.insert(children, v)
|
||||
break
|
||||
end
|
||||
end
|
||||
if(self.updateDraw~=nil)then
|
||||
self:updateDraw()
|
||||
end
|
||||
sorted = false
|
||||
end,
|
||||
|
||||
sortChildren = function(self)
|
||||
if (sorted) then
|
||||
return
|
||||
end
|
||||
table.sort(children, objSort)
|
||||
for event, _ in pairs(events) do
|
||||
table.sort(events[event], evSort)
|
||||
end
|
||||
sorted = true
|
||||
end,
|
||||
|
||||
clearFocusedChild = function(self)
|
||||
if(focusedChild~=nil)then
|
||||
if(getChild(self, focusedChild)~=nil)then
|
||||
focusedChild:loseFocusHandler()
|
||||
end
|
||||
end
|
||||
focusedChild = nil
|
||||
return self
|
||||
end,
|
||||
|
||||
setFocusedChild = function(self, obj)
|
||||
if(focusedChild~=obj)then
|
||||
if(focusedChild~=nil)then
|
||||
if(getChild(self, focusedChild)~=nil)then
|
||||
focusedChild:loseFocusHandler()
|
||||
end
|
||||
end
|
||||
if(obj~=nil)then
|
||||
if(getChild(self, obj)~=nil)then
|
||||
obj:getFocusHandler()
|
||||
end
|
||||
end
|
||||
focusedChild = obj
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
getFocused = function(self)
|
||||
return focusedChild
|
||||
end,
|
||||
|
||||
getChild = getChild,
|
||||
getChildren = getChildren,
|
||||
getDeepChildren = getDeepChild,
|
||||
addChild = addChild,
|
||||
removeChild = removeChild,
|
||||
removeChildren = removeChildren,
|
||||
getEvents = getEvents,
|
||||
getEvent = getEvent,
|
||||
addEvent = addEvent,
|
||||
removeEvent = removeEvent,
|
||||
removeEvents = removeEvents,
|
||||
updateZIndex = updateZIndex,
|
||||
|
||||
listenEvent = function(self, event, active)
|
||||
base.listenEvent(self, event, active)
|
||||
if(events[event]==nil)then events[event] = {} end
|
||||
return self
|
||||
end,
|
||||
|
||||
customEventHandler = function(self, ...)
|
||||
base.customEventHandler(self, ...)
|
||||
for _, o in pairs(children) do
|
||||
if (o.element.customEventHandler ~= nil) then
|
||||
o.element:customEventHandler(...)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
loseFocusHandler = function(self)
|
||||
base.loseFocusHandler(self)
|
||||
if(focusedChild~=nil)then focusedChild:loseFocusHandler() focusedChild = nil end
|
||||
end,
|
||||
|
||||
getBasalt = function(self)
|
||||
return basalt
|
||||
end,
|
||||
|
||||
setPalette = function(self, col, ...)
|
||||
local parent = self:getParent()
|
||||
parent:setPalette(col, ...)
|
||||
return self
|
||||
end,
|
||||
|
||||
eventHandler = function(self, ...)
|
||||
if(base.eventHandler~=nil)then
|
||||
base.eventHandler(self, ...)
|
||||
if(events["other_event"]~=nil)then
|
||||
self:sortChildren()
|
||||
for _, obj in ipairs(events["other_event"]) do
|
||||
if (obj.element.eventHandler ~= nil) then
|
||||
obj.element.eventHandler(obj.element, ...)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
for k,v in pairs({mouse_click={"mouseHandler", true},mouse_up={"mouseUpHandler", false},mouse_drag={"dragHandler", false},mouse_scroll={"scrollHandler", true},mouse_hover={"hoverHandler", false}})do
|
||||
container[v[1]] = function(self, btn, x, y, ...)
|
||||
if(base[v[1]]~=nil)then
|
||||
if(base[v[1]](self, btn, x, y, ...))then
|
||||
if(events[k]~=nil)then
|
||||
self:sortChildren()
|
||||
for _, obj in ipairs(events[k]) do
|
||||
if (obj.element[v[1]] ~= nil) then
|
||||
local xO, yO = 0, 0
|
||||
if(self.getOffset~=nil)then
|
||||
xO, yO = self:getOffset()
|
||||
end
|
||||
if(obj.element.getIgnoreOffset~=nil)then
|
||||
if(obj.element.getIgnoreOffset())then
|
||||
xO, yO = 0, 0
|
||||
end
|
||||
end
|
||||
if (obj.element[v[1]](obj.element, btn, x+xO, y+yO, ...)) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
if(v[2])then
|
||||
self:clearFocusedChild()
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for k,v in pairs({key="keyHandler",key_up="keyUpHandler",char="charHandler"})do
|
||||
container[v] = function(self, ...)
|
||||
if(base[v]~=nil)then
|
||||
if(base[v](self, ...))then
|
||||
if(events[k]~=nil)then
|
||||
self:sortChildren()
|
||||
for _, obj in ipairs(events[k]) do
|
||||
if (obj.element[v] ~= nil) then
|
||||
if (obj.element[v](obj.element, ...)) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for objectName, _ in pairs(basalt.getObjects()) do
|
||||
container["add" .. objectName] = function(self, id)
|
||||
return self:addChild(basalt.createObject(objectName, id))
|
||||
end
|
||||
end
|
||||
|
||||
container.__index = container
|
||||
return setmetatable(container, base)
|
||||
end
|
||||
@@ -1,23 +1,20 @@
|
||||
local Object = require("Object")
|
||||
local utils = require("utils")
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
local tHex = require("tHex")
|
||||
|
||||
return function(name)
|
||||
local base = Object(name)
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("List")(name, basalt)
|
||||
local objectType = "Dropdown"
|
||||
base.width = 12
|
||||
base.height = 1
|
||||
|
||||
base:setSize(12, 1)
|
||||
base:setZIndex(6)
|
||||
|
||||
local list = {}
|
||||
local itemSelectedBG
|
||||
local itemSelectedFG
|
||||
local selectionColorActive = true
|
||||
local align = "left"
|
||||
local yOffset = 0
|
||||
|
||||
local dropdownW = 16
|
||||
local dropdownH = 6
|
||||
local dropdownW = 0
|
||||
local dropdownH = 0
|
||||
local autoSize = true
|
||||
local closedSymbol = "\16"
|
||||
local openedSymbol = "\31"
|
||||
local isOpened = false
|
||||
@@ -25,113 +22,106 @@ return function(name)
|
||||
local object = {
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("selectionBG", data)~=nil)then itemSelectedBG = colors[xmlValue("selectionBG", data)] end
|
||||
if(xmlValue("selectionFG", data)~=nil)then itemSelectedFG = colors[xmlValue("selectionFG", data)] end
|
||||
if(xmlValue("dropdownWidth", data)~=nil)then dropdownW = xmlValue("dropdownWidth", data) end
|
||||
if(xmlValue("dropdownHeight", data)~=nil)then dropdownH = xmlValue("dropdownHeight", data) end
|
||||
if(xmlValue("offset", data)~=nil)then yOffset = xmlValue("offset", data) end
|
||||
if(data["item"]~=nil)then
|
||||
local tab = data["item"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
self:addItem(xmlValue("text", v), colors[xmlValue("bg", v)], colors[xmlValue("fg", v)])
|
||||
end
|
||||
end
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
load = function(self)
|
||||
self:listenEvent("mouse_click", self)
|
||||
self:listenEvent("mouse_up", self)
|
||||
self:listenEvent("mouse_scroll", self)
|
||||
self:listenEvent("mouse_drag", self)
|
||||
end,
|
||||
|
||||
setOffset = function(self, yOff)
|
||||
yOffset = yOff
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getOffset = function(self)
|
||||
return yOffset
|
||||
end;
|
||||
end,
|
||||
|
||||
addItem = function(self, text, bgCol, fgCol, ...)
|
||||
table.insert(list, { text = text, bgCol = bgCol or self.bgColor, fgCol = fgCol or self.fgColor, args = { ... } })
|
||||
self:updateDraw()
|
||||
addItem = function(self, t, ...)
|
||||
base.addItem(self, t, ...)
|
||||
if(autoSize)then
|
||||
dropdownW = math.max(dropdownW, #t)
|
||||
dropdownH = dropdownH + 1
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
getAll = function(self)
|
||||
return list
|
||||
end;
|
||||
end,
|
||||
|
||||
removeItem = function(self, index)
|
||||
table.remove(list, index)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getItem = function(self, index)
|
||||
return list[index]
|
||||
end;
|
||||
|
||||
getItemIndex = function(self)
|
||||
local selected = self:getValue()
|
||||
for key, value in pairs(list) do
|
||||
if (value == selected) then
|
||||
return key
|
||||
base.removeItem(self, index)
|
||||
if(autoSize)then
|
||||
dropdownW = 0
|
||||
dropdownH = 0
|
||||
for n = 1, #list do
|
||||
dropdownW = math.max(dropdownW, #list[n].text)
|
||||
end
|
||||
dropdownH = #list
|
||||
end
|
||||
end;
|
||||
end,
|
||||
|
||||
clear = function(self)
|
||||
list = {}
|
||||
self:setValue({})
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
isOpened = function(self)
|
||||
return isOpened
|
||||
end,
|
||||
|
||||
getItemCount = function(self)
|
||||
return #list
|
||||
end;
|
||||
|
||||
editItem = function(self, index, text, bgCol, fgCol, ...)
|
||||
table.remove(list, index)
|
||||
table.insert(list, index, { text = text, bgCol = bgCol or self.bgColor, fgCol = fgCol or self.fgColor, args = { ... } })
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
selectItem = function(self, index)
|
||||
self:setValue(list[index] or {})
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setSelectedItem = function(self, bgCol, fgCol, active)
|
||||
itemSelectedBG = bgCol or self.bgColor
|
||||
itemSelectedFG = fgCol or self.fgColor
|
||||
selectionColorActive = active
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setDropdownSize = function(self, width, height)
|
||||
dropdownW, dropdownH = width, height
|
||||
setOpened = function(self, open)
|
||||
isOpened = open
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y)
|
||||
setDropdownSize = function(self, width, height)
|
||||
dropdownW, dropdownH = width, height
|
||||
autoSize = false
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setDropdownWidth = function(self, width)
|
||||
return self:setDropdownSize(width, dropdownH)
|
||||
end,
|
||||
|
||||
setDropdownHeight = function(self, height)
|
||||
return self:setDropdownSize(dropdownW, height)
|
||||
end,
|
||||
|
||||
getDropdownSize = function(self)
|
||||
return dropdownW, dropdownH
|
||||
end,
|
||||
|
||||
getDropdownWidth = function(self)
|
||||
return dropdownW
|
||||
end,
|
||||
|
||||
getDropdownHeight = function(self)
|
||||
return dropdownH
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y, isMon)
|
||||
if (isOpened) then
|
||||
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
local obx, oby = self:getAbsolutePosition()
|
||||
if(button==1)then
|
||||
local list = self:getAll()
|
||||
if (#list > 0) then
|
||||
for n = 1, dropdownH do
|
||||
if (list[n + yOffset] ~= nil) then
|
||||
if (obx <= x) and (obx + dropdownW > x) and (oby + n == y) then
|
||||
self:setValue(list[n + yOffset])
|
||||
self:updateDraw()
|
||||
local val = self:getEventSystem():sendEvent("mouse_click", self, "mouse_click", dir, x, y)
|
||||
local val = self:sendEvent("mouse_click", self, "mouse_click", button, x, y)
|
||||
if(val==false)then return val end
|
||||
if(isMon)then
|
||||
basalt.schedule(function()
|
||||
sleep(0.1)
|
||||
self:mouseUpHandler(button, x, y)
|
||||
end)()
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
@@ -139,8 +129,10 @@ return function(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
local base = base:getBase()
|
||||
if (base.mouseHandler(self, button, x, y)) then
|
||||
isOpened = (not isOpened)
|
||||
isOpened = not isOpened
|
||||
self:getParent():setImportant(self)
|
||||
self:updateDraw()
|
||||
return true
|
||||
else
|
||||
@@ -154,15 +146,16 @@ return function(name)
|
||||
|
||||
mouseUpHandler = function(self, button, x, y)
|
||||
if (isOpened) then
|
||||
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
local obx, oby = self:getAbsolutePosition()
|
||||
if(button==1)then
|
||||
local list = self:getAll()
|
||||
if (#list > 0) then
|
||||
for n = 1, dropdownH do
|
||||
if (list[n + yOffset] ~= nil) then
|
||||
if (obx <= x) and (obx + dropdownW > x) and (oby + n == y) then
|
||||
isOpened = false
|
||||
self:updateDraw()
|
||||
local val = self:getEventSystem():sendEvent("mouse_up", self, "mouse_up", dir, x, y)
|
||||
local val = self:sendEvent("mouse_up", self, "mouse_up", button, x, y)
|
||||
if(val==false)then return val end
|
||||
return true
|
||||
end
|
||||
@@ -173,8 +166,27 @@ return function(name)
|
||||
end
|
||||
end,
|
||||
|
||||
dragHandler = function(self, btn, x, y)
|
||||
if(base.dragHandler(self, btn, x, y))then
|
||||
isOpened = true
|
||||
end
|
||||
end,
|
||||
|
||||
scrollHandler = function(self, dir, x, y)
|
||||
if(isOpened)then
|
||||
local xPos, yPos = self:getAbsolutePosition()
|
||||
if(x >= xPos)and(x <= xPos + dropdownW)and(y >= yPos)and(y <= yPos + dropdownH)then
|
||||
self:setFocus()
|
||||
end
|
||||
end
|
||||
if (isOpened)and(self:isFocused()) then
|
||||
local xPos, yPos = self:getAbsolutePosition()
|
||||
if(x < xPos)or(x > xPos + dropdownW)or(y < yPos)or(y > yPos + dropdownH)then
|
||||
return false
|
||||
end
|
||||
if(#self:getAll() <= dropdownH)then return false end
|
||||
|
||||
local list = self:getAll()
|
||||
yOffset = yOffset + dir
|
||||
if (yOffset < 0) then
|
||||
yOffset = 0
|
||||
@@ -188,7 +200,7 @@ return function(name)
|
||||
yOffset = math.min(#list - 1, 0)
|
||||
end
|
||||
end
|
||||
local val = self:getEventSystem():sendEvent("mouse_scroll", self, "mouse_scroll", dir, x, y)
|
||||
local val = self:sendEvent("mouse_scroll", self, "mouse_scroll", dir, x, y)
|
||||
if(val==false)then return val end
|
||||
self:updateDraw()
|
||||
return true
|
||||
@@ -196,46 +208,41 @@ return function(name)
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
base.draw(self)
|
||||
self:setDrawState("list", false)
|
||||
self:addDraw("dropdown", function()
|
||||
local obx, oby = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
if (self.parent ~= nil) then
|
||||
if(self.bgColor~=false)then self.parent:drawBackgroundBox(obx, oby, w, h, self.bgColor) end
|
||||
local val = self:getValue()
|
||||
local text = utils.getTextHorizontalAlign((val~=nil and val.text or ""), w, align):sub(1, w - 1) .. (isOpened and openedSymbol or closedSymbol)
|
||||
self.parent:writeText(obx, oby, text, self.bgColor, self.fgColor)
|
||||
local val = self:getValue()
|
||||
local list = self:getAll()
|
||||
local bgCol, fgCol = self:getBackground(), self:getForeground()
|
||||
local text = utils.getTextHorizontalAlign((val~=nil and val.text or ""), w, align):sub(1, w - 1) .. (isOpened and openedSymbol or closedSymbol)
|
||||
self:addBlit(1, 1, text, tHex[fgCol]:rep(#text), tHex[bgCol]:rep(#text))
|
||||
|
||||
if (isOpened) then
|
||||
for n = 1, dropdownH do
|
||||
if (list[n + yOffset] ~= nil) then
|
||||
if (list[n + yOffset] == val) then
|
||||
if (selectionColorActive) then
|
||||
self.parent:writeText(obx, oby + n, utils.getTextHorizontalAlign(list[n + yOffset].text, dropdownW, align), itemSelectedBG, itemSelectedFG)
|
||||
else
|
||||
self.parent:writeText(obx, oby + n, utils.getTextHorizontalAlign(list[n + yOffset].text, dropdownW, align), list[n + yOffset].bgCol, list[n + yOffset].fgCol)
|
||||
end
|
||||
if (isOpened) then
|
||||
self:addTextBox(1, 2, dropdownW, dropdownH, " ")
|
||||
self:addBackgroundBox(1, 2, dropdownW, dropdownH, bgCol)
|
||||
self:addForegroundBox(1, 2, dropdownW, dropdownH, fgCol)
|
||||
for n = 1, dropdownH do
|
||||
if (list[n + yOffset] ~= nil) then
|
||||
local t =utils.getTextHorizontalAlign(list[n + yOffset].text, dropdownW, align)
|
||||
if (list[n + yOffset] == val) then
|
||||
if (selectionColorActive) then
|
||||
local itemSelectedBG, itemSelectedFG = self:getSelectionColor()
|
||||
self:addBlit(1, n+1, t, tHex[itemSelectedFG]:rep(#t), tHex[itemSelectedBG]:rep(#t))
|
||||
else
|
||||
self.parent:writeText(obx, oby + n, utils.getTextHorizontalAlign(list[n + yOffset].text, dropdownW, align), list[n + yOffset].bgCol, list[n + yOffset].fgCol)
|
||||
self:addBlit(1, n+1, t, tHex[list[n + yOffset].fgCol]:rep(#t), tHex[list[n + yOffset].bgCol]:rep(#t))
|
||||
end
|
||||
else
|
||||
self:addBlit(1, n+1, t, tHex[list[n + yOffset].fgCol]:rep(#t), tHex[list[n + yOffset].bgCol]:rep(#t))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
self.parent:addEvent("mouse_up", self)
|
||||
self.parent:addEvent("mouse_scroll", self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("DropdownBG")
|
||||
self.fgColor = self.parent:getTheme("DropdownText")
|
||||
itemSelectedBG = self.parent:getTheme("SelectionBG")
|
||||
itemSelectedFG = self.parent:getTheme("SelectionText")
|
||||
end
|
||||
end)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
452
Basalt/objects/Flexbox.lua
Normal file
@@ -0,0 +1,452 @@
|
||||
|
||||
local function flexObjectPlugin(base, basalt)
|
||||
local flexGrow = 0
|
||||
local flexShrink = 0
|
||||
local flexBasis = 0
|
||||
|
||||
local baseWidth, baseHeight = base:getSize()
|
||||
|
||||
local object = {
|
||||
getFlexGrow = function(self)
|
||||
return flexGrow
|
||||
end,
|
||||
|
||||
setFlexGrow = function(self, value)
|
||||
flexGrow = value
|
||||
return self
|
||||
end,
|
||||
|
||||
getFlexShrink = function(self)
|
||||
return flexShrink
|
||||
end,
|
||||
|
||||
setFlexShrink = function(self, value)
|
||||
flexShrink = value
|
||||
return self
|
||||
end,
|
||||
|
||||
getFlexBasis = function(self)
|
||||
return flexBasis
|
||||
end,
|
||||
|
||||
setFlexBasis = function(self, value)
|
||||
flexBasis = value
|
||||
return self
|
||||
end,
|
||||
|
||||
getSize = function(self)
|
||||
return baseWidth, baseHeight
|
||||
end,
|
||||
|
||||
getWidth = function(self)
|
||||
return baseWidth
|
||||
end,
|
||||
|
||||
getHeight = function(self)
|
||||
return baseHeight
|
||||
end,
|
||||
|
||||
setSize = function(self, width, height, rel, internalCall)
|
||||
base.setSize(self, width, height, rel)
|
||||
if not internalCall then
|
||||
baseWidth, baseHeight = base:getSize()
|
||||
end
|
||||
return self
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("ScrollableFrame")(name, basalt)
|
||||
local objectType = "Flexbox"
|
||||
|
||||
local direction = "row"
|
||||
local spacing = 1
|
||||
local justifyContent = "flex-start"
|
||||
local wrap = "nowrap"
|
||||
local children = {}
|
||||
local sortedChildren = {}
|
||||
local updateLayout = false
|
||||
local lineBreakFakeObject = flexObjectPlugin({
|
||||
getHeight = function(self) return 0 end,
|
||||
getWidth = function(self) return 0 end,
|
||||
getPosition = function(self) return 0, 0 end,
|
||||
getSize = function(self) return 0, 0 end,
|
||||
isType = function(self) return false end,
|
||||
getType = function(self) return "lineBreakFakeObject" end,
|
||||
setPosition = function(self) end,
|
||||
setSize = function(self) end,
|
||||
})
|
||||
lineBreakFakeObject:setFlexBasis(0):setFlexGrow(0):setFlexShrink(0)
|
||||
|
||||
local function sortChildren(self)
|
||||
if(wrap=="nowrap")then
|
||||
sortedChildren = {}
|
||||
local index = 1
|
||||
local lineSize = 1
|
||||
local lineOffset = 1
|
||||
for _,v in pairs(children)do
|
||||
if(sortedChildren[index]==nil)then sortedChildren[index]={offset=1} end
|
||||
|
||||
local childHeight = direction == "row" and v:getHeight() or v:getWidth()
|
||||
if childHeight > lineSize then
|
||||
lineSize = childHeight
|
||||
end
|
||||
if(v == lineBreakFakeObject)then
|
||||
lineOffset = lineOffset + lineSize + spacing
|
||||
lineSize = 1
|
||||
index = index + 1
|
||||
sortedChildren[index] = {offset=lineOffset}
|
||||
else
|
||||
table.insert(sortedChildren[index], v)
|
||||
end
|
||||
end
|
||||
elseif(wrap=="wrap")then
|
||||
sortedChildren = {}
|
||||
local lineSize = 1
|
||||
local lineOffset = 1
|
||||
|
||||
local maxSize = direction == "row" and self:getWidth() or self:getHeight()
|
||||
local usedSize = 0
|
||||
local index = 1
|
||||
|
||||
for _,v in pairs(children) do
|
||||
if(sortedChildren[index]==nil) then sortedChildren[index]={offset=1} end
|
||||
|
||||
if v == lineBreakFakeObject then
|
||||
lineOffset = lineOffset + lineSize + spacing
|
||||
usedSize = 0
|
||||
lineSize = 1
|
||||
index = index + 1
|
||||
sortedChildren[index] = {offset=lineOffset}
|
||||
else
|
||||
local objSize = direction == "row" and v:getWidth() or v:getHeight()
|
||||
if(objSize+usedSize<=maxSize) then
|
||||
table.insert(sortedChildren[index], v)
|
||||
usedSize = usedSize + objSize + spacing
|
||||
else
|
||||
lineOffset = lineOffset + lineSize + spacing
|
||||
lineSize = direction == "row" and v:getHeight() or v:getWidth()
|
||||
index = index + 1
|
||||
usedSize = objSize + spacing
|
||||
sortedChildren[index] = {offset=lineOffset, v}
|
||||
end
|
||||
|
||||
local childHeight = direction == "row" and v:getHeight() or v:getWidth()
|
||||
if childHeight > lineSize then
|
||||
lineSize = childHeight
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function calculateRow(self, children)
|
||||
local containerWidth, containerHeight = self:getSize()
|
||||
local totalFlexGrow = 0
|
||||
local totalFlexShrink = 0
|
||||
local totalFlexBasis = 0
|
||||
|
||||
for _, child in ipairs(children) do
|
||||
totalFlexGrow = totalFlexGrow + child:getFlexGrow()
|
||||
totalFlexShrink = totalFlexShrink + child:getFlexShrink()
|
||||
totalFlexBasis = totalFlexBasis + child:getFlexBasis()
|
||||
end
|
||||
|
||||
local remainingSpace = containerWidth - totalFlexBasis - (spacing * (#children - 1))
|
||||
|
||||
local currentX = 1
|
||||
for _, child in ipairs(children) do
|
||||
if(child~=lineBreakFakeObject)then
|
||||
local childWidth
|
||||
|
||||
local flexGrow = child:getFlexGrow()
|
||||
local flexShrink = child:getFlexShrink()
|
||||
|
||||
local baseWidth = child:getFlexBasis() ~= 0 and child:getFlexBasis() or child:getWidth()
|
||||
if totalFlexGrow > 0 then
|
||||
childWidth = baseWidth + flexGrow / totalFlexGrow * remainingSpace
|
||||
else
|
||||
childWidth = baseWidth
|
||||
end
|
||||
|
||||
if remainingSpace < 0 and totalFlexShrink > 0 then
|
||||
childWidth = baseWidth + flexShrink / totalFlexShrink * remainingSpace
|
||||
end
|
||||
|
||||
child:setPosition(currentX, children.offset or 1)
|
||||
child:setSize(childWidth, child:getHeight(), false, true)
|
||||
currentX = currentX + childWidth + spacing
|
||||
end
|
||||
end
|
||||
|
||||
if justifyContent == "flex-end" then
|
||||
local totalWidth = currentX - spacing
|
||||
local offset = containerWidth - totalWidth + 1
|
||||
for _, child in ipairs(children) do
|
||||
local x, y = child:getPosition()
|
||||
child:setPosition(x + offset, y)
|
||||
end
|
||||
elseif justifyContent == "center" then
|
||||
local totalWidth = currentX - spacing
|
||||
local offset = (containerWidth - totalWidth) / 2 + 1
|
||||
for _, child in ipairs(children) do
|
||||
local x, y = child:getPosition()
|
||||
child:setPosition(x + offset, y)
|
||||
end
|
||||
elseif justifyContent == "space-between" then
|
||||
local totalWidth = currentX - spacing
|
||||
local offset = (containerWidth - totalWidth) / (#children - 1) + 1
|
||||
for i, child in ipairs(children) do
|
||||
if i > 1 then
|
||||
local x, y = child:getPosition()
|
||||
child:setPosition(x + offset * (i - 1), y)
|
||||
end
|
||||
end
|
||||
elseif justifyContent == "space-around" then
|
||||
local totalWidth = currentX - spacing
|
||||
local offset = (containerWidth - totalWidth) / #children
|
||||
for i, child in ipairs(children) do
|
||||
local x, y = child:getPosition()
|
||||
child:setPosition(x + offset * i - offset / 2, y)
|
||||
end
|
||||
elseif justifyContent == "space-evenly" then
|
||||
local numSpaces = #children + 1
|
||||
local totalChildWidth = 0
|
||||
for _, child in ipairs(children) do
|
||||
totalChildWidth = totalChildWidth + child:getWidth()
|
||||
end
|
||||
local totalSpace = containerWidth - totalChildWidth
|
||||
local offset = math.floor(totalSpace / numSpaces)
|
||||
local remaining = totalSpace - offset * numSpaces
|
||||
currentX = offset + (remaining > 0 and 1 or 0)
|
||||
remaining = remaining > 0 and remaining - 1 or 0
|
||||
for _, child in ipairs(children) do
|
||||
child:setPosition(currentX, 1)
|
||||
currentX = currentX + child:getWidth() + offset + (remaining > 0 and 1 or 0)
|
||||
remaining = remaining > 0 and remaining - 1 or 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function calculateColumn(self, children)
|
||||
local containerWidth, containerHeight = self:getSize()
|
||||
local totalFlexGrow = 0
|
||||
local totalFlexShrink = 0
|
||||
local totalFlexBasis = 0
|
||||
|
||||
for _, child in ipairs(children) do
|
||||
totalFlexGrow = totalFlexGrow + child:getFlexGrow()
|
||||
totalFlexShrink = totalFlexShrink + child:getFlexShrink()
|
||||
totalFlexBasis = totalFlexBasis + child:getFlexBasis()
|
||||
end
|
||||
|
||||
local remainingSpace = containerHeight - totalFlexBasis - (spacing * (#children - 1))
|
||||
|
||||
local currentY = 1
|
||||
|
||||
for _, child in ipairs(children) do
|
||||
if(child~=lineBreakFakeObject)then
|
||||
local childHeight
|
||||
|
||||
local flexGrow = child:getFlexGrow()
|
||||
local flexShrink = child:getFlexShrink()
|
||||
|
||||
local baseHeight = child:getFlexBasis() ~= 0 and child:getFlexBasis() or child:getHeight()
|
||||
if totalFlexGrow > 0 then
|
||||
childHeight = baseHeight + flexGrow / totalFlexGrow * remainingSpace
|
||||
else
|
||||
childHeight = baseHeight
|
||||
end
|
||||
|
||||
if remainingSpace < 0 and totalFlexShrink > 0 then
|
||||
childHeight = baseHeight + flexShrink / totalFlexShrink * remainingSpace
|
||||
end
|
||||
|
||||
child:setPosition(children.offset, currentY)
|
||||
child:setSize(child:getWidth(), childHeight, false, true)
|
||||
currentY = currentY + childHeight + spacing
|
||||
end
|
||||
end
|
||||
|
||||
if justifyContent == "flex-end" then
|
||||
local totalHeight = currentY - spacing
|
||||
local offset = containerHeight - totalHeight + 1
|
||||
for _, child in ipairs(children) do
|
||||
local x, y = child:getPosition()
|
||||
child:setPosition(x, y + offset)
|
||||
end
|
||||
elseif justifyContent == "center" then
|
||||
local totalHeight = currentY - spacing
|
||||
local offset = (containerHeight - totalHeight) / 2
|
||||
for _, child in ipairs(children) do
|
||||
local x, y = child:getPosition()
|
||||
child:setPosition(x, y + offset)
|
||||
end
|
||||
elseif justifyContent == "space-between" then
|
||||
local totalHeight = currentY - spacing
|
||||
local offset = (containerHeight - totalHeight) / (#children - 1) + 1
|
||||
for i, child in ipairs(children) do
|
||||
if i > 1 then
|
||||
local x, y = child:getPosition()
|
||||
child:setPosition(x, y + offset * (i - 1))
|
||||
end
|
||||
end
|
||||
elseif justifyContent == "space-around" then
|
||||
local totalHeight = currentY - spacing
|
||||
local offset = (containerHeight - totalHeight) / #children
|
||||
for i, child in ipairs(children) do
|
||||
local x, y = child:getPosition()
|
||||
child:setPosition(x, y + offset * i - offset / 2)
|
||||
end
|
||||
elseif justifyContent == "space-evenly" then
|
||||
local numSpaces = #children + 1
|
||||
local totalChildHeight = 0
|
||||
for _, child in ipairs(children) do
|
||||
totalChildHeight = totalChildHeight + child:getHeight()
|
||||
end
|
||||
local totalSpace = containerHeight - totalChildHeight
|
||||
local offset = math.floor(totalSpace / numSpaces)
|
||||
local remaining = totalSpace - offset * numSpaces
|
||||
currentY = offset + (remaining > 0 and 1 or 0)
|
||||
remaining = remaining > 0 and remaining - 1 or 0
|
||||
for _, child in ipairs(children) do
|
||||
local x, y = child:getPosition()
|
||||
child:setPosition(x, currentY)
|
||||
currentY = currentY + child:getHeight() + offset + (remaining > 0 and 1 or 0)
|
||||
remaining = remaining > 0 and remaining - 1 or 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function applyLayout(self)
|
||||
sortChildren(self)
|
||||
if direction == "row" then
|
||||
for _,v in pairs(sortedChildren)do
|
||||
calculateRow(self, v)
|
||||
end
|
||||
else
|
||||
for _,v in pairs(sortedChildren)do
|
||||
calculateColumn(self, v)
|
||||
end
|
||||
end
|
||||
updateLayout = false
|
||||
end
|
||||
|
||||
local object = {
|
||||
getType = function()
|
||||
return objectType
|
||||
end,
|
||||
|
||||
isType = function(self, t)
|
||||
return objectType == t or base.isType ~= nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
setJustifyContent = function(self, value)
|
||||
justifyContent = value
|
||||
updateLayout = true
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getJustifyContent = function(self)
|
||||
return justifyContent
|
||||
end,
|
||||
|
||||
setDirection = function(self, value)
|
||||
direction = value
|
||||
updateLayout = true
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getDirection = function(self)
|
||||
return direction
|
||||
end,
|
||||
|
||||
setSpacing = function(self, value)
|
||||
spacing = value
|
||||
updateLayout = true
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getSpacing = function(self)
|
||||
return spacing
|
||||
end,
|
||||
|
||||
setWrap = function(self, value)
|
||||
wrap = value
|
||||
updateLayout = true
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getWrap = function(self)
|
||||
return wrap
|
||||
end,
|
||||
|
||||
updateLayout = function(self)
|
||||
updateLayout = true
|
||||
self:updateDraw()
|
||||
end,
|
||||
|
||||
addBreak = function(self)
|
||||
table.insert(children, lineBreakFakeObject)
|
||||
updateLayout = true
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
customEventHandler = function(self, event, ...)
|
||||
base.customEventHandler(self, event, ...)
|
||||
if event == "basalt_FrameResize" then
|
||||
updateLayout = true
|
||||
end
|
||||
end,
|
||||
|
||||
removeChild = function(self, child)
|
||||
if (type(child)=="string") then
|
||||
child = self:getChild(child)
|
||||
end
|
||||
if (child==nil) then
|
||||
return
|
||||
end
|
||||
base.removeChild(self, child)
|
||||
for k, v in pairs(children) do
|
||||
if v:getName() == child:getName() then
|
||||
table.remove(children, k)
|
||||
break
|
||||
end
|
||||
end
|
||||
updateLayout = true
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
base.draw(self)
|
||||
self:addDraw("flexboxDraw", function()
|
||||
if updateLayout then
|
||||
applyLayout(self)
|
||||
end
|
||||
end, 1)
|
||||
end
|
||||
}
|
||||
|
||||
for k, _ in pairs(basalt.getObjects()) do
|
||||
object["add" .. k] = function(self, name)
|
||||
local baseChild = base["add" .. k](self, name)
|
||||
local child = flexObjectPlugin(baseChild, basalt)
|
||||
table.insert(children, child)
|
||||
updateLayout = true
|
||||
return child
|
||||
end
|
||||
end
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
133
Basalt/objects/Frame.lua
Normal file
@@ -0,0 +1,133 @@
|
||||
local utils = require("utils")
|
||||
|
||||
local max,min,sub,rep,len = math.max,math.min,string.sub,string.rep,string.len
|
||||
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("Container")(name, basalt)
|
||||
local objectType = "Frame"
|
||||
local parent
|
||||
|
||||
local updateRender = true
|
||||
|
||||
local xOffset, yOffset = 0, 0
|
||||
|
||||
base:setSize(30, 10)
|
||||
base:setZIndex(10)
|
||||
|
||||
local object = {
|
||||
getType = function()
|
||||
return objectType
|
||||
end,
|
||||
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
getOffset = function(self)
|
||||
return xOffset, yOffset
|
||||
end,
|
||||
|
||||
setOffset = function(self, xOff, yOff)
|
||||
xOffset = xOff or xOffset
|
||||
yOffset = yOff or yOffset
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getXOffset = function(self)
|
||||
return xOffset
|
||||
end,
|
||||
|
||||
setXOffset = function(self, newXOffset)
|
||||
return self:setOffset(newXOffset, nil)
|
||||
end,
|
||||
|
||||
getYOffset = function(self)
|
||||
return yOffset
|
||||
end,
|
||||
|
||||
setYOffset = function(self, newYOffset)
|
||||
return self:setOffset(nil, newYOffset)
|
||||
end,
|
||||
|
||||
setParent = function(self, p, ...)
|
||||
base.setParent(self, p, ...)
|
||||
parent = p
|
||||
return self
|
||||
end,
|
||||
|
||||
render = function(self)
|
||||
if(base.render~=nil)then
|
||||
if(self:isVisible())then
|
||||
base.render(self)
|
||||
local children = self:getChildren()
|
||||
for _, child in ipairs(children) do
|
||||
if (child.element.render ~= nil) then
|
||||
child.element:render()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
updateDraw = function(self)
|
||||
if(parent~=nil)then
|
||||
parent:updateDraw()
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
blit = function (self, x, y, t, f, b)
|
||||
local obx, oby = self:getPosition()
|
||||
local xO, yO = parent:getOffset()
|
||||
obx = obx - xO
|
||||
oby = oby - yO
|
||||
local w, h = self:getSize()
|
||||
if y >= 1 and y <= h then
|
||||
local t_visible = sub(t, max(1 - x + 1, 1), max(w - x + 1, 1))
|
||||
local f_visible = sub(f, max(1 - x + 1, 1), max(w - x + 1, 1))
|
||||
local b_visible = sub(b, max(1 - x + 1, 1), max(w - x + 1, 1))
|
||||
parent:blit(max(x + (obx - 1), obx), oby + y - 1, t_visible, f_visible, b_visible)
|
||||
end
|
||||
end,
|
||||
|
||||
setCursor = function(self, blink, x, y, color)
|
||||
local obx, oby = self:getPosition()
|
||||
local xO, yO = self:getOffset()
|
||||
parent:setCursor(blink or false, (x or 0)+obx-1 - xO, (y or 0)+oby-1 - yO, color or colors.white)
|
||||
return self
|
||||
end,
|
||||
}
|
||||
|
||||
for k,v in pairs({"drawBackgroundBox", "drawForegroundBox", "drawTextBox"})do
|
||||
object[v] = function(self, x, y, width, height, symbol)
|
||||
local obx, oby = self:getPosition()
|
||||
local xO, yO = parent:getOffset()
|
||||
obx = obx - xO
|
||||
oby = oby - yO
|
||||
height = (y < 1 and (height + y > self:getHeight() and self:getHeight() or height + y - 1) or (height + y > self:getHeight() and self:getHeight() - y + 1 or height))
|
||||
width = (x < 1 and (width + x > self:getWidth() and self:getWidth() or width + x - 1) or (width + x > self:getWidth() and self:getWidth() - x + 1 or width))
|
||||
parent[v](parent, max(x + (obx - 1), obx), max(y + (oby - 1), oby), width, height, symbol)
|
||||
end
|
||||
end
|
||||
|
||||
for k,v in pairs({"setBG", "setFG", "setText"})do
|
||||
object[v] = function(self, x, y, str)
|
||||
local obx, oby = self:getPosition()
|
||||
local xO, yO = parent:getOffset()
|
||||
obx = obx - xO
|
||||
oby = oby - yO
|
||||
local w, h = self:getSize()
|
||||
if (y >= 1) and (y <= h) then
|
||||
parent[v](parent, max(x + (obx - 1), obx), oby + y - 1, sub(str, max(1 - x + 1, 1), max(w - x + 1,1)))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
171
Basalt/objects/Graph.lua
Normal file
@@ -0,0 +1,171 @@
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("ChangeableObject")(name, basalt)
|
||||
local objectType = "Graph"
|
||||
|
||||
base:setZIndex(5)
|
||||
base:setSize(30, 10)
|
||||
|
||||
local graphData = {}
|
||||
local graphColor = colors.gray
|
||||
local graphSymbol = "\7"
|
||||
local graphSymbolCol = colors.black
|
||||
local maxValue = 100
|
||||
local minValue = 0
|
||||
local graphType = "line"
|
||||
local maxEntries = 10
|
||||
|
||||
local object = {
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end,
|
||||
|
||||
setGraphColor = function(self, color)
|
||||
graphColor = color or graphColor
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setGraphSymbol = function(self, symbol, symbolcolor)
|
||||
graphSymbol = symbol or graphSymbol
|
||||
graphSymbolCol = symbolcolor or graphSymbolCol
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setGraphSymbolColor = function(self, symbolColor)
|
||||
return self:setGraphSymbolColor(nil, symbolColor)
|
||||
end,
|
||||
|
||||
getGraphSymbol = function(self)
|
||||
return graphSymbol, graphSymbolCol
|
||||
end,
|
||||
|
||||
getGraphSymbolColor = function(self)
|
||||
return graphSymbolCol
|
||||
end,
|
||||
|
||||
addDataPoint = function(self, value)
|
||||
if value >= minValue and value <= maxValue then
|
||||
table.insert(graphData, value)
|
||||
self:updateDraw()
|
||||
end
|
||||
if(#graphData>100)then -- 100 is hard capped to prevent memory leaks
|
||||
table.remove(graphData,1)
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
setMaxValue = function(self, value)
|
||||
maxValue = value
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getMaxValue = function(self)
|
||||
return maxValue
|
||||
end,
|
||||
|
||||
setMinValue = function(self, value)
|
||||
minValue = value
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getMinValue = function(self)
|
||||
return minValue
|
||||
end,
|
||||
|
||||
setGraphType = function(self, graph_type)
|
||||
if graph_type == "scatter" or graph_type == "line" or graph_type == "bar" then
|
||||
graphType = graph_type
|
||||
self:updateDraw()
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
getGraphType = function(self)
|
||||
return graphType
|
||||
end,
|
||||
|
||||
setMaxEntries = function(self, value)
|
||||
maxEntries = value
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getMaxEntries = function(self)
|
||||
return maxEntries
|
||||
end,
|
||||
|
||||
clear = function(self)
|
||||
graphData = {}
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
base.draw(self)
|
||||
self:addDraw("graph", function()
|
||||
local obx, oby = self:getPosition()
|
||||
local w, h = self:getSize()
|
||||
local bgCol, fgCol = self:getBackground(), self:getForeground()
|
||||
|
||||
local range = maxValue - minValue
|
||||
local prev_x, prev_y
|
||||
|
||||
local startIndex = #graphData - maxEntries + 1
|
||||
if startIndex < 1 then startIndex = 1 end
|
||||
|
||||
for i = startIndex, #graphData do
|
||||
local data = graphData[i]
|
||||
local x = math.floor(((w - 1) / (maxEntries - 1)) * (i - startIndex) + 1.5)
|
||||
local y = math.floor((h - 1) - ((h - 1) / range) * (data - minValue) + 1.5)
|
||||
|
||||
|
||||
if graphType == "scatter" then
|
||||
self:addBackgroundBox(x, y, 1, 1, graphColor)
|
||||
self:addForegroundBox(x, y, 1, 1, graphSymbolCol)
|
||||
self:addTextBox(x, y, 1, 1, graphSymbol)
|
||||
elseif graphType == "line" then
|
||||
if prev_x and prev_y then
|
||||
local dx = math.abs(x - prev_x)
|
||||
local dy = math.abs(y - prev_y)
|
||||
local sx = prev_x < x and 1 or -1
|
||||
local sy = prev_y < y and 1 or -1
|
||||
local err = dx - dy
|
||||
|
||||
while true do
|
||||
self:addBackgroundBox(prev_x, prev_y, 1, 1, graphColor)
|
||||
self:addForegroundBox(prev_x, prev_y, 1, 1, graphSymbolCol)
|
||||
self:addTextBox(prev_x, prev_y, 1, 1, graphSymbol)
|
||||
|
||||
if prev_x == x and prev_y == y then
|
||||
break
|
||||
end
|
||||
|
||||
local e2 = 2 * err
|
||||
|
||||
if e2 > -dy then
|
||||
err = err - dy
|
||||
prev_x = prev_x + sx
|
||||
end
|
||||
|
||||
if e2 < dx then
|
||||
err = err + dx
|
||||
prev_y = prev_y + sy
|
||||
end
|
||||
end
|
||||
end
|
||||
prev_x, prev_y = x, y
|
||||
elseif graphType == "bar" then
|
||||
self:addBackgroundBox(x - 1, y, 1, h - y, graphColor)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end,
|
||||
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,202 +1,365 @@
|
||||
local Object = require("Object")
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
|
||||
return function(name)
|
||||
local images = require("images")
|
||||
local bimg = require("bimg")
|
||||
local unpack,sub,max,min = table.unpack,string.sub,math.max,math.min
|
||||
return function(name, basalt)
|
||||
-- Image
|
||||
local base = Object(name)
|
||||
local base = basalt.getObject("VisualObject")(name, basalt)
|
||||
local objectType = "Image"
|
||||
base:setZIndex(2)
|
||||
|
||||
local bimgLibrary = bimg()
|
||||
local bimgFrame = bimgLibrary.getFrameObject(1)
|
||||
local originalImage
|
||||
local image
|
||||
local shrinkedImage
|
||||
local imageGotShrinked = false
|
||||
local activeFrame = 1
|
||||
|
||||
local function shrink()
|
||||
-- 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 infinitePlay = false
|
||||
local animTimer
|
||||
local usePalette = false
|
||||
local autoSize = true
|
||||
|
||||
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 xOffset, yOffset = 0, 0
|
||||
|
||||
local thisRel = relations[i - 1]
|
||||
for i = 1, #thisRel do
|
||||
thisRel[i] = 2 ^ thisRel[i]
|
||||
end
|
||||
base:setSize(24, 8)
|
||||
base:setZIndex(2)
|
||||
|
||||
local function getPalette(id)
|
||||
local p = {}
|
||||
for k,v in pairs(colors)do
|
||||
if(type(v)=="number")then
|
||||
p[k] = {term.nativePaletteColor(v)}
|
||||
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]]
|
||||
local globalPalette = bimgLibrary.getMetadata("palette")
|
||||
if(globalPalette~=nil)then
|
||||
for k,v in pairs(globalPalette)do
|
||||
p[k] = tonumber(v)
|
||||
end
|
||||
end
|
||||
|
||||
local results, width, height, bgCol = { {}, {}, {} }, 0, #image + #image % 3, base.bgColor or colors.black
|
||||
for i = 1, #image do
|
||||
if #image[i] > width then
|
||||
width = #image[i]
|
||||
local localPalette = bimgLibrary.getFrameData("palette")
|
||||
basalt.log(localPalette)
|
||||
if(localPalette~=nil)then
|
||||
for k,v in pairs(localPalette)do
|
||||
p[k] = tonumber(v)
|
||||
end
|
||||
end
|
||||
return p
|
||||
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] = (image[y + yy] and image[y + yy][x + xx]) and (image[y + yy][x + xx] == 0 and bgCol or image[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
|
||||
local function checkAutoSize()
|
||||
if(autoSize)then
|
||||
if(bimgLibrary~=nil)then
|
||||
base:setSize(bimgLibrary.getSize())
|
||||
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]
|
||||
|
||||
shrinkedImage = results
|
||||
end
|
||||
|
||||
local object = {
|
||||
init = function(self)
|
||||
self.bgColor = self.parent:getTheme("ImageBG")
|
||||
end,
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
end,
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
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
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setXOffset = function(self, _x)
|
||||
return self:setOffset(self, _x, nil)
|
||||
end,
|
||||
|
||||
setYOffset = function(self, _y)
|
||||
return self:setOffset(self, nil, _y)
|
||||
end,
|
||||
|
||||
setSize = function(self, _x, _y)
|
||||
base:setSize(_x, _y)
|
||||
autoSize = false
|
||||
return self
|
||||
end,
|
||||
|
||||
getOffset = function(self)
|
||||
return xOffset, yOffset
|
||||
end,
|
||||
|
||||
getXOffset = function(self)
|
||||
return xOffset
|
||||
end,
|
||||
|
||||
getYOffset = function(self)
|
||||
return yOffset
|
||||
end,
|
||||
|
||||
selectFrame = function(self, id)
|
||||
if(bimgLibrary.getFrameObject(id)==nil)then
|
||||
bimgLibrary.addFrame(id)
|
||||
end
|
||||
bimgFrame = bimgLibrary.getFrameObject(id)
|
||||
image = bimgFrame.getImage(id)
|
||||
activeFrame = id
|
||||
self:updateDraw()
|
||||
end,
|
||||
|
||||
addFrame = function(self, id)
|
||||
bimgLibrary.addFrame(id)
|
||||
return self
|
||||
end,
|
||||
|
||||
getFrame = function(self, id)
|
||||
return bimgLibrary.getFrame(id)
|
||||
end,
|
||||
|
||||
getFrameObject = function(self, id)
|
||||
return bimgLibrary.getFrameObject(id)
|
||||
end,
|
||||
|
||||
removeFrame = function(self, id)
|
||||
bimgLibrary.removeFrame(id)
|
||||
return self
|
||||
end,
|
||||
|
||||
moveFrame = function(self, id, dir)
|
||||
bimgLibrary.moveFrame(id, dir)
|
||||
return self
|
||||
end,
|
||||
|
||||
getFrames = function(self)
|
||||
return bimgLibrary.getFrames()
|
||||
end,
|
||||
|
||||
getFrameCount = function(self)
|
||||
return #bimgLibrary.getFrames()
|
||||
end,
|
||||
|
||||
getActiveFrame = function(self)
|
||||
return activeFrame
|
||||
end,
|
||||
|
||||
loadImage = function(self, path)
|
||||
image = paintutils.loadImage(path)
|
||||
imageGotShrinked = false
|
||||
if(fs.exists(path))then
|
||||
local newBimg = images.loadBIMG(path)
|
||||
print(newBimg[1][1])
|
||||
sleep(1)
|
||||
bimgLibrary = bimg(newBimg)
|
||||
activeFrame = 1
|
||||
bimgFrame = bimgLibrary.getFrameObject(1)
|
||||
originalImage = bimgLibrary.createBimg()
|
||||
image = bimgFrame.getImage()
|
||||
checkAutoSize()
|
||||
self:updateDraw()
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
setPath = function(self, path)
|
||||
return self:loadImage(path)
|
||||
end,
|
||||
|
||||
setImage = function(self, t)
|
||||
if(type(t)=="table")then
|
||||
bimgLibrary = bimg(t)
|
||||
activeFrame = 1
|
||||
bimgFrame = bimgLibrary.getFrameObject(1)
|
||||
originalImage = bimgLibrary.createBimg()
|
||||
image = bimgFrame.getImage()
|
||||
checkAutoSize()
|
||||
self:updateDraw()
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
clear = function(self)
|
||||
bimgLibrary = bimg()
|
||||
bimgFrame = bimgLibrary.getFrameObject(1)
|
||||
image = nil
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getImage = function(self)
|
||||
return bimgLibrary.createBimg()
|
||||
end,
|
||||
|
||||
shrink = function(self)
|
||||
shrink()
|
||||
imageGotShrinked = true
|
||||
getImageFrame = function(self, id)
|
||||
return bimgFrame.getImage(id)
|
||||
end,
|
||||
|
||||
usePalette = function(self, use)
|
||||
usePalette = use~=nil and use or true
|
||||
return self
|
||||
end,
|
||||
|
||||
getUsePalette = function(self)
|
||||
return usePalette
|
||||
end,
|
||||
|
||||
setUsePalette = function(self, use)
|
||||
return self:usePalette(use)
|
||||
end,
|
||||
|
||||
play = function(self, inf)
|
||||
if(bimgLibrary.getMetadata("animated"))then
|
||||
local t = bimgLibrary.getMetadata("duration") or bimgLibrary.getMetadata("secondsPerFrame") or 0.2
|
||||
self:listenEvent("other_event")
|
||||
animTimer = os.startTimer(t)
|
||||
infinitePlay = inf or false
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
setPlay = function(self, inf)
|
||||
return self:play(inf)
|
||||
end,
|
||||
|
||||
stop = function(self)
|
||||
os.cancelTimer(animTimer)
|
||||
animTimer = nil
|
||||
infinitePlay = false
|
||||
return self
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, timerId, ...)
|
||||
base.eventHandler(self, event, timerId, ...)
|
||||
if(event=="timer")then
|
||||
if(timerId==animTimer)then
|
||||
if(bimgLibrary.getFrame(activeFrame+1)~=nil)then
|
||||
activeFrame = activeFrame + 1
|
||||
self:selectFrame(activeFrame)
|
||||
local t = bimgLibrary.getFrameData(activeFrame, "duration") or bimgLibrary.getMetadata("secondsPerFrame") or 0.2
|
||||
animTimer = os.startTimer(t)
|
||||
else
|
||||
if(infinitePlay)then
|
||||
activeFrame = 1
|
||||
self:selectFrame(activeFrame)
|
||||
local t = bimgLibrary.getFrameData(activeFrame, "duration") or bimgLibrary.getMetadata("secondsPerFrame") or 0.2
|
||||
animTimer = os.startTimer(t)
|
||||
end
|
||||
end
|
||||
self:updateDraw()
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
setMetadata = function(self, key, value)
|
||||
bimgLibrary.setMetadata(key, value)
|
||||
return self
|
||||
end,
|
||||
|
||||
getMetadata = function(self, key)
|
||||
return bimgLibrary.getMetadata(key)
|
||||
end,
|
||||
|
||||
getFrameMetadata = function(self, id, key)
|
||||
return bimgLibrary.getFrameData(id, key)
|
||||
end,
|
||||
|
||||
setFrameMetadata = function(self, id, key, value)
|
||||
bimgLibrary.setFrameData(id, key, value)
|
||||
return self
|
||||
end,
|
||||
|
||||
blit = function(self, text, fg, bg, _x, _y)
|
||||
x = _x or x
|
||||
y = _y or y
|
||||
bimgFrame.blit(text, fg, bg, x, y)
|
||||
image = bimgFrame.getImage()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("shrink", data)~=nil)then if(xmlValue("shrink", data))then self:shrink() end end
|
||||
if(xmlValue("path", data)~=nil)then self:loadImage(xmlValue("path", data)) end
|
||||
setText = function(self, text, _x, _y)
|
||||
x = _x or x
|
||||
y = _y or y
|
||||
bimgFrame.text(text, x, y)
|
||||
image = bimgFrame.getImage()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setBg = function(self, bg, _x, _y)
|
||||
x = _x or x
|
||||
y = _y or y
|
||||
bimgFrame.bg(bg, x, y)
|
||||
image = bimgFrame.getImage()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setFg = function(self, fg, _x, _y)
|
||||
x = _x or x
|
||||
y = _y or y
|
||||
bimgFrame.fg(fg, x, y)
|
||||
image = bimgFrame.getImage()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getImageSize = function(self)
|
||||
return bimgLibrary.getSize()
|
||||
end,
|
||||
|
||||
setImageSize = function(self, w, h)
|
||||
bimgLibrary.setSize(w, h)
|
||||
image = bimgFrame.getImage()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
resizeImage = function(self, w, h)
|
||||
local newBimg = images.resizeBIMG(originalImage, w, h)
|
||||
bimgLibrary = bimg(newBimg)
|
||||
activeFrame = 1
|
||||
bimgFrame = bimgLibrary.getFrameObject(1)
|
||||
image = bimgFrame.getImage()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
if (self.parent ~= nil) then
|
||||
if (image ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local w,h = self:getSize()
|
||||
if (imageGotShrinked) 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 = shrinkedImage[1], shrinkedImage[2], shrinkedImage[3]
|
||||
for i = 1, shrinkedImage.height do
|
||||
local tI = t[i]
|
||||
if type(tI) == "string" then
|
||||
self.parent:setText(obx, oby + i - 1, tI)
|
||||
self.parent:setFG(obx, oby + i - 1, tC[i])
|
||||
self.parent:setBG(obx, oby + i - 1, bC[i])
|
||||
elseif type(tI) == "table" then
|
||||
self.parent:setText(obx, oby + i - 1, tI[2])
|
||||
self.parent:setFG(obx, oby + i - 1, tC[i])
|
||||
self.parent:setBG(obx, oby + i - 1, bC[i])
|
||||
end
|
||||
end
|
||||
else
|
||||
for yPos = 1, math.min(#image, h) do
|
||||
local line = image[yPos]
|
||||
for xPos = 1, math.min(#line, w) do
|
||||
if line[xPos] > 0 then
|
||||
self.parent:drawBackgroundBox(obx + xPos - 1, oby + yPos - 1, 1, 1, line[xPos])
|
||||
end
|
||||
end
|
||||
end
|
||||
base.draw(self)
|
||||
self:addDraw("image", function()
|
||||
local w,h = self:getSize()
|
||||
local x, y = self:getPosition()
|
||||
local wParent, hParent = self:getParent():getSize()
|
||||
local parentXOffset, parentYOffset = self:getParent():getOffset()
|
||||
|
||||
if(x - parentXOffset > wParent)or(y - parentYOffset > hParent)or(x - parentXOffset + w < 1)or(y - parentYOffset + h < 1)then
|
||||
return
|
||||
end
|
||||
|
||||
if(usePalette)then
|
||||
self:getParent():setPalette(getPalette(activeFrame))
|
||||
end
|
||||
|
||||
if(image~=nil)then
|
||||
for k,v in pairs(image)do
|
||||
if(k+yOffset<=h)and(k+yOffset>=1)then
|
||||
local t,f,b = v[1],v[2],v[3]
|
||||
|
||||
local startIdx = max(1 - xOffset, 1)
|
||||
local endIdx = min(w - xOffset, #t)
|
||||
|
||||
t = sub(t, startIdx, endIdx)
|
||||
f = sub(f, startIdx, endIdx)
|
||||
b = sub(b, startIdx, endIdx)
|
||||
|
||||
self:addBlit(max(1 + xOffset, 1), k + yOffset, t, f, b)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,46 +1,54 @@
|
||||
local Object = require("Object")
|
||||
local utils = require("utils")
|
||||
local log = require("basaltLogs")
|
||||
local xmlValue = utils.getValueFromXML
|
||||
local tHex = require("tHex")
|
||||
|
||||
return function(name)
|
||||
return function(name, basalt)
|
||||
-- Input
|
||||
local base = Object(name)
|
||||
local base = basalt.getObject("ChangeableObject")(name, basalt)
|
||||
local objectType = "Input"
|
||||
|
||||
local inputType = "text"
|
||||
local inputLimit = 0
|
||||
base:setZIndex(5)
|
||||
base:setValue("")
|
||||
base.width = 10
|
||||
base.height = 1
|
||||
base:setSize(12, 1)
|
||||
|
||||
local textX = 1
|
||||
local wIndex = 1
|
||||
|
||||
local defaultText = ""
|
||||
local defaultBGCol
|
||||
local defaultFGCol
|
||||
local defaultBGCol = colors.black
|
||||
local defaultFGCol = colors.lightGray
|
||||
local showingText = defaultText
|
||||
local internalValueChange = false
|
||||
|
||||
local object = {
|
||||
load = function(self)
|
||||
self:listenEvent("mouse_click")
|
||||
self:listenEvent("key")
|
||||
self:listenEvent("char")
|
||||
self:listenEvent("other_event")
|
||||
self:listenEvent("mouse_drag")
|
||||
end,
|
||||
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
end,
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
setInputType = function(self, iType)
|
||||
if (iType == "password") or (iType == "number") or (iType == "text") then
|
||||
inputType = iType
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
setDefaultFG = function(self, fCol)
|
||||
return self:setDefaultText(self, defaultText, fCol, nil)
|
||||
end,
|
||||
|
||||
setDefaultBG = function(self, bCol)
|
||||
return self:setDefaultText(self, defaultText, nil, bCol)
|
||||
end,
|
||||
|
||||
setDefaultText = function(self, text, fCol, bCol)
|
||||
defaultText = text
|
||||
defaultBGCol = bCol or defaultBGCol
|
||||
defaultFGCol = fCol or defaultFGCol
|
||||
defaultBGCol = bCol or defaultBGCol
|
||||
if (self:isFocused()) then
|
||||
showingText = ""
|
||||
else
|
||||
@@ -48,117 +56,126 @@ return function(name)
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getDefaultText = function(self)
|
||||
return defaultText, defaultFGCol, defaultBGCol
|
||||
end,
|
||||
|
||||
setOffset = function(self, x)
|
||||
wIndex = x
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getOffset = function(self)
|
||||
return wIndex
|
||||
end,
|
||||
|
||||
setTextOffset = function(self, x)
|
||||
textX = x
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getTextOffset = function(self)
|
||||
return textX
|
||||
end,
|
||||
|
||||
setInputType = function(self, t)
|
||||
inputType = t
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getInputType = function(self)
|
||||
return inputType
|
||||
end;
|
||||
end,
|
||||
|
||||
setValue = function(self, val)
|
||||
base.setValue(self, tostring(val))
|
||||
if not (internalValueChange) then
|
||||
textX = tostring(val):len() + 1
|
||||
wIndex = math.max(1, textX-self:getWidth()+1)
|
||||
if(self:isFocused())then
|
||||
textX = tostring(val):len() + 1
|
||||
wIndex = math.max(1, textX-self:getWidth()+1)
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
self.parent:setCursor(true, obx + textX - wIndex, oby+math.floor(self:getHeight()/2), self.fgColor)
|
||||
local parent = self:getParent()
|
||||
local obx, oby = self:getPosition()
|
||||
parent:setCursor(true, obx + textX - wIndex, oby+math.floor(self:getHeight()/2), self:getForeground())
|
||||
end
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getValue = function(self)
|
||||
local val = base.getValue(self)
|
||||
return inputType == "number" and tonumber(val) or val
|
||||
end;
|
||||
end,
|
||||
|
||||
setInputLimit = function(self, limit)
|
||||
inputLimit = tonumber(limit) or inputLimit
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getInputLimit = function(self)
|
||||
return inputLimit
|
||||
end;
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
local dBG,dFG
|
||||
if(xmlValue("defaultBG", data)~=nil)then dBG = xmlValue("defaultBG", data) end
|
||||
if(xmlValue("defaultFG", data)~=nil)then dFG = xmlValue("defaultFG", data) end
|
||||
if(xmlValue("default", data)~=nil)then self:setDefaultText(xmlValue("default", data), dFG~=nil and colors[dFG], dBG~=nil and colors[dBG]) end
|
||||
if(xmlValue("limit", data)~=nil)then self:setInputLimit(xmlValue("limit", data)) end
|
||||
if(xmlValue("type", data)~=nil)then self:setInputType(xmlValue("type", data)) end
|
||||
return self
|
||||
end,
|
||||
|
||||
getFocusHandler = function(self)
|
||||
base.getFocusHandler(self)
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local parent = self:getParent()
|
||||
if (parent ~= nil) then
|
||||
local obx, oby = self:getPosition()
|
||||
showingText = ""
|
||||
if(defaultText~="")then
|
||||
self:updateDraw()
|
||||
end
|
||||
self.parent:setCursor(true, obx + textX - wIndex, oby+math.max(math.ceil(self:getHeight()/2-1, 1)), self.fgColor)
|
||||
parent:setCursor(true, obx + textX - wIndex, oby+math.max(math.ceil(self:getHeight()/2-1, 1)), self:getForeground())
|
||||
end
|
||||
end;
|
||||
end,
|
||||
|
||||
loseFocusHandler = function(self)
|
||||
base.loseFocusHandler(self)
|
||||
if (self.parent ~= nil) then
|
||||
showingText = defaultText
|
||||
if(defaultText~="")then
|
||||
self:updateDraw()
|
||||
end
|
||||
self.parent:setCursor(false)
|
||||
local parent = self:getParent()
|
||||
showingText = defaultText
|
||||
if(defaultText~="")then
|
||||
self:updateDraw()
|
||||
end
|
||||
end;
|
||||
parent:setCursor(false)
|
||||
end,
|
||||
|
||||
keyHandler = function(self, key)
|
||||
if (base.keyHandler(self, key)) then
|
||||
local w,h = self:getSize()
|
||||
local parent = self:getParent()
|
||||
internalValueChange = true
|
||||
if (key == keys.backspace) then
|
||||
-- on backspace
|
||||
local text = tostring(base.getValue())
|
||||
if (textX > 1) then
|
||||
self:setValue(text:sub(1, textX - 2) .. text:sub(textX, text:len()))
|
||||
if (textX > 1) then
|
||||
textX = textX - 1
|
||||
end
|
||||
if (wIndex > 1) then
|
||||
if (textX < wIndex) then
|
||||
wIndex = wIndex - 1
|
||||
end
|
||||
textX = math.max(textX - 1, 1)
|
||||
if (textX < wIndex) then
|
||||
wIndex = math.max(wIndex - 1, 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
if (key == keys.enter) then
|
||||
-- on enter
|
||||
if (self.parent ~= nil) then
|
||||
--self.parent:removeFocusedObject(self)
|
||||
end
|
||||
parent:clearFocusedChild(self)
|
||||
end
|
||||
if (key == keys.right) then
|
||||
-- right arrow
|
||||
local tLength = tostring(base.getValue()):len()
|
||||
textX = textX + 1
|
||||
|
||||
if (textX > tLength) then
|
||||
textX = tLength + 1
|
||||
end
|
||||
if (textX < 1) then
|
||||
textX = 1
|
||||
end
|
||||
textX = math.max(textX, 1)
|
||||
if (textX < wIndex) or (textX >= w + wIndex) then
|
||||
wIndex = textX - w + 1
|
||||
end
|
||||
if (wIndex < 1) then
|
||||
wIndex = 1
|
||||
end
|
||||
wIndex = math.max(wIndex, 1)
|
||||
end
|
||||
|
||||
if (key == keys.left) then
|
||||
@@ -169,29 +186,16 @@ return function(name)
|
||||
wIndex = textX
|
||||
end
|
||||
end
|
||||
if (textX < 1) then
|
||||
textX = 1
|
||||
end
|
||||
if (wIndex < 1) then
|
||||
wIndex = 1
|
||||
end
|
||||
textX = math.max(textX, 1)
|
||||
wIndex = math.max(wIndex, 1)
|
||||
end
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local obx, oby = self:getPosition()
|
||||
local val = tostring(base.getValue())
|
||||
local cursorX = (textX <= val:len() and textX - 1 or val:len()) - (wIndex - 1)
|
||||
|
||||
local inpX = self:getX()
|
||||
if (cursorX > inpX + w - 1) then
|
||||
cursorX = inpX + w - 1
|
||||
end
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:setCursor(true, obx + cursorX, oby+math.max(math.ceil(h/2-1, 1)), self.fgColor)
|
||||
end
|
||||
|
||||
self:updateDraw()
|
||||
internalValueChange = false
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
charHandler = function(self, char)
|
||||
@@ -202,12 +206,15 @@ return function(name)
|
||||
if (text:len() < inputLimit or inputLimit <= 0) then
|
||||
if (inputType == "number") then
|
||||
local cache = text
|
||||
if (#text==0 and char == "-") or (char == ".") or (tonumber(char) ~= nil) then
|
||||
if (textX==1 and char == "-") or (char == ".") or (tonumber(char) ~= nil) then
|
||||
self:setValue(text:sub(1, textX - 1) .. char .. text:sub(textX, text:len()))
|
||||
textX = textX + 1
|
||||
end
|
||||
if (tonumber(base.getValue()) == nil) then
|
||||
--self:setValue(cache)
|
||||
if(char==".")or(char=="-")and(#text>0)then
|
||||
if (tonumber(base.getValue()) == nil) then
|
||||
self:setValue(cache)
|
||||
textX = textX - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
self:setValue(text:sub(1, textX - 1) .. char .. text:sub(textX, text:len()))
|
||||
@@ -217,27 +224,19 @@ return function(name)
|
||||
wIndex = wIndex + 1
|
||||
end
|
||||
end
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local obx, oby = self:getPosition()
|
||||
local val = tostring(base.getValue())
|
||||
local cursorX = (textX <= val:len() and textX - 1 or val:len()) - (wIndex - 1)
|
||||
|
||||
local x = self:getX()
|
||||
if (cursorX > x + w - 1) then
|
||||
cursorX = x + w - 1
|
||||
end
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:setCursor(true, obx + cursorX, oby+math.max(math.ceil(h/2-1, 1)), self.fgColor)
|
||||
end
|
||||
internalValueChange = false
|
||||
self:updateDraw()
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y)
|
||||
if(base.mouseHandler(self, button, x, y))then
|
||||
local ax, ay = self:getAnchorPosition()
|
||||
local parent = self:getParent()
|
||||
local ax, ay = self:getPosition()
|
||||
local obx, oby = self:getAbsolutePosition(ax, ay)
|
||||
local w, h = self:getSize()
|
||||
textX = x - obx + wIndex
|
||||
@@ -251,7 +250,7 @@ return function(name)
|
||||
wIndex = 1
|
||||
end
|
||||
end
|
||||
self.parent:setCursor(true, ax + textX - wIndex, ay+math.max(math.ceil(h/2-1, 1)), self.fgColor)
|
||||
parent:setCursor(true, ax + textX - wIndex, ay+math.max(math.ceil(h/2-1, 1)), self:getForeground())
|
||||
return true
|
||||
end
|
||||
end,
|
||||
@@ -263,109 +262,51 @@ return function(name)
|
||||
return true
|
||||
end
|
||||
end
|
||||
self.parent:removeFocusedObject()
|
||||
end
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, paste, p2, p3, p4)
|
||||
if(base.eventHandler(self, event, paste, p2, p3, p4))then
|
||||
if(event=="paste")then
|
||||
if(self:isFocused())then
|
||||
local text = base.getValue()
|
||||
local w, h = self:getSize()
|
||||
internalValueChange = true
|
||||
if (inputType == "number") then
|
||||
local cache = text
|
||||
if (paste == ".") or (tonumber(paste) ~= nil) then
|
||||
self:setValue(text:sub(1, textX - 1) .. paste .. text:sub(textX, text:len()))
|
||||
textX = textX + paste:len()
|
||||
end
|
||||
if (tonumber(base.getValue()) == nil) then
|
||||
self:setValue(cache)
|
||||
end
|
||||
else
|
||||
self:setValue(text:sub(1, textX - 1) .. paste .. text:sub(textX, text:len()))
|
||||
textX = textX + paste:len()
|
||||
end
|
||||
if (textX >= w + wIndex) then
|
||||
wIndex = (textX+1)-w
|
||||
end
|
||||
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local val = tostring(base.getValue())
|
||||
local cursorX = (textX <= val:len() and textX - 1 or val:len()) - (wIndex - 1)
|
||||
|
||||
local x = self:getX()
|
||||
if (cursorX > x + w - 1) then
|
||||
cursorX = x + w - 1
|
||||
end
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:setCursor(true, obx + cursorX, oby+math.max(math.ceil(h/2-1, 1)), self.fgColor)
|
||||
end
|
||||
self:updateDraw()
|
||||
internalValueChange = false
|
||||
end
|
||||
end
|
||||
local parent = self:getParent()
|
||||
parent:clearFocusedChild()
|
||||
end
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local w,h = self:getSize()
|
||||
local verticalAlign = utils.getTextVerticalAlign(h, "center")
|
||||
|
||||
if(self.bgColor~=false)then self.parent:drawBackgroundBox(obx, oby, w, h, self.bgColor) end
|
||||
for n = 1, h do
|
||||
if (n == verticalAlign) then
|
||||
local val = tostring(base.getValue())
|
||||
local bCol = self.bgColor
|
||||
local fCol = self.fgColor
|
||||
local text
|
||||
if (val:len() <= 0) then
|
||||
text = showingText
|
||||
bCol = defaultBGCol or bCol
|
||||
fCol = defaultFGCol or fCol
|
||||
end
|
||||
|
||||
text = showingText
|
||||
if (val ~= "") then
|
||||
text = val
|
||||
end
|
||||
text = text:sub(wIndex, w + wIndex - 1)
|
||||
local space = w - text:len()
|
||||
if (space < 0) then
|
||||
space = 0
|
||||
end
|
||||
if (inputType == "password") and (val ~= "") then
|
||||
text = string.rep("*", text:len())
|
||||
end
|
||||
text = text .. string.rep(self.bgSymbol, space)
|
||||
self.parent:writeText(obx, oby + (n - 1), text, bCol, fCol)
|
||||
end
|
||||
end
|
||||
if(self:isFocused())then
|
||||
self.parent:setCursor(true, obx + textX - wIndex, oby+math.floor(self:getHeight()/2), self.fgColor)
|
||||
end
|
||||
base.draw(self)
|
||||
self:addDraw("input", function()
|
||||
local parent = self:getParent()
|
||||
local obx, oby = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
local verticalAlign = utils.getTextVerticalAlign(h, textVerticalAlign)
|
||||
|
||||
local val = tostring(base.getValue())
|
||||
local bCol = self:getBackground()
|
||||
local fCol = self:getForeground()
|
||||
local text
|
||||
if (val:len() <= 0) then
|
||||
text = showingText
|
||||
bCol = defaultBGCol or bCol
|
||||
fCol = defaultFGCol or fCol
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
self.parent:addEvent("key", self)
|
||||
self.parent:addEvent("char", self)
|
||||
self.parent:addEvent("other_event", self)
|
||||
self.parent:addEvent("mouse_drag", self)
|
||||
end
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("InputBG")
|
||||
self.fgColor = self.parent:getTheme("InputText")
|
||||
end
|
||||
text = showingText
|
||||
if (val ~= "") then
|
||||
text = val
|
||||
end
|
||||
text = text:sub(wIndex, w + wIndex - 1)
|
||||
local space = w - text:len()
|
||||
if (space < 0) then
|
||||
space = 0
|
||||
end
|
||||
if (inputType == "password") and (val ~= "") then
|
||||
text = string.rep("*", text:len())
|
||||
end
|
||||
text = text .. string.rep(" ", space)
|
||||
self:addBlit(1, verticalAlign, text, tHex[fCol]:rep(text:len()), tHex[bCol]:rep(text:len()))
|
||||
|
||||
if(self:isFocused())then
|
||||
parent:setCursor(true, obx + textX - wIndex, oby+math.floor(self:getHeight()/2), self:getForeground())
|
||||
end
|
||||
end)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,169 +1,115 @@
|
||||
local Object = require("Object")
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.getValueFromXML
|
||||
local createText = utils.createText
|
||||
local wrapText = utils.wrapText
|
||||
local writeWrappedText = utils.writeWrappedText
|
||||
local tHex = require("tHex")
|
||||
local bigFont = require("bigfont")
|
||||
|
||||
return function(name)
|
||||
return function(name, basalt)
|
||||
-- Label
|
||||
local base = Object(name)
|
||||
local base = basalt.getObject("VisualObject")(name, basalt)
|
||||
local objectType = "Label"
|
||||
|
||||
base:setZIndex(3)
|
||||
base:setSize(5, 1)
|
||||
base:setBackground(false)
|
||||
|
||||
local autoSize = true
|
||||
base:setValue("Label")
|
||||
base.width = 5
|
||||
|
||||
local textHorizontalAlign = "left"
|
||||
local textVerticalAlign = "top"
|
||||
local fontsize = 0
|
||||
|
||||
local fgColChanged,bgColChanged = false,false
|
||||
local text, textAlign = "Label", "left"
|
||||
|
||||
local object = {
|
||||
--- Returns the object type.
|
||||
--- @return string
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
end,
|
||||
|
||||
--- Returns the label's base object.
|
||||
--- @return object
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
setText = function(self, text)
|
||||
text = tostring(text)
|
||||
base:setValue(text)
|
||||
if (autoSize) then
|
||||
if(text:len()+self:getX()>self.parent:getWidth())then
|
||||
local newW = self.parent:getWidth() - self:getX()
|
||||
base.setSize(self, newW, #createText(text, newW))
|
||||
else
|
||||
base.setSize(self, text:len(), 1)
|
||||
--- Changes the label's text.
|
||||
--- @param newText string The new text of the label.
|
||||
--- @return object
|
||||
setText = function(self, newText)
|
||||
text = tostring(newText)
|
||||
if(autoSize)then
|
||||
local t = wrapText(text, #text)
|
||||
local newW, newH = 1,1
|
||||
for k,v in pairs(t)do
|
||||
newH = newH+1
|
||||
newW = math.max(newW, v:len())
|
||||
end
|
||||
self:setSize(newW, newH)
|
||||
autoSize = true
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setBackground = function(self, col)
|
||||
base.setBackground(self, col)
|
||||
bgColChanged = true
|
||||
self:updateDraw()
|
||||
--- Returns the label's autoSize property.
|
||||
--- @return boolean
|
||||
getAutoSize = function(self)
|
||||
return autoSize
|
||||
end,
|
||||
|
||||
--- Sets the label's autoSize property.
|
||||
--- @param bool boolean The new value of the autoSize property.
|
||||
--- @return object
|
||||
setAutoSize = function(self, bool)
|
||||
autoSize = bool
|
||||
return self
|
||||
end,
|
||||
|
||||
setForeground = function(self, col)
|
||||
base.setForeground(self, col)
|
||||
fgColChanged = true
|
||||
self:updateDraw()
|
||||
return self
|
||||
--- Returns the label's text.
|
||||
--- @return string
|
||||
getText = function(self)
|
||||
return text
|
||||
end,
|
||||
|
||||
setTextAlign = function(self, hor, vert)
|
||||
textHorizontalAlign = hor or textHorizontalAlign
|
||||
textVerticalAlign = vert or textVerticalAlign
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setFontSize = function(self, size)
|
||||
if(size>0)and(size<=4)then
|
||||
fontsize = size-1 or 0
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getFontSize = function(self)
|
||||
return fontsize+1
|
||||
end;
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("text", data)~=nil)then self:setText(xmlValue("text", data)) end
|
||||
if(xmlValue("verticalAlign", data)~=nil)then textVerticalAlign = xmlValue("verticalAlign", data) end
|
||||
if(xmlValue("horizontalAlign", data)~=nil)then textHorizontalAlign = xmlValue("horizontalAlign", data) end
|
||||
if(xmlValue("font", data)~=nil)then self:setFontSize(xmlValue("font", data)) end
|
||||
return self
|
||||
end,
|
||||
|
||||
setSize = function(self, width, height, rel)
|
||||
base.setSize(self, width, height, rel)
|
||||
--- Sets the size of the label.
|
||||
--- @param width number The width of the label.
|
||||
--- @param height number The height of the label.
|
||||
--- @return object
|
||||
setSize = function(self, width, height)
|
||||
base.setSize(self, width, height)
|
||||
autoSize = false
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
eventHandler = function(self, event)
|
||||
if(event=="basalt_resize")then
|
||||
if (autoSize) then
|
||||
local text = self:getValue()
|
||||
if(text:len()+self:getX()>self.parent:getWidth())then
|
||||
local newW = self.parent:getWidth() - self:getX()
|
||||
base.setSize(self, newW, #createText(text, newW))
|
||||
else
|
||||
base.setSize(self, text:len(), 1)
|
||||
end
|
||||
else
|
||||
--self.parent:removeEvent("other_event", self)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
--- Gets the text alignment of the label.
|
||||
--- @return string
|
||||
getTextAlign = function(self)
|
||||
return textAlign
|
||||
end,
|
||||
|
||||
--- Sets the text alignment of the label.
|
||||
--- @param align string The alignment of the text. Can be "left", "center", or "right".
|
||||
--- @return object
|
||||
setTextAlign = function(self, align)
|
||||
textAlign = align or textAlign
|
||||
return self;
|
||||
end,
|
||||
|
||||
--- Queues a new draw function to be called when the object is drawn.
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local w,h = self:getSize()
|
||||
local verticalAlign = utils.getTextVerticalAlign(h, textVerticalAlign)
|
||||
if(fontsize==0)then
|
||||
if not(autoSize)then
|
||||
local text = createText(self:getValue(), self:getWidth())
|
||||
for k,v in pairs(text)do
|
||||
if(k<=h)then
|
||||
self.parent:writeText(obx, oby+k-1, v, self.bgColor, self.fgColor)
|
||||
end
|
||||
end
|
||||
else
|
||||
if(#self:getValue()+obx>self.parent:getWidth())then
|
||||
local text = createText(self:getValue(), self:getWidth())
|
||||
for k,v in pairs(text)do
|
||||
if(k<=h)then
|
||||
self.parent:writeText(obx, oby+k-1, v, self.bgColor, self.fgColor)
|
||||
end
|
||||
end
|
||||
else
|
||||
self.parent:writeText(obx, oby, self:getValue(), self.bgColor, self.fgColor)
|
||||
end
|
||||
end
|
||||
else
|
||||
local tData = bigFont(fontsize, self:getValue(), self.fgColor, self.bgColor or colors.lightGray)
|
||||
if(autoSize)then
|
||||
self:setSize(#tData[1][1], #tData[1]-1)
|
||||
end
|
||||
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 - 1, tData[2][i])
|
||||
self.parent:setBG(obx, oby + i - 1, tData[3][i])
|
||||
self.parent:setText(obx, oby + i - 1, tData[1][i])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
base.draw(self)
|
||||
self:addDraw("label", function()
|
||||
local w, h = self:getSize()
|
||||
local align = textAlign=="center" and math.floor(w/2-text:len()/2+0.5) or textAlign=="right" and w-(text:len()-1) or 1
|
||||
writeWrappedText(self, align, 1, text, w+1, h)
|
||||
end)
|
||||
end,
|
||||
|
||||
--- Initializes the label.
|
||||
init = function(self)
|
||||
self.parent:addEvent("other_event", self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("LabelBG")
|
||||
self.fgColor = self.parent:getTheme("LabelText")
|
||||
if(self.parent.bgColor==colors.black)and(self.fgColor==colors.black)then
|
||||
self.fgColor = colors.lightGray
|
||||
end
|
||||
end
|
||||
base.init(self)
|
||||
local parent = self:getParent()
|
||||
self:setForeground(parent:getForeground())
|
||||
end
|
||||
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,59 +1,114 @@
|
||||
local Object = require("Object")
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.getValueFromXML
|
||||
local tHex = require("tHex")
|
||||
|
||||
return function(name)
|
||||
local base = Object(name)
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("ChangeableObject")(name, basalt)
|
||||
local objectType = "List"
|
||||
base.width = 16
|
||||
base.height = 6
|
||||
base:setZIndex(5)
|
||||
|
||||
local list = {}
|
||||
local itemSelectedBG
|
||||
local itemSelectedFG
|
||||
local itemSelectedBG = colors.black
|
||||
local itemSelectedFG = colors.lightGray
|
||||
local selectionColorActive = true
|
||||
local align = "left"
|
||||
local textAlign = "left"
|
||||
local yOffset = 0
|
||||
local scrollable = true
|
||||
|
||||
base:setSize(16, 8)
|
||||
base:setZIndex(5)
|
||||
|
||||
local object = {
|
||||
init = function(self)
|
||||
local parent = self:getParent()
|
||||
self:listenEvent("mouse_click")
|
||||
self:listenEvent("mouse_drag")
|
||||
self:listenEvent("mouse_scroll")
|
||||
return base.init(self)
|
||||
end,
|
||||
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
setTextAlign = function(self, align)
|
||||
textAlign = align
|
||||
return self
|
||||
end,
|
||||
|
||||
getTextAlign = function(self)
|
||||
return textAlign
|
||||
end,
|
||||
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
end,
|
||||
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
addItem = function(self, text, bgCol, fgCol, ...)
|
||||
table.insert(list, { text = text, bgCol = bgCol or self.bgColor, fgCol = fgCol or self.fgColor, args = { ... } })
|
||||
if (#list == 1) then
|
||||
self:setValue(list[1])
|
||||
table.insert(list, { text = text, bgCol = bgCol or self:getBackground(), fgCol = fgCol or self:getForeground(), args = { ... } })
|
||||
if (#list <= 1) then
|
||||
self:setValue(list[1], false)
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setOptions = function(self, ...)
|
||||
list = {}
|
||||
for k,v in pairs(...)do
|
||||
if(type(v)=="string")then
|
||||
table.insert(list, { text = v, bgCol = self:getBackground(), fgCol = self:getForeground(), args = {} })
|
||||
else
|
||||
table.insert(list, { text = v[1], bgCol = v[2] or self:getBackground(), fgCol = v[3] or self:getForeground(), args = v[4] or {} })
|
||||
end
|
||||
end
|
||||
self:setValue(list[1], false)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setOffset = function(self, yOff)
|
||||
yOffset = yOff
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getOffset = function(self)
|
||||
return yOffset
|
||||
end;
|
||||
end,
|
||||
|
||||
removeItem = function(self, index)
|
||||
table.remove(list, index)
|
||||
if(type(index)=="number")then
|
||||
table.remove(list, index)
|
||||
elseif(type(index)=="table")then
|
||||
for k,v in pairs(list)do
|
||||
if(v==index)then
|
||||
table.remove(list, k)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getItem = function(self, index)
|
||||
return list[index]
|
||||
end;
|
||||
end,
|
||||
|
||||
getAll = function(self)
|
||||
return list
|
||||
end;
|
||||
end,
|
||||
|
||||
getOptions = function(self)
|
||||
return list
|
||||
end,
|
||||
|
||||
getItemIndex = function(self)
|
||||
local selected = self:getValue()
|
||||
@@ -62,61 +117,73 @@ return function(name)
|
||||
return key
|
||||
end
|
||||
end
|
||||
end;
|
||||
end,
|
||||
|
||||
clear = function(self)
|
||||
list = {}
|
||||
self:setValue({})
|
||||
self:setValue({}, false)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getItemCount = function(self)
|
||||
return #list
|
||||
end;
|
||||
end,
|
||||
|
||||
editItem = function(self, index, text, bgCol, fgCol, ...)
|
||||
table.remove(list, index)
|
||||
table.insert(list, index, { text = text, bgCol = bgCol or self.bgColor, fgCol = fgCol or self.fgColor, args = { ... } })
|
||||
table.insert(list, index, { text = text, bgCol = bgCol or self:getBackground(), fgCol = fgCol or self:getForeground(), args = { ... } })
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
selectItem = function(self, index)
|
||||
self:setValue(list[index] or {})
|
||||
self:setValue(list[index] or {}, false)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setSelectedItem = function(self, bgCol, fgCol, active)
|
||||
itemSelectedBG = bgCol or self.bgColor
|
||||
itemSelectedFG = fgCol or self.fgColor
|
||||
setSelectionColor = function(self, bgCol, fgCol, active)
|
||||
itemSelectedBG = bgCol or self:getBackground()
|
||||
itemSelectedFG = fgCol or self:getForeground()
|
||||
selectionColorActive = active~=nil and active or true
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setSelectionBG = function(self, bgCol)
|
||||
return self:setSelectionColor(bgCol, nil, selectionColorActive)
|
||||
end,
|
||||
|
||||
setSelectionFG = function(self, fgCol)
|
||||
return self:setSelectionColor(nil, fgCol, selectionColorActive)
|
||||
end,
|
||||
|
||||
getSelectionColor = function(self)
|
||||
return itemSelectedBG, itemSelectedFG
|
||||
end,
|
||||
|
||||
getSelectionBG = function(self)
|
||||
return itemSelectedBG
|
||||
end,
|
||||
|
||||
getSelectionFG = function(self)
|
||||
return itemSelectedFG
|
||||
end,
|
||||
|
||||
isSelectionColorActive = function(self)
|
||||
return selectionColorActive
|
||||
end,
|
||||
|
||||
setScrollable = function(self, scroll)
|
||||
scrollable = scroll
|
||||
if(scroll==nil)then scrollable = true end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("selectionBG", data)~=nil)then itemSelectedBG = colors[xmlValue("selectionBG", data)] end
|
||||
if(xmlValue("selectionFG", data)~=nil)then itemSelectedFG = colors[xmlValue("selectionFG", data)] end
|
||||
if(xmlValue("scrollable", data)~=nil)then if(xmlValue("scrollable", data))then self:setScrollable(true) else self:setScrollable(false) end end
|
||||
if(xmlValue("offset", data)~=nil)then yOffset = xmlValue("offset", data) end
|
||||
if(data["item"]~=nil)then
|
||||
local tab = data["item"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
self:addItem(xmlValue("text", v), colors[xmlValue("bg", v)], colors[xmlValue("fg", v)])
|
||||
end
|
||||
end
|
||||
return self
|
||||
getScrollable = function(self)
|
||||
return scrollable
|
||||
end,
|
||||
|
||||
scrollHandler = function(self, dir, x, y)
|
||||
@@ -148,13 +215,14 @@ return function(name)
|
||||
|
||||
mouseHandler = function(self, button, x, y)
|
||||
if(base.mouseHandler(self, button, x, y))then
|
||||
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
local obx, oby = self:getAbsolutePosition()
|
||||
local w,h = self:getSize()
|
||||
if (#list > 0) then
|
||||
for n = 1, h do
|
||||
if (list[n + yOffset] ~= nil) then
|
||||
if (obx <= x) and (obx + w > x) and (oby + n - 1 == y) then
|
||||
self:setValue(list[n + yOffset])
|
||||
self:selectHandler()
|
||||
self:updateDraw()
|
||||
end
|
||||
end
|
||||
@@ -173,43 +241,39 @@ return function(name)
|
||||
return self:mouseHandler(1, x, y)
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
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
|
||||
for n = 1, h do
|
||||
if (list[n + yOffset] ~= nil) then
|
||||
if (list[n + yOffset] == self:getValue()) then
|
||||
if (selectionColorActive) then
|
||||
self.parent:writeText(obx, oby + n - 1, utils.getTextHorizontalAlign(list[n + yOffset].text, w, align), itemSelectedBG, itemSelectedFG)
|
||||
else
|
||||
self.parent:writeText(obx, oby + n - 1, utils.getTextHorizontalAlign(list[n + yOffset].text, w, align), list[n + yOffset].bgCol, list[n + yOffset].fgCol)
|
||||
end
|
||||
else
|
||||
self.parent:writeText(obx, oby + n - 1, utils.getTextHorizontalAlign(list[n + yOffset].text, w, align), list[n + yOffset].bgCol, list[n + yOffset].fgCol)
|
||||
end
|
||||
end
|
||||
end
|
||||
onSelect = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("select_item", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
self.parent:addEvent("mouse_drag", self)
|
||||
self.parent:addEvent("mouse_scroll", self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("ListBG")
|
||||
self.fgColor = self.parent:getTheme("ListText")
|
||||
itemSelectedBG = self.parent:getTheme("SelectionBG")
|
||||
itemSelectedFG = self.parent:getTheme("SelectionText")
|
||||
end
|
||||
selectHandler = function(self)
|
||||
self:sendEvent("select_item", self:getValue())
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
base.draw(self)
|
||||
self:addDraw("list", function()
|
||||
local w, h = self:getSize()
|
||||
for n = 1, h do
|
||||
if list[n + yOffset] then
|
||||
local t = list[n + yOffset].text
|
||||
local fg, bg = list[n + yOffset].fgCol, list[n + yOffset].bgCol
|
||||
if list[n + yOffset] == self:getValue() and selectionColorActive then
|
||||
fg, bg = itemSelectedFG, itemSelectedBG
|
||||
end
|
||||
self:addText(1, n, t:sub(1,w))
|
||||
self:addBG(1, n, tHex[bg]:rep(w))
|
||||
self:addFG(1, n, tHex[fg]:rep(w))
|
||||
end
|
||||
end
|
||||
end)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,170 +1,76 @@
|
||||
local Object = require("Object")
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.getValueFromXML
|
||||
local tHex = require("tHex")
|
||||
|
||||
return function(name)
|
||||
local base = Object(name)
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("List")(name, basalt)
|
||||
local objectType = "Menubar"
|
||||
local object = {}
|
||||
|
||||
base.width = 30
|
||||
base.height = 1
|
||||
base:setSize(30, 1)
|
||||
base:setZIndex(5)
|
||||
|
||||
local list = {}
|
||||
local itemSelectedBG
|
||||
local itemSelectedFG
|
||||
local selectionColorActive = true
|
||||
local align = "left"
|
||||
local itemOffset = 0
|
||||
local space = 1
|
||||
local scrollable = false
|
||||
local space, outerSpace = 1, 1
|
||||
local scrollable = true
|
||||
|
||||
local function maxScroll()
|
||||
local mScroll = 0
|
||||
local xPos = 0
|
||||
local w = object:getWidth()
|
||||
local w = base:getWidth()
|
||||
local list = base:getAll()
|
||||
for n = 1, #list do
|
||||
if (xPos + list[n].text:len() + space * 2 > w) then
|
||||
if(xPos < w)then
|
||||
mScroll = mScroll + (list[n].text:len() + space * 2-(w - xPos))
|
||||
else
|
||||
mScroll = mScroll + list[n].text:len() + space * 2
|
||||
end
|
||||
end
|
||||
xPos = xPos + list[n].text:len() + space * 2
|
||||
|
||||
mScroll = mScroll + list[n].text:len() + space * 2
|
||||
end
|
||||
return mScroll
|
||||
return math.max(mScroll - w, 0)
|
||||
end
|
||||
|
||||
object = {
|
||||
init = function(self)
|
||||
local parent = self:getParent()
|
||||
self:listenEvent("mouse_click")
|
||||
self:listenEvent("mouse_drag")
|
||||
self:listenEvent("mouse_scroll")
|
||||
return base.init(self)
|
||||
end,
|
||||
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
end,
|
||||
|
||||
addItem = function(self, text, bgCol, fgCol, ...)
|
||||
table.insert(list, { text = tostring(text), bgCol = bgCol or self.bgColor, fgCol = fgCol or self.fgColor, args = { ... } })
|
||||
if (#list == 1) then
|
||||
self:setValue(list[1])
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getAll = function(self)
|
||||
return list
|
||||
end;
|
||||
|
||||
getItemIndex = function(self)
|
||||
local selected = self:getValue()
|
||||
for key, value in pairs(list) do
|
||||
if (value == selected) then
|
||||
return key
|
||||
end
|
||||
end
|
||||
end;
|
||||
|
||||
clear = function(self)
|
||||
list = {}
|
||||
self:setValue({})
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
setSpace = function(self, _space)
|
||||
space = _space or space
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setOffset = function(self, offset)
|
||||
itemOffset = offset or 0
|
||||
if (itemOffset < 0) then
|
||||
itemOffset = 0
|
||||
end
|
||||
|
||||
local mScroll = maxScroll()
|
||||
if (itemOffset > mScroll) then
|
||||
itemOffset = mScroll
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getOffset = function(self)
|
||||
return itemOffset
|
||||
end;
|
||||
getSpace = function(self)
|
||||
return space
|
||||
end,
|
||||
|
||||
setScrollable = function(self, scroll)
|
||||
scrollable = scroll
|
||||
if(scroll==nil)then scrollable = true end
|
||||
return self
|
||||
end;
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("selectionBG", data)~=nil)then itemSelectedBG = colors[xmlValue("selectionBG", data)] end
|
||||
if(xmlValue("selectionFG", data)~=nil)then itemSelectedFG = colors[xmlValue("selectionFG", data)] end
|
||||
if(xmlValue("scrollable", data)~=nil)then if(xmlValue("scrollable", data))then self:setScrollable(true) else self:setScrollable(false) end end
|
||||
if(xmlValue("offset", data)~=nil)then self:setOffset(xmlValue("offset", data)) end
|
||||
if(xmlValue("space", data)~=nil)then space = xmlValue("space", data) end
|
||||
if(data["item"]~=nil)then
|
||||
local tab = data["item"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
self:addItem(xmlValue("text", v), colors[xmlValue("bg", v)], colors[xmlValue("fg", v)])
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
removeItem = function(self, index)
|
||||
table.remove(list, index)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getItem = function(self, index)
|
||||
return list[index]
|
||||
end;
|
||||
|
||||
getItemCount = function(self)
|
||||
return #list
|
||||
end;
|
||||
|
||||
editItem = function(self, index, text, bgCol, fgCol, ...)
|
||||
table.remove(list, index)
|
||||
table.insert(list, index, { text = text, bgCol = bgCol or self.bgColor, fgCol = fgCol or self.fgColor, args = { ... } })
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
selectItem = function(self, index)
|
||||
self:setValue(list[index] or {})
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setSelectedItem = function(self, bgCol, fgCol, active)
|
||||
itemSelectedBG = bgCol or self.bgColor
|
||||
itemSelectedFG = fgCol or self.fgColor
|
||||
selectionColorActive = active
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
getScrollable = function(self)
|
||||
return scrollable
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y)
|
||||
if(base.mouseHandler(self, button, x, y))then
|
||||
local objX, objY = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
if(base:getBase().mouseHandler(self, button, x, y))then
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local w,h = self:getSize()
|
||||
local xPos = 0
|
||||
local list = self:getAll()
|
||||
for n = 1, #list do
|
||||
if (list[n] ~= nil) then
|
||||
if (objX + xPos <= x + itemOffset) and (objX + xPos + list[n].text:len() + (space*2) > x + itemOffset) and (objY == y) then
|
||||
self:setValue(list[n])
|
||||
self:getEventSystem():sendEvent(event, self, event, 0, x, y, list[n])
|
||||
self:sendEvent(event, self, event, 0, x, y, list[n])
|
||||
end
|
||||
xPos = xPos + list[n].text:len() + space * 2
|
||||
end
|
||||
@@ -172,11 +78,10 @@ return function(name)
|
||||
self:updateDraw()
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
scrollHandler = function(self, dir, x, y)
|
||||
if(base.scrollHandler(self, dir, x, y))then
|
||||
if(base:getBase().scrollHandler(self, dir, x, y))then
|
||||
if(scrollable)then
|
||||
itemOffset = itemOffset + dir
|
||||
if (itemOffset < 0) then
|
||||
@@ -196,46 +101,31 @@ return function(name)
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
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)
|
||||
base.draw(self)
|
||||
self:addDraw("list", function()
|
||||
local parent = self:getParent()
|
||||
local w,h = self:getSize()
|
||||
local text = ""
|
||||
local textBGCol = ""
|
||||
local textFGCol = ""
|
||||
local itemSelectedBG, itemSelectedFG = self:getSelectionColor()
|
||||
for _, v in pairs(self:getAll()) do
|
||||
local newItem = (" "):rep(space) .. v.text .. (" "):rep(space)
|
||||
text = text .. newItem
|
||||
if(v == self:getValue())then
|
||||
textBGCol = textBGCol .. tHex[itemSelectedBG or v.bgCol or self:getBackground()]:rep(newItem:len())
|
||||
textFGCol = textFGCol .. tHex[itemSelectedFG or v.FgCol or self:getForeground()]:rep(newItem:len())
|
||||
else
|
||||
textBGCol = textBGCol .. tHex[v.bgCol or self:getBackground()]:rep(newItem:len())
|
||||
textFGCol = textFGCol .. tHex[v.FgCol or self:getForeground()]:rep(newItem:len())
|
||||
end
|
||||
local text = ""
|
||||
local textBGCol = ""
|
||||
local textFGCol = ""
|
||||
for _, v in pairs(list) do
|
||||
local newItem = (" "):rep(space) .. v.text .. (" "):rep(space)
|
||||
text = text .. newItem
|
||||
if(v == self:getValue())then
|
||||
textBGCol = textBGCol .. tHex[itemSelectedBG or v.bgCol or self.bgColor]:rep(newItem:len())
|
||||
textFGCol = textFGCol .. tHex[itemSelectedFG or v.FgCol or self.fgColor]:rep(newItem:len())
|
||||
else
|
||||
textBGCol = textBGCol .. tHex[v.bgCol or self.bgColor]:rep(newItem:len())
|
||||
textFGCol = textFGCol .. tHex[v.FgCol or self.fgColor]:rep(newItem:len())
|
||||
end
|
||||
end
|
||||
|
||||
self.parent:setText(obx, oby, text:sub(itemOffset+1, w+itemOffset))
|
||||
self.parent:setBG(obx, oby, textBGCol:sub(itemOffset+1, w+itemOffset))
|
||||
self.parent:setFG(obx, oby, textFGCol:sub(itemOffset+1, w+itemOffset))
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
self.parent:addEvent("mouse_scroll", self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("MenubarBG")
|
||||
self.fgColor = self.parent:getTheme("MenubarText")
|
||||
itemSelectedBG = self.parent:getTheme("SelectionBG")
|
||||
itemSelectedFG = self.parent:getTheme("SelectionText")
|
||||
end
|
||||
self:addBlit(1, 1, text:sub(itemOffset+1, w+itemOffset), textFGCol:sub(itemOffset+1, w+itemOffset), textBGCol:sub(itemOffset+1, w+itemOffset))
|
||||
end)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
81
Basalt/objects/MonitorFrame.lua
Normal file
@@ -0,0 +1,81 @@
|
||||
local basaltMon = require("basaltMon")
|
||||
local max,min,sub,rep = math.max,math.min,string.sub,string.rep
|
||||
|
||||
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("BaseFrame")(name, basalt)
|
||||
local objectType = "MonitorFrame"
|
||||
|
||||
base:setTerm(nil)
|
||||
local isMonitorGroup = false
|
||||
local monGroup
|
||||
|
||||
local object = {
|
||||
|
||||
getType = function()
|
||||
return objectType
|
||||
end,
|
||||
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
setMonitor = function(self, newMon)
|
||||
if(type(newMon)=="string")then
|
||||
local mon = peripheral.wrap(newMon)
|
||||
if(mon~=nil)then
|
||||
self:setTerm(mon)
|
||||
end
|
||||
elseif(type(newMon)=="table")then
|
||||
self:setTerm(newMon)
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
setMonitorGroup = function(self, monGrp)
|
||||
monGroup = basaltMon(monGrp)
|
||||
self:setTerm(monGroup)
|
||||
isMonitorGroup = true
|
||||
return self
|
||||
end,
|
||||
|
||||
render = function(self)
|
||||
if(self:getTerm()~=nil)then
|
||||
base.render(self)
|
||||
end
|
||||
end,
|
||||
|
||||
show = function(self)
|
||||
base:getBase().show(self)
|
||||
basalt.setActiveFrame(self)
|
||||
for k,v in pairs(colors)do
|
||||
if(type(v)=="number")then
|
||||
termObject.setPaletteColor(v, colors.packRGB(term.nativePaletteColor((v))))
|
||||
end
|
||||
end
|
||||
for k,v in pairs(colorTheme)do
|
||||
if(type(v)=="number")then
|
||||
termObject.setPaletteColor(type(k)=="number" and k or colors[k], v)
|
||||
else
|
||||
local r,g,b = table.unpack(v)
|
||||
termObject.setPaletteColor(type(k)=="number" and k or colors[k], r,g,b)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
}
|
||||
|
||||
object.mouseHandler = function(self, btn, x, y, isMon, monitor, ...)
|
||||
if(isMonitorGroup)then
|
||||
x, y = monGroup.calculateClick(monitor, x, y)
|
||||
end
|
||||
base.mouseHandler(self, btn, x, y, isMon, monitor, ...)
|
||||
end
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
99
Basalt/objects/MovableFrame.lua
Normal file
@@ -0,0 +1,99 @@
|
||||
local max,min,sub,rep = math.max,math.min,string.sub,string.rep
|
||||
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("Frame")(name, basalt)
|
||||
local objectType = "MovableFrame"
|
||||
local parent
|
||||
|
||||
local dragXOffset, dragYOffset, isDragging = 0, 0, false
|
||||
|
||||
local dragMap = {
|
||||
{x1 = 1, x2 = "width", y1 = 1, y2 = 1}
|
||||
}
|
||||
|
||||
local object = {
|
||||
getType = function()
|
||||
return objectType
|
||||
end,
|
||||
|
||||
setDraggingMap = function(self, t)
|
||||
dragMap = t
|
||||
return self
|
||||
end,
|
||||
|
||||
getDraggingMap = function(self)
|
||||
return dragMap
|
||||
end,
|
||||
|
||||
isType = function(self, t)
|
||||
return objectType==t or (base.isType~=nil and base.isType(t)) or false
|
||||
end,
|
||||
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
load = function(self)
|
||||
base.load(self)
|
||||
self:listenEvent("mouse_click")
|
||||
self:listenEvent("mouse_up")
|
||||
self:listenEvent("mouse_drag")
|
||||
end,
|
||||
|
||||
removeChildren = function(self)
|
||||
base.removeChildren(self)
|
||||
self:listenEvent("mouse_click")
|
||||
self:listenEvent("mouse_up")
|
||||
self:listenEvent("mouse_drag")
|
||||
end,
|
||||
|
||||
dragHandler = function(self, btn, x, y)
|
||||
if(base.dragHandler(self, btn, x, y))then
|
||||
if (isDragging) then
|
||||
local xO, yO = parent:getOffset()
|
||||
xO = xO < 0 and math.abs(xO) or -xO
|
||||
yO = yO < 0 and math.abs(yO) or -yO
|
||||
local parentX = 1
|
||||
local parentY = 1
|
||||
parentX, parentY = parent:getAbsolutePosition()
|
||||
self:setPosition(x + dragXOffset - (parentX - 1) + xO, y + dragYOffset - (parentY - 1) + yO)
|
||||
self:updateDraw()
|
||||
end
|
||||
return true
|
||||
end
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, btn, x, y, ...)
|
||||
if(base.mouseHandler(self, btn, x, y, ...))then
|
||||
parent:setImportant(self)
|
||||
local fx, fy = self:getAbsolutePosition()
|
||||
local w, h = self:getSize()
|
||||
for k,v in pairs(dragMap)do
|
||||
local x1, x2 = v.x1=="width" and w or v.x1, v.x2=="width" and w or v.x2
|
||||
local y1, y2= v.y1=="height" and h or v.y1, v.y2=="height" and h or v.y2
|
||||
if(x>=fx+x1-1)and(x<=fx+x2-1)and(y>=fy+y1-1)and(y<=fy+y2-1)then
|
||||
isDragging = true
|
||||
dragXOffset = fx - x
|
||||
dragYOffset = fy - y
|
||||
return true
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
end,
|
||||
|
||||
mouseUpHandler = function(self, ...)
|
||||
isDragging = false
|
||||
return base.mouseUpHandler(self, ...)
|
||||
end,
|
||||
|
||||
setParent = function(self, p, ...)
|
||||
base.setParent(self, p, ...)
|
||||
parent = p
|
||||
return self
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
290
Basalt/objects/Object.lua
Normal file
@@ -0,0 +1,290 @@
|
||||
local basaltEvent = require("basaltEvent")
|
||||
local utils = require("utils")
|
||||
local uuid = utils.uuid
|
||||
|
||||
local unpack,sub = table.unpack,string.sub
|
||||
|
||||
return function(name, basalt)
|
||||
name = name or uuid()
|
||||
assert(basalt~=nil, "Unable to find basalt instance! ID: "..name)
|
||||
|
||||
-- Base object
|
||||
local objectType = "Object" -- not changeable
|
||||
local isEnabled,initialized = true,false
|
||||
|
||||
local eventSystem = basaltEvent()
|
||||
local registeredEvents = {}
|
||||
local activeEvents = {}
|
||||
|
||||
local parent
|
||||
|
||||
local object = {
|
||||
init = function(self)
|
||||
if(initialized)then return false end
|
||||
initialized = true
|
||||
return true
|
||||
end,
|
||||
|
||||
load = function(self)
|
||||
end,
|
||||
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end,
|
||||
isType = function(self, t)
|
||||
return objectType==t
|
||||
end,
|
||||
|
||||
getProperty = function(self, name)
|
||||
local get = self["get" .. name:gsub("^%l", string.upper)]
|
||||
if (get ~= nil) then
|
||||
return get(self)
|
||||
end
|
||||
end,
|
||||
|
||||
setProperty = function(self, name, ...)
|
||||
local set = self["set" .. name:gsub("^%l", string.upper)]
|
||||
if (set ~= nil) then
|
||||
return set(self, ...)
|
||||
end
|
||||
end,
|
||||
|
||||
getName = function(self)
|
||||
return name
|
||||
end,
|
||||
|
||||
getParent = function(self)
|
||||
return parent
|
||||
end,
|
||||
|
||||
setParent = function(self, newParent, noRemove)
|
||||
if(noRemove)then parent = newParent return self end
|
||||
if (newParent.getType ~= nil and newParent:isType("Container")) then
|
||||
self:remove()
|
||||
newParent:addChild(self)
|
||||
if (self.show) then
|
||||
self:show()
|
||||
end
|
||||
parent = newParent
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
updateEvents = function(self)
|
||||
for k,v in pairs(activeEvents)do
|
||||
parent:removeEvent(k, self)
|
||||
if(v)then
|
||||
parent:addEvent(k, self)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
listenEvent = function(self, event, active)
|
||||
if(parent~=nil)then
|
||||
if(active)or(active==nil)then
|
||||
activeEvents[event] = true
|
||||
parent:addEvent(event, self)
|
||||
elseif(active==false)then
|
||||
activeEvents[event] = false
|
||||
parent:removeEvent(event, self)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
getZIndex = function(self)
|
||||
return 1
|
||||
end,
|
||||
|
||||
enable = function(self)
|
||||
isEnabled = true
|
||||
return self
|
||||
end,
|
||||
|
||||
disable = function(self)
|
||||
isEnabled = false
|
||||
return self
|
||||
end,
|
||||
|
||||
isEnabled = function(self)
|
||||
return isEnabled
|
||||
end,
|
||||
|
||||
remove = function(self)
|
||||
if (parent ~= nil) then
|
||||
parent:removeChild(self)
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getBaseFrame = function(self)
|
||||
if(parent~=nil)then
|
||||
return parent:getBaseFrame()
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onEvent = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("other_event", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
getEventSystem = function(self)
|
||||
return eventSystem
|
||||
end,
|
||||
|
||||
getRegisteredEvents = function(self)
|
||||
return registeredEvents
|
||||
end,
|
||||
|
||||
registerEvent = function(self, event, func)
|
||||
if(parent~=nil)then
|
||||
if(event=="mouse_drag")then
|
||||
parent:addEvent("mouse_click", self)
|
||||
parent:addEvent("mouse_up", self)
|
||||
end
|
||||
if(event=="mouse_release")then
|
||||
parent:addEvent("mouse_up", self)
|
||||
end
|
||||
parent:addEvent(event, self)
|
||||
end
|
||||
eventSystem:registerEvent(event, func)
|
||||
if (registeredEvents[event] == nil) then
|
||||
registeredEvents[event] = {}
|
||||
end
|
||||
table.insert(registeredEvents[event], func)
|
||||
end,
|
||||
|
||||
removeEvent = function(self, event, index)
|
||||
if(eventSystem:getEventCount(event)<1)then
|
||||
if(parent~=nil)then
|
||||
parent:removeEvent(event, self)
|
||||
end
|
||||
end
|
||||
eventSystem:removeEvent(event, index)
|
||||
if (registeredEvents[event] ~= nil) then
|
||||
table.remove(registeredEvents[event], index)
|
||||
if (#registeredEvents[event] == 0) then
|
||||
registeredEvents[event] = nil
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, ...)
|
||||
local val = self:sendEvent("other_event", event, ...)
|
||||
if(val~=nil)then return val end
|
||||
end,
|
||||
|
||||
customEventHandler = function(self, event, ...)
|
||||
local val = self:sendEvent("custom_event", event, ...)
|
||||
if(val~=nil)then return val end
|
||||
return true
|
||||
end,
|
||||
|
||||
sendEvent = function(self, event, ...)
|
||||
if(event=="other_event")or(event=="custom_event")then
|
||||
return eventSystem:sendEvent(event, self, ...)
|
||||
end
|
||||
return eventSystem:sendEvent(event, self, event, ...)
|
||||
end,
|
||||
|
||||
onClick = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_click", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onClickUp = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_up", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onRelease = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_release", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onScroll = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_scroll", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onHover = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_hover", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onLeave = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_leave", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onDrag = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("mouse_drag", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onKey = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("key", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onChar = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("char", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onKeyUp = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("key_up", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return object
|
||||
end
|
||||
@@ -1,28 +1,16 @@
|
||||
local Object = require("Object")
|
||||
local log = require("basaltLogs")
|
||||
|
||||
return function(name)
|
||||
return function(name, basalt)
|
||||
-- Pane
|
||||
local base = Object(name)
|
||||
local base = basalt.getObject("VisualObject")(name, basalt)
|
||||
local objectType = "Pane"
|
||||
|
||||
base:setSize(25, 10)
|
||||
|
||||
local object = {
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
|
||||
setBackground = function(self, col, sym, symC)
|
||||
base.setBackground(self, col, sym, symC)
|
||||
return self
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("PaneBG")
|
||||
self.fgColor = self.parent:getTheme("PaneBG")
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,19 +1,16 @@
|
||||
local Object = require("Object")
|
||||
local tHex = require("tHex")
|
||||
local process = require("process")
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
|
||||
local sub = string.sub
|
||||
|
||||
return function(name, parent)
|
||||
local base = Object(name)
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("VisualObject")(name, basalt)
|
||||
local objectType = "Program"
|
||||
base:setZIndex(5)
|
||||
local object
|
||||
local cachedPath
|
||||
local enviroment = {}
|
||||
|
||||
local function createBasaltWindow(x, y, width, height, self)
|
||||
local function createBasaltWindow(x, y, width, height)
|
||||
local xCursor, yCursor = 1, 1
|
||||
local bgColor, fgColor = colors.black, colors.white
|
||||
local cursorBlink = false
|
||||
@@ -30,7 +27,7 @@ return function(name, parent)
|
||||
|
||||
for i = 0, 15 do
|
||||
local c = 2 ^ i
|
||||
tPalette[c] = { parent:getBasaltInstance().getBaseTerm().getPaletteColour(c) }
|
||||
tPalette[c] = { basalt.getTerm().getPaletteColour(c) }
|
||||
end
|
||||
|
||||
local function createEmptyLines()
|
||||
@@ -66,67 +63,17 @@ return function(name, parent)
|
||||
end
|
||||
|
||||
local function internalBlit(sText, sTextColor, sBackgroundColor)
|
||||
-- copy pasti strikes again (cc: window.lua)
|
||||
local nStart = xCursor
|
||||
local nEnd = nStart + #sText - 1
|
||||
if yCursor >= 1 and yCursor <= height then
|
||||
if nStart <= width and nEnd >= 1 then
|
||||
-- Modify line
|
||||
if nStart == 1 and nEnd == width then
|
||||
cacheT[yCursor] = sText
|
||||
cacheFG[yCursor] = sTextColor
|
||||
cacheBG[yCursor] = sBackgroundColor
|
||||
else
|
||||
local sClippedText, sClippedTextColor, sClippedBackgroundColor
|
||||
if nStart < 1 then
|
||||
local nClipStart = 1 - nStart + 1
|
||||
local nClipEnd = width - nStart + 1
|
||||
sClippedText = sub(sText, nClipStart, nClipEnd)
|
||||
sClippedTextColor = sub(sTextColor, nClipStart, nClipEnd)
|
||||
sClippedBackgroundColor = sub(sBackgroundColor, nClipStart, nClipEnd)
|
||||
elseif nEnd > width then
|
||||
local nClipEnd = width - nStart + 1
|
||||
sClippedText = sub(sText, 1, nClipEnd)
|
||||
sClippedTextColor = sub(sTextColor, 1, nClipEnd)
|
||||
sClippedBackgroundColor = sub(sBackgroundColor, 1, nClipEnd)
|
||||
else
|
||||
sClippedText = sText
|
||||
sClippedTextColor = sTextColor
|
||||
sClippedBackgroundColor = sBackgroundColor
|
||||
end
|
||||
|
||||
local sOldText = cacheT[yCursor]
|
||||
local sOldTextColor = cacheFG[yCursor]
|
||||
local sOldBackgroundColor = cacheBG[yCursor]
|
||||
local sNewText, sNewTextColor, sNewBackgroundColor
|
||||
if nStart > 1 then
|
||||
local nOldEnd = nStart - 1
|
||||
sNewText = sub(sOldText, 1, nOldEnd) .. sClippedText
|
||||
sNewTextColor = sub(sOldTextColor, 1, nOldEnd) .. sClippedTextColor
|
||||
sNewBackgroundColor = sub(sOldBackgroundColor, 1, nOldEnd) .. sClippedBackgroundColor
|
||||
else
|
||||
sNewText = sClippedText
|
||||
sNewTextColor = sClippedTextColor
|
||||
sNewBackgroundColor = sClippedBackgroundColor
|
||||
end
|
||||
if nEnd < width then
|
||||
local nOldStart = nEnd + 1
|
||||
sNewText = sNewText .. sub(sOldText, nOldStart, width)
|
||||
sNewTextColor = sNewTextColor .. sub(sOldTextColor, nOldStart, width)
|
||||
sNewBackgroundColor = sNewBackgroundColor .. sub(sOldBackgroundColor, nOldStart, width)
|
||||
end
|
||||
|
||||
cacheT[yCursor] = sNewText
|
||||
cacheFG[yCursor] = sNewTextColor
|
||||
cacheBG[yCursor] = sNewBackgroundColor
|
||||
end
|
||||
object:updateDraw()
|
||||
end
|
||||
xCursor = nEnd + 1
|
||||
if (visible) then
|
||||
updateCursor()
|
||||
end
|
||||
if yCursor < 1 or yCursor > height or xCursor < 1 or xCursor + #sText - 1 > width then
|
||||
return
|
||||
end
|
||||
cacheT[yCursor] = sub(cacheT[yCursor], 1, xCursor - 1) .. sText .. sub(cacheT[yCursor], xCursor + #sText, width)
|
||||
cacheFG[yCursor] = sub(cacheFG[yCursor], 1, xCursor - 1) .. sTextColor .. sub(cacheFG[yCursor], xCursor + #sText, width)
|
||||
cacheBG[yCursor] = sub(cacheBG[yCursor], 1, xCursor - 1) .. sBackgroundColor .. sub(cacheBG[yCursor], xCursor + #sText, width)
|
||||
xCursor = xCursor + #sText
|
||||
if visible then
|
||||
updateCursor()
|
||||
end
|
||||
object:updateDraw()
|
||||
end
|
||||
|
||||
local function setText(_x, _y, text)
|
||||
@@ -233,7 +180,7 @@ return function(name, parent)
|
||||
if (visible) then
|
||||
updateCursor()
|
||||
end
|
||||
end;
|
||||
end,
|
||||
|
||||
getCursorPos = function()
|
||||
return xCursor, yCursor
|
||||
@@ -310,57 +257,40 @@ return function(name, parent)
|
||||
end
|
||||
end;
|
||||
|
||||
writeText = function(_x, _y, text, bgCol, fgCol)
|
||||
bgCol = bgCol or bgColor
|
||||
fgCol = fgCol or fgColor
|
||||
setText(x, _y, text)
|
||||
setBG(_x, _y, tHex[bgCol]:rep(text:len()))
|
||||
setFG(_x, _y, tHex[fgCol]:rep(text:len()))
|
||||
end;
|
||||
|
||||
basalt_update = function()
|
||||
if (parent ~= nil) then
|
||||
for n = 1, height do
|
||||
parent:setText(x, y + (n - 1), cacheT[n])
|
||||
parent:setBG(x, y + (n - 1), cacheBG[n])
|
||||
parent:setFG(x, y + (n - 1), cacheFG[n])
|
||||
end
|
||||
for n = 1, height do
|
||||
object:addBlit(1, n, cacheT[n], cacheFG[n], cacheBG[n])
|
||||
end
|
||||
end;
|
||||
end,
|
||||
|
||||
scroll = function(offset)
|
||||
if type(offset) ~= "number" then
|
||||
error("bad argument #1 (expected number, got " .. type(offset) .. ")", 2)
|
||||
end
|
||||
assert(type(offset) == "number", "bad argument #1 (expected number, got " .. type(offset) .. ")")
|
||||
if offset ~= 0 then
|
||||
local sEmptyText = emptySpaceLine
|
||||
local sEmptyTextColor = emptyColorLines[fgColor]
|
||||
local sEmptyBackgroundColor = emptyColorLines[bgColor]
|
||||
for newY = 1, height do
|
||||
local y = newY + offset
|
||||
if y >= 1 and y <= height then
|
||||
cacheT[newY] = cacheT[y]
|
||||
cacheBG[newY] = cacheBG[y]
|
||||
cacheFG[newY] = cacheFG[y]
|
||||
else
|
||||
cacheT[newY] = sEmptyText
|
||||
cacheFG[newY] = sEmptyTextColor
|
||||
cacheBG[newY] = sEmptyBackgroundColor
|
||||
end
|
||||
for newY = 1, height do
|
||||
local y = newY + offset
|
||||
if y < 1 or y > height then
|
||||
cacheT[newY] = emptySpaceLine
|
||||
cacheFG[newY] = emptyColorLines[fgColor]
|
||||
cacheBG[newY] = emptyColorLines[bgColor]
|
||||
else
|
||||
cacheT[newY] = cacheT[y]
|
||||
cacheBG[newY] = cacheBG[y]
|
||||
cacheFG[newY] = cacheFG[y]
|
||||
end
|
||||
end
|
||||
end
|
||||
if (visible) then
|
||||
updateCursor()
|
||||
end
|
||||
end;
|
||||
end,
|
||||
|
||||
|
||||
isColor = function()
|
||||
return parent:getBasaltInstance().getBaseTerm().isColor()
|
||||
return basalt.getTerm().isColor()
|
||||
end;
|
||||
|
||||
isColour = function()
|
||||
return parent:getBasaltInstance().getBaseTerm().isColor()
|
||||
return basalt.getTerm().isColor()
|
||||
end;
|
||||
|
||||
write = function(text)
|
||||
@@ -416,19 +346,20 @@ return function(name, parent)
|
||||
return basaltwindow
|
||||
end
|
||||
|
||||
base.width = 30
|
||||
base.height = 12
|
||||
local pWindow = createBasaltWindow(1, 1, base.width, base.height)
|
||||
base:setZIndex(5)
|
||||
base:setSize(30, 12)
|
||||
local pWindow = createBasaltWindow(1, 1, 30, 12)
|
||||
local curProcess
|
||||
local paused = false
|
||||
local queuedEvent = {}
|
||||
|
||||
local function updateCursor(self)
|
||||
local parent = self:getParent()
|
||||
local xCur, yCur = pWindow.getCursorPos()
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local obx, oby = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
if (obx + xCur - 1 >= 1 and obx + xCur - 1 <= obx + w - 1 and yCur + oby - 1 >= 1 and yCur + oby - 1 <= oby + h - 1) then
|
||||
self.parent:setCursor(self:isFocused() and pWindow.getCursorBlink(), obx + xCur - 1, yCur + oby - 1, pWindow.getTextColor())
|
||||
parent:setCursor(self:isFocused() and pWindow.getCursorBlink(), obx + xCur - 1, yCur + oby - 1, pWindow.getTextColor())
|
||||
end
|
||||
end
|
||||
|
||||
@@ -452,7 +383,7 @@ return function(name, parent)
|
||||
end
|
||||
if not (curProcess:isDead()) then
|
||||
if not (paused) then
|
||||
local absX, absY = self:getAbsolutePosition(self:getAnchorPosition(nil, nil, true))
|
||||
local absX, absY = self:getAbsolutePosition()
|
||||
resumeProcess(self, event, p1, x-absX+1, y-absY+1)
|
||||
updateCursor(self)
|
||||
end
|
||||
@@ -480,8 +411,8 @@ return function(name, parent)
|
||||
|
||||
show = function(self)
|
||||
base.show(self)
|
||||
pWindow.setBackgroundColor(self.bgColor)
|
||||
pWindow.setTextColor(self.fgColor)
|
||||
pWindow.setBackgroundColor(self:getBackground())
|
||||
pWindow.setTextColor(self:getForeground())
|
||||
pWindow.basalt_setVisible(true)
|
||||
return self
|
||||
end;
|
||||
@@ -494,14 +425,8 @@ return function(name, parent)
|
||||
|
||||
setPosition = function(self, x, y, rel)
|
||||
base.setPosition(self, x, y, rel)
|
||||
pWindow.basalt_reposition(self:getAnchorPosition())
|
||||
pWindow.basalt_reposition(self:getPosition())
|
||||
return self
|
||||
end;
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("path", data)~=nil)then cachedPath = xmlValue("path", data) end
|
||||
if(xmlValue("execute", data)~=nil)then if(xmlValue("execute", data))then if(cachedPath~=nil)then self:execute(cachedPath) end end end
|
||||
end,
|
||||
|
||||
getBasaltWindow = function()
|
||||
@@ -537,37 +462,38 @@ return function(name, parent)
|
||||
pWindow.setTextColor(colors.white)
|
||||
pWindow.clear()
|
||||
pWindow.setCursorPos(1, 1)
|
||||
pWindow.setBackgroundColor(self.bgColor)
|
||||
pWindow.setTextColor(self.fgColor)
|
||||
pWindow.setBackgroundColor(self:getBackground())
|
||||
pWindow.setTextColor(self:getForeground() or colors.white)
|
||||
pWindow.basalt_setVisible(true)
|
||||
|
||||
resumeProcess(self)
|
||||
paused = false
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
self.parent:addEvent("mouse_up", self)
|
||||
self.parent:addEvent("mouse_drag", self)
|
||||
self.parent:addEvent("mouse_scroll", self)
|
||||
self.parent:addEvent("key", self)
|
||||
self.parent:addEvent("key_up", self)
|
||||
self.parent:addEvent("char", self)
|
||||
self.parent:addEvent("other_event", self)
|
||||
end
|
||||
self:listenEvent("mouse_click", self)
|
||||
self:listenEvent("mouse_up", self)
|
||||
self:listenEvent("mouse_drag", self)
|
||||
self:listenEvent("mouse_scroll", self)
|
||||
self:listenEvent("key", self)
|
||||
self:listenEvent("key_up", self)
|
||||
self:listenEvent("char", self)
|
||||
self:listenEvent("other_event", self)
|
||||
return self
|
||||
end;
|
||||
|
||||
stop = function(self)
|
||||
setExecute = function(self, path, ...)
|
||||
return self:execute(path, ...)
|
||||
end,
|
||||
|
||||
stop = function(self)
|
||||
local parent = self:getParent()
|
||||
if (curProcess ~= nil) then
|
||||
if not (curProcess:isDead()) then
|
||||
resumeProcess(self, "terminate")
|
||||
if (curProcess:isDead()) then
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:setCursor(false)
|
||||
end
|
||||
parent:setCursor(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
self.parent:removeEvents(self)
|
||||
parent:removeEvents(self)
|
||||
return self
|
||||
end;
|
||||
|
||||
@@ -576,7 +502,7 @@ return function(name, parent)
|
||||
if (curProcess ~= nil) then
|
||||
if not (curProcess:isDead()) then
|
||||
if not (paused) then
|
||||
self:injectEvents(queuedEvent)
|
||||
self:injectEvents(table.unpack(queuedEvent))
|
||||
queuedEvent = {}
|
||||
end
|
||||
end
|
||||
@@ -588,13 +514,13 @@ return function(name, parent)
|
||||
return paused
|
||||
end;
|
||||
|
||||
injectEvent = function(self, event, p1, p2, p3, p4, ign)
|
||||
injectEvent = function(self, event, ign, ...)
|
||||
if (curProcess ~= nil) then
|
||||
if not (curProcess:isDead()) then
|
||||
if (paused == false) or (ign) then
|
||||
resumeProcess(self, event, p1, p2, p3, p4)
|
||||
resumeProcess(self, event, ...)
|
||||
else
|
||||
table.insert(queuedEvent, { event = event, args = { p1, p2, p3, p4 } })
|
||||
table.insert(queuedEvent, { event = event, args = {...} })
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -610,10 +536,10 @@ return function(name, parent)
|
||||
return self
|
||||
end;
|
||||
|
||||
injectEvents = function(self, events)
|
||||
injectEvents = function(self, ...)
|
||||
if (curProcess ~= nil) then
|
||||
if not (curProcess:isDead()) then
|
||||
for _, value in pairs(events) do
|
||||
for _, value in pairs({...}) do
|
||||
resumeProcess(self, value.event, table.unpack(value.args))
|
||||
end
|
||||
end
|
||||
@@ -682,12 +608,13 @@ return function(name, parent)
|
||||
if (curProcess ~= nil) then
|
||||
if not (curProcess:isDead()) then
|
||||
if not (paused) then
|
||||
if (self.parent ~= nil) then
|
||||
local parent = self:getParent()
|
||||
if (parent ~= nil) then
|
||||
local xCur, yCur = pWindow.getCursorPos()
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local obx, oby = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
if (obx + xCur - 1 >= 1 and obx + xCur - 1 <= obx + w - 1 and yCur + oby - 1 >= 1 and yCur + oby - 1 <= oby + h - 1) then
|
||||
self.parent:setCursor(pWindow.getCursorBlink(), obx + xCur - 1, yCur + oby - 1, pWindow.getTextColor())
|
||||
parent:setCursor(pWindow.getCursorBlink(), obx + xCur - 1, yCur + oby - 1, pWindow.getTextColor())
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -699,72 +626,70 @@ return function(name, parent)
|
||||
base.loseFocusHandler(self)
|
||||
if (curProcess ~= nil) then
|
||||
if not (curProcess:isDead()) then
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:setCursor(false)
|
||||
local parent = self:getParent()
|
||||
if (parent ~= nil) then
|
||||
parent:setCursor(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, p1, p2, p3, p4)
|
||||
if(base.eventHandler(self, event, p1, p2, p3, p4))then
|
||||
if (curProcess == nil) then
|
||||
return
|
||||
end
|
||||
if(event=="dynamicValueEvent")then
|
||||
local w, h = pWindow.getSize()
|
||||
local pW, pH = self:getSize()
|
||||
if(w~=pW)or(h~=pH)then
|
||||
pWindow.basalt_resize(pW, pH)
|
||||
if not (curProcess:isDead()) then
|
||||
resumeProcess(self, "term_resize")
|
||||
eventHandler = function(self, event, ...)
|
||||
base.eventHandler(self, event, ...)
|
||||
if curProcess == nil then
|
||||
return
|
||||
end
|
||||
if not curProcess:isDead() then
|
||||
if not paused then
|
||||
resumeProcess(self, event, ...)
|
||||
if self:isFocused() then
|
||||
local parent = self:getParent()
|
||||
local obx, oby = self:getPosition()
|
||||
local xCur, yCur = pWindow.getCursorPos()
|
||||
local w,h = self:getSize()
|
||||
if obx + xCur - 1 >= 1 and obx + xCur - 1 <= obx + w - 1 and yCur + oby - 1 >= 1 and yCur + oby - 1 <= oby + h - 1 then
|
||||
parent:setCursor(pWindow.getCursorBlink(), obx + xCur - 1, yCur + oby - 1, pWindow.getTextColor())
|
||||
end
|
||||
end
|
||||
pWindow.basalt_reposition(self:getAnchorPosition())
|
||||
|
||||
else
|
||||
table.insert(queuedEvent, { event = event, args = { ... } })
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
resizeHandler = function(self, ...)
|
||||
base.resizeHandler(self, ...)
|
||||
if (curProcess ~= nil) then
|
||||
if not (curProcess:isDead()) then
|
||||
if not (paused) then
|
||||
if(event ~= "terminate") then
|
||||
resumeProcess(self, event, p1, p2, p3, p4)
|
||||
end
|
||||
if (self:isFocused()) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local xCur, yCur = pWindow.getCursorPos()
|
||||
if (self.parent ~= nil) then
|
||||
local w,h = self:getSize()
|
||||
if (obx + xCur - 1 >= 1 and obx + xCur - 1 <= obx + w - 1 and yCur + oby - 1 >= 1 and yCur + oby - 1 <= oby + h - 1) then
|
||||
self.parent:setCursor(pWindow.getCursorBlink(), obx + xCur - 1, yCur + oby - 1, pWindow.getTextColor())
|
||||
end
|
||||
end
|
||||
|
||||
if (event == "terminate") then
|
||||
resumeProcess(self, event)
|
||||
self.parent:setCursor(false)
|
||||
return true
|
||||
end
|
||||
end
|
||||
pWindow.basalt_resize(self:getSize())
|
||||
resumeProcess(self, "term_resize", self:getSize())
|
||||
else
|
||||
table.insert(queuedEvent, { event = event, args = { p1, p2, p3, p4 } })
|
||||
pWindow.basalt_resize(self:getSize())
|
||||
table.insert(queuedEvent, { event = "term_resize", args = { self:getSize() } })
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
end,
|
||||
|
||||
repositionHandler = function(self, ...)
|
||||
base.repositionHandler(self, ...)
|
||||
if (curProcess ~= nil) then
|
||||
if not (curProcess:isDead()) then
|
||||
pWindow.basalt_reposition(self:getPosition())
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local xCur, yCur = pWindow.getCursorPos()
|
||||
local w,h = self:getSize()
|
||||
pWindow.basalt_reposition(obx, oby)
|
||||
pWindow.basalt_update()
|
||||
if (obx + xCur - 1 >= 1 and obx + xCur - 1 <= obx + w - 1 and yCur + oby - 1 >= 1 and yCur + oby - 1 <= oby + h - 1) then
|
||||
self.parent:setCursor(self:isFocused() and pWindow.getCursorBlink(), obx + xCur - 1, yCur + oby - 1, pWindow.getTextColor())
|
||||
end
|
||||
end
|
||||
end
|
||||
base.draw(self)
|
||||
self:addDraw("program", function()
|
||||
local parent = self:getParent()
|
||||
local obx, oby = self:getPosition()
|
||||
local xCur, yCur = pWindow.getCursorPos()
|
||||
local w,h = self:getSize()
|
||||
pWindow.basalt_update()
|
||||
end)
|
||||
end,
|
||||
|
||||
onError = function(self, ...)
|
||||
@@ -773,9 +698,8 @@ return function(name, parent)
|
||||
self:registerEvent("program_error", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("other_event", self)
|
||||
end
|
||||
local parent = self:getParent()
|
||||
self:listenEvent("other_event")
|
||||
return self
|
||||
end,
|
||||
|
||||
@@ -785,19 +709,13 @@ return function(name, parent)
|
||||
self:registerEvent("program_done", v)
|
||||
end
|
||||
end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:addEvent("other_event", self)
|
||||
end
|
||||
local parent = self:getParent()
|
||||
self:listenEvent("other_event")
|
||||
return self
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
if(base.init(self))then
|
||||
elf.bgColor = self.parent:getTheme("ProgramBG")
|
||||
end
|
||||
end,
|
||||
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,52 +1,33 @@
|
||||
local Object = require("Object")
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
|
||||
return function(name)
|
||||
-- Checkbox
|
||||
local base = Object(name)
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("ChangeableObject")(name, basalt)
|
||||
local objectType = "Progressbar"
|
||||
|
||||
local progress = 0
|
||||
|
||||
base:setZIndex(5)
|
||||
base:setValue(false)
|
||||
base.width = 25
|
||||
base.height = 1
|
||||
base:setSize(25, 3)
|
||||
|
||||
local activeBarColor
|
||||
local activeBarColor = colors.black
|
||||
local activeBarSymbol = ""
|
||||
local activeBarSymbolCol = colors.white
|
||||
local bgBarSymbol = ""
|
||||
local direction = 0
|
||||
|
||||
local object = {
|
||||
init = function(self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("ProgressbarBG")
|
||||
self.fgColor = self.parent:getTheme("ProgressbarText")
|
||||
activeBarColor = self.parent:getTheme("ProgressbarActiveBG")
|
||||
end
|
||||
end,
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("direction", data)~=nil)then direction = xmlValue("direction", data) end
|
||||
if(xmlValue("progressColor", data)~=nil)then activeBarColor = colors[xmlValue("progressColor", data)] end
|
||||
if(xmlValue("progressSymbol", data)~=nil)then activeBarSymbol = xmlValue("progressSymbol", data) end
|
||||
if(xmlValue("backgroundSymbol", data)~=nil)then bgBarSymbol = xmlValue("backgroundSymbol", data) end
|
||||
if(xmlValue("progressSymbolColor", data)~=nil)then activeBarSymbolCol = colors[xmlValue("progressSymbolColor", data)] end
|
||||
if(xmlValue("onDone", data)~=nil)then self:generateXMLEventFunction(self.onProgressDone, xmlValue("onDone", data)) end
|
||||
return self
|
||||
end,
|
||||
|
||||
setDirection = function(self, dir)
|
||||
direction = dir
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getDirection = function(self)
|
||||
return direction
|
||||
end,
|
||||
|
||||
setProgressBar = function(self, color, symbol, symbolcolor)
|
||||
activeBarColor = color or activeBarColor
|
||||
@@ -54,13 +35,45 @@ return function(name)
|
||||
activeBarSymbolCol = symbolcolor or activeBarSymbolCol
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getProgressBar = function(self)
|
||||
return activeBarColor, activeBarSymbol, activeBarSymbolCol
|
||||
end,
|
||||
|
||||
setActiveBarColor = function(self, color)
|
||||
return self:setProgressBar(color, nil, nil)
|
||||
end,
|
||||
|
||||
getActiveBarColor = function(self)
|
||||
return activeBarColor
|
||||
end,
|
||||
|
||||
setActiveBarSymbol = function(self, symbol)
|
||||
return self:setProgressBar(nil, symbol, nil)
|
||||
end,
|
||||
|
||||
getActiveBarSymbol = function(self)
|
||||
return activeBarSymbol
|
||||
end,
|
||||
|
||||
setActiveBarSymbolColor = function(self, symbolColor)
|
||||
return self:setProgressBar(nil, nil, symbolColor)
|
||||
end,
|
||||
|
||||
getActiveBarSymbolColor = function(self)
|
||||
return activeBarSymbolCol
|
||||
end,
|
||||
|
||||
setBackgroundSymbol = function(self, symbol)
|
||||
bgBarSymbol = symbol:sub(1, 1)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getBackgroundSymbol = function(self)
|
||||
return bgBarSymbol
|
||||
end,
|
||||
|
||||
setProgress = function(self, value)
|
||||
if (value >= 0) and (value <= 100) and (progress ~= value) then
|
||||
@@ -72,51 +85,52 @@ return function(name)
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getProgress = function(self)
|
||||
return progress
|
||||
end;
|
||||
end,
|
||||
|
||||
onProgressDone = function(self, f)
|
||||
self:registerEvent("progress_done", f)
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
progressDoneHandler = function(self)
|
||||
self:sendEvent("progress_done", self)
|
||||
end;
|
||||
self:sendEvent("progress_done")
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
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(bgBarSymbol~="")then self.parent:drawTextBox(obx, oby, w, h, bgBarSymbol) end
|
||||
if(self.fgColor~=false)then self.parent:drawForegroundBox(obx, oby, w, h, self.fgColor) end
|
||||
if (direction == 1) then
|
||||
self.parent:drawBackgroundBox(obx, oby, w, h / 100 * progress, activeBarColor)
|
||||
self.parent:drawForegroundBox(obx, oby, w, h / 100 * progress, activeBarSymbolCol)
|
||||
self.parent:drawTextBox(obx, oby, w, h / 100 * progress, activeBarSymbol)
|
||||
elseif (direction == 2) then
|
||||
self.parent:drawBackgroundBox(obx, oby + math.ceil(h - h / 100 * progress), w, h / 100 * progress, activeBarColor)
|
||||
self.parent:drawForegroundBox(obx, oby + math.ceil(h - h / 100 * progress), w, h / 100 * progress, activeBarSymbolCol)
|
||||
self.parent:drawTextBox(obx, oby + math.ceil(h - h / 100 * progress), w, h / 100 * progress, activeBarSymbol)
|
||||
elseif (direction == 3) then
|
||||
self.parent:drawBackgroundBox(obx + math.ceil(w - w / 100 * progress), oby, w / 100 * progress, h, activeBarColor)
|
||||
self.parent:drawForegroundBox(obx + math.ceil(w - w / 100 * progress), oby, w / 100 * progress, h, activeBarSymbolCol)
|
||||
self.parent:drawTextBox(obx + math.ceil(w - w / 100 * progress), oby, w / 100 * progress, h, activeBarSymbol)
|
||||
else
|
||||
self.parent:drawBackgroundBox(obx, oby, w / 100 * progress, h, activeBarColor)
|
||||
self.parent:drawForegroundBox(obx, oby, w / 100 * progress, h, activeBarSymbolCol)
|
||||
self.parent:drawTextBox(obx, oby, w / 100 * progress, h, activeBarSymbol)
|
||||
end
|
||||
base.draw(self)
|
||||
self:addDraw("progressbar", function()
|
||||
local obx, oby = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
local bgCol,fgCol = self:getBackground(), self:getForeground()
|
||||
if(bgCol~=false)then self:addBackgroundBox(1, 1, w, h, bgCol) end
|
||||
if(bgBarSymbol~="")then self:addTextBox(1, 1, w, h, bgBarSymbol) end
|
||||
if(fgCol~=false)then self:addForegroundBox(1, 1, w, h, fgCol) end
|
||||
if (direction == 1) then
|
||||
self:addBackgroundBox(1, 1, w, h / 100 * progress, activeBarColor)
|
||||
self:addForegroundBox(1, 1, w, h / 100 * progress, activeBarSymbolCol)
|
||||
self:addTextBox(1, 1, w, h / 100 * progress, activeBarSymbol)
|
||||
elseif (direction == 3) then
|
||||
self:addBackgroundBox(1, 1 + math.ceil(h - h / 100 * progress), w, h / 100 * progress, activeBarColor)
|
||||
self:addForegroundBox(1, 1 + math.ceil(h - h / 100 * progress), w, h / 100 * progress, activeBarSymbolCol)
|
||||
self:addTextBox(1, 1 + math.ceil(h - h / 100 * progress), w, h / 100 * progress, activeBarSymbol)
|
||||
elseif (direction == 2) then
|
||||
self:addBackgroundBox(1 + math.ceil(w - w / 100 * progress), 1, w / 100 * progress, h, activeBarColor)
|
||||
self:addForegroundBox(1 + math.ceil(w - w / 100 * progress), 1, w / 100 * progress, h, activeBarSymbolCol)
|
||||
self:addTextBox(1 + math.ceil(w - w / 100 * progress), 1, w / 100 * progress, h, activeBarSymbol)
|
||||
else
|
||||
self:addBackgroundBox(1, 1, math.ceil( w / 100 * progress), h, activeBarColor)
|
||||
self:addForegroundBox(1, 1, math.ceil(w / 100 * progress), h, activeBarSymbolCol)
|
||||
self:addTextBox(1, 1, math.ceil(w / 100 * progress), h, activeBarSymbol)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end,
|
||||
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,20 +1,18 @@
|
||||
local Object = require("Object")
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.getValueFromXML
|
||||
local tHex = require("tHex")
|
||||
|
||||
return function(name)
|
||||
local base = Object(name)
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("List")(name, basalt)
|
||||
local objectType = "Radio"
|
||||
base.width = 8
|
||||
|
||||
base:setSize(1, 1)
|
||||
base:setZIndex(5)
|
||||
|
||||
local list = {}
|
||||
local itemSelectedBG
|
||||
local itemSelectedFG
|
||||
local boxSelectedBG
|
||||
local boxSelectedFG
|
||||
local boxNotSelectedBG
|
||||
local boxNotSelectedFG
|
||||
local boxSelectedBG = colors.black
|
||||
local boxSelectedFG = colors.green
|
||||
local boxNotSelectedBG = colors.black
|
||||
local boxNotSelectedFG = colors.red
|
||||
local selectionColorActive = true
|
||||
local symbol = "\7"
|
||||
local align = "left"
|
||||
@@ -22,148 +20,119 @@ return function(name)
|
||||
local object = {
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("selectionBG", data)~=nil)then itemSelectedBG = colors[xmlValue("selectionBG", data)] end
|
||||
if(xmlValue("selectionFG", data)~=nil)then itemSelectedFG = colors[xmlValue("selectionFG", data)] end
|
||||
if(xmlValue("boxBG", data)~=nil)then boxSelectedBG = colors[xmlValue("boxBG", data)] end
|
||||
if(xmlValue("inactiveBoxBG", data)~=nil)then boxNotSelectedBG = colors[xmlValue("inactiveBoxBG", data)] end
|
||||
if(xmlValue("inactiveBoxFG", data)~=nil)then boxNotSelectedFG = colors[xmlValue("inactiveBoxFG", data)] end
|
||||
if(xmlValue("boxFG", data)~=nil)then boxSelectedFG = colors[xmlValue("boxFG", data)] end
|
||||
if(xmlValue("symbol", data)~=nil)then symbol = xmlValue("symbol", data) end
|
||||
if(data["item"]~=nil)then
|
||||
local tab = data["item"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
for k,v in pairs(tab)do
|
||||
self:addItem(xmlValue("text", v), xmlValue("x", v), xmlValue("y", v), colors[xmlValue("bg", v)], colors[xmlValue("fg", v)])
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
addItem = function(self, text, x, y, bgCol, fgCol, ...)
|
||||
table.insert(list, { x = x or 1, y = y or 1, text = text, bgCol = bgCol or self.bgColor, fgCol = fgCol or self.fgColor, args = { ... } })
|
||||
if (#list == 1) then
|
||||
self:setValue(list[1])
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getAll = function(self)
|
||||
return list
|
||||
end;
|
||||
|
||||
removeItem = function(self, index)
|
||||
table.remove(list, index)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getItem = function(self, index)
|
||||
return list[index]
|
||||
end;
|
||||
|
||||
getItemIndex = function(self)
|
||||
local selected = self:getValue()
|
||||
for key, value in pairs(list) do
|
||||
if (value == selected) then
|
||||
return key
|
||||
end
|
||||
end
|
||||
end;
|
||||
|
||||
clear = function(self)
|
||||
list = {}
|
||||
self:setValue({})
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
getItemCount = function(self)
|
||||
return #list
|
||||
end;
|
||||
|
||||
editItem = function(self, index, text, x, y, bgCol, fgCol, ...)
|
||||
table.remove(list, index)
|
||||
table.insert(list, index, { x = x or 1, y = y or 1, text = text, bgCol = bgCol or self.bgColor, fgCol = fgCol or self.fgColor, args = { ... } })
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
selectItem = function(self, index)
|
||||
self:setValue(list[index] or {})
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setActiveSymbol = function(self, sym)
|
||||
symbol = sym:sub(1,1)
|
||||
self:updateDraw()
|
||||
base.addItem(self, text, bgCol, fgCol, ...)
|
||||
table.insert(list, { x = x or 1, y = y or #list * 2 })
|
||||
return self
|
||||
end,
|
||||
|
||||
setSelectedItem = function(self, bgCol, fgCol, boxBG, boxFG, active)
|
||||
itemSelectedBG = bgCol or itemSelectedBG
|
||||
itemSelectedFG = fgCol or itemSelectedFG
|
||||
boxSelectedBG = boxBG or boxSelectedBG
|
||||
boxSelectedFG = boxFG or boxSelectedFG
|
||||
selectionColorActive = active~=nil and active or true
|
||||
self:updateDraw()
|
||||
removeItem = function(self, index)
|
||||
base.removeItem(self, index)
|
||||
table.remove(list, index)
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y)
|
||||
clear = function(self)
|
||||
base.clear(self)
|
||||
list = {}
|
||||
return self
|
||||
end,
|
||||
|
||||
editItem = function(self, index, text, x, y, bgCol, fgCol, ...)
|
||||
base.editItem(self, index, text, bgCol, fgCol, ...)
|
||||
table.remove(list, index)
|
||||
table.insert(list, index, { x = x or 1, y = y or 1 })
|
||||
return self
|
||||
end,
|
||||
|
||||
setBoxSelectionColor = function(self, bg, fg)
|
||||
boxSelectedBG = bg
|
||||
boxSelectedFG = fg
|
||||
return self
|
||||
end,
|
||||
|
||||
setBoxSelectionBG = function(self, bg)
|
||||
return self:setBoxSelectionColor(bg, boxSelectedFG)
|
||||
end,
|
||||
|
||||
setBoxSelectionFG = function(self, fg)
|
||||
return self:setBoxSelectionColor(boxSelectedBG, fg)
|
||||
end,
|
||||
|
||||
getBoxSelectionColor = function(self)
|
||||
return boxSelectedBG, boxSelectedFG
|
||||
end,
|
||||
|
||||
getBoxSelectionBG = function(self)
|
||||
return boxSelectedBG
|
||||
end,
|
||||
|
||||
getBoxSelectionFG = function(self)
|
||||
return boxSelectedFG
|
||||
end,
|
||||
|
||||
setBoxDefaultColor = function(self, bg, fg)
|
||||
boxNotSelectedBG = bg
|
||||
boxNotSelectedFG = fg
|
||||
return self
|
||||
end,
|
||||
|
||||
setBoxDefaultBG = function(self, bg)
|
||||
return self:setBoxDefaultColor(bg, boxNotSelectedFG)
|
||||
end,
|
||||
|
||||
setBoxDefaultFG = function(self, fg)
|
||||
return self:setBoxDefaultColor(boxNotSelectedBG, fg)
|
||||
end,
|
||||
|
||||
getBoxDefaultColor = function(self)
|
||||
return boxNotSelectedBG, boxNotSelectedFG
|
||||
end,
|
||||
|
||||
getBoxDefaultBG = function(self)
|
||||
return boxNotSelectedBG
|
||||
end,
|
||||
|
||||
getBoxDefaultFG = function(self)
|
||||
return boxNotSelectedFG
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y, ...)
|
||||
if (#list > 0) then
|
||||
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
for _, value in pairs(list) do
|
||||
if (obx + value.x - 1 <= x) and (obx + value.x - 1 + value.text:len() + 1 >= x) and (oby + value.y - 1 == y) then
|
||||
local obx, oby = self:getAbsolutePosition()
|
||||
local baseList = self:getAll()
|
||||
for k, value in pairs(baseList) do
|
||||
if (obx + list[k].x - 1 <= x) and (obx + list[k].x - 1 + value.text:len() + 1 >= x) and (oby + list[k].y - 1 == y) then
|
||||
self:setValue(value)
|
||||
local val = self:getEventSystem():sendEvent("mouse_click", self, "mouse_click", button, x, y)
|
||||
if(val==false)then return val end
|
||||
if(self.parent~=nil)then
|
||||
self.parent:setFocusedObject(self)
|
||||
end
|
||||
local val = self:sendEvent("mouse_click", self, "mouse_click", button, x, y, ...)
|
||||
self:updateDraw()
|
||||
if(val==false)then return val end
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end;
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
for _, value in pairs(list) do
|
||||
self:addDraw("radio", function()
|
||||
local itemSelectedBG, itemSelectedFG = self:getSelectionColor()
|
||||
local baseList = self:getAll()
|
||||
for k, value in pairs(baseList) do
|
||||
if (value == self:getValue()) then
|
||||
if (align == "left") then
|
||||
self.parent:writeText(value.x + obx - 1, value.y + oby - 1, symbol, boxSelectedBG, boxSelectedFG)
|
||||
self.parent:writeText(value.x + 2 + obx - 1, value.y + oby - 1, value.text, itemSelectedBG, itemSelectedFG)
|
||||
end
|
||||
self:addBlit(list[k].x, list[k].y, symbol, tHex[boxSelectedFG], tHex[boxSelectedBG])
|
||||
self:addBlit(list[k].x + 2, list[k].y, value.text, tHex[itemSelectedFG]:rep(#value.text), tHex[itemSelectedBG]:rep(#value.text))
|
||||
else
|
||||
self.parent:drawBackgroundBox(value.x + obx - 1, value.y + oby - 1, 1, 1, boxNotSelectedBG or self.bgColor)
|
||||
self.parent:writeText(value.x + 2 + obx - 1, value.y + oby - 1, value.text, value.bgCol, value.fgCol)
|
||||
self:addBackgroundBox(list[k].x, list[k].y, 1, 1, boxNotSelectedBG or colors.black)
|
||||
self:addBlit(list[k].x + 2, list[k].y, value.text, tHex[value.fgCol]:rep(#value.text), tHex[value.bgCol]:rep(#value.text))
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("MenubarBG")
|
||||
self.fgColor = self.parent:getTheme("MenubarFG")
|
||||
itemSelectedBG = self.parent:getTheme("SelectionBG")
|
||||
itemSelectedFG = self.parent:getTheme("SelectionText")
|
||||
boxSelectedBG = self.parent:getTheme("MenubarBG")
|
||||
boxSelectedFG = self.parent:getTheme("MenubarText")
|
||||
end
|
||||
end)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
147
Basalt/objects/ScrollableFrame.lua
Normal file
@@ -0,0 +1,147 @@
|
||||
local max,min,sub,rep = math.max,math.min,string.sub,string.rep
|
||||
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("Frame")(name, basalt)
|
||||
local objectType = "ScrollableFrame"
|
||||
local parent
|
||||
|
||||
local direction = 0
|
||||
local manualScrollAmount = 0
|
||||
local calculateScrollAmount = true
|
||||
|
||||
local function getHorizontalScrollAmount(self)
|
||||
local amount = 0
|
||||
local children = self:getChildren()
|
||||
for _, b in pairs(children) do
|
||||
if(b.element.getWidth~=nil)and(b.element.getX~=nil)then
|
||||
local w, x = b.element:getWidth(), b.element:getX()
|
||||
local width = self:getWidth()
|
||||
if(b.element:getType()=="Dropdown")then
|
||||
if(b.element:isOpened())then
|
||||
local dropdownW = b.element:getDropdownSize()
|
||||
if (dropdownW + x - width >= amount) then
|
||||
amount = max(dropdownW + x - width, 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if (w + x - width >= amount) then
|
||||
amount = max(w + x - width, 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
return amount
|
||||
end
|
||||
|
||||
local function getVerticalScrollAmount(self)
|
||||
local amount = 0
|
||||
local children = self:getChildren()
|
||||
for _, b in pairs(children) do
|
||||
if(b.element.getHeight~=nil)and(b.element.getY~=nil)then
|
||||
local h, y = b.element:getHeight(), b.element:getY()
|
||||
local height = self:getHeight()
|
||||
if(b.element:getType()=="Dropdown")then
|
||||
if(b.element:isOpened())then
|
||||
local _,dropdownH = b.element:getDropdownSize()
|
||||
if (dropdownH + y - height >= amount) then
|
||||
amount = max(dropdownH + y - height, 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
if (h + y - height >= amount) then
|
||||
amount = max(h + y - height, 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
return amount
|
||||
end
|
||||
|
||||
local function scrollHandler(self, dir)
|
||||
local xO, yO = self:getOffset()
|
||||
local scrollAmn
|
||||
if(direction==1)then
|
||||
scrollAmn = calculateScrollAmount and getHorizontalScrollAmount(self) or manualScrollAmount
|
||||
self:setOffset(min(scrollAmn, max(0, xO + dir)), yO)
|
||||
elseif(direction==0)then
|
||||
scrollAmn = calculateScrollAmount and getVerticalScrollAmount(self) or manualScrollAmount
|
||||
self:setOffset(xO, min(scrollAmn, max(0, yO + dir)))
|
||||
end
|
||||
self:updateDraw()
|
||||
end
|
||||
|
||||
local object = {
|
||||
getType = function()
|
||||
return objectType
|
||||
end,
|
||||
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
setDirection = function(self, dir)
|
||||
direction = dir=="horizontal" and 1 or dir=="vertical" and 0 or direction
|
||||
return self
|
||||
end,
|
||||
|
||||
setScrollAmount = function(self, amount)
|
||||
manualScrollAmount = amount
|
||||
calculateScrollAmount = false
|
||||
return self
|
||||
end,
|
||||
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
load = function(self)
|
||||
base.load(self)
|
||||
self:listenEvent("mouse_scroll")
|
||||
end,
|
||||
|
||||
removeChildren = function(self)
|
||||
base.removeChildren(self)
|
||||
self:listenEvent("mouse_scroll")
|
||||
end,
|
||||
|
||||
setParent = function(self, p, ...)
|
||||
base.setParent(self, p, ...)
|
||||
parent = p
|
||||
return self
|
||||
end,
|
||||
|
||||
scrollHandler = function(self, dir, x, y)
|
||||
if(base:getBase().scrollHandler(self, dir, x, y))then
|
||||
self:sortChildren()
|
||||
for _, obj in ipairs(self:getEvents("mouse_scroll")) do
|
||||
if (obj.element.scrollHandler ~= nil) then
|
||||
local xO, yO = 0, 0
|
||||
if(self.getOffset~=nil)then
|
||||
xO, yO = self:getOffset()
|
||||
end
|
||||
if(obj.element.getIgnoreOffset())then
|
||||
xO, yO = 0, 0
|
||||
end
|
||||
if (obj.element.scrollHandler(obj.element, dir, x+xO, y+yO)) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
scrollHandler(self, dir, x, y)
|
||||
self:clearFocusedChild()
|
||||
return true
|
||||
end
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
base.draw(self)
|
||||
self:addDraw("scrollableFrame", function()
|
||||
if(calculateScrollAmount)then
|
||||
scrollHandler(self, 0)
|
||||
end
|
||||
end, 0)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,42 +1,40 @@
|
||||
local Object = require("Object")
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
local tHex = require("tHex")
|
||||
|
||||
return function(name)
|
||||
local base = Object(name)
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("VisualObject")(name, basalt)
|
||||
local objectType = "Scrollbar"
|
||||
|
||||
base.width = 1
|
||||
base.height = 8
|
||||
base:setValue(1)
|
||||
base:setZIndex(2)
|
||||
base:setSize(1, 8)
|
||||
base:setBackground(colors.lightGray, "\127", colors.gray)
|
||||
|
||||
local barType = "vertical"
|
||||
local symbol = " "
|
||||
local symbolColor
|
||||
local bgSymbol = "\127"
|
||||
local maxValue = base.height
|
||||
local symbolBG = colors.black
|
||||
local symbolFG = colors.black
|
||||
local scrollAmount = 3
|
||||
local index = 1
|
||||
local symbolSize = 1
|
||||
local symbolAutoSize = true
|
||||
|
||||
local function updateSymbolSize()
|
||||
local w,h = base:getSize()
|
||||
if(symbolAutoSize)then
|
||||
symbolSize = math.max((barType == "vertical" and h or w-(#symbol)) - (scrollAmount-1), 1)
|
||||
end
|
||||
end
|
||||
updateSymbolSize()
|
||||
|
||||
local function mouseEvent(self, button, x, y)
|
||||
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
local obx, oby = self:getAbsolutePosition()
|
||||
local w,h = self:getSize()
|
||||
if (barType == "horizontal") then
|
||||
for _index = 0, w do
|
||||
if (obx + _index == x) and (oby <= y) and (oby + h > y) then
|
||||
index = math.min(_index + 1, w - (symbolSize - 1))
|
||||
self:setValue(maxValue / w * (index))
|
||||
self:updateDraw()
|
||||
end
|
||||
end
|
||||
end
|
||||
if (barType == "vertical") then
|
||||
for _index = 0, h do
|
||||
if (oby + _index == y) and (obx <= x) and (obx + w > x) then
|
||||
index = math.min(_index + 1, h - (symbolSize - 1))
|
||||
self:setValue(maxValue / h * (index))
|
||||
self:updateDraw()
|
||||
end
|
||||
updateSymbolSize()
|
||||
local size = barType == "vertical" and h or w
|
||||
for i = 0, size do
|
||||
if ((barType == "vertical" and oby + i == y) or (barType == "horizontal" and obx + i == x)) and (obx <= x) and (obx + w > x) and (oby <= y) and (oby + h > y) then
|
||||
index = math.min(i + 1, size - (#symbol + symbolSize - 2))
|
||||
self:scrollbarMoveHandler()
|
||||
self:updateDraw()
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -44,23 +42,44 @@ return function(name)
|
||||
local object = {
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
end,
|
||||
|
||||
setSymbol = function(self, _symbol)
|
||||
symbol = _symbol:sub(1, 1)
|
||||
load = function(self)
|
||||
base.load(self)
|
||||
local parent = self:getParent()
|
||||
self:listenEvent("mouse_click")
|
||||
self:listenEvent("mouse_up")
|
||||
self:listenEvent("mouse_scroll")
|
||||
self:listenEvent("mouse_drag")
|
||||
end,
|
||||
|
||||
setSymbol = function(self, _symbol, bg, fg)
|
||||
symbol = _symbol:sub(1,1)
|
||||
symbolBG = bg or symbolBG
|
||||
symbolFG = fg or symbolFG
|
||||
updateSymbolSize()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("maxValue", data)~=nil)then maxValue = xmlValue("maxValue", data) end
|
||||
if(xmlValue("backgroundSymbol", data)~=nil)then bgSymbol = xmlValue("backgroundSymbol", data):sub(1,1) end
|
||||
if(xmlValue("symbol", data)~=nil)then symbol = xmlValue("symbol", data):sub(1,1) end
|
||||
if(xmlValue("barType", data)~=nil)then barType = xmlValue("barType", data):lower() end
|
||||
if(xmlValue("symbolSize", data)~=nil)then self:setSymbolSize(xmlValue("symbolSize", data)) end
|
||||
if(xmlValue("symbolColor", data)~=nil)then symbolColor = colors[xmlValue("symbolColor", data)] end
|
||||
if(xmlValue("index", data)~=nil)then self:setIndex(xmlValue("index", data)) end
|
||||
setSymbolBG = function(self, bg)
|
||||
return self:setSymbol(symbol, bg, nil)
|
||||
end,
|
||||
|
||||
setSymbolFG = function(self, fg)
|
||||
return self:setSymbol(symbol, nil, fg)
|
||||
end,
|
||||
|
||||
getSymbol = function(self)
|
||||
return symbol
|
||||
end,
|
||||
|
||||
getSymbolBG = function(self)
|
||||
return symbolBG
|
||||
end,
|
||||
|
||||
getSymbolFG = function(self)
|
||||
return symbolFG
|
||||
end,
|
||||
|
||||
setIndex = function(self, _index)
|
||||
@@ -69,54 +88,53 @@ return function(name)
|
||||
index = 1
|
||||
end
|
||||
local w,h = self:getSize()
|
||||
index = math.min(index, (barType == "vertical" and h or w) - (symbolSize - 1))
|
||||
self:setValue(maxValue / (barType == "vertical" and h or w) * index)
|
||||
--index = math.min(index, (barType == "vertical" and h or w) - (symbolSize - 1))
|
||||
updateSymbolSize()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setScrollAmount = function(self, amount)
|
||||
scrollAmount = amount
|
||||
updateSymbolSize()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getScrollAmount = function(self)
|
||||
return scrollAmount
|
||||
end,
|
||||
|
||||
getIndex = function(self)
|
||||
return index
|
||||
local w,h = self:getSize()
|
||||
return scrollAmount > (barType=="vertical" and h or w) and math.floor(scrollAmount/(barType=="vertical" and h or w) * index) or index
|
||||
end,
|
||||
|
||||
setSymbolSize = function(self, size)
|
||||
symbolSize = tonumber(size) or 1
|
||||
local w,h = self:getSize()
|
||||
if (barType == "vertical") then
|
||||
self:setValue(index - 1 * (maxValue / (h - (symbolSize - 1))) - (maxValue / (h - (symbolSize - 1))))
|
||||
elseif (barType == "horizontal") then
|
||||
self:setValue(index - 1 * (maxValue / (w - (symbolSize - 1))) - (maxValue / (w - (symbolSize - 1))))
|
||||
end
|
||||
symbolAutoSize = size~=false and false or true
|
||||
updateSymbolSize()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setMaxValue = function(self, val)
|
||||
maxValue = val
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setBackgroundSymbol = function(self, _bgSymbol)
|
||||
bgSymbol = string.sub(_bgSymbol, 1, 1)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setSymbolColor = function(self, col)
|
||||
symbolColor = col
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
getSymbolSize = function(self)
|
||||
return symbolSize
|
||||
end,
|
||||
|
||||
setBarType = function(self, _typ)
|
||||
barType = _typ:lower()
|
||||
updateSymbolSize()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y)
|
||||
if (base.mouseHandler(self, button, x, y)) then
|
||||
getBarType = function(self)
|
||||
return barType
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y, ...)
|
||||
if (base.mouseHandler(self, button, x, y, ...)) then
|
||||
mouseEvent(self, button, x, y)
|
||||
return true
|
||||
end
|
||||
@@ -131,58 +149,70 @@ return function(name)
|
||||
return false
|
||||
end,
|
||||
|
||||
setSize = function(self, ...)
|
||||
base.setSize(self, ...)
|
||||
updateSymbolSize()
|
||||
return self
|
||||
end,
|
||||
|
||||
scrollHandler = function(self, dir, x, y)
|
||||
if(base.scrollHandler(self, dir, x, y))then
|
||||
local w,h = self:getSize()
|
||||
updateSymbolSize()
|
||||
index = index + dir
|
||||
if (index < 1) then
|
||||
index = 1
|
||||
end
|
||||
index = math.min(index, (barType == "vertical" and h or w) - (symbolSize - 1))
|
||||
self:setValue(maxValue / (barType == "vertical" and h or w) * index)
|
||||
index = math.min(index, (barType == "vertical" and h or w) - (barType == "vertical" and symbolSize - 1 or #symbol+symbolSize-2))
|
||||
self:scrollbarMoveHandler()
|
||||
self:updateDraw()
|
||||
end
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local w,h = self:getSize()
|
||||
if (barType == "horizontal") then
|
||||
self.parent:writeText(obx, oby, bgSymbol:rep(index - 1), self.bgColor, self.fgColor)
|
||||
self.parent:writeText(obx + index - 1, oby, symbol:rep(symbolSize), symbolColor, symbolColor)
|
||||
self.parent:writeText(obx + index + symbolSize - 1, oby, bgSymbol:rep(w - (index + symbolSize - 1)), self.bgColor, self.fgColor)
|
||||
end
|
||||
onChange = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("scrollbar_moved", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
if (barType == "vertical") then
|
||||
for n = 0, h - 1 do
|
||||
if (index == n + 1) then
|
||||
for curIndexOffset = 0, math.min(symbolSize - 1, h) do
|
||||
self.parent:writeText(obx, oby + n + curIndexOffset, symbol, symbolColor, symbolColor)
|
||||
end
|
||||
else
|
||||
if (n + 1 < index) or (n + 1 > index - 1 + symbolSize) then
|
||||
self.parent:writeText(obx, oby + n, bgSymbol, self.bgColor, self.fgColor)
|
||||
end
|
||||
|
||||
scrollbarMoveHandler = function(self)
|
||||
self:sendEvent("scrollbar_moved", self:getIndex())
|
||||
end,
|
||||
|
||||
customEventHandler = function(self, event, ...)
|
||||
base.customEventHandler(self, event, ...)
|
||||
if(event=="basalt_FrameResize")then
|
||||
updateSymbolSize()
|
||||
end
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
base.draw(self)
|
||||
self:addDraw("scrollbar", function()
|
||||
local parent = self:getParent()
|
||||
local w,h = self:getSize()
|
||||
local bgCol,fgCol = self:getBackground(), self:getForeground()
|
||||
if (barType == "horizontal") then
|
||||
for n = 0, h - 1 do
|
||||
self:addBlit(index, 1 + n, symbol:rep(symbolSize), tHex[symbolFG]:rep(#symbol*symbolSize), tHex[symbolBG]:rep(#symbol*symbolSize))
|
||||
end
|
||||
elseif (barType == "vertical") then
|
||||
for n = 0, h - 1 do
|
||||
if (index == n + 1) then
|
||||
for curIndexOffset = 0, math.min(symbolSize - 1, h) do
|
||||
self:addBlit(1, index + curIndexOffset, symbol:rep(math.max(#symbol, w)), tHex[symbolFG]:rep(math.max(#symbol, w)), tHex[symbolBG]:rep(math.max(#symbol, w)))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
self.parent:addEvent("mouse_drag", self)
|
||||
self.parent:addEvent("mouse_scroll", self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("ScrollbarBG")
|
||||
self.fgColor = self.parent:getTheme("ScrollbarText")
|
||||
symbolColor = self.parent:getTheme("ScrollbarSymbolColor")
|
||||
end
|
||||
end)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,66 +1,55 @@
|
||||
local Object = require("Object")
|
||||
local log = require("basaltLogs")
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
local tHex = require("tHex")
|
||||
|
||||
return function(name)
|
||||
local base = Object(name)
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("ChangeableObject")(name, basalt)
|
||||
local objectType = "Slider"
|
||||
|
||||
base.width = 8
|
||||
base.height = 1
|
||||
base:setSize(12, 1)
|
||||
base:setValue(1)
|
||||
base:setBackground(false, "\140", colors.black)
|
||||
|
||||
local barType = "horizontal"
|
||||
local symbol = " "
|
||||
local symbolColor
|
||||
local bgSymbol = "\140"
|
||||
local maxValue = base.width
|
||||
local symbolFG = colors.gray
|
||||
local symbolBG = colors.gray
|
||||
local bgSymbol = " "
|
||||
local symbolColor = colors.black
|
||||
local maxValue = 12
|
||||
local index = 1
|
||||
local symbolSize = 1
|
||||
|
||||
local function mouseEvent(self, button, x, y)
|
||||
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
local w,h = self:getSize()
|
||||
if (barType == "horizontal") then
|
||||
for _index = 0, w do
|
||||
if (obx + _index == x) and (oby <= y) and (oby + h > y) then
|
||||
index = math.min(_index + 1, w - (symbolSize - 1))
|
||||
self:setValue(maxValue / w * (index))
|
||||
self:updateDraw()
|
||||
end
|
||||
end
|
||||
end
|
||||
if (barType == "vertical") then
|
||||
for _index = 0, h do
|
||||
if (oby + _index == y) and (obx <= x) and (obx + w > x) then
|
||||
index = math.min(_index + 1, h - (symbolSize - 1))
|
||||
self:setValue(maxValue / h * (index))
|
||||
self:updateDraw()
|
||||
end
|
||||
end
|
||||
local obx, oby = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
local size = barType == "vertical" and h or w
|
||||
for i = 0, size do
|
||||
if ((barType == "vertical" and oby + i == y) or (barType == "horizontal" and obx + i == x)) and (obx <= x) and (obx + w > x) and (oby <= y) and (oby + h > y) then
|
||||
index = math.min(i + 1, size - (#symbol + symbolSize - 2))
|
||||
self:setValue(maxValue / size * index)
|
||||
self:updateDraw()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local object = {
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
end,
|
||||
|
||||
load = function(self)
|
||||
self:listenEvent("mouse_click")
|
||||
self:listenEvent("mouse_drag")
|
||||
self:listenEvent("mouse_scroll")
|
||||
end,
|
||||
|
||||
setSymbol = function(self, _symbol)
|
||||
symbol = _symbol:sub(1, 1)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("maxValue", data)~=nil)then maxValue = xmlValue("maxValue", data) end
|
||||
if(xmlValue("backgroundSymbol", data)~=nil)then bgSymbol = xmlValue("backgroundSymbol", data):sub(1,1) end
|
||||
if(xmlValue("barType", data)~=nil)then barType = xmlValue("barType", data):lower() end
|
||||
if(xmlValue("symbol", data)~=nil)then symbol = xmlValue("symbol", data):sub(1,1) end
|
||||
if(xmlValue("symbolSize", data)~=nil)then self:setSymbolSize(xmlValue("symbolSize", data)) end
|
||||
if(xmlValue("symbolColor", data)~=nil)then symbolColor = colors[xmlValue("symbolColor", data)] end
|
||||
if(xmlValue("index", data)~=nil)then self:setIndex(xmlValue("index", data)) end
|
||||
getSymbol = function(self)
|
||||
return symbol
|
||||
end,
|
||||
|
||||
setIndex = function(self, _index)
|
||||
@@ -79,39 +68,34 @@ return function(name)
|
||||
return index
|
||||
end,
|
||||
|
||||
setSymbolSize = function(self, size)
|
||||
symbolSize = tonumber(size) or 1
|
||||
if (barType == "vertical") then
|
||||
self:setValue(index - 1 * (maxValue / (h - (symbolSize - 1))) - (maxValue / (h - (symbolSize - 1))))
|
||||
elseif (barType == "horizontal") then
|
||||
self:setValue(index - 1 * (maxValue / (w - (symbolSize - 1))) - (maxValue / (w - (symbolSize - 1))))
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setMaxValue = function(self, val)
|
||||
maxValue = val
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setBackgroundSymbol = function(self, _bgSymbol)
|
||||
bgSymbol = string.sub(_bgSymbol, 1, 1)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
getMaxValue = function(self)
|
||||
return maxValue
|
||||
end,
|
||||
|
||||
setSymbolColor = function(self, col)
|
||||
symbolColor = col
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getSymbolColor = function(self)
|
||||
return symbolColor
|
||||
end,
|
||||
|
||||
setBarType = function(self, _typ)
|
||||
barType = _typ:lower()
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getBarType = function(self)
|
||||
return barType
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y)
|
||||
if (base.mouseHandler(self, button, x, y)) then
|
||||
@@ -145,44 +129,37 @@ return function(name)
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local w,h = self:getSize()
|
||||
if (barType == "horizontal") then
|
||||
self.parent:writeText(obx, oby, bgSymbol:rep(index - 1), self.bgColor, self.fgColor)
|
||||
self.parent:writeText(obx + index - 1, oby, symbol:rep(symbolSize), symbolColor, symbolColor)
|
||||
self.parent:writeText(obx + index + symbolSize - 1, oby, bgSymbol:rep(w - (index + symbolSize - 1)), self.bgColor, self.fgColor)
|
||||
end
|
||||
base.draw(self)
|
||||
self:addDraw("slider", function()
|
||||
local w,h = self:getSize()
|
||||
local obx, oby = self:getPosition()
|
||||
local bgCol,fgCol = self:getBackground(), self:getForeground()
|
||||
if (barType == "horizontal") then
|
||||
self:addText(index, oby, symbol:rep(symbolSize))
|
||||
if(symbolBG~=false)then self:addBG(index, 1, tHex[symbolBG]:rep(#symbol*symbolSize)) end
|
||||
if(symbolFG~=false)then self:addFG(index, 1, tHex[symbolFG]:rep(#symbol*symbolSize)) end
|
||||
end
|
||||
|
||||
if (barType == "vertical") then
|
||||
for n = 0, h - 1 do
|
||||
if (index == n + 1) then
|
||||
for curIndexOffset = 0, math.min(symbolSize - 1, h) do
|
||||
self.parent:writeText(obx, oby + n + curIndexOffset, symbol, symbolColor, symbolColor)
|
||||
end
|
||||
else
|
||||
if (n + 1 < index) or (n + 1 > index - 1 + symbolSize) then
|
||||
self.parent:writeText(obx, oby + n, bgSymbol, self.bgColor, self.fgColor)
|
||||
end
|
||||
if (barType == "vertical") then
|
||||
for n = 0, h - 1 do
|
||||
if (index == n + 1) then
|
||||
for curIndexOffset = 0, math.min(symbolSize - 1, h) do
|
||||
self:addBlit(1, 1+n+curIndexOffset, symbol, tHex[symbolColor], tHex[symbolColor])
|
||||
end
|
||||
else
|
||||
if (n + 1 < index) or (n + 1 > index - 1 + symbolSize) then
|
||||
self:addText(1, 1+n, bgSymbol)
|
||||
if(bgCol~=false)then self:addBG(1, 1+n, tHex[bgCol]) end
|
||||
if(fgCol~=false)then self:addFG(1, 1+n, tHex[fgCol]) end
|
||||
--self:addBlit(1, 1+n, bgSymbol, tHex[fgCol], tHex[bgCol])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
self.parent:addEvent("mouse_drag", self)
|
||||
self.parent:addEvent("mouse_scroll", self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("SliderBG")
|
||||
self.fgColor = self.parent:getTheme("SliderText")
|
||||
symbolColor = self.parent:getTheme("SliderSymbolColor")
|
||||
end
|
||||
end)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,14 +1,8 @@
|
||||
local Object = require("Object")
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
|
||||
return function(name)
|
||||
local base = Object(name)
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("ChangeableObject")(name, basalt)
|
||||
local objectType = "Switch"
|
||||
|
||||
base.width = 2
|
||||
base.height = 1
|
||||
base.bgColor = colors.lightGray
|
||||
base.fgColor = colors.gray
|
||||
base:setSize(4, 1)
|
||||
base:setValue(false)
|
||||
base:setZIndex(5)
|
||||
|
||||
@@ -19,71 +13,65 @@ return function(name)
|
||||
local object = {
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
|
||||
setSymbolColor = function(self, symbolColor)
|
||||
bgSymbol = symbolColor
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setActiveBackground = function(self, bgcol)
|
||||
activeBG = bgcol
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setInactiveBackground = function(self, bgcol)
|
||||
inactiveBG = bgcol
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(xmlValue("inactiveBG", data)~=nil)then inactiveBG = colors[xmlValue("inactiveBG", data)] end
|
||||
if(xmlValue("activeBG", data)~=nil)then activeBG = colors[xmlValue("activeBG", data)] end
|
||||
if(xmlValue("symbolColor", data)~=nil)then bgSymbol = colors[xmlValue("symbolColor", data)] end
|
||||
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y)
|
||||
if (base.mouseHandler(self, button, x, y)) then
|
||||
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
setSymbol = function(self, col)
|
||||
bgSymbol = col
|
||||
return self
|
||||
end,
|
||||
|
||||
getSymbol = function(self)
|
||||
return bgSymbol
|
||||
end,
|
||||
|
||||
setActiveBackground = function(self, col)
|
||||
activeBG = col
|
||||
return self
|
||||
end,
|
||||
|
||||
getActiveBackground = function(self)
|
||||
return activeBG
|
||||
end,
|
||||
|
||||
setInactiveBackground = function(self, col)
|
||||
inactiveBG = col
|
||||
return self
|
||||
end,
|
||||
|
||||
getInactiveBackground = function(self)
|
||||
return inactiveBG
|
||||
end,
|
||||
|
||||
|
||||
load = function(self)
|
||||
self:listenEvent("mouse_click")
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, ...)
|
||||
if (base.mouseHandler(self, ...)) then
|
||||
self:setValue(not self:getValue())
|
||||
self:updateDraw()
|
||||
return true
|
||||
end
|
||||
end;
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local w,h = self:getSize()
|
||||
self.parent:drawBackgroundBox(obx, oby, w, h, self.bgColor)
|
||||
if(self:getValue())then
|
||||
self.parent:drawBackgroundBox(obx, oby, 1, h, activeBG)
|
||||
self.parent:drawBackgroundBox(obx+1, oby, 1, h, bgSymbol)
|
||||
else
|
||||
self.parent:drawBackgroundBox(obx, oby, 1, h, bgSymbol)
|
||||
self.parent:drawBackgroundBox(obx+1, oby, 1, h, inactiveBG)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("SwitchBG")
|
||||
self.fgColor = self.parent:getTheme("SwitchText")
|
||||
bgSymbol = self.parent:getTheme("SwitchBGSymbol")
|
||||
inactiveBG = self.parent:getTheme("SwitchInactive")
|
||||
activeBG = self.parent:getTheme("SwitchActive")
|
||||
end
|
||||
draw = function(self)
|
||||
base.draw(self)
|
||||
self:addDraw("switch", function()
|
||||
local parent = self:getParent()
|
||||
local bgCol,fgCol = self:getBackground(), self:getForeground()
|
||||
local w,h = self:getSize()
|
||||
if(self:getValue())then
|
||||
self:addBackgroundBox(1, 1, w, h, activeBG)
|
||||
self:addBackgroundBox(w, 1, 1, h, bgSymbol)
|
||||
else
|
||||
self:addBackgroundBox(1, 1, w, h, inactiveBG)
|
||||
self:addBackgroundBox(1, 1, 1, h, bgSymbol)
|
||||
end
|
||||
end)
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,12 +1,9 @@
|
||||
local Object = require("Object")
|
||||
local tHex = require("tHex")
|
||||
local log = require("basaltLogs")
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
|
||||
local rep = string.rep
|
||||
local rep,find,gmatch,sub,len = string.rep,string.find,string.gmatch,string.sub,string.len
|
||||
|
||||
return function(name)
|
||||
local base = Object(name)
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("ChangeableObject")(name, basalt)
|
||||
local objectType = "Textfield"
|
||||
local hIndex, wIndex, textX, textY = 1, 1, 1, 1
|
||||
|
||||
@@ -16,20 +13,84 @@ return function(name)
|
||||
local keyWords = { }
|
||||
local rules = { }
|
||||
|
||||
base.width = 30
|
||||
base.height = 12
|
||||
local startSelX,endSelX,startSelY,endSelY
|
||||
|
||||
local selectionBG,selectionFG = colors.lightBlue,colors.black
|
||||
|
||||
base:setSize(30, 12)
|
||||
base:setZIndex(5)
|
||||
|
||||
local function isSelected()
|
||||
if(startSelX~=nil)and(endSelX~=nil)and(startSelY~=nil)and(endSelY~=nil)then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function getSelectionCoordinates()
|
||||
local sx, ex, sy, ey = startSelX, endSelX, startSelY, endSelY
|
||||
if isSelected() then
|
||||
if startSelX < endSelX and startSelY <= endSelY then
|
||||
sx = startSelX
|
||||
ex = endSelX
|
||||
if startSelY < endSelY then
|
||||
sy = startSelY
|
||||
ey = endSelY
|
||||
else
|
||||
sy = endSelY
|
||||
ey = startSelY
|
||||
end
|
||||
elseif startSelX > endSelX and startSelY >= endSelY then
|
||||
sx = endSelX
|
||||
ex = startSelX
|
||||
if startSelY > endSelY then
|
||||
sy = endSelY
|
||||
ey = startSelY
|
||||
else
|
||||
sy = startSelY
|
||||
ey = endSelY
|
||||
end
|
||||
elseif startSelY > endSelY then
|
||||
sx = endSelX
|
||||
ex = startSelX
|
||||
sy = endSelY
|
||||
ey = startSelY
|
||||
end
|
||||
return sx, ex, sy, ey
|
||||
end
|
||||
end
|
||||
|
||||
local function removeSelection(self)
|
||||
local sx, ex, sy, ey = getSelectionCoordinates()
|
||||
local startLine = lines[sy]
|
||||
local endLine = lines[ey]
|
||||
lines[sy] = startLine:sub(1, sx - 1) .. endLine:sub(ex + 1, endLine:len())
|
||||
bgLines[sy] = bgLines[sy]:sub(1, sx - 1) .. bgLines[ey]:sub(ex + 1, bgLines[ey]:len())
|
||||
fgLines[sy] = fgLines[sy]:sub(1, sx - 1) .. fgLines[ey]:sub(ex + 1, fgLines[ey]:len())
|
||||
|
||||
for i = ey, sy + 1, -1 do
|
||||
if i ~= sy then
|
||||
table.remove(lines, i)
|
||||
table.remove(bgLines, i)
|
||||
table.remove(fgLines, i)
|
||||
end
|
||||
end
|
||||
|
||||
textX, textY = sx, sy
|
||||
startSelX, endSelX, startSelY, endSelY = nil, nil, nil, nil
|
||||
return self
|
||||
end
|
||||
|
||||
local function stringGetPositions(str, word)
|
||||
local pos = {}
|
||||
if(str:len()>0)then
|
||||
for w in string.gmatch(str, word)do
|
||||
local s, e = string.find(str, w)
|
||||
for w in gmatch(str, word)do
|
||||
local s, e = find(str, w)
|
||||
if(s~=nil)and(e~=nil)then
|
||||
table.insert(pos,s)
|
||||
table.insert(pos,e)
|
||||
local startL = string.sub(str, 1, (s-1))
|
||||
local endL = string.sub(str, e+1, str:len())
|
||||
local startL = sub(str, 1, (s-1))
|
||||
local endL = sub(str, e+1, str:len())
|
||||
str = startL..(":"):rep(w:len())..endL
|
||||
end
|
||||
end
|
||||
@@ -39,8 +100,8 @@ return function(name)
|
||||
|
||||
local function updateColors(self, l)
|
||||
l = l or textY
|
||||
local fgLine = tHex[self.fgColor]:rep(fgLines[l]:len())
|
||||
local bgLine = tHex[self.bgColor]:rep(bgLines[l]:len())
|
||||
local fgLine = tHex[self:getForeground()]:rep(fgLines[l]:len())
|
||||
local bgLine = tHex[self:getBackground()]:rep(bgLines[l]:len())
|
||||
for k,v in pairs(rules)do
|
||||
local pos = stringGetPositions(lines[l], v[1])
|
||||
if(#pos>0)then
|
||||
@@ -94,65 +155,52 @@ return function(name)
|
||||
return self
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
if(data["lines"]~=nil)then
|
||||
local l = data["lines"]["line"]
|
||||
if(l.properties~=nil)then l = {l} end
|
||||
for k,v in pairs(l)do
|
||||
self:addLine(v:value())
|
||||
end
|
||||
end
|
||||
if(data["keywords"]~=nil)then
|
||||
for k,v in pairs(data["keywords"])do
|
||||
if(colors[k]~=nil)then
|
||||
local entry = v
|
||||
if(entry.properties~=nil)then entry = {entry} end
|
||||
local tab = {}
|
||||
for a,b in pairs(entry)do
|
||||
local keywordList = b["keyword"]
|
||||
if(b["keyword"].properties~=nil)then keywordList = {b["keyword"]} end
|
||||
for c,d in pairs(keywordList)do
|
||||
table.insert(tab, d:value())
|
||||
end
|
||||
end
|
||||
self:addKeywords(colors[k], tab)
|
||||
end
|
||||
end
|
||||
end
|
||||
if(data["rules"]~=nil)then
|
||||
if(data["rules"]["rule"]~=nil)then
|
||||
local tab = data["rules"]["rule"]
|
||||
if(data["rules"]["rule"].properties~=nil)then tab = {data["rules"]["rule"]} end
|
||||
for k,v in pairs(tab)do
|
||||
setSelection = function(self, fg, bg)
|
||||
selectionFG = fg or selectionFG
|
||||
selectionBG = bg or selectionBG
|
||||
return self
|
||||
end,
|
||||
|
||||
if(xmlValue("pattern", v)~=nil)then
|
||||
self:addRule(xmlValue("pattern", v), colors[xmlValue("fg", v)], colors[xmlValue("bg", v)])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
setSelectionFG = function(self, fg)
|
||||
return self:setSelection(fg, nil)
|
||||
end,
|
||||
|
||||
setSelectionBG = function(self, bg)
|
||||
return self:setSelection(nil, bg)
|
||||
end,
|
||||
|
||||
getSelection = function(self)
|
||||
return selectionFG, selectionBG
|
||||
end,
|
||||
|
||||
getSelectionFG = function(self)
|
||||
return selectionFG
|
||||
end,
|
||||
|
||||
getSelectionBG = function(self)
|
||||
return selectionBG
|
||||
end,
|
||||
|
||||
getLines = function(self)
|
||||
return lines
|
||||
end;
|
||||
end,
|
||||
|
||||
getLine = function(self, index)
|
||||
return lines[index]
|
||||
end;
|
||||
end,
|
||||
|
||||
editLine = function(self, index, text)
|
||||
lines[index] = text or lines[index]
|
||||
updateColors(self, index)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
clear = function(self)
|
||||
lines = {""}
|
||||
bgLines = {""}
|
||||
fgLines = {""}
|
||||
startSelX,endSelX,startSelY,endSelY = nil,nil,nil,nil
|
||||
hIndex, wIndex, textX, textY = 1, 1, 1, 1
|
||||
self:updateDraw()
|
||||
return self
|
||||
@@ -160,21 +208,23 @@ return function(name)
|
||||
|
||||
addLine = function(self, text, index)
|
||||
if(text~=nil)then
|
||||
local bgColor = self:getBackground()
|
||||
local fgColor = self:getForeground()
|
||||
if(#lines==1)and(lines[1]=="")then
|
||||
lines[1] = text
|
||||
bgLines[1] = tHex[self.bgColor]:rep(text:len())
|
||||
fgLines[1] = tHex[self.fgColor]:rep(text:len())
|
||||
bgLines[1] = tHex[bgColor]:rep(text:len())
|
||||
fgLines[1] = tHex[fgColor]:rep(text:len())
|
||||
updateColors(self, 1)
|
||||
return self
|
||||
end
|
||||
if (index ~= nil) then
|
||||
table.insert(lines, index, text)
|
||||
table.insert(bgLines, index, tHex[self.bgColor]:rep(text:len()))
|
||||
table.insert(fgLines, index, tHex[self.fgColor]:rep(text:len()))
|
||||
table.insert(bgLines, index, tHex[bgColor]:rep(text:len()))
|
||||
table.insert(fgLines, index, tHex[fgColor]:rep(text:len()))
|
||||
else
|
||||
table.insert(lines, text)
|
||||
table.insert(bgLines, tHex[self.bgColor]:rep(text:len()))
|
||||
table.insert(fgLines, tHex[self.fgColor]:rep(text:len()))
|
||||
table.insert(bgLines, tHex[bgColor]:rep(text:len()))
|
||||
table.insert(fgLines, tHex[fgColor]:rep(text:len()))
|
||||
end
|
||||
end
|
||||
updateColors(self, index or #lines)
|
||||
@@ -218,115 +268,149 @@ return function(name)
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setKeywords = function(self, color, tab)
|
||||
keyWords[color] = tab
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
removeLine = function(self, index)
|
||||
table.remove(lines, index or #lines)
|
||||
if (#lines <= 0) then
|
||||
table.insert(lines, "")
|
||||
if(#lines>1)then
|
||||
table.remove(lines, index or #lines)
|
||||
table.remove(bgLines, index or #bgLines)
|
||||
table.remove(fgLines, index or #fgLines)
|
||||
else
|
||||
lines = {""}
|
||||
bgLines = {""}
|
||||
fgLines = {""}
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getTextCursor = function(self)
|
||||
return textX, textY
|
||||
end;
|
||||
end,
|
||||
|
||||
getOffset = function(self)
|
||||
return wIndex, hIndex
|
||||
end,
|
||||
|
||||
setOffset = function(self, xOff, yOff)
|
||||
wIndex = xOff or wIndex
|
||||
hIndex = yOff or hIndex
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getXOffset = function(self)
|
||||
return wIndex
|
||||
end,
|
||||
|
||||
setXOffset = function(self, xOff)
|
||||
return self:setOffset(xOff, nil)
|
||||
end,
|
||||
|
||||
getYOffset = function(self)
|
||||
return hIndex
|
||||
end,
|
||||
|
||||
setYOffset = function(self, yOff)
|
||||
return self:setOffset(nil, yOff)
|
||||
end,
|
||||
|
||||
getFocusHandler = function(self)
|
||||
base.getFocusHandler(self)
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:setCursor(true, obx + textX - wIndex, oby + textY - hIndex, self.fgColor)
|
||||
end
|
||||
end
|
||||
end;
|
||||
local obx, oby = self:getPosition()
|
||||
self:getParent():setCursor(true, obx + textX - wIndex, oby + textY - hIndex, self:getForeground())
|
||||
end,
|
||||
|
||||
loseFocusHandler = function(self)
|
||||
base.loseFocusHandler(self)
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:setCursor(false)
|
||||
end
|
||||
end;
|
||||
self:getParent():setCursor(false)
|
||||
end,
|
||||
|
||||
keyHandler = function(self, key)
|
||||
if (base.keyHandler(self, event, key)) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
if (base.keyHandler(self, key)) then
|
||||
local parent = self:getParent()
|
||||
local obx, oby = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
if (key == keys.backspace) then
|
||||
-- on backspace
|
||||
if (lines[textY] == "") then
|
||||
if (textY > 1) then
|
||||
table.remove(lines, textY)
|
||||
table.remove(fgLines, textY)
|
||||
table.remove(bgLines, textY)
|
||||
textX = lines[textY - 1]:len() + 1
|
||||
wIndex = textX - w + 1
|
||||
if (wIndex < 1) then
|
||||
wIndex = 1
|
||||
end
|
||||
textY = textY - 1
|
||||
end
|
||||
elseif (textX <= 1) then
|
||||
if (textY > 1) then
|
||||
textX = lines[textY - 1]:len() + 1
|
||||
wIndex = textX - w + 1
|
||||
if (wIndex < 1) then
|
||||
wIndex = 1
|
||||
end
|
||||
lines[textY - 1] = lines[textY - 1] .. lines[textY]
|
||||
fgLines[textY - 1] = fgLines[textY - 1] .. fgLines[textY]
|
||||
bgLines[textY - 1] = bgLines[textY - 1] .. bgLines[textY]
|
||||
table.remove(lines, textY)
|
||||
table.remove(fgLines, textY)
|
||||
table.remove(bgLines, textY)
|
||||
textY = textY - 1
|
||||
end
|
||||
if(isSelected())then
|
||||
removeSelection(self)
|
||||
else
|
||||
lines[textY] = lines[textY]:sub(1, textX - 2) .. lines[textY]:sub(textX, lines[textY]:len())
|
||||
fgLines[textY] = fgLines[textY]:sub(1, textX - 2) .. fgLines[textY]:sub(textX, fgLines[textY]:len())
|
||||
bgLines[textY] = bgLines[textY]:sub(1, textX - 2) .. bgLines[textY]:sub(textX, bgLines[textY]:len())
|
||||
if (textX > 1) then
|
||||
textX = textX - 1
|
||||
end
|
||||
if (wIndex > 1) then
|
||||
if (textX < wIndex) then
|
||||
wIndex = wIndex - 1
|
||||
if (lines[textY] == "") then
|
||||
if (textY > 1) then
|
||||
table.remove(lines, textY)
|
||||
table.remove(fgLines, textY)
|
||||
table.remove(bgLines, textY)
|
||||
textX = lines[textY - 1]:len() + 1
|
||||
wIndex = textX - w + 1
|
||||
if (wIndex < 1) then
|
||||
wIndex = 1
|
||||
end
|
||||
textY = textY - 1
|
||||
end
|
||||
elseif (textX <= 1) then
|
||||
if (textY > 1) then
|
||||
textX = lines[textY - 1]:len() + 1
|
||||
wIndex = textX - w + 1
|
||||
if (wIndex < 1) then
|
||||
wIndex = 1
|
||||
end
|
||||
lines[textY - 1] = lines[textY - 1] .. lines[textY]
|
||||
fgLines[textY - 1] = fgLines[textY - 1] .. fgLines[textY]
|
||||
bgLines[textY - 1] = bgLines[textY - 1] .. bgLines[textY]
|
||||
table.remove(lines, textY)
|
||||
table.remove(fgLines, textY)
|
||||
table.remove(bgLines, textY)
|
||||
textY = textY - 1
|
||||
end
|
||||
else
|
||||
lines[textY] = lines[textY]:sub(1, textX - 2) .. lines[textY]:sub(textX, lines[textY]:len())
|
||||
fgLines[textY] = fgLines[textY]:sub(1, textX - 2) .. fgLines[textY]:sub(textX, fgLines[textY]:len())
|
||||
bgLines[textY] = bgLines[textY]:sub(1, textX - 2) .. bgLines[textY]:sub(textX, bgLines[textY]:len())
|
||||
if (textX > 1) then
|
||||
textX = textX - 1
|
||||
end
|
||||
if (wIndex > 1) then
|
||||
if (textX < wIndex) then
|
||||
wIndex = wIndex - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if (textY < hIndex) then
|
||||
hIndex = hIndex - 1
|
||||
if (textY < hIndex) then
|
||||
hIndex = hIndex - 1
|
||||
end
|
||||
end
|
||||
updateColors(self)
|
||||
self:setValue("")
|
||||
end
|
||||
|
||||
if (key == keys.delete) then
|
||||
elseif (key == keys.delete) then
|
||||
-- on delete
|
||||
if (textX > lines[textY]:len()) then
|
||||
if (lines[textY + 1] ~= nil) then
|
||||
lines[textY] = lines[textY] .. lines[textY + 1]
|
||||
table.remove(lines, textY + 1)
|
||||
table.remove(bgLines, textY + 1)
|
||||
table.remove(fgLines, textY + 1)
|
||||
end
|
||||
if(isSelected())then
|
||||
removeSelection(self)
|
||||
else
|
||||
lines[textY] = lines[textY]:sub(1, textX - 1) .. lines[textY]:sub(textX + 1, lines[textY]:len())
|
||||
fgLines[textY] = fgLines[textY]:sub(1, textX - 1) .. fgLines[textY]:sub(textX + 1, fgLines[textY]:len())
|
||||
bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. bgLines[textY]:sub(textX + 1, bgLines[textY]:len())
|
||||
if (textX > lines[textY]:len()) then
|
||||
if (lines[textY + 1] ~= nil) then
|
||||
lines[textY] = lines[textY] .. lines[textY + 1]
|
||||
table.remove(lines, textY + 1)
|
||||
table.remove(bgLines, textY + 1)
|
||||
table.remove(fgLines, textY + 1)
|
||||
end
|
||||
else
|
||||
lines[textY] = lines[textY]:sub(1, textX - 1) .. lines[textY]:sub(textX + 1, lines[textY]:len())
|
||||
fgLines[textY] = fgLines[textY]:sub(1, textX - 1) .. fgLines[textY]:sub(textX + 1, fgLines[textY]:len())
|
||||
bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. bgLines[textY]:sub(textX + 1, bgLines[textY]:len())
|
||||
end
|
||||
end
|
||||
updateColors(self)
|
||||
end
|
||||
|
||||
if (key == keys.enter) then
|
||||
elseif (key == keys.enter) then
|
||||
if(isSelected())then
|
||||
removeSelection(self)
|
||||
end
|
||||
-- on enter
|
||||
table.insert(lines, textY + 1, lines[textY]:sub(textX, lines[textY]:len()))
|
||||
table.insert(fgLines, textY + 1, fgLines[textY]:sub(textX, fgLines[textY]:len()))
|
||||
@@ -341,9 +425,8 @@ return function(name)
|
||||
hIndex = hIndex + 1
|
||||
end
|
||||
self:setValue("")
|
||||
end
|
||||
|
||||
if (key == keys.up) then
|
||||
elseif (key == keys.up) then
|
||||
startSelX, startSelY, endSelX, endSelY = nil, nil, nil, nil
|
||||
-- arrow up
|
||||
if (textY > 1) then
|
||||
textY = textY - 1
|
||||
@@ -364,8 +447,8 @@ return function(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if (key == keys.down) then
|
||||
elseif (key == keys.down) then
|
||||
startSelX, startSelY, endSelX, endSelY = nil, nil, nil, nil
|
||||
-- arrow down
|
||||
if (textY < #lines) then
|
||||
textY = textY + 1
|
||||
@@ -384,8 +467,8 @@ return function(name)
|
||||
hIndex = hIndex + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
if (key == keys.right) then
|
||||
elseif (key == keys.right) then
|
||||
startSelX, startSelY, endSelX, endSelY = nil, nil, nil, nil
|
||||
-- arrow right
|
||||
textX = textX + 1
|
||||
if (textY < #lines) then
|
||||
@@ -406,8 +489,8 @@ return function(name)
|
||||
wIndex = 1
|
||||
end
|
||||
|
||||
end
|
||||
if (key == keys.left) then
|
||||
elseif (key == keys.left) then
|
||||
startSelX, startSelY, endSelX, endSelY = nil, nil, nil, nil
|
||||
-- arrow left
|
||||
textX = textX - 1
|
||||
if (textX >= 1) then
|
||||
@@ -428,17 +511,35 @@ return function(name)
|
||||
if (wIndex < 1) then
|
||||
wIndex = 1
|
||||
end
|
||||
elseif(key == keys.tab)then
|
||||
if(textX % 3 == 0 )then
|
||||
lines[textY] = lines[textY]:sub(1, textX - 1) .. " " .. lines[textY]:sub(textX, lines[textY]:len())
|
||||
fgLines[textY] = fgLines[textY]:sub(1, textX - 1) .. tHex[self:getForeground()] .. fgLines[textY]:sub(textX, fgLines[textY]:len())
|
||||
bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. tHex[self:getBackground()] .. bgLines[textY]:sub(textX, bgLines[textY]:len())
|
||||
textX = textX + 1
|
||||
end
|
||||
while textX % 3 ~= 0 do
|
||||
lines[textY] = lines[textY]:sub(1, textX - 1) .. " " .. lines[textY]:sub(textX, lines[textY]:len())
|
||||
fgLines[textY] = fgLines[textY]:sub(1, textX - 1) .. tHex[self:getForeground()] .. fgLines[textY]:sub(textX, fgLines[textY]:len())
|
||||
bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. tHex[self:getBackground()] .. bgLines[textY]:sub(textX, bgLines[textY]:len())
|
||||
textX = textX + 1
|
||||
end
|
||||
end
|
||||
|
||||
if not((obx + textX - wIndex >= obx and obx + textX - wIndex < obx + w) and (oby + textY - hIndex >= oby and oby + textY - hIndex < oby + h)) then
|
||||
wIndex = math.max(1, lines[textY]:len()-w+1)
|
||||
hIndex = math.max(1, textY - h + 1)
|
||||
end
|
||||
|
||||
local cursorX = (textX <= lines[textY]:len() and textX - 1 or lines[textY]:len()) - (wIndex - 1)
|
||||
if (cursorX > self.x + w - 1) then
|
||||
cursorX = self.x + w - 1
|
||||
if (cursorX > self:getX() + w - 1) then
|
||||
cursorX = self:getX() + w - 1
|
||||
end
|
||||
local cursorY = (textY - hIndex < h and textY - hIndex or textY - hIndex - 1)
|
||||
if (cursorX < 1) then
|
||||
cursorX = 0
|
||||
end
|
||||
self.parent:setCursor(true, obx + cursorX, oby + cursorY, self.fgColor)
|
||||
parent:setCursor(true, obx + cursorX, oby + cursorY, self:getForeground())
|
||||
self:updateDraw()
|
||||
return true
|
||||
end
|
||||
@@ -446,11 +547,15 @@ return function(name)
|
||||
|
||||
charHandler = function(self, char)
|
||||
if(base.charHandler(self, char))then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local parent = self:getParent()
|
||||
local obx, oby = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
if(isSelected())then
|
||||
removeSelection(self)
|
||||
end
|
||||
lines[textY] = lines[textY]:sub(1, textX - 1) .. char .. lines[textY]:sub(textX, lines[textY]:len())
|
||||
fgLines[textY] = fgLines[textY]:sub(1, textX - 1) .. tHex[self.fgColor] .. fgLines[textY]:sub(textX, fgLines[textY]:len())
|
||||
bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. tHex[self.bgColor] .. bgLines[textY]:sub(textX, bgLines[textY]:len())
|
||||
fgLines[textY] = fgLines[textY]:sub(1, textX - 1) .. tHex[self:getForeground()] .. fgLines[textY]:sub(textX, fgLines[textY]:len())
|
||||
bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. tHex[self:getBackground()] .. bgLines[textY]:sub(textX, bgLines[textY]:len())
|
||||
textX = textX + 1
|
||||
if (textX >= w + wIndex) then
|
||||
wIndex = wIndex + 1
|
||||
@@ -458,15 +563,20 @@ return function(name)
|
||||
updateColors(self)
|
||||
self:setValue("")
|
||||
|
||||
if not((obx + textX - wIndex >= obx and obx + textX - wIndex < obx + w) and (oby + textY - hIndex >= oby and oby + textY - hIndex < oby + h)) then
|
||||
wIndex = math.max(1, lines[textY]:len()-w+1)
|
||||
hIndex = math.max(1, textY - h + 1)
|
||||
end
|
||||
|
||||
local cursorX = (textX <= lines[textY]:len() and textX - 1 or lines[textY]:len()) - (wIndex - 1)
|
||||
if (cursorX > self.x + w - 1) then
|
||||
cursorX = self.x + w - 1
|
||||
if (cursorX > self:getX() + w - 1) then
|
||||
cursorX = self:getX() + w - 1
|
||||
end
|
||||
local cursorY = (textY - hIndex < h and textY - hIndex or textY - hIndex - 1)
|
||||
if (cursorX < 1) then
|
||||
cursorX = 0
|
||||
end
|
||||
self.parent:setCursor(true, obx + cursorX, oby + cursorY, self.fgColor)
|
||||
parent:setCursor(true, obx + cursorX, oby + cursorY, self:getForeground())
|
||||
self:updateDraw()
|
||||
return true
|
||||
end
|
||||
@@ -474,25 +584,28 @@ return function(name)
|
||||
|
||||
dragHandler = function(self, button, x, y)
|
||||
if (base.dragHandler(self, button, x, y)) then
|
||||
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
local anchx, anchy = self:getAnchorPosition()
|
||||
local parent = self:getParent()
|
||||
local obx, oby = self:getAbsolutePosition()
|
||||
local ox, oy = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
if (lines[y - oby + hIndex] ~= nil) then
|
||||
if(anchx+w > anchx + x - (obx+1)+ wIndex)and(anchx < anchx + x - obx+ wIndex)then
|
||||
if(x - obx + wIndex > 0)and(x - obx + wIndex <= w)then
|
||||
textX = x - obx + wIndex
|
||||
textY = y - oby + hIndex
|
||||
if (textX > lines[textY]:len()) then
|
||||
|
||||
if textX > lines[textY]:len() then
|
||||
textX = lines[textY]:len() + 1
|
||||
end
|
||||
if (textX < wIndex) then
|
||||
endSelX = textX
|
||||
endSelY = textY
|
||||
|
||||
if textX < wIndex then
|
||||
wIndex = textX - 1
|
||||
if (wIndex < 1) then
|
||||
if wIndex < 1 then
|
||||
wIndex = 1
|
||||
end
|
||||
end
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:setCursor(true, anchx + textX - wIndex, anchy + textY - hIndex, self.fgColor)
|
||||
end
|
||||
parent:setCursor(not isSelected(), ox + textX - wIndex, oy + textY - hIndex, self:getForeground())
|
||||
self:updateDraw()
|
||||
end
|
||||
end
|
||||
@@ -502,8 +615,9 @@ return function(name)
|
||||
|
||||
scrollHandler = function(self, dir, x, y)
|
||||
if (base.scrollHandler(self, dir, x, y)) then
|
||||
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
local anchx, anchy = self:getAnchorPosition()
|
||||
local parent = self:getParent()
|
||||
local obx, oby = self:getAbsolutePosition()
|
||||
local anchx, anchy = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
hIndex = hIndex + dir
|
||||
if (hIndex > #lines - (h - 1)) then
|
||||
@@ -514,12 +628,10 @@ return function(name)
|
||||
hIndex = 1
|
||||
end
|
||||
|
||||
if (self.parent ~= nil) then
|
||||
if (obx + textX - wIndex >= obx and obx + textX - wIndex < obx + w) and (oby + textY - hIndex >= oby and oby + textY - hIndex < oby + h) then
|
||||
self.parent:setCursor(true, anchx + textX - wIndex, anchy + textY - hIndex, self.fgColor)
|
||||
else
|
||||
self.parent:setCursor(false)
|
||||
end
|
||||
if (obx + textX - wIndex >= obx and obx + textX - wIndex < obx + w) and (anchy + textY - hIndex >= anchy and anchy + textY - hIndex < anchy + h) then
|
||||
parent:setCursor(not isSelected(), anchx + textX - wIndex, anchy + textY - hIndex, self:getForeground())
|
||||
else
|
||||
parent:setCursor(false)
|
||||
end
|
||||
self:updateDraw()
|
||||
return true
|
||||
@@ -528,13 +640,19 @@ return function(name)
|
||||
|
||||
mouseHandler = function(self, button, x, y)
|
||||
if (base.mouseHandler(self, button, x, y)) then
|
||||
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition())
|
||||
local anchx, anchy = self:getAnchorPosition()
|
||||
local parent = self:getParent()
|
||||
local obx, oby = self:getAbsolutePosition()
|
||||
local anchx, anchy = self:getPosition()
|
||||
if (lines[y - oby + hIndex] ~= nil) then
|
||||
textX = x - obx + wIndex
|
||||
textY = y - oby + hIndex
|
||||
endSelX = nil
|
||||
endSelY = nil
|
||||
startSelX = textX
|
||||
startSelY = textY
|
||||
if (textX > lines[textY]:len()) then
|
||||
textX = lines[textY]:len() + 1
|
||||
startSelX = textX
|
||||
end
|
||||
if (textX < wIndex) then
|
||||
wIndex = textX - 1
|
||||
@@ -542,84 +660,115 @@ return function(name)
|
||||
wIndex = 1
|
||||
end
|
||||
end
|
||||
self:updateDraw()
|
||||
end
|
||||
if (self.parent ~= nil) then
|
||||
self.parent:setCursor(true, anchx + textX - wIndex, anchy + textY - hIndex, self.fgColor)
|
||||
parent:setCursor(true, anchx + textX - wIndex, anchy + textY - hIndex, self:getForeground())
|
||||
return true
|
||||
end
|
||||
end,
|
||||
|
||||
mouseUpHandler = function(self, button, x, y)
|
||||
if (base.mouseUpHandler(self, button, x, y)) then
|
||||
local obx, oby = self:getAbsolutePosition()
|
||||
if (lines[y - oby + hIndex] ~= nil) then
|
||||
endSelX = x - obx + wIndex
|
||||
endSelY = y - oby + hIndex
|
||||
if (endSelX > lines[endSelY]:len()) then
|
||||
endSelX = lines[endSelY]:len() + 1
|
||||
end
|
||||
if(startSelX==endSelX)and(startSelY==endSelY)then
|
||||
startSelX, endSelX, startSelY, endSelY = nil, nil, nil, nil
|
||||
end
|
||||
self:updateDraw()
|
||||
end
|
||||
return true
|
||||
end
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, paste, p2, p3, p4)
|
||||
if(base.eventHandler(self, event, paste, p2, p3, p4))then
|
||||
if(event=="paste")then
|
||||
if(self:isFocused())then
|
||||
local w, h = self:getSize()
|
||||
lines[textY] = lines[textY]:sub(1, textX - 1) .. paste .. lines[textY]:sub(textX, lines[textY]:len())
|
||||
fgLines[textY] = fgLines[textY]:sub(1, textX - 1) .. tHex[self.fgColor]:rep(paste:len()) .. fgLines[textY]:sub(textX, fgLines[textY]:len())
|
||||
bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. tHex[self.bgColor]:rep(paste:len()) .. bgLines[textY]:sub(textX, bgLines[textY]:len())
|
||||
textX = textX + paste:len()
|
||||
if (textX >= w + wIndex) then
|
||||
wIndex = (textX+1)-w
|
||||
end
|
||||
local anchx, anchy = self:getAnchorPosition()
|
||||
self.parent:setCursor(true, anchx + textX - wIndex, anchy + textY - hIndex, self.fgColor)
|
||||
updateColors(self)
|
||||
self:updateDraw()
|
||||
eventHandler = function(self, event, paste, ...)
|
||||
base.eventHandler(self, event, paste, ...)
|
||||
if(event=="paste")then
|
||||
if(self:isFocused())then
|
||||
local parent = self:getParent()
|
||||
local fgColor, bgColor = self:getForeground(), self:getBackground()
|
||||
local w, h = self:getSize()
|
||||
lines[textY] = lines[textY]:sub(1, textX - 1) .. paste .. lines[textY]:sub(textX, lines[textY]:len())
|
||||
fgLines[textY] = fgLines[textY]:sub(1, textX - 1) .. tHex[fgColor]:rep(paste:len()) .. fgLines[textY]:sub(textX, fgLines[textY]:len())
|
||||
bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. tHex[bgColor]:rep(paste:len()) .. bgLines[textY]:sub(textX, bgLines[textY]:len())
|
||||
textX = textX + paste:len()
|
||||
if (textX >= w + wIndex) then
|
||||
wIndex = (textX+1)-w
|
||||
end
|
||||
local anchx, anchy = self:getPosition()
|
||||
parent:setCursor(true, anchx + textX - wIndex, anchy + textY - hIndex, fgColor)
|
||||
updateColors(self)
|
||||
self:updateDraw()
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
if (base.draw(self)) then
|
||||
if (self.parent ~= nil) then
|
||||
local obx, oby = self:getAnchorPosition()
|
||||
local w,h = self:getSize()
|
||||
for n = 1, h do
|
||||
local text = ""
|
||||
local bg = ""
|
||||
local fg = ""
|
||||
if (lines[n + hIndex - 1] ~= nil) then
|
||||
text = lines[n + hIndex - 1]
|
||||
fg = fgLines[n + hIndex - 1]
|
||||
bg = bgLines[n + hIndex - 1]
|
||||
end
|
||||
text = text:sub(wIndex, w + wIndex - 1)
|
||||
bg = bg:sub(wIndex, w + wIndex - 1)
|
||||
fg = fg:sub(wIndex, w + wIndex - 1)
|
||||
local space = w - text:len()
|
||||
if (space < 0) then
|
||||
space = 0
|
||||
end
|
||||
text = text .. rep(self.bgSymbol, space)
|
||||
bg = bg .. rep(tHex[self.bgColor], space)
|
||||
fg = fg .. rep(tHex[self.fgColor], space)
|
||||
self.parent:setText(obx, oby + n - 1, text)
|
||||
self.parent:setBG(obx, oby + n - 1, bg)
|
||||
self.parent:setFG(obx, oby + n - 1, fg)
|
||||
base.draw(self)
|
||||
self:addDraw("textfield", function()
|
||||
local w, h = self:getSize()
|
||||
local bgColor = tHex[self:getBackground()]
|
||||
local fgColor = tHex[self:getForeground()]
|
||||
|
||||
for n = 1, h do
|
||||
local text = ""
|
||||
local bg = ""
|
||||
local fg = ""
|
||||
if lines[n + hIndex - 1] then
|
||||
text = lines[n + hIndex - 1]
|
||||
fg = fgLines[n + hIndex - 1]
|
||||
bg = bgLines[n + hIndex - 1]
|
||||
end
|
||||
if(self:isFocused())then
|
||||
local anchx, anchy = self:getAnchorPosition()
|
||||
self.parent:setCursor(true, anchx + textX - wIndex, anchy + textY - hIndex, self.fgColor)
|
||||
|
||||
text = sub(text, wIndex, w + wIndex - 1)
|
||||
bg = rep(bgColor, w)
|
||||
fg = rep(fgColor, w)
|
||||
|
||||
self:addText(1, n, text)
|
||||
self:addBG(1, n, bg)
|
||||
self:addFG(1, n, fg)
|
||||
self:addBlit(1, n, text, fg, bg)
|
||||
end
|
||||
|
||||
if startSelX and endSelX and startSelY and endSelY then
|
||||
local sx, ex, sy, ey = getSelectionCoordinates()
|
||||
for n = sy, ey do
|
||||
local line = #lines[n]
|
||||
local xOffset = 0
|
||||
if n == sy and n == ey then
|
||||
xOffset = sx - 1 - (wIndex - 1)
|
||||
line = line - (sx - 1 - (wIndex - 1)) - (line - ex + (wIndex - 1))
|
||||
elseif n == ey then
|
||||
line = line - (line - ex + (wIndex - 1))
|
||||
elseif n == sy then
|
||||
line = line - (sx - 1)
|
||||
xOffset = sx - 1 - (wIndex - 1)
|
||||
end
|
||||
|
||||
local visible_line_length = math.min(line, w - xOffset)
|
||||
|
||||
self:addBG(1 + xOffset, n, rep(tHex[selectionBG], visible_line_length))
|
||||
self:addFG(1 + xOffset, n, rep(tHex[selectionFG], visible_line_length))
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
end,
|
||||
|
||||
init = function(self)
|
||||
self.parent:addEvent("mouse_click", self)
|
||||
self.parent:addEvent("mouse_scroll", self)
|
||||
self.parent:addEvent("mouse_drag", self)
|
||||
self.parent:addEvent("key", self)
|
||||
self.parent:addEvent("char", self)
|
||||
self.parent:addEvent("other_event", self)
|
||||
if(base.init(self))then
|
||||
self.bgColor = self.parent:getTheme("TextfieldBG")
|
||||
self.fgColor = self.parent:getTheme("TextfieldText")
|
||||
end
|
||||
load = function(self)
|
||||
self:listenEvent("mouse_click")
|
||||
self:listenEvent("mouse_up")
|
||||
self:listenEvent("mouse_scroll")
|
||||
self:listenEvent("mouse_drag")
|
||||
self:listenEvent("key")
|
||||
self:listenEvent("char")
|
||||
self:listenEvent("other_event")
|
||||
end,
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,49 +1,16 @@
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("Object")(name, basalt)
|
||||
|
||||
return function(name)
|
||||
local object
|
||||
local objectType = "Thread"
|
||||
|
||||
local func
|
||||
local cRoutine
|
||||
local isActive = false
|
||||
local filter
|
||||
|
||||
local generateXMLEventFunction = function(self, str)
|
||||
if(str:sub(1,1)=="#")then
|
||||
local o = self:getBaseFrame():getDeepObject(str:sub(2,str:len()))
|
||||
if(o~=nil)and(o.internalObjetCall~=nil)then
|
||||
return (function()o:internalObjetCall()end)
|
||||
end
|
||||
else
|
||||
return self:getBaseFrame():getVariable(str)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
object = {
|
||||
name = name,
|
||||
local object = {
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
getZIndex = function(self)
|
||||
return 1
|
||||
end;
|
||||
getName = function(self)
|
||||
return self.name
|
||||
end;
|
||||
|
||||
getBaseFrame = function(self)
|
||||
if(self.parent~=nil)then
|
||||
return self.parent:getBaseFrame()
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
local f
|
||||
if(xmlValue("thread", data)~=nil)then f = generateXMLEventFunction(self, xmlValue("thread", data)) end
|
||||
if(xmlValue("start", data)~=nil)then if(xmlValue("start", data))and(f~=nil)then self:start(f) end end
|
||||
return self
|
||||
end,
|
||||
|
||||
start = function(self, f)
|
||||
@@ -53,47 +20,93 @@ return function(name)
|
||||
func = f
|
||||
cRoutine = coroutine.create(func)
|
||||
isActive = true
|
||||
filter=nil
|
||||
local ok, result = coroutine.resume(cRoutine)
|
||||
filter = result
|
||||
if not (ok) then
|
||||
if (result ~= "Terminated") then
|
||||
error("Thread Error Occurred - " .. result)
|
||||
end
|
||||
end
|
||||
self.parent:addEvent("other_event", self)
|
||||
self:listenEvent("mouse_click")
|
||||
self:listenEvent("mouse_up")
|
||||
self:listenEvent("mouse_scroll")
|
||||
self:listenEvent("mouse_drag")
|
||||
self:listenEvent("key")
|
||||
self:listenEvent("key_up")
|
||||
self:listenEvent("char")
|
||||
self:listenEvent("other_event")
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getStatus = function(self, f)
|
||||
if (cRoutine ~= nil) then
|
||||
return coroutine.status(cRoutine)
|
||||
end
|
||||
return nil
|
||||
end;
|
||||
end,
|
||||
|
||||
stop = function(self, f)
|
||||
isActive = false
|
||||
self.parent:removeEvent("other_event", self)
|
||||
self:listenEvent("mouse_click", false)
|
||||
self:listenEvent("mouse_up", false)
|
||||
self:listenEvent("mouse_scroll", false)
|
||||
self:listenEvent("mouse_drag", false)
|
||||
self:listenEvent("key", false)
|
||||
self:listenEvent("key_up", false)
|
||||
self:listenEvent("char", false)
|
||||
self:listenEvent("other_event", false)
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, p1, p2, p3)
|
||||
mouseHandler = function(self, ...)
|
||||
self:eventHandler("mouse_click", ...)
|
||||
end,
|
||||
mouseUpHandler = function(self, ...)
|
||||
self:eventHandler("mouse_up", ...)
|
||||
end,
|
||||
mouseScrollHandler = function(self, ...)
|
||||
self:eventHandler("mouse_scroll", ...)
|
||||
end,
|
||||
mouseDragHandler = function(self, ...)
|
||||
self:eventHandler("mouse_drag", ...)
|
||||
end,
|
||||
mouseMoveHandler = function(self, ...)
|
||||
self:eventHandler("mouse_move", ...)
|
||||
end,
|
||||
keyHandler = function(self, ...)
|
||||
self:eventHandler("key", ...)
|
||||
end,
|
||||
keyUpHandler = function(self, ...)
|
||||
self:eventHandler("key_up", ...)
|
||||
end,
|
||||
charHandler = function(self, ...)
|
||||
self:eventHandler("char", ...)
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, ...)
|
||||
base.eventHandler(self, event, ...)
|
||||
if (isActive) then
|
||||
if (coroutine.status(cRoutine) ~= "dead") then
|
||||
local ok, result = coroutine.resume(cRoutine, event, p1, p2, p3)
|
||||
if (coroutine.status(cRoutine) == "suspended") then
|
||||
if(filter~=nil)then
|
||||
if(event~=filter)then return end
|
||||
filter=nil
|
||||
end
|
||||
local ok, result = coroutine.resume(cRoutine, event, ...)
|
||||
filter = result
|
||||
if not (ok) then
|
||||
if (result ~= "Terminated") then
|
||||
error("Thread Error Occurred - " .. result)
|
||||
end
|
||||
end
|
||||
else
|
||||
isActive = false
|
||||
self:stop()
|
||||
end
|
||||
end
|
||||
end;
|
||||
end,
|
||||
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
|
||||
return object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
@@ -1,71 +1,27 @@
|
||||
local basaltEvent = require("basaltEvent")
|
||||
local xmlValue = require("utils").getValueFromXML
|
||||
|
||||
return function(name)
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("Object")(name, basalt)
|
||||
local objectType = "Timer"
|
||||
|
||||
local timer = 0
|
||||
local savedRepeats = 0
|
||||
local repeats = 0
|
||||
local timerObj
|
||||
local eventSystem = basaltEvent()
|
||||
local timerIsActive = false
|
||||
|
||||
local generateXMLEventFunction = function(self, func, val)
|
||||
local createF = function(str)
|
||||
if(str:sub(1,1)=="#")then
|
||||
local o = self:getBaseFrame():getDeepObject(str:sub(2,str:len()))
|
||||
if(o~=nil)and(o.internalObjetCall~=nil)then
|
||||
func(self,function()o:internalObjetCall()end)
|
||||
end
|
||||
else
|
||||
func(self,self:getBaseFrame():getVariable(str))
|
||||
end
|
||||
end
|
||||
if(type(val)=="string")then
|
||||
createF(val)
|
||||
elseif(type(val)=="table")then
|
||||
for k,v in pairs(val)do
|
||||
createF(v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
local object = {
|
||||
name = name,
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end;
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
if(xmlValue("time", data)~=nil)then timer = xmlValue("time", data) end
|
||||
if(xmlValue("repeat", data)~=nil)then savedRepeats = xmlValue("repeat", data) end
|
||||
if(xmlValue("start", data)~=nil)then if(xmlValue("start", data))then self:start() end end
|
||||
if(xmlValue("onCall", data)~=nil)then generateXMLEventFunction(self, self.onCall, xmlValue("onCall", data)) end
|
||||
return self
|
||||
end,
|
||||
|
||||
getBaseFrame = function(self)
|
||||
if(self.parent~=nil)then
|
||||
return self.parent:getBaseFrame()
|
||||
end
|
||||
return self
|
||||
end;
|
||||
|
||||
getZIndex = function(self)
|
||||
return 1
|
||||
end;
|
||||
|
||||
getName = function(self)
|
||||
return self.name
|
||||
end;
|
||||
|
||||
setTime = function(self, _timer, _repeats)
|
||||
timer = _timer or 0
|
||||
savedRepeats = _repeats or 1
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
getTime = function(self)
|
||||
return timer
|
||||
end,
|
||||
|
||||
start = function(self)
|
||||
if(timerIsActive)then
|
||||
@@ -74,31 +30,40 @@ return function(name)
|
||||
repeats = savedRepeats
|
||||
timerObj = os.startTimer(timer)
|
||||
timerIsActive = true
|
||||
self.parent:addEvent("other_event", self)
|
||||
self:listenEvent("other_event")
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
isActive = function(self)
|
||||
return timerIsActive
|
||||
end;
|
||||
end,
|
||||
|
||||
cancel = function(self)
|
||||
if (timerObj ~= nil) then
|
||||
os.cancelTimer(timerObj)
|
||||
end
|
||||
timerIsActive = false
|
||||
self.parent:removeEvent("other_event", self)
|
||||
self:removeEvent("other_event")
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
setStart = function(self, start)
|
||||
if (start == true) then
|
||||
return self:start()
|
||||
else
|
||||
return self:cancel()
|
||||
end
|
||||
end,
|
||||
|
||||
onCall = function(self, func)
|
||||
eventSystem:registerEvent("timed_event", func)
|
||||
self:registerEvent("timed_event", func)
|
||||
return self
|
||||
end;
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, tObj)
|
||||
eventHandler = function(self, event, ...)
|
||||
base.eventHandler(self, event, ...)
|
||||
if event == "timer" and tObj == timerObj and timerIsActive then
|
||||
eventSystem:sendEvent("timed_event", self)
|
||||
self:sendEvent("timed_event")
|
||||
if (repeats >= 1) then
|
||||
repeats = repeats - 1
|
||||
if (repeats >= 1) then
|
||||
@@ -108,9 +73,9 @@ return function(name)
|
||||
timerObj = os.startTimer(timer)
|
||||
end
|
||||
end
|
||||
end;
|
||||
end,
|
||||
}
|
||||
object.__index = object
|
||||
|
||||
return object
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
356
Basalt/objects/Treeview.lua
Normal file
@@ -0,0 +1,356 @@
|
||||
local utils = require("utils")
|
||||
local tHex = require("tHex")
|
||||
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("ChangeableObject")(name, basalt)
|
||||
local objectType = "Treeview"
|
||||
|
||||
local nodes = {}
|
||||
local itemSelectedBG = colors.black
|
||||
local itemSelectedFG = colors.lightGray
|
||||
local selectionColorActive = true
|
||||
local textAlign = "left"
|
||||
local xOffset, yOffset = 0, 0
|
||||
local scrollable = true
|
||||
|
||||
base:setSize(16, 8)
|
||||
base:setZIndex(5)
|
||||
|
||||
local function newNode(text, expandable)
|
||||
text = text or ""
|
||||
expandable = expandable or false
|
||||
local expanded = false
|
||||
local parent = nil
|
||||
local children = {}
|
||||
|
||||
local node = {}
|
||||
|
||||
local onSelect
|
||||
|
||||
node = {
|
||||
getChildren = function(self)
|
||||
return children
|
||||
end,
|
||||
|
||||
setParent = function(self, p)
|
||||
if(parent~=nil)then
|
||||
parent.removeChild(parent.findChildrenByText(node.getText()))
|
||||
end
|
||||
parent = p
|
||||
base:updateDraw()
|
||||
return node
|
||||
end,
|
||||
|
||||
getParent = function(self)
|
||||
return parent
|
||||
end,
|
||||
|
||||
addChild = function(self, text, expandable)
|
||||
local childNode = newNode(text, expandable)
|
||||
childNode.setParent(node)
|
||||
table.insert(children, childNode)
|
||||
base:updateDraw()
|
||||
return childNode
|
||||
end,
|
||||
|
||||
setExpanded = function(self, exp)
|
||||
if(expandable)then
|
||||
expanded = exp
|
||||
end
|
||||
base:updateDraw()
|
||||
return node
|
||||
end,
|
||||
|
||||
isExpanded = function(self)
|
||||
return expanded
|
||||
end,
|
||||
|
||||
onSelect = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
onSelect = v
|
||||
end
|
||||
end
|
||||
return node
|
||||
end,
|
||||
|
||||
callOnSelect = function(self)
|
||||
if(onSelect~=nil)then
|
||||
onSelect(node)
|
||||
end
|
||||
end,
|
||||
|
||||
setExpandable = function(self, expandable)
|
||||
expandable = expandable
|
||||
base:updateDraw()
|
||||
return node
|
||||
end,
|
||||
|
||||
isExpandable = function(self)
|
||||
return expandable
|
||||
end,
|
||||
|
||||
removeChild = function(self, index)
|
||||
if(type(index)=="table")then
|
||||
for k,v in pairs(index)do
|
||||
if(v==index)then
|
||||
index = k
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
table.remove(children, index)
|
||||
base:updateDraw()
|
||||
return node
|
||||
end,
|
||||
|
||||
findChildrenByText = function(self, searchText)
|
||||
local foundNodes = {}
|
||||
for _, child in ipairs(children) do
|
||||
if string.find(child.getText(), searchText) then
|
||||
table.insert(foundNodes, child)
|
||||
end
|
||||
end
|
||||
return foundNodes
|
||||
end,
|
||||
|
||||
getText = function(self)
|
||||
return text
|
||||
end,
|
||||
|
||||
setText = function(self, t)
|
||||
text = t
|
||||
base:updateDraw()
|
||||
return node
|
||||
end
|
||||
}
|
||||
|
||||
return node
|
||||
end
|
||||
|
||||
local root = newNode("Root", true)
|
||||
root:setExpanded(true)
|
||||
|
||||
local object = {
|
||||
init = function(self)
|
||||
local parent = self:getParent()
|
||||
self:listenEvent("mouse_click")
|
||||
self:listenEvent("mouse_scroll")
|
||||
return base.init(self)
|
||||
end,
|
||||
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end,
|
||||
|
||||
isType = function(self, t)
|
||||
return objectType == t or base.isType ~= nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
setOffset = function(self, x, y)
|
||||
xOffset = x
|
||||
yOffset = y
|
||||
return self
|
||||
end,
|
||||
|
||||
setXOffset = function(self, x)
|
||||
return self:setOffset(x, yOffset)
|
||||
end,
|
||||
|
||||
setYOffset = function(self, y)
|
||||
return self:setOffset(xOffset, y)
|
||||
end,
|
||||
|
||||
getOffset = function(self)
|
||||
return xOffset, yOffset
|
||||
end,
|
||||
|
||||
getXOffset = function(self)
|
||||
return xOffset
|
||||
end,
|
||||
|
||||
getYOffset = function(self)
|
||||
return yOffset
|
||||
end,
|
||||
|
||||
setScrollable = function(self, scroll)
|
||||
scrollable = scroll
|
||||
return self
|
||||
end,
|
||||
|
||||
getScrollable = function(self, scroll)
|
||||
return scrollable
|
||||
end,
|
||||
|
||||
setSelectionColor = function(self, bgCol, fgCol, active)
|
||||
itemSelectedBG = bgCol or self:getBackground()
|
||||
itemSelectedFG = fgCol or self:getForeground()
|
||||
selectionColorActive = active~=nil and active or true
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setSelectionBG = function(self, bgCol)
|
||||
return self:setSelectionColor(bgCol, nil, selectionColorActive)
|
||||
end,
|
||||
|
||||
setSelectionFG = function(self, fgCol)
|
||||
return self:setSelectionColor(nil, fgCol, selectionColorActive)
|
||||
end,
|
||||
|
||||
getSelectionColor = function(self)
|
||||
return itemSelectedBG, itemSelectedFG
|
||||
end,
|
||||
|
||||
getSelectionBG = function(self)
|
||||
return itemSelectedBG
|
||||
end,
|
||||
|
||||
getSelectionFG = function(self)
|
||||
return itemSelectedFG
|
||||
end,
|
||||
|
||||
isSelectionColorActive = function(self)
|
||||
return selectionColorActive
|
||||
end,
|
||||
|
||||
getRoot = function(self)
|
||||
return root
|
||||
end,
|
||||
|
||||
setRoot = function(self, node)
|
||||
root = node
|
||||
node.setParent(nil)
|
||||
return self
|
||||
end,
|
||||
|
||||
onSelect = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("treeview_select", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
selectionHandler = function(self, node)
|
||||
node.callOnSelect(node)
|
||||
self:sendEvent("treeview_select", node)
|
||||
return self
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y)
|
||||
if base.mouseHandler(self, button, x, y) then
|
||||
local currentLine = 1 - yOffset
|
||||
local obx, oby = self:getAbsolutePosition()
|
||||
local w, h = self:getSize()
|
||||
local function checkNodeClick(node, level)
|
||||
if y == oby+currentLine-1 then
|
||||
if x >= obx and x < obx + w then
|
||||
node:setExpanded(not node:isExpanded())
|
||||
self:selectionHandler(node)
|
||||
self:setValue(node)
|
||||
self:updateDraw()
|
||||
return true
|
||||
end
|
||||
end
|
||||
currentLine = currentLine + 1
|
||||
if node:isExpanded() then
|
||||
for _, child in ipairs(node:getChildren()) do
|
||||
if checkNodeClick(child, level + 1) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
for _, item in ipairs(root:getChildren()) do
|
||||
if checkNodeClick(item, 1) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
scrollHandler = function(self, dir, x, y)
|
||||
if base.scrollHandler(self, dir, x, y) then
|
||||
if scrollable then
|
||||
local _, h = self:getSize()
|
||||
yOffset = yOffset + dir
|
||||
|
||||
if yOffset < 0 then
|
||||
yOffset = 0
|
||||
end
|
||||
|
||||
if dir >= 1 then
|
||||
local visibleLines = 0
|
||||
local function countVisibleLines(node, level)
|
||||
visibleLines = visibleLines + 1
|
||||
if node:isExpanded() then
|
||||
for _, child in ipairs(node:getChildren()) do
|
||||
countVisibleLines(child, level + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, item in ipairs(root:getChildren()) do
|
||||
countVisibleLines(item, 1)
|
||||
end
|
||||
|
||||
if visibleLines > h then
|
||||
if yOffset > visibleLines - h then
|
||||
yOffset = visibleLines - h
|
||||
end
|
||||
else
|
||||
yOffset = yOffset - 1
|
||||
end
|
||||
end
|
||||
self:updateDraw()
|
||||
end
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
base.draw(self)
|
||||
self:addDraw("treeview", function()
|
||||
local currentLine = 1 - yOffset
|
||||
local lastClickedNode = self:getValue()
|
||||
local function drawNode(node, level)
|
||||
local w, h = self:getSize()
|
||||
|
||||
if currentLine >= 1 and currentLine <= h then
|
||||
local bg = (node == lastClickedNode) and itemSelectedBG or self:getBackground()
|
||||
local fg = (node == lastClickedNode) and itemSelectedFG or self:getForeground()
|
||||
|
||||
local text = node.getText()
|
||||
self:addBlit(1 + level + xOffset, currentLine, text, tHex[fg]:rep(#text), tHex[bg]:rep(#text))
|
||||
end
|
||||
|
||||
currentLine = currentLine + 1
|
||||
|
||||
if node:isExpanded() then
|
||||
for _, child in ipairs(node:getChildren()) do
|
||||
drawNode(child, level + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, item in ipairs(root:getChildren()) do
|
||||
drawNode(item, 1)
|
||||
end
|
||||
end)
|
||||
end,
|
||||
|
||||
|
||||
}
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
646
Basalt/objects/VisualObject.lua
Normal file
@@ -0,0 +1,646 @@
|
||||
local utils = require("utils")
|
||||
local tHex = require("tHex")
|
||||
|
||||
local sub, find, insert = string.sub, string.find, table.insert
|
||||
|
||||
return function(name, basalt)
|
||||
local base = basalt.getObject("Object")(name, basalt)
|
||||
-- Base object
|
||||
local objectType = "VisualObject" -- not changeable
|
||||
|
||||
local isVisible,ignOffset,isHovered,isClicked,isDragging = true,false,false,false,false
|
||||
local zIndex = 1
|
||||
|
||||
local x, y, width, height = 1,1,1,1
|
||||
local dragStartX, dragStartY, dragXOffset, dragYOffset = 0, 0, 0, 0
|
||||
|
||||
local bgColor,fgColor, transparency = colors.black, colors.white, false
|
||||
local parent
|
||||
|
||||
local preDrawQueue = {}
|
||||
local drawQueue = {}
|
||||
local postDrawQueue = {}
|
||||
|
||||
local renderObject = {}
|
||||
|
||||
local function split(str, d)
|
||||
local result = {}
|
||||
if str == "" then
|
||||
return result
|
||||
end
|
||||
d = d or " "
|
||||
local start = 1
|
||||
local delim_start, delim_end = find(str, d, start)
|
||||
while delim_start do
|
||||
insert(result, {x=start, value=sub(str, start, delim_start - 1)})
|
||||
start = delim_end + 1
|
||||
delim_start, delim_end = find(str, d, start)
|
||||
end
|
||||
insert(result, {x=start, value=sub(str, start)})
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
local object = {
|
||||
getType = function(self)
|
||||
return objectType
|
||||
end,
|
||||
|
||||
getBase = function(self)
|
||||
return base
|
||||
end,
|
||||
|
||||
isType = function(self, t)
|
||||
return objectType==t or base.isType~=nil and base.isType(t) or false
|
||||
end,
|
||||
|
||||
getBasalt = function(self)
|
||||
return basalt
|
||||
end,
|
||||
|
||||
show = function(self)
|
||||
isVisible = true
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
hide = function(self)
|
||||
isVisible = false
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
isVisible = function(self)
|
||||
return isVisible
|
||||
end,
|
||||
|
||||
setVisible = function(self, _isVisible)
|
||||
isVisible = _isVisible or not isVisible
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setTransparency = function(self, _transparency)
|
||||
transparency = _transparency~= nil and _transparency or true
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setParent = function(self, newParent, noRemove)
|
||||
base.setParent(self, newParent, noRemove)
|
||||
parent = newParent
|
||||
return self
|
||||
end,
|
||||
|
||||
setFocus = function(self)
|
||||
if (parent ~= nil) then
|
||||
parent:setFocusedChild(self)
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
setZIndex = function(self, index)
|
||||
zIndex = index
|
||||
if (parent ~= nil) then
|
||||
parent:updateZIndex(self, zIndex)
|
||||
self:updateDraw()
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
getZIndex = function(self)
|
||||
return zIndex
|
||||
end,
|
||||
|
||||
updateDraw = function(self)
|
||||
if (parent ~= nil) then
|
||||
parent:updateDraw()
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
setPosition = function(self, xPos, yPos, rel)
|
||||
local curX, curY = x, y
|
||||
if(type(xPos)=="number")then
|
||||
x = rel and x+xPos or xPos
|
||||
end
|
||||
if(type(yPos)=="number")then
|
||||
y = rel and y+yPos or yPos
|
||||
end
|
||||
if(parent~=nil)then parent:customEventHandler("basalt_FrameReposition", self) end
|
||||
if(self:getType()=="Container")then parent:customEventHandler("basalt_FrameReposition", self) end
|
||||
self:updateDraw()
|
||||
self:repositionHandler(curX, curY)
|
||||
return self
|
||||
end,
|
||||
|
||||
getX = function(self)
|
||||
return x
|
||||
end,
|
||||
|
||||
setX = function(self, newX)
|
||||
return self:setPosition(newX, y)
|
||||
end,
|
||||
|
||||
getY = function(self)
|
||||
return y
|
||||
end,
|
||||
|
||||
setY = function(self, newY)
|
||||
return self:setPosition(x, newY)
|
||||
end,
|
||||
|
||||
getPosition = function(self)
|
||||
return x, y
|
||||
end,
|
||||
|
||||
setSize = function(self, newWidth, newHeight, rel)
|
||||
local oldW, oldH = width, height
|
||||
if(type(newWidth)=="number")then
|
||||
width = rel and width+newWidth or newWidth
|
||||
end
|
||||
if(type(newHeight)=="number")then
|
||||
height = rel and height+newHeight or newHeight
|
||||
end
|
||||
if(parent~=nil)then
|
||||
parent:customEventHandler("basalt_FrameResize", self)
|
||||
if(self:getType()=="Container")then parent:customEventHandler("basalt_FrameResize", self) end
|
||||
end
|
||||
self:resizeHandler(oldW, oldH)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getHeight = function(self)
|
||||
return height
|
||||
end,
|
||||
|
||||
setHeight = function(self, newHeight)
|
||||
return self:setSize(width, newHeight)
|
||||
end,
|
||||
|
||||
getWidth = function(self)
|
||||
return width
|
||||
end,
|
||||
|
||||
setWidth = function(self, newWidth)
|
||||
return self:setSize(newWidth, height)
|
||||
end,
|
||||
|
||||
getSize = function(self)
|
||||
return width, height
|
||||
end,
|
||||
|
||||
setBackground = function(self, color)
|
||||
bgColor = color
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getBackground = function(self)
|
||||
return bgColor
|
||||
end,
|
||||
|
||||
setForeground = function(self, color)
|
||||
fgColor = color or false
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getForeground = function(self)
|
||||
return fgColor
|
||||
end,
|
||||
|
||||
getAbsolutePosition = function(self, x, y)
|
||||
-- relative position to absolute position
|
||||
if (x == nil) or (y == nil) then
|
||||
x, y = self:getPosition()
|
||||
end
|
||||
|
||||
if (parent ~= nil) then
|
||||
local fx, fy = parent:getAbsolutePosition()
|
||||
x = fx + x - 1
|
||||
y = fy + y - 1
|
||||
end
|
||||
return x, y
|
||||
end,
|
||||
|
||||
ignoreOffset = function(self, ignore)
|
||||
ignOffset = ignore
|
||||
if(ignore==nil)then ignOffset = true end
|
||||
return self
|
||||
end,
|
||||
|
||||
getIgnoreOffset = function(self)
|
||||
return ignOffset
|
||||
end,
|
||||
|
||||
isCoordsInObject = function(self, x, y)
|
||||
if(isVisible)and(self:isEnabled())then
|
||||
if(x==nil)or(y==nil)then return false end
|
||||
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
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
|
||||
onGetFocus = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("get_focus", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onLoseFocus = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("lose_focus", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
isFocused = function(self)
|
||||
if (parent ~= nil) then
|
||||
return parent:getFocused() == self
|
||||
end
|
||||
return true
|
||||
end,
|
||||
|
||||
resizeHandler = function(self, ...)
|
||||
if(self:isEnabled())then
|
||||
local val = self:sendEvent("basalt_resize", ...)
|
||||
if(val==false)then return false end
|
||||
end
|
||||
return true
|
||||
end,
|
||||
|
||||
repositionHandler = function(self, ...)
|
||||
if(self:isEnabled())then
|
||||
local val = self:sendEvent("basalt_reposition", ...)
|
||||
if(val==false)then return false end
|
||||
end
|
||||
return true
|
||||
end,
|
||||
|
||||
onResize = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("basalt_resize", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
onReposition = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("basalt_reposition", v)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
mouseHandler = function(self, button, x, y, isMon)
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local val = self:sendEvent("mouse_click", button, x - (objX-1), y - (objY-1), x, y, isMon)
|
||||
if(val==false)then return false end
|
||||
if(parent~=nil)then
|
||||
parent:setFocusedChild(self)
|
||||
end
|
||||
isClicked = true
|
||||
isDragging = true
|
||||
dragStartX, dragStartY = x, y
|
||||
return true
|
||||
end
|
||||
end,
|
||||
|
||||
mouseUpHandler = function(self, button, x, y)
|
||||
isDragging = false
|
||||
if(isClicked)then
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local val = self:sendEvent("mouse_release", button, x - (objX-1), y - (objY-1), x, y)
|
||||
isClicked = false
|
||||
end
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local val = self:sendEvent("mouse_up", button, x - (objX-1), y - (objY-1), x, y)
|
||||
if(val==false)then return false end
|
||||
return true
|
||||
end
|
||||
end,
|
||||
|
||||
dragHandler = function(self, button, x, y)
|
||||
if(isDragging)then
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local val = self:sendEvent("mouse_drag", button, x - (objX-1), y - (objY-1), dragStartX-x, dragStartY-y, x, y)
|
||||
dragStartX, dragStartY = x, y
|
||||
if(val==false)then return false end
|
||||
if(parent~=nil)then
|
||||
parent:setFocusedChild(self)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
dragStartX, dragStartY = x, y
|
||||
dragXOffset, dragYOffset = objX - x, objY - y
|
||||
end
|
||||
end,
|
||||
|
||||
scrollHandler = function(self, dir, x, y)
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
local objX, objY = self:getAbsolutePosition()
|
||||
local val = self:sendEvent("mouse_scroll", dir, x - (objX-1), y - (objY-1))
|
||||
if(val==false)then return false end
|
||||
if(parent~=nil)then
|
||||
parent:setFocusedChild(self)
|
||||
end
|
||||
return true
|
||||
end
|
||||
end,
|
||||
|
||||
hoverHandler = function(self, x, y, stopped)
|
||||
if(self:isCoordsInObject(x, y))then
|
||||
local val = self:sendEvent("mouse_hover", x, y, stopped)
|
||||
if(val==false)then return false end
|
||||
isHovered = true
|
||||
return true
|
||||
end
|
||||
if(isHovered)then
|
||||
local val = self:sendEvent("mouse_leave", x, y, stopped)
|
||||
if(val==false)then return false end
|
||||
isHovered = false
|
||||
end
|
||||
end,
|
||||
|
||||
keyHandler = function(self, key, isHolding)
|
||||
if(self:isEnabled())and(isVisible)then
|
||||
if (self:isFocused()) then
|
||||
local val = self:sendEvent("key", key, isHolding)
|
||||
if(val==false)then return false end
|
||||
return true
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
keyUpHandler = function(self, key)
|
||||
if(self:isEnabled())and(isVisible)then
|
||||
if (self:isFocused()) then
|
||||
local val = self:sendEvent("key_up", key)
|
||||
if(val==false)then return false end
|
||||
return true
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
charHandler = function(self, char)
|
||||
if(self:isEnabled())and(isVisible)then
|
||||
if(self:isFocused())then
|
||||
local val = self:sendEvent("char", char)
|
||||
if(val==false)then return false end
|
||||
return true
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
getFocusHandler = function(self)
|
||||
local val = self:sendEvent("get_focus")
|
||||
if(val~=nil)then return val end
|
||||
return true
|
||||
end,
|
||||
|
||||
loseFocusHandler = function(self)
|
||||
isDragging = false
|
||||
local val = self:sendEvent("lose_focus")
|
||||
if(val~=nil)then return val end
|
||||
return true
|
||||
end,
|
||||
|
||||
addDraw = function(self, name, f, pos, typ, active)
|
||||
local queue = (typ==nil or typ==1) and drawQueue or typ==2 and preDrawQueue or typ==3 and postDrawQueue
|
||||
pos = pos or #queue+1
|
||||
if(name~=nil)then
|
||||
for k,v in pairs(queue)do
|
||||
if(v.name==name)then
|
||||
table.remove(queue, k)
|
||||
break
|
||||
end
|
||||
end
|
||||
local t = {name=name, f=f, pos=pos, active=active~=nil and active or true}
|
||||
table.insert(queue, pos, t)
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
addPreDraw = function(self, name, f, pos, typ)
|
||||
self:addDraw(name, f, pos, 2)
|
||||
return self
|
||||
end,
|
||||
|
||||
addPostDraw = function(self, name, f, pos, typ)
|
||||
self:addDraw(name, f, pos, 3)
|
||||
return self
|
||||
end,
|
||||
|
||||
setDrawState = function(self, name, state, typ)
|
||||
local queue = (typ==nil or typ==1) and drawQueue or typ==2 and preDrawQueue or typ==3 and postDrawQueue
|
||||
for k,v in pairs(queue)do
|
||||
if(v.name==name)then
|
||||
v.active = state
|
||||
break
|
||||
end
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getDrawId = function(self, name, typ)
|
||||
local queue = typ==1 and drawQueue or typ==2 and preDrawQueue or typ==3 and postDrawQueue or drawQueue
|
||||
for k,v in pairs(queue)do
|
||||
if(v.name==name)then
|
||||
return k
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
addText = function(self, x, y, text)
|
||||
local obj = self:getParent() or self
|
||||
local xPos,yPos = self:getPosition()
|
||||
if(parent~=nil)then
|
||||
local xO, yO = parent:getOffset()
|
||||
xPos = ignOffset and xPos or xPos - xO
|
||||
yPos = ignOffset and yPos or yPos - yO
|
||||
end
|
||||
if not(transparency)then
|
||||
obj:setText(x+xPos-1, y+yPos-1, text)
|
||||
return
|
||||
end
|
||||
local t = split(text, "\0")
|
||||
for k,v in pairs(t)do
|
||||
if(v.value~="")and(v.value~="\0")then
|
||||
obj:setText(x+v.x+xPos-2, y+yPos-1, v.value)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
addBG = function(self, x, y, bg, noText)
|
||||
local obj = parent or self
|
||||
local xPos,yPos = self:getPosition()
|
||||
if(parent~=nil)then
|
||||
local xO, yO = parent:getOffset()
|
||||
xPos = ignOffset and xPos or xPos - xO
|
||||
yPos = ignOffset and yPos or yPos - yO
|
||||
end
|
||||
if not(transparency)then
|
||||
obj:setBG(x+xPos-1, y+yPos-1, bg)
|
||||
return
|
||||
end
|
||||
local t = split(bg)
|
||||
for k,v in pairs(t)do
|
||||
if(v.value~="")and(v.value~=" ")then
|
||||
if(noText~=true)then
|
||||
obj:setText(x+v.x+xPos-2, y+yPos-1, (" "):rep(#v.value))
|
||||
obj:setBG(x+v.x+xPos-2, y+yPos-1, v.value)
|
||||
else
|
||||
table.insert(renderObject, {x=x+v.x-1,y=y,bg=v.value})
|
||||
obj:setBG(x+xPos-1, y+yPos-1, fg)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
addFG = function(self, x, y, fg)
|
||||
local obj = parent or self
|
||||
local xPos,yPos = self:getPosition()
|
||||
if(parent~=nil)then
|
||||
local xO, yO = parent:getOffset()
|
||||
xPos = ignOffset and xPos or xPos - xO
|
||||
yPos = ignOffset and yPos or yPos - yO
|
||||
end
|
||||
if not(transparency)then
|
||||
obj:setFG(x+xPos-1, y+yPos-1, fg)
|
||||
return
|
||||
end
|
||||
local t = split(fg)
|
||||
for k,v in pairs(t)do
|
||||
if(v.value~="")and(v.value~=" ")then
|
||||
obj:setFG(x+v.x+xPos-2, y+yPos-1, v.value)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
addBlit = function(self, x, y, t, fg, bg)
|
||||
local obj = parent or self
|
||||
local xPos,yPos = self:getPosition()
|
||||
if(parent~=nil)then
|
||||
local xO, yO = parent:getOffset()
|
||||
xPos = ignOffset and xPos or xPos - xO
|
||||
yPos = ignOffset and yPos or yPos - yO
|
||||
end
|
||||
if not(transparency)then
|
||||
obj:blit(x+xPos-1, y+yPos-1, t, fg, bg)
|
||||
return
|
||||
end
|
||||
local _text = split(t, "\0")
|
||||
local _fg = split(fg)
|
||||
local _bg = split(bg)
|
||||
for k,v in pairs(_text)do
|
||||
if(v.value~="")or(v.value~="\0")then
|
||||
obj:setText(x+v.x+xPos-2, y+yPos-1, v.value)
|
||||
end
|
||||
end
|
||||
for k,v in pairs(_bg)do
|
||||
if(v.value~="")or(v.value~=" ")then
|
||||
obj:setBG(x+v.x+xPos-2, y+yPos-1, v.value)
|
||||
end
|
||||
end
|
||||
for k,v in pairs(_fg)do
|
||||
if(v.value~="")or(v.value~=" ")then
|
||||
obj:setFG(x+v.x+xPos-2, y+yPos-1, v.value)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
addTextBox = function(self, x, y, w, h, text)
|
||||
local obj = parent or self
|
||||
local xPos,yPos = self:getPosition()
|
||||
if(parent~=nil)then
|
||||
local xO, yO = parent:getOffset()
|
||||
xPos = ignOffset and xPos or xPos - xO
|
||||
yPos = ignOffset and yPos or yPos - yO
|
||||
end
|
||||
obj:drawTextBox(x+xPos-1, y+yPos-1, w, h, text)
|
||||
end,
|
||||
|
||||
addForegroundBox = function(self, x, y, w, h, col)
|
||||
local obj = parent or self
|
||||
local xPos,yPos = self:getPosition()
|
||||
if(parent~=nil)then
|
||||
local xO, yO = parent:getOffset()
|
||||
xPos = ignOffset and xPos or xPos - xO
|
||||
yPos = ignOffset and yPos or yPos - yO
|
||||
end
|
||||
obj:drawForegroundBox(x+xPos-1, y+yPos-1, w, h, col)
|
||||
end,
|
||||
|
||||
addBackgroundBox = function(self, x, y, w, h, col)
|
||||
local obj = parent or self
|
||||
local xPos,yPos = self:getPosition()
|
||||
if(parent~=nil)then
|
||||
local xO, yO = parent:getOffset()
|
||||
xPos = ignOffset and xPos or xPos - xO
|
||||
yPos = ignOffset and yPos or yPos - yO
|
||||
end
|
||||
obj:drawBackgroundBox(x+xPos-1, y+yPos-1, w, h, col)
|
||||
end,
|
||||
|
||||
render = function(self)
|
||||
if (isVisible)then
|
||||
self:redraw()
|
||||
end
|
||||
end,
|
||||
|
||||
redraw = function(self)
|
||||
for k,v in pairs(preDrawQueue)do
|
||||
if (v.active)then
|
||||
v.f(self)
|
||||
end
|
||||
end
|
||||
for k,v in pairs(drawQueue)do
|
||||
if (v.active)then
|
||||
v.f(self)
|
||||
end
|
||||
end
|
||||
for k,v in pairs(postDrawQueue)do
|
||||
if (v.active)then
|
||||
v.f(self)
|
||||
end
|
||||
end
|
||||
return true
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
self:addDraw("base", function()
|
||||
local w,h = self:getSize()
|
||||
if(bgColor~=false)then
|
||||
self:addTextBox(1, 1, w, h, " ")
|
||||
self:addBackgroundBox(1, 1, w, h, bgColor)
|
||||
end
|
||||
if(fgColor~=false)then
|
||||
self:addForegroundBox(1, 1, w, h, fgColor)
|
||||
end
|
||||
end, 1)
|
||||
end,
|
||||
}
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
112
Basalt/plugin.lua
Normal file
@@ -0,0 +1,112 @@
|
||||
local args = {...}
|
||||
local plugins = {}
|
||||
local pluginNames = {}
|
||||
|
||||
local dir = fs.getDir(args[2] or "Basalt")
|
||||
local pluginDir = fs.combine(dir, "plugins")
|
||||
if(packaged)then
|
||||
for k,v in pairs(getProject("plugins"))do
|
||||
table.insert(pluginNames, k)
|
||||
local newPlugin = v()
|
||||
if(type(newPlugin)=="table")then
|
||||
for a,b in pairs(newPlugin)do
|
||||
if(type(a)=="string")then
|
||||
if(plugins[a]==nil)then plugins[a] = {} end
|
||||
table.insert(plugins[a], b)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
if(fs.exists(pluginDir))then
|
||||
for _,v in pairs(fs.list(pluginDir))do
|
||||
local newPlugin
|
||||
if(fs.isDir(fs.combine(pluginDir, v)))then
|
||||
table.insert(pluginNames, fs.combine(pluginDir, v))
|
||||
newPlugin = require(v.."/init")
|
||||
else
|
||||
table.insert(pluginNames, v)
|
||||
newPlugin = require(v:gsub(".lua", ""))
|
||||
end
|
||||
if(type(newPlugin)=="table")then
|
||||
for a,b in pairs(newPlugin)do
|
||||
if(type(a)=="string")then
|
||||
if(plugins[a]==nil)then plugins[a] = {} end
|
||||
table.insert(plugins[a], b)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function get(name)
|
||||
return plugins[name]
|
||||
end
|
||||
|
||||
return {
|
||||
--- Gets a plugin list
|
||||
--- @param name string name of plugin list
|
||||
--- @return table plugins
|
||||
get = get,
|
||||
getAvailablePlugins = function()
|
||||
return pluginNames
|
||||
end,
|
||||
|
||||
--- Adds a plugin to basalt's plugin list
|
||||
--- @param path string path to plugin
|
||||
addPlugin = function(path)
|
||||
if(fs.exists(path))then
|
||||
if(fs.isDir(path))then
|
||||
for _,v in pairs(fs.list(path))do
|
||||
table.insert(pluginNames, v)
|
||||
if not(fs.isDir(fs.combine(path, v)))then
|
||||
local pluginName = v:gsub(".lua", "")
|
||||
local newPlugin = require(fs.combine(path, pluginName))
|
||||
if(type(newPlugin)=="table")then
|
||||
for a,b in pairs(newPlugin)do
|
||||
if(type(a)=="string")then
|
||||
if(plugins[a]==nil)then plugins[a] = {} end
|
||||
table.insert(plugins[a], b)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
local newPlugin = require(path:gsub(".lua", ""))
|
||||
table.insert(pluginNames, path:match("[\\/]?([^\\/]-([^%.]+))$"))
|
||||
if(type(newPlugin)=="table")then
|
||||
for a,b in pairs(newPlugin)do
|
||||
if(type(a)=="string")then
|
||||
if(plugins[a]==nil)then plugins[a] = {} end
|
||||
table.insert(plugins[a], b)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
--- Loads all available plugins into basalt's objects
|
||||
--- @param objects table objects to load plugins into
|
||||
--- @param basalt table basalt
|
||||
--- @return table objects modified objects
|
||||
loadPlugins = function(objects, basalt)
|
||||
for k,v in pairs(objects)do
|
||||
local plugList = plugins[k]
|
||||
if(plugList~=nil)then
|
||||
objects[k] = function(...)
|
||||
local moddedObject = v(...)
|
||||
for _,b in pairs(plugList)do
|
||||
local ext = b(moddedObject, basalt, ...)
|
||||
ext.__index = ext
|
||||
moddedObject = setmetatable(ext, moddedObject)
|
||||
end
|
||||
return moddedObject
|
||||
end
|
||||
end
|
||||
end
|
||||
return objects
|
||||
end
|
||||
}
|
||||
47
Basalt/plugins/advancedBackground.lua
Normal file
@@ -0,0 +1,47 @@
|
||||
local XMLParser = require("xmlParser")
|
||||
|
||||
return {
|
||||
VisualObject = function(base)
|
||||
local bgSymbol = false
|
||||
local bgSymbolColor = colors.black
|
||||
|
||||
local object = {
|
||||
setBackground = function(self, bg, symbol, symbolCol)
|
||||
base.setBackground(self, bg)
|
||||
bgSymbol = symbol or bgSymbol
|
||||
bgSymbolColor = symbolCol or bgSymbolColor
|
||||
return self
|
||||
end,
|
||||
|
||||
setBackgroundSymbol = function(self, symbol, symbolCol)
|
||||
bgSymbol = symbol
|
||||
bgSymbolColor = symbolCol or bgSymbolColor
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getBackgroundSymbol = function(self)
|
||||
return bgSymbol
|
||||
end,
|
||||
|
||||
getBackgroundSymbolColor = function(self)
|
||||
return bgSymbolColor
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
base.draw(self)
|
||||
self:addDraw("advanced-bg", function()
|
||||
local w, h = self:getSize()
|
||||
if(bgSymbol~=false)then
|
||||
self:addTextBox(1, 1, w, h, bgSymbol:sub(1,1))
|
||||
if(bgSymbol~=" ")then
|
||||
self:addForegroundBox(1, 1, w, h, bgSymbolColor)
|
||||
end
|
||||
end
|
||||
end, 2)
|
||||
end
|
||||
}
|
||||
|
||||
return object
|
||||
end
|
||||
}
|
||||
344
Basalt/plugins/animations.lua
Normal file
@@ -0,0 +1,344 @@
|
||||
local floor,sin,cos,pi,sqrt,pow = math.floor,math.sin,math.cos,math.pi,math.sqrt,math.pow
|
||||
|
||||
-- You can find the easing curves here https://easings.net
|
||||
|
||||
local function lerp(s, e, pct)
|
||||
return s + (e - s) * pct
|
||||
end
|
||||
|
||||
local function linear(t)
|
||||
return t
|
||||
end
|
||||
|
||||
local function flip(t)
|
||||
return 1 - t
|
||||
end
|
||||
|
||||
local function easeIn(t)
|
||||
return t * t * t
|
||||
end
|
||||
|
||||
local function easeOut(t)
|
||||
return flip(easeIn(flip(t)))
|
||||
end
|
||||
|
||||
local function easeInOut(t)
|
||||
return lerp(easeIn(t), easeOut(t), t)
|
||||
end
|
||||
|
||||
local function easeOutSine(t)
|
||||
return sin((t * pi) / 2);
|
||||
end
|
||||
|
||||
local function easeInSine(t)
|
||||
return flip(cos((t * pi) / 2))
|
||||
end
|
||||
|
||||
local function easeInOutSine(t)
|
||||
return -(cos(pi * x) - 1) / 2
|
||||
end
|
||||
|
||||
local function easeInBack(t)
|
||||
local c1 = 1.70158;
|
||||
local c3 = c1 + 1
|
||||
return c3*t^3-c1*t^2
|
||||
end
|
||||
|
||||
local function easeInCubic(t)
|
||||
return t^3
|
||||
end
|
||||
|
||||
local function easeInElastic(t)
|
||||
local c4 = (2*pi)/3;
|
||||
return t == 0 and 0 or (t == 1 and 1 or (
|
||||
-2^(10*t-10)*sin((t*10-10.75)*c4)
|
||||
))
|
||||
end
|
||||
|
||||
local function easeInExpo(t)
|
||||
return t == 0 and 0 or 2^(10*t-10)
|
||||
end
|
||||
|
||||
local function easeInExpo(t)
|
||||
return t == 0 and 0 or 2^(10*t-10)
|
||||
end
|
||||
|
||||
local function easeInOutBack(t)
|
||||
local c1 = 1.70158;
|
||||
local c2 = c1 * 1.525;
|
||||
return t < 0.5 and ((2*t)^2*((c2+1)*2*t-c2))/2 or ((2*t-2)^2*((c2+1)*(t*2-2)+c2)+2)/2
|
||||
end
|
||||
|
||||
local function easeInOutCubic(t)
|
||||
return t < 0.5 and 4 * t^3 or 1-(-2*t+2)^3 / 2
|
||||
end
|
||||
|
||||
local function easeInOutElastic(t)
|
||||
local c5 = (2*pi) / 4.5
|
||||
return t==0 and 0 or (t == 1 and 1 or (t < 0.5 and -(2^(20*t-10) * sin((20*t - 11.125) * c5))/2 or (2^(-20*t+10) * sin((20*t - 11.125) * c5))/2 + 1))
|
||||
end
|
||||
|
||||
local function easeInOutExpo(t)
|
||||
return t == 0 and 0 or (t == 1 and 1 or (t < 0.5 and 2^(20*t-10)/2 or (2-2^(-20*t+10)) /2))
|
||||
end
|
||||
|
||||
local function easeInOutQuad(t)
|
||||
return t < 0.5 and 2*t^2 or 1-(-2*t+2)^2/2
|
||||
end
|
||||
|
||||
local function easeInOutQuart(t)
|
||||
return t < 0.5 and 8*t^4 or 1 - (-2*t+2)^4 / 2
|
||||
end
|
||||
|
||||
local function easeInOutQuint(t)
|
||||
return t < 0.5 and 16*t^5 or 1-(-2*t+2)^5 / 2
|
||||
end
|
||||
|
||||
local function easeInQuad(t)
|
||||
return t^2
|
||||
end
|
||||
|
||||
local function easeInQuart(t)
|
||||
return t^4
|
||||
end
|
||||
|
||||
local function easeInQuint(t)
|
||||
return t^5
|
||||
end
|
||||
|
||||
local function easeOutBack(t)
|
||||
local c1 = 1.70158;
|
||||
local c3 = c1 + 1
|
||||
return 1+c3*(t-1)^3+c1*(t-1)^2
|
||||
end
|
||||
|
||||
local function easeOutCubic(t)
|
||||
return 1 - (1-t)^3
|
||||
end
|
||||
|
||||
local function easeOutElastic(t)
|
||||
local c4 = (2*pi)/3;
|
||||
|
||||
return t == 0 and 0 or (t == 1 and 1 or (2^(-10*t)*sin((t*10-0.75)*c4)+1))
|
||||
end
|
||||
|
||||
local function easeOutExpo(t)
|
||||
return t == 1 and 1 or 1-2^(-10*t)
|
||||
end
|
||||
|
||||
local function easeOutQuad(t)
|
||||
return 1 - (1 - t) * (1 - t)
|
||||
end
|
||||
|
||||
local function easeOutQuart(t)
|
||||
return 1 - (1-t)^4
|
||||
end
|
||||
|
||||
local function easeOutQuint(t)
|
||||
return 1 - (1 - t)^5
|
||||
end
|
||||
|
||||
local function easeInCirc(t)
|
||||
return 1 - sqrt(1 - pow(t, 2))
|
||||
end
|
||||
|
||||
local function easeOutCirc(t)
|
||||
return sqrt(1 - pow(t - 1, 2))
|
||||
end
|
||||
|
||||
local function easeInOutCirc(t)
|
||||
return t < 0.5 and (1 - sqrt(1 - pow(2 * t, 2))) / 2 or (sqrt(1 - pow(-2 * t + 2, 2)) + 1) / 2;
|
||||
end
|
||||
|
||||
local function easeOutBounce(t)
|
||||
local n1 = 7.5625;
|
||||
local d1 = 2.75;
|
||||
|
||||
if (t < 1 / d1)then
|
||||
return n1 * t * t
|
||||
elseif (t < 2 / d1)then
|
||||
local a = t - 1.5 / d1
|
||||
return n1 * a * a + 0.75;
|
||||
elseif (t < 2.5 / d1)then
|
||||
local a = t - 2.25 / d1
|
||||
return n1 * a * a + 0.9375;
|
||||
else
|
||||
local a = t - 2.625 / d1
|
||||
return n1 * a * a + 0.984375;
|
||||
end
|
||||
end
|
||||
|
||||
local function easeInBounce(t)
|
||||
return 1 - easeOutBounce(1 - t)
|
||||
end
|
||||
|
||||
local function easeInOutBounce(t)
|
||||
return x < 0.5 and (1 - easeOutBounce(1 - 2 * t)) / 2 or (1 + easeOutBounce(2 * t - 1)) / 2;
|
||||
end
|
||||
|
||||
local lerp = {
|
||||
linear = linear,
|
||||
lerp = lerp,
|
||||
flip=flip,
|
||||
easeIn=easeIn,
|
||||
easeInSine = easeInSine,
|
||||
easeInBack=easeInBack,
|
||||
easeInCubic=easeInCubic,
|
||||
easeInElastic=easeInElastic,
|
||||
easeInExpo=easeInExpo,
|
||||
easeInQuad=easeInQuad,
|
||||
easeInQuart=easeInQuart,
|
||||
easeInQuint=easeInQuint,
|
||||
easeInCirc=easeInCirc,
|
||||
easeInBounce=easeInBounce,
|
||||
easeOut=easeOut,
|
||||
easeOutSine = easeOutSine,
|
||||
easeOutBack=easeOutBack,
|
||||
easeOutCubic=easeOutCubic,
|
||||
easeOutElastic=easeOutElastic,
|
||||
easeOutExpo=easeOutExpo,
|
||||
easeOutQuad=easeOutQuad,
|
||||
easeOutQuart=easeOutQuart,
|
||||
easeOutQuint=easeOutQuint,
|
||||
easeOutCirc=easeOutCirc,
|
||||
easeOutBounce=easeOutBounce,
|
||||
easeInOut=easeInOut,
|
||||
easeInOutSine = easeInOutSine,
|
||||
easeInOutBack=easeInOutBack,
|
||||
easeInOutCubic=easeInOutCubic,
|
||||
easeInOutElastic=easeInOutElastic,
|
||||
easeInOutExpo=easeInOutExpo,
|
||||
easeInOutQuad=easeInOutQuad,
|
||||
easeInOutQuart=easeInOutQuart,
|
||||
easeInOutQuint=easeInOutQuint,
|
||||
easeInOutCirc=easeInOutCirc,
|
||||
easeInOutBounce=easeInOutBounce,
|
||||
}
|
||||
|
||||
local XMLParser = require("xmlParser")
|
||||
|
||||
return {
|
||||
VisualObject = function(base, basalt)
|
||||
local activeAnimations = {}
|
||||
local defaultMode = "linear"
|
||||
|
||||
local function getAnimation(self, timerId)
|
||||
for k,v in pairs(activeAnimations)do
|
||||
if(v.timerId==timerId)then
|
||||
return v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function createAnimation(self, v1, v2, duration, timeOffset, mode, typ, f, get, set)
|
||||
local v1Val, v2Val = get(self)
|
||||
if(activeAnimations[typ]~=nil)then
|
||||
os.cancelTimer(activeAnimations[typ].timerId)
|
||||
end
|
||||
activeAnimations[typ] = {}
|
||||
activeAnimations[typ].call = function()
|
||||
local progress = activeAnimations[typ].progress
|
||||
local _v1 = math.floor(lerp.lerp(v1Val, v1, lerp[mode](progress / duration))+0.5)
|
||||
local _v2 = math.floor(lerp.lerp(v2Val, v2, lerp[mode](progress / duration))+0.5)
|
||||
set(self, _v1, _v2)
|
||||
end
|
||||
activeAnimations[typ].finished = function()
|
||||
set(self, v1, v2)
|
||||
if(f~=nil)then f(self) end
|
||||
end
|
||||
|
||||
activeAnimations[typ].timerId=os.startTimer(0.05+timeOffset)
|
||||
activeAnimations[typ].progress=0
|
||||
activeAnimations[typ].duration=duration
|
||||
activeAnimations[typ].mode=mode
|
||||
self:listenEvent("other_event")
|
||||
end
|
||||
|
||||
local function createColorAnimation(self, duration, timeOffset, typ, set, ...)
|
||||
local newColors = {...}
|
||||
if(activeAnimations[typ]~=nil)then
|
||||
os.cancelTimer(activeAnimations[typ].timerId)
|
||||
end
|
||||
activeAnimations[typ] = {}
|
||||
local colorIndex = 1
|
||||
activeAnimations[typ].call = function()
|
||||
local color = newColors[colorIndex]
|
||||
set(self, color)
|
||||
end
|
||||
end
|
||||
|
||||
local object = {
|
||||
animatePosition = function(self, x, y, duration, timeOffset, mode, f)
|
||||
mode = mode or defaultMode
|
||||
duration = duration or 1
|
||||
timeOffset = timeOffset or 0
|
||||
x = math.floor(x+0.5)
|
||||
y = math.floor(y+0.5)
|
||||
createAnimation(self, x, y, duration, timeOffset, mode, "position", f, self.getPosition, self.setPosition)
|
||||
return self
|
||||
end,
|
||||
|
||||
animateSize = function(self, w, h, duration, timeOffset, mode, f)
|
||||
mode = mode or defaultMode
|
||||
duration = duration or 1
|
||||
timeOffset = timeOffset or 0
|
||||
createAnimation(self, w, h, duration, timeOffset, mode, "size", f, self.getSize, self.setSize)
|
||||
return self
|
||||
end,
|
||||
|
||||
animateOffset = function(self, x, y, duration, timeOffset, mode, f)
|
||||
mode = mode or defaultMode
|
||||
duration = duration or 1
|
||||
timeOffset = timeOffset or 0
|
||||
createAnimation(self, x, y, duration, timeOffset, mode, "offset", f, self.getOffset, self.setOffset)
|
||||
return self
|
||||
end,
|
||||
|
||||
animateBackground = function(self, color, duration, timeOffset, mode, f)
|
||||
mode = mode or defaultMode
|
||||
duration = duration or 1
|
||||
timeOffset = timeOffset or 0
|
||||
createColorAnimation(self, color, nil, duration, timeOffset, mode, "background", f, self.getBackground, self.setBackground)
|
||||
return self
|
||||
end,
|
||||
|
||||
doneHandler = function(self, timerId, ...)
|
||||
for k,v in pairs(activeAnimations)do
|
||||
if(v.timerId==timerId)then
|
||||
activeAnimations[k] = nil
|
||||
self:sendEvent("animation_done", self, "animation_done", k)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
onAnimationDone = function(self, ...)
|
||||
for _,v in pairs(table.pack(...))do
|
||||
if(type(v)=="function")then
|
||||
self:registerEvent("animation_done", v)
|
||||
end
|
||||
end
|
||||
|
||||
return self
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, timerId, ...)
|
||||
base.eventHandler(self, event, timerId, ...)
|
||||
if(event=="timer")then
|
||||
local animation = getAnimation(self, timerId)
|
||||
if(animation~=nil)then
|
||||
if(animation.progress<animation.duration)then
|
||||
animation.call()
|
||||
animation.progress = animation.progress+0.05
|
||||
animation.timerId=os.startTimer(0.05)
|
||||
else
|
||||
animation.finished()
|
||||
self:doneHandler(timerId)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
return object
|
||||
end
|
||||
}
|
||||
10
Basalt/plugins/basaltAdditions.lua
Normal file
@@ -0,0 +1,10 @@
|
||||
return {
|
||||
basalt = function()
|
||||
return {
|
||||
cool = function()
|
||||
print("ello")
|
||||
sleep(2)
|
||||
end
|
||||
}
|
||||
end
|
||||
}
|
||||
87
Basalt/plugins/border.lua
Normal file
@@ -0,0 +1,87 @@
|
||||
local XMLParser = require("xmlParser")
|
||||
|
||||
return {
|
||||
VisualObject = function(base)
|
||||
local inline = true
|
||||
local borderColors = {top = false, bottom = false, left = false, right = false}
|
||||
|
||||
local object = {
|
||||
setBorder = function(self, ...)
|
||||
local t = {...}
|
||||
if(t~=nil)then
|
||||
for k,v in pairs(t)do
|
||||
if(v=="left")or(#t==1)then
|
||||
borderColors["left"] = t[1]
|
||||
end
|
||||
if(v=="top")or(#t==1)then
|
||||
borderColors["top"] = t[1]
|
||||
end
|
||||
if(v=="right")or(#t==1)then
|
||||
borderColors["right"] = t[1]
|
||||
end
|
||||
if(v=="bottom")or(#t==1)then
|
||||
borderColors["bottom"] = t[1]
|
||||
end
|
||||
end
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
base.draw(self)
|
||||
self:addDraw("border", function()
|
||||
local x, y = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
local bgCol = self:getBackground()
|
||||
if(inline)then
|
||||
if(borderColors["left"]~=false)then
|
||||
self:addTextBox(1, 1, 1, h, "\149")
|
||||
if(bgCol~=false)then self:addBackgroundBox(1, 1, 1, h, bgCol) end
|
||||
self:addForegroundBox(1, 1, 1, h, borderColors["left"])
|
||||
end
|
||||
|
||||
if(borderColors["top"]~=false)then
|
||||
self:addTextBox(1, 1, w, 1, "\131")
|
||||
if(bgCol~=false)then self:addBackgroundBox(1, 1, w, 1, bgCol) end
|
||||
self:addForegroundBox(1, 1, w, 1, borderColors["top"])
|
||||
end
|
||||
|
||||
if(borderColors["left"]~=false)and(borderColors["top"]~=false)then
|
||||
self:addTextBox(1, 1, 1, 1, "\151")
|
||||
if(bgCol~=false)then self:addBackgroundBox(1, 1, 1, 1, bgCol) end
|
||||
self:addForegroundBox(1, 1, 1, 1, borderColors["left"])
|
||||
end
|
||||
if(borderColors["right"]~=false)then
|
||||
self:addTextBox(w, 1, 1, h, "\149")
|
||||
if(bgCol~=false)then self:addForegroundBox(w, 1, 1, h, bgCol) end
|
||||
self:addBackgroundBox(w, 1, 1, h, borderColors["right"])
|
||||
end
|
||||
if(borderColors["bottom"]~=false)then
|
||||
self:addTextBox(1, h, w, 1, "\143")
|
||||
if(bgCol~=false)then self:addForegroundBox(1, h, w, 1, bgCol) end
|
||||
self:addBackgroundBox(1, h, w, 1, borderColors["bottom"])
|
||||
end
|
||||
if(borderColors["top"]~=false)and(borderColors["right"]~=false)then
|
||||
self:addTextBox(w, 1, 1, 1, "\148")
|
||||
if(bgCol~=false)then self:addForegroundBox(w, 1, 1, 1, bgCol) end
|
||||
self:addBackgroundBox(w, 1, 1, 1, borderColors["right"])
|
||||
end
|
||||
if(borderColors["right"]~=false)and(borderColors["bottom"]~=false)then
|
||||
self:addTextBox(w, h, 1, 1, "\133")
|
||||
if(bgCol~=false)then self:addForegroundBox(w, h, 1, 1, bgCol) end
|
||||
self:addBackgroundBox(w, h, 1, 1, borderColors["right"])
|
||||
end
|
||||
if(borderColors["bottom"]~=false)and(borderColors["left"]~=false)then
|
||||
self:addTextBox(1, h, 1, 1, "\138")
|
||||
if(bgCol~=false)then self:addForegroundBox(0, h, 1, 1, bgCol) end
|
||||
self:addBackgroundBox(1, h, 1, 1, borderColors["left"])
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
}
|
||||
|
||||
return object
|
||||
end
|
||||
}
|
||||
97
Basalt/plugins/debug.lua
Normal file
@@ -0,0 +1,97 @@
|
||||
local utils = require("utils")
|
||||
local wrapText = utils.wrapText
|
||||
|
||||
return {
|
||||
basalt = function(basalt)
|
||||
local mainFrame = basalt.getMainFrame()
|
||||
local debugFrame
|
||||
local debugList
|
||||
local debugLabel
|
||||
local debugExitButton
|
||||
|
||||
local function createDebuggingFrame()
|
||||
local minW = 16
|
||||
local minH = 6
|
||||
local maxW = 99
|
||||
local maxH = 99
|
||||
local w, h = mainFrame:getSize()
|
||||
debugFrame = mainFrame:addMovableFrame("basaltDebuggingFrame"):setSize(w-20, h-10):setBackground(colors.gray):setForeground(colors.white):setZIndex(100):hide()
|
||||
debugFrame:addPane():setSize("parent.w", 1):setPosition(1, 1):setBackground(colors.black):setForeground(colors.white)
|
||||
debugFrame:setPosition(-w, h/2-debugFrame:getHeight()/2):setBorder(colors.black)
|
||||
local resizeButton = debugFrame:addButton()
|
||||
:setPosition("parent.w", "parent.h")
|
||||
:setSize(1, 1)
|
||||
:setText("\133")
|
||||
:setForeground(colors.gray)
|
||||
:setBackground(colors.black)
|
||||
:onClick(function() end)
|
||||
:onDrag(function(self, event, btn, xOffset, yOffset)
|
||||
local w, h = debugFrame:getSize()
|
||||
local wOff, hOff = w, h
|
||||
if(w+xOffset-1>=minW)and(w+xOffset-1<=maxW)then
|
||||
wOff = w+xOffset-1
|
||||
end
|
||||
if(h+yOffset-1>=minH)and(h+yOffset-1<=maxH)then
|
||||
hOff = h+yOffset-1
|
||||
end
|
||||
debugFrame:setSize(wOff, hOff)
|
||||
end)
|
||||
|
||||
debugExitButton = debugFrame:addButton():setText("Close"):setPosition("parent.w - 6", 1):setSize(7, 1):setBackground(colors.red):setForeground(colors.white):onClick(function()
|
||||
debugFrame:animatePosition(-w, h/2-debugFrame:getHeight()/2, 0.5)
|
||||
end)
|
||||
debugList = debugFrame:addList()
|
||||
:setSize("parent.w - 2", "parent.h - 3")
|
||||
:setPosition(2, 3)
|
||||
:setBackground(colors.gray)
|
||||
:setForeground(colors.white)
|
||||
:setSelectionColor(colors.gray, colors.white)
|
||||
if(debugLabel==nil)then
|
||||
debugLabel = mainFrame:addLabel()
|
||||
:setPosition(1, "parent.h")
|
||||
:setBackground(colors.black)
|
||||
:setForeground(colors.white)
|
||||
:setZIndex(100)
|
||||
:onClick(function()
|
||||
debugFrame:show()
|
||||
debugFrame:animatePosition(w/2-debugFrame:getWidth()/2, h/2-debugFrame:getHeight()/2, 0.5)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
debug = function(...)
|
||||
local args = { ... }
|
||||
if(mainFrame==nil)then
|
||||
mainFrame = basalt.getMainFrame()
|
||||
if(mainFrame~=nil)then
|
||||
createDebuggingFrame()
|
||||
else
|
||||
print(...) return
|
||||
end
|
||||
end
|
||||
if (mainFrame:getName() ~= "basaltDebuggingFrame") then
|
||||
if (mainFrame ~= debugFrame) then
|
||||
debugLabel:setParent(mainFrame)
|
||||
end
|
||||
end
|
||||
local str = ""
|
||||
for key, value in pairs(args) do
|
||||
str = str .. tostring(value) .. (#args ~= key and ", " or "")
|
||||
end
|
||||
debugLabel:setText("[Debug] " .. str)
|
||||
for k,v in pairs(wrapText(str, debugList:getWidth()))do
|
||||
debugList:addItem(v)
|
||||
end
|
||||
if (debugList:getItemCount() > 50) then
|
||||
debugList:removeItem(1)
|
||||
end
|
||||
debugList:setValue(debugList:getItem(debugList:getItemCount()))
|
||||
if(debugList.getItemCount() > debugList:getHeight())then
|
||||
debugList:setOffset(debugList:getItemCount() - debugList:getHeight())
|
||||
end
|
||||
debugLabel:show()
|
||||
end
|
||||
}
|
||||
end
|
||||
}
|
||||
124
Basalt/plugins/dynamicValues.lua
Normal file
@@ -0,0 +1,124 @@
|
||||
local utils = require("utils")
|
||||
local count = utils.tableCount
|
||||
|
||||
return {
|
||||
VisualObject = function(base, basalt)
|
||||
local dynObjects = {}
|
||||
local curProperties = {}
|
||||
local properties = {x="getX", y="getY", w="getWidth", h="getHeight"}
|
||||
|
||||
local function stringToNumber(str)
|
||||
local ok, result = pcall(load("return " .. str, "", nil, {math=math}))
|
||||
if not(ok)then error(str.." - is not a valid dynamic value string") end
|
||||
return result
|
||||
end
|
||||
|
||||
local function createDynamicValue(self, key, val)
|
||||
local objectGroup = {}
|
||||
local properties = properties
|
||||
for a,b in pairs(properties)do
|
||||
for v in val:gmatch("%a+%."..a)do
|
||||
local name = v:gsub("%."..a, "")
|
||||
if(name~="self")and(name~="parent")then
|
||||
table.insert(objectGroup, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local parent = self:getParent()
|
||||
local objects = {}
|
||||
for k,v in pairs(objectGroup)do
|
||||
objects[v] = parent:getChild(v)
|
||||
if(objects[v]==nil)then
|
||||
error("Dynamic Values - unable to find object: "..v)
|
||||
end
|
||||
end
|
||||
objects["self"] = self
|
||||
objects["parent"] = parent
|
||||
|
||||
dynObjects[key] = function()
|
||||
local mainVal = val
|
||||
for a,b in pairs(properties)do
|
||||
for v in val:gmatch("%w+%."..a) do
|
||||
local obj = objects[v:gsub("%."..a, "")]
|
||||
if(obj~=nil)then
|
||||
mainVal = mainVal:gsub(v, obj[b](obj))
|
||||
else
|
||||
error("Dynamic Values - unable to find object: "..v)
|
||||
end
|
||||
end
|
||||
end
|
||||
curProperties[key] = math.floor(stringToNumber(mainVal)+0.5)
|
||||
end
|
||||
dynObjects[key]()
|
||||
end
|
||||
|
||||
local function updatePositions(self)
|
||||
if(count(dynObjects)>0)then
|
||||
for k,v in pairs(dynObjects)do
|
||||
v()
|
||||
end
|
||||
local properties = {x="getX", y="getY", w="getWidth", h="getHeight"}
|
||||
for k,v in pairs(properties)do
|
||||
if(dynObjects[k]~=nil)then
|
||||
if(curProperties[k]~=self[v](self))then
|
||||
if(k=="x")or(k=="y")then
|
||||
base.setPosition(self, curProperties["x"] or self:getX(), curProperties["y"] or self:getY())
|
||||
end
|
||||
if(k=="w")or(k=="h")then
|
||||
base.setSize(self, curProperties["w"] or self:getWidth(), curProperties["h"] or self:getHeight())
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local object = {
|
||||
updatePositions = updatePositions,
|
||||
createDynamicValue = createDynamicValue,
|
||||
|
||||
setPosition = function(self, xPos, yPos, rel)
|
||||
curProperties.x = xPos
|
||||
curProperties.y = yPos
|
||||
if(type(xPos)=="string")then
|
||||
createDynamicValue(self, "x", xPos)
|
||||
else
|
||||
dynObjects["x"] = nil
|
||||
end
|
||||
if(type(yPos)=="string")then
|
||||
createDynamicValue(self, "y", yPos)
|
||||
else
|
||||
dynObjects["y"] = nil
|
||||
end
|
||||
base.setPosition(self, curProperties.x, curProperties.y, rel)
|
||||
return self
|
||||
end,
|
||||
|
||||
setSize = function(self, w, h, rel)
|
||||
curProperties.w = w
|
||||
curProperties.h = h
|
||||
if(type(w)=="string")then
|
||||
createDynamicValue(self, "w", w)
|
||||
else
|
||||
dynObjects["w"] = nil
|
||||
end
|
||||
if(type(h)=="string")then
|
||||
createDynamicValue(self, "h", h)
|
||||
else
|
||||
dynObjects["h"] = nil
|
||||
end
|
||||
base.setSize(self, curProperties.w, curProperties.h, rel)
|
||||
return self
|
||||
end,
|
||||
|
||||
customEventHandler = function(self, event, ...)
|
||||
base.customEventHandler(self, event, ...)
|
||||
if(event=="basalt_FrameReposition")or(event=="basalt_FrameResize")then
|
||||
updatePositions(self)
|
||||
end
|
||||
end,
|
||||
}
|
||||
return object
|
||||
end
|
||||
}
|
||||
221
Basalt/plugins/pixelbox.lua
Normal file
@@ -0,0 +1,221 @@
|
||||
-- Most of this is made by Dev9551, you can find his awesome work here: https://github.com/9551-Dev/apis/blob/main/pixelbox_lite.lua
|
||||
-- Slighly modified by NyoriE to work with Basalt
|
||||
|
||||
--[[
|
||||
The MIT License (MIT)
|
||||
Copyright © 2022 Oliver Caha (9551Dev)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ?Software?), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED ?AS IS?, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
]]
|
||||
local t_sort,t_cat,s_char = table.sort,table.concat,string.char
|
||||
local function sort(a,b) return a[2] > b[2] end
|
||||
|
||||
local distances = {
|
||||
{5,256,16,8,64,32},
|
||||
{4,16,16384,256,128},
|
||||
[4] = {4,64,1024,256,128},
|
||||
[8] = {4,512,2048,256,1},
|
||||
[16] = {4,2,16384,256,1},
|
||||
[32] = {4,8192,4096,256,1},
|
||||
[64] = {4,4,1024,256,1},
|
||||
[128] = {6,32768,256,1024,2048,4096,16384},
|
||||
[256] = {6,1,128,2,512,4,8192},
|
||||
[512] = {4,8,2048,256,128},
|
||||
[1024] = {4,4,64,128,32768},
|
||||
[2048] = {4,512,8,128,32768},
|
||||
[4096] = {4,8192,32,128,32768},
|
||||
[8192] = {3,32,4096,256128},
|
||||
[16384] = {4,2,16,128,32768},
|
||||
[32768] = {5,128,1024,2048,4096,16384}
|
||||
}
|
||||
|
||||
local to_colors = {}
|
||||
for i = 0, 15 do
|
||||
to_colors[("%x"):format(i)] = 2^i
|
||||
end
|
||||
|
||||
local to_blit = {}
|
||||
for i = 0, 15 do
|
||||
to_blit[2^i] = ("%x"):format(i)
|
||||
end
|
||||
|
||||
local function pixelbox(colTable, defaultCol)
|
||||
defaultCol = defaultCol or "f"
|
||||
local width, height = #colTable[1], #colTable
|
||||
local cache = {}
|
||||
local canv = {}
|
||||
local cached = false
|
||||
|
||||
local function generateCanvas()
|
||||
for y = 1, height * 3 do
|
||||
for x = 1, width * 2 do
|
||||
if not canv[y] then canv[y] = {} end
|
||||
canv[y][x] = defaultCol
|
||||
end
|
||||
end
|
||||
|
||||
for k, v in ipairs(colTable) do
|
||||
for x = 1, #v do
|
||||
local col = v:sub(x, x)
|
||||
canv[k][x] = to_colors[col]
|
||||
end
|
||||
end
|
||||
end
|
||||
generateCanvas()
|
||||
|
||||
local function setSize(w,h)
|
||||
width, height = w, h
|
||||
canv = {}
|
||||
cached = false
|
||||
generateCanvas()
|
||||
end
|
||||
|
||||
|
||||
local function generateChar(a,b,c,d,e,f)
|
||||
local arr = {a,b,c,d,e,f}
|
||||
local c_types = {}
|
||||
local sortable = {}
|
||||
local ind = 0
|
||||
for i=1,6 do
|
||||
local c = arr[i]
|
||||
if not c_types[c] then
|
||||
ind = ind + 1
|
||||
c_types[c] = {0,ind}
|
||||
end
|
||||
|
||||
local t = c_types[c]
|
||||
local t1 = t[1] + 1
|
||||
|
||||
t[1] = t1
|
||||
sortable[t[2]] = {c,t1}
|
||||
end
|
||||
local n = #sortable
|
||||
while n > 2 do
|
||||
t_sort(sortable,sort)
|
||||
local bit6 = distances[sortable[n][1]]
|
||||
local index,run = 1,false
|
||||
local nm1 = n - 1
|
||||
for i=2,bit6[1] do
|
||||
if run then break end
|
||||
local tab = bit6[i]
|
||||
for j=1,nm1 do
|
||||
if sortable[j][1] == tab then
|
||||
index = j
|
||||
run = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
local from,to = sortable[n][1],sortable[index][1]
|
||||
for i=1,6 do
|
||||
if arr[i] == from then
|
||||
arr[i] = to
|
||||
local sindex = sortable[index]
|
||||
sindex[2] = sindex[2] + 1
|
||||
end
|
||||
end
|
||||
|
||||
sortable[n] = nil
|
||||
n = n - 1
|
||||
end
|
||||
|
||||
local n = 128
|
||||
local a6 = arr[6]
|
||||
|
||||
if arr[1] ~= a6 then n = n + 1 end
|
||||
if arr[2] ~= a6 then n = n + 2 end
|
||||
if arr[3] ~= a6 then n = n + 4 end
|
||||
if arr[4] ~= a6 then n = n + 8 end
|
||||
if arr[5] ~= a6 then n = n + 16 end
|
||||
|
||||
if sortable[1][1] == arr[6] then
|
||||
return s_char(n),sortable[2][1],arr[6]
|
||||
else
|
||||
return s_char(n),sortable[1][1],arr[6]
|
||||
end
|
||||
end
|
||||
|
||||
local function convert()
|
||||
local w_double = width * 2
|
||||
|
||||
local sy = 0
|
||||
for y = 1, height * 3, 3 do
|
||||
sy = sy + 1
|
||||
local layer_1 = canv[y]
|
||||
local layer_2 = canv[y + 1]
|
||||
local layer_3 = canv[y + 2]
|
||||
local char_line, fg_line, bg_line = {}, {}, {}
|
||||
local n = 0
|
||||
for x = 1, w_double, 2 do
|
||||
local xp1 = x + 1
|
||||
local b11, b21, b12, b22, b13, b23 = layer_1[x], layer_1[xp1], layer_2[x], layer_2[xp1], layer_3[x], layer_3[xp1]
|
||||
|
||||
local char, fg, bg = " ", 1, b11
|
||||
if not (b21 == b11 and b12 == b11 and b22 == b11 and b13 == b11 and b23 == b11) then
|
||||
char, fg, bg = generateChar(b11, b21, b12, b22, b13, b23)
|
||||
end
|
||||
n = n + 1
|
||||
char_line[n] = char
|
||||
fg_line[n] = to_blit[fg]
|
||||
bg_line[n] = to_blit[bg]
|
||||
end
|
||||
|
||||
cache[sy] = {t_cat(char_line), t_cat(fg_line), t_cat(bg_line)}
|
||||
end
|
||||
cached = true
|
||||
end
|
||||
|
||||
return {
|
||||
convert = convert,
|
||||
generateCanvas = generateCanvas,
|
||||
setSize = setSize,
|
||||
getSize = function()
|
||||
return width, height
|
||||
end,
|
||||
set = function(colTab, defCol)
|
||||
colTable = colTab
|
||||
defaultCol = defCol or defaultCol
|
||||
canv = {}
|
||||
cached = false
|
||||
generateCanvas()
|
||||
end,
|
||||
get = function(y)
|
||||
if not cached then convert() end
|
||||
return y~= nil and cache[y] or cache
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
return {
|
||||
Image = function(base, basalt)
|
||||
return {
|
||||
shrink = function(self)
|
||||
local bimg = self:getImageFrame(1)
|
||||
local img = {}
|
||||
for k,v in pairs(bimg)do
|
||||
if(type(k)=="number")then
|
||||
table.insert(img,v[3])
|
||||
end
|
||||
end
|
||||
local shrinkedImg = pixelbox(img, self:getBackground()).get()
|
||||
self:setImage(shrinkedImg)
|
||||
return self
|
||||
end,
|
||||
|
||||
getShrinkedImage = function(self)
|
||||
local bimg = self:getImageFrame(1)
|
||||
local img = {}
|
||||
for k,v in pairs(bimg)do
|
||||
if(type(k)=="number")then
|
||||
table.insert(img, v[3])
|
||||
end
|
||||
end
|
||||
return pixelbox(img, self:getBackground()).get()
|
||||
end,
|
||||
}
|
||||
end,
|
||||
}
|
||||
232
Basalt/plugins/reactive.lua
Normal file
@@ -0,0 +1,232 @@
|
||||
local XMLParser = require("xmlParser")
|
||||
|
||||
local Reactive = {}
|
||||
|
||||
Reactive.currentEffect = nil
|
||||
|
||||
Reactive.observable = function(initialValue)
|
||||
local value = initialValue
|
||||
local observerEffects = {}
|
||||
local get = function()
|
||||
if (Reactive.currentEffect ~= nil) then
|
||||
table.insert(observerEffects, Reactive.currentEffect)
|
||||
table.insert(Reactive.currentEffect.dependencies, observerEffects)
|
||||
end
|
||||
return value
|
||||
end
|
||||
local set = function(newValue)
|
||||
value = newValue
|
||||
local observerEffectsCopy = {}
|
||||
for index, effect in ipairs(observerEffects) do
|
||||
observerEffectsCopy[index] = effect
|
||||
end
|
||||
for _, effect in ipairs(observerEffectsCopy) do
|
||||
effect.execute()
|
||||
end
|
||||
end
|
||||
return get, set
|
||||
end
|
||||
|
||||
Reactive.untracked = function(getter)
|
||||
local parentEffect = Reactive.currentEffect
|
||||
Reactive.currentEffect = nil
|
||||
local value = getter()
|
||||
Reactive.currentEffect = parentEffect
|
||||
return value
|
||||
end
|
||||
|
||||
Reactive.effect = function(effectFn)
|
||||
local effect = {dependencies = {}}
|
||||
local execute = function()
|
||||
Reactive.clearEffectDependencies(effect)
|
||||
local parentEffect = Reactive.currentEffect
|
||||
Reactive.currentEffect = effect
|
||||
effectFn()
|
||||
Reactive.currentEffect = parentEffect
|
||||
end
|
||||
effect.execute = execute
|
||||
effect.execute()
|
||||
end
|
||||
|
||||
Reactive.derived = function(computeFn)
|
||||
local getValue, setValue = Reactive.observable();
|
||||
Reactive.effect(function()
|
||||
setValue(computeFn())
|
||||
end)
|
||||
return getValue;
|
||||
end
|
||||
|
||||
Reactive.clearEffectDependencies = function(effect)
|
||||
for _, dependency in ipairs(effect.dependencies) do
|
||||
for index, backlink in ipairs(dependency) do
|
||||
if (backlink == effect) then
|
||||
table.remove(dependency, index)
|
||||
end
|
||||
end
|
||||
end
|
||||
effect.dependencies = {};
|
||||
end
|
||||
|
||||
local Layout = {
|
||||
fromXML = function(text)
|
||||
local nodes = XMLParser.parseText(text)
|
||||
local script = nil
|
||||
for index, node in ipairs(nodes) do
|
||||
if (node.tag == "script") then
|
||||
script = node.value
|
||||
table.remove(nodes, index)
|
||||
break
|
||||
end
|
||||
end
|
||||
return {
|
||||
nodes = nodes,
|
||||
script = script
|
||||
}
|
||||
end
|
||||
}
|
||||
|
||||
local executeScript = function(script, env)
|
||||
return load(script, nil, "t", env)()
|
||||
end
|
||||
|
||||
local registerFunctionEvent = function(object, event, script, env)
|
||||
event(object, function(...)
|
||||
local success, msg = pcall(load(script, nil, "t", env))
|
||||
if not success then
|
||||
error("XML Error: "..msg)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
return {
|
||||
basalt = function(basalt)
|
||||
local function createObjectsFromXMLNode(node, env)
|
||||
local layout = env[node.tag]
|
||||
if (layout ~= nil) then
|
||||
local props = {}
|
||||
for prop, expression in pairs(node.attributes) do
|
||||
props[prop] = load("return " .. expression, nil, "t", env)
|
||||
end
|
||||
return basalt.createObjectsFromLayout(layout, props)
|
||||
end
|
||||
local objectName = node.tag:gsub("^%l", string.upper)
|
||||
local object = basalt.createObject(objectName, node.attributes["id"])
|
||||
for attribute, expression in pairs(node.attributes) do
|
||||
if (attribute:sub(1, 2) == "on") then
|
||||
object[attribute](object, function(...)
|
||||
local basaltCallback = basalt.getVariable(expression:gsub("\"", ""):gsub("\'", ""))
|
||||
if(basaltCallback ~= nil) then
|
||||
basaltCallback()
|
||||
elseif(env[expression] ~= nil) then
|
||||
env[expression]()
|
||||
else
|
||||
registerFunctionEvent(object, object[attribute], expression .. "()", env)
|
||||
end
|
||||
end)
|
||||
else
|
||||
local update = function()
|
||||
local value = load("return " .. expression, nil, "t", env)()
|
||||
object:setProperty(attribute, value)
|
||||
end
|
||||
basalt.effect(update)
|
||||
end
|
||||
end
|
||||
for _, child in ipairs(node.children) do
|
||||
local childObjects = createObjectsFromXMLNode(child, env)
|
||||
for _, childObject in ipairs(childObjects) do
|
||||
object:addChild(childObject)
|
||||
end
|
||||
end
|
||||
return {object}
|
||||
end
|
||||
|
||||
local object = {
|
||||
observable = Reactive.observable,
|
||||
untracked = Reactive.untracked,
|
||||
effect = Reactive.effect,
|
||||
derived = Reactive.derived,
|
||||
|
||||
layout = function(path)
|
||||
if (not fs.exists(path)) then
|
||||
error("Can't open file " .. path)
|
||||
end
|
||||
local f = fs.open(path, "r")
|
||||
local text = f.readAll()
|
||||
f.close()
|
||||
return Layout.fromXML(text)
|
||||
end,
|
||||
|
||||
createObjectsFromLayout = function(layout, props)
|
||||
local env = _ENV
|
||||
env.props = {}
|
||||
local updateFns = {}
|
||||
for prop, getFn in pairs(props) do
|
||||
updateFns[prop] = basalt.derived(function()
|
||||
return getFn()
|
||||
end)
|
||||
end
|
||||
setmetatable(env.props, {
|
||||
__index = function(_, k)
|
||||
if(updateFns[k] == nil) then
|
||||
error("Property " .. k .. " not found")
|
||||
end
|
||||
return updateFns[k]()
|
||||
end
|
||||
})
|
||||
if (layout.script ~= nil) then
|
||||
executeScript(layout.script, env)
|
||||
end
|
||||
local objects = {}
|
||||
for _, node in ipairs(layout.nodes) do
|
||||
local _objects = createObjectsFromXMLNode(node, env)
|
||||
for _, object in ipairs(_objects) do
|
||||
table.insert(objects, object)
|
||||
end
|
||||
end
|
||||
return objects
|
||||
end
|
||||
}
|
||||
return object
|
||||
end,
|
||||
|
||||
Container = function(base, basalt)
|
||||
local object = {
|
||||
loadLayout = function(self, path, props)
|
||||
local wrappedProps = {}
|
||||
if (props == nil) then
|
||||
props = {}
|
||||
end
|
||||
for prop, value in pairs(props) do
|
||||
wrappedProps[prop] = function()
|
||||
return value
|
||||
end
|
||||
end
|
||||
local layout = basalt.layout(path)
|
||||
local objects = basalt.createObjectsFromLayout(layout, wrappedProps)
|
||||
for _, object in ipairs(objects) do
|
||||
self:addChild(object)
|
||||
end
|
||||
return self
|
||||
end,
|
||||
|
||||
loadLayoutFromString = function(self, xmlContent, props)
|
||||
local wrappedProps = {}
|
||||
if (props == nil) then
|
||||
props = {}
|
||||
end
|
||||
for prop, value in pairs(props) do
|
||||
wrappedProps[prop] = function()
|
||||
return value
|
||||
end
|
||||
end
|
||||
local layout = Layout.fromXML(xmlContent)
|
||||
local objects = basalt.createObjectsFromLayout(layout, wrappedProps)
|
||||
for _, object in ipairs(objects) do
|
||||
self:addChild(object)
|
||||
end
|
||||
return self
|
||||
end
|
||||
}
|
||||
return object
|
||||
end
|
||||
}
|
||||
36
Basalt/plugins/shadow.lua
Normal file
@@ -0,0 +1,36 @@
|
||||
local XMLParser = require("xmlParser")
|
||||
|
||||
return {
|
||||
VisualObject = function(base)
|
||||
local shadow = false
|
||||
|
||||
local object = {
|
||||
setShadow = function(self, color)
|
||||
shadow = color
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
getShadow = function(self)
|
||||
return shadow
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
base.draw(self)
|
||||
self:addDraw("shadow", function()
|
||||
if(shadow~=false)then
|
||||
local w,h = self:getSize()
|
||||
if(shadow)then
|
||||
self:addBackgroundBox(w+1, 2, 1, h, shadow)
|
||||
self:addBackgroundBox(2, h+1, w, 1, shadow)
|
||||
self:addForegroundBox(w+1, 2, 1, h, shadow)
|
||||
self:addForegroundBox(2, h+1, w, 1, shadow)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
}
|
||||
|
||||
return object
|
||||
end
|
||||
}
|
||||
115
Basalt/plugins/textures.lua
Normal file
@@ -0,0 +1,115 @@
|
||||
local images = require("images")
|
||||
local utils = require("utils")
|
||||
local XMLParser = require("xmlParser")
|
||||
return {
|
||||
VisualObject = function(base)
|
||||
local textureId, infinitePlay = 1, true
|
||||
local bimg, texture, textureTimerId
|
||||
local textureMode = "default"
|
||||
|
||||
local object = {
|
||||
addTexture = function(self, path, animate)
|
||||
bimg = images.loadImageAsBimg(path)
|
||||
texture = bimg[1]
|
||||
if(animate)then
|
||||
if(bimg.animated)then
|
||||
self:listenEvent("other_event")
|
||||
local t = bimg[textureId].duration or bimg.secondsPerFrame or 0.2
|
||||
textureTimerId = os.startTimer(t)
|
||||
end
|
||||
end
|
||||
self:setBackground(false)
|
||||
self:setForeground(false)
|
||||
self:setDrawState("texture-base", true)
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setTextureMode = function(self, mode)
|
||||
textureMode = mode or textureMode
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
|
||||
setInfinitePlay = function(self, state)
|
||||
infinitePlay = state
|
||||
return self
|
||||
end,
|
||||
|
||||
eventHandler = function(self, event, timerId, ...)
|
||||
base.eventHandler(self, event, timerId, ...)
|
||||
if(event=="timer")then
|
||||
if(timerId == textureTimerId)then
|
||||
if(bimg[textureId+1]~=nil)then
|
||||
textureId = textureId + 1
|
||||
texture = bimg[textureId]
|
||||
local t = bimg[textureId].duration or bimg.secondsPerFrame or 0.2
|
||||
textureTimerId = os.startTimer(t)
|
||||
self:updateDraw()
|
||||
else
|
||||
if(infinitePlay)then
|
||||
textureId = 1
|
||||
texture = bimg[1]
|
||||
local t = bimg[textureId].duration or bimg.secondsPerFrame or 0.2
|
||||
textureTimerId = os.startTimer(t)
|
||||
self:updateDraw()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
base.draw(self)
|
||||
self:addDraw("texture-base", function()
|
||||
local obj = self:getParent() or self
|
||||
local x, y = self:getPosition()
|
||||
local w,h = self:getSize()
|
||||
local wP,hP = obj:getSize()
|
||||
|
||||
local textureWidth = bimg.width or #bimg[textureId][1][1]
|
||||
local textureHeight = bimg.height or #bimg[textureId]
|
||||
|
||||
local startX, startY = 0, 0
|
||||
|
||||
if (textureMode == "center") then
|
||||
startX = x + math.floor((w - textureWidth) / 2 + 0.5) - 1
|
||||
startY = y + math.floor((h - textureHeight) / 2 + 0.5) - 1
|
||||
elseif (textureMode == "default") then
|
||||
startX, startY = x, y
|
||||
elseif (textureMode == "right") then
|
||||
startX, startY = x + w - textureWidth, y + h - textureHeight
|
||||
end
|
||||
|
||||
local textureX = x - startX
|
||||
local textureY = y - startY
|
||||
|
||||
if startX < x then
|
||||
startX = x
|
||||
textureWidth = textureWidth - textureX
|
||||
end
|
||||
if startY < y then
|
||||
startY = y
|
||||
textureHeight = textureHeight - textureY
|
||||
end
|
||||
if startX + textureWidth > x + w then
|
||||
textureWidth = (x + w) - startX
|
||||
end
|
||||
if startY + textureHeight > y + h then
|
||||
textureHeight = (y + h) - startY
|
||||
end
|
||||
|
||||
for k = 1, textureHeight do
|
||||
if(texture[k+textureY]~=nil)then
|
||||
local t, f, b = table.unpack(texture[k+textureY])
|
||||
self:addBlit(1, k, t:sub(textureX, textureX + textureWidth), f:sub(textureX, textureX + textureWidth), b:sub(textureX, textureX + textureWidth))
|
||||
end
|
||||
end
|
||||
end, 1)
|
||||
self:setDrawState("texture-base", false)
|
||||
end
|
||||
}
|
||||
|
||||
return object
|
||||
end
|
||||
}
|
||||
99
Basalt/plugins/themes.lua
Normal file
@@ -0,0 +1,99 @@
|
||||
local baseTheme = { -- The default main theme for basalt!
|
||||
BaseFrameBG = colors.lightGray,
|
||||
BaseFrameText = colors.black,
|
||||
FrameBG = colors.gray,
|
||||
FrameText = colors.black,
|
||||
ButtonBG = colors.gray,
|
||||
ButtonText = colors.black,
|
||||
CheckboxBG = colors.lightGray,
|
||||
CheckboxText = colors.black,
|
||||
InputBG = colors.black,
|
||||
InputText = colors.lightGray,
|
||||
TextfieldBG = colors.black,
|
||||
TextfieldText = colors.white,
|
||||
ListBG = colors.gray,
|
||||
ListText = colors.black,
|
||||
MenubarBG = colors.gray,
|
||||
MenubarText = colors.black,
|
||||
DropdownBG = colors.gray,
|
||||
DropdownText = colors.black,
|
||||
RadioBG = colors.gray,
|
||||
RadioText = colors.black,
|
||||
SelectionBG = colors.black,
|
||||
SelectionText = colors.lightGray,
|
||||
GraphicBG = colors.black,
|
||||
ImageBG = colors.black,
|
||||
PaneBG = colors.black,
|
||||
ProgramBG = colors.black,
|
||||
ProgressbarBG = colors.gray,
|
||||
ProgressbarText = colors.black,
|
||||
ProgressbarActiveBG = colors.black,
|
||||
ScrollbarBG = colors.lightGray,
|
||||
ScrollbarText = colors.gray,
|
||||
ScrollbarSymbolColor = colors.black,
|
||||
SliderBG = false,
|
||||
SliderText = colors.gray,
|
||||
SliderSymbolColor = colors.black,
|
||||
SwitchBG = colors.lightGray,
|
||||
SwitchText = colors.gray,
|
||||
LabelBG = false,
|
||||
LabelText = colors.black,
|
||||
GraphBG = colors.gray,
|
||||
GraphText = colors.black
|
||||
}
|
||||
|
||||
local plugin = {
|
||||
Container = function(base, name, basalt)
|
||||
local theme = {}
|
||||
|
||||
local object = {
|
||||
getTheme = function(self, name)
|
||||
local parent = self:getParent()
|
||||
return theme[name] or (parent~=nil and parent:getTheme(name) or baseTheme[name])
|
||||
end,
|
||||
setTheme = function(self, _theme, col)
|
||||
if(type(_theme)=="table")then
|
||||
theme = _theme
|
||||
elseif(type(_theme)=="string")then
|
||||
theme[_theme] = col
|
||||
end
|
||||
self:updateDraw()
|
||||
return self
|
||||
end,
|
||||
}
|
||||
return object
|
||||
end,
|
||||
|
||||
basalt = function()
|
||||
return {
|
||||
getTheme = function(name)
|
||||
return baseTheme[name]
|
||||
end,
|
||||
setTheme = function(_theme, col)
|
||||
if(type(_theme)=="table")then
|
||||
baseTheme = _theme
|
||||
elseif(type(_theme)=="string")then
|
||||
baseTheme[_theme] = col
|
||||
end
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
}
|
||||
|
||||
for k,v in pairs({"BaseFrame", "Frame", "ScrollableFrame", "MovableFrame", "Button", "Checkbox", "Dropdown", "Graph", "Graphic", "Input", "Label", "List", "Menubar", "Pane", "Program", "Progressbar", "Radio", "Scrollbar", "Slider", "Switch", "Textfield"})do
|
||||
plugin[v] = function(base, name, basalt)
|
||||
local object = {
|
||||
init = function(self)
|
||||
if(base.init(self))then
|
||||
local parent = self:getParent() or self
|
||||
self:setBackground(parent:getTheme(v.."BG"))
|
||||
self:setForeground(parent:getTheme(v.."Text"))
|
||||
end
|
||||
end
|
||||
}
|
||||
return object
|
||||
end
|
||||
end
|
||||
|
||||
return plugin
|
||||
@@ -1,45 +0,0 @@
|
||||
return { -- The default main theme for basalt!
|
||||
BasaltBG = colors.lightGray,
|
||||
BasaltText = colors.black,
|
||||
FrameBG = colors.gray,
|
||||
FrameText = colors.black,
|
||||
ButtonBG = colors.gray,
|
||||
ButtonText = colors.black,
|
||||
CheckboxBG = colors.gray,
|
||||
CheckboxText = colors.black,
|
||||
InputBG = colors.gray,
|
||||
InputText = colors.black,
|
||||
TextfieldBG = colors.gray,
|
||||
TextfieldText = colors.black,
|
||||
ListBG = colors.gray,
|
||||
ListText = colors.black,
|
||||
MenubarBG = colors.gray,
|
||||
MenubarText = colors.black,
|
||||
DropdownBG = colors.gray,
|
||||
DropdownText = colors.black,
|
||||
RadioBG = colors.gray,
|
||||
RadioText = colors.black,
|
||||
SelectionBG = colors.black,
|
||||
SelectionText = colors.lightGray,
|
||||
GraphicBG = colors.black,
|
||||
ImageBG = colors.black,
|
||||
PaneBG = colors.black,
|
||||
ProgramBG = colors.black,
|
||||
ProgressbarBG = colors.gray,
|
||||
ProgressbarText = colors.black,
|
||||
ProgressbarActiveBG = colors.black,
|
||||
ScrollbarBG = colors.lightGray,
|
||||
ScrollbarText = colors.gray,
|
||||
ScrollbarSymbolColor = colors.black,
|
||||
SliderBG = false,
|
||||
SliderText = colors.gray,
|
||||
SliderSymbolColor = colors.black,
|
||||
SwitchBG = colors.lightGray,
|
||||
SwitchText = colors.gray,
|
||||
SwitchBGSymbol = colors.black,
|
||||
SwitchInactive = colors.red,
|
||||
SwitchActive = colors.green,
|
||||
LabelBG = false,
|
||||
LabelText = colors.black
|
||||
|
||||
}
|
||||
13
README.md
@@ -1,15 +1,14 @@
|
||||
# Basalt - A UI Framework for CC:Tweaked
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
[](https://discord.gg/yNNnmBVBpE)
|
||||
|
||||
Basalt is intended to be an easy-to-understand UI Framework designed for CC:Tweaked (Also know as "ComputerCraft: Tweaked") - a popular minecraft mod. For more information about CC:Tweaked, checkout the project's [wiki](https://tweaked.cc/) or [download](https://www.curseforge.com/minecraft/mc-mods/cc-tweaked).<br>
|
||||
Basalt is intended to be an easy-to-understand UI Framework designed for CC:Tweaked - a popular minecraft mod. For more information about CC:Tweaked, checkout the project's [wiki](https://tweaked.cc/) or [download](https://modrinth.com/mod/cc-tweaked).
|
||||
**Note:** Basalt is still under developement and you may find bugs!
|
||||
|
||||
Check out the [wiki](https://basalt.madefor.cc/) for more information.<br>
|
||||
If you have questions, feel free to join the discord server: [https://discord.gg/yNNnmBVBpE](https://discord.gg/yNNnmBVBpE).
|
||||
Check out the [wiki](https://basalt.madefor.cc/) for more information.
|
||||
If you have questions, feel free to join the discord server: [discord.gg/yNNnmBVBpE](https://discord.gg/yNNnmBVBpE).
|
||||
|
||||
## Demo
|
||||
<img src="https://raw.githubusercontent.com/Pyroxenium/Basalt/master/docs/_media/basaltPreview2.gif" width="300">
|
||||
|
||||

|
||||
|
||||
@@ -1,507 +0,0 @@
|
||||
-- This file is able to minify and create a single file out of a project folder
|
||||
-- only works for basalt
|
||||
|
||||
|
||||
|
||||
|
||||
-- The minify part is fully made by stravant and can be found here: https://github.com/stravant/LuaMinify/blob/master/RobloxPlugin/Minify.lua
|
||||
-- Thanks to him for his awesome work!
|
||||
|
||||
--
|
||||
-- Minify.lua
|
||||
--
|
||||
-- A compilation of all of the neccesary code to Minify a source file, all into one single
|
||||
-- script for usage on Roblox. Needed to deal with Roblox' lack of `require`.
|
||||
--
|
||||
|
||||
--The minify part is also minified, if you're looking for the packaging part, check out the bottom!
|
||||
function lookupify(cd)for dd,__a in pairs(cd)do cd[__a]=true end;return cd end
|
||||
function CountTable(cd)local dd=0;for __a in pairs(cd)do dd=dd+1 end;return dd end
|
||||
function PrintTable(cd,dd)if cd.Print then return cd.Print()end;dd=dd or 0
|
||||
local __a=(CountTable(cd)>1)local a_a=string.rep(' ',dd+1)
|
||||
local b_a="{".. (__a and'\n'or'')
|
||||
for c_a,d_a in pairs(cd)do
|
||||
if type(d_a)~='function'then
|
||||
b_a=b_a.. (__a and a_a or'')
|
||||
if type(c_a)=='number'then elseif type(c_a)=='string'and
|
||||
c_a:match("^[A-Za-z_][A-Za-z0-9_]*$")then b_a=b_a..c_a.." = "elseif
|
||||
type(c_a)=='string'then b_a=b_a.."[\""..c_a.."\"] = "else b_a=b_a.."["..
|
||||
tostring(c_a).."] = "end
|
||||
if type(d_a)=='string'then b_a=b_a.."\""..d_a.."\""elseif type(d_a)==
|
||||
'number'then b_a=b_a..d_a elseif type(d_a)=='table'then b_a=b_a..
|
||||
PrintTable(d_a,dd+ (__a and 1 or 0))else
|
||||
b_a=b_a..tostring(d_a)end;if next(cd,c_a)then b_a=b_a..","end;if __a then b_a=b_a..'\n'end end end;b_a=b_a..
|
||||
(__a and string.rep(' ',dd)or'').."}"return b_a end;local bb=lookupify{' ','\n','\t','\r'}
|
||||
local cb={['\r']='\\r',['\n']='\\n',['\t']='\\t',['"']='\\"',["'"]="\\'"}
|
||||
local db=lookupify{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}
|
||||
local _c=lookupify{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}
|
||||
local ac=lookupify{'0','1','2','3','4','5','6','7','8','9'}
|
||||
local bc=lookupify{'0','1','2','3','4','5','6','7','8','9','A','a','B','b','C','c','D','d','E','e','F','f'}
|
||||
local cc=lookupify{'+','-','*','/','^','%',',','{','}','[',']','(',')',';','#'}
|
||||
local dc=lookupify{'and','break','do','else','elseif','end','false','for','function','goto','if','in','local','nil','not','or','repeat','return','then','true','until','while'}
|
||||
function LexLua(cd)local dd={}
|
||||
local __a,a_a=pcall(function()local _aa=1;local aaa=1;local baa=1
|
||||
local function caa()local cba=cd:sub(_aa,_aa)if cba=='\n'then baa=1
|
||||
aaa=aaa+1 else baa=baa+1 end;_aa=_aa+1;return cba end
|
||||
local function daa(cba)cba=cba or 0;return cd:sub(_aa+cba,_aa+cba)end;local function _ba(cba)local dba=daa()
|
||||
for i=1,#cba do if dba==cba:sub(i,i)then return caa()end end end;local function aba(cba)
|
||||
return error(">> :"..aaa..":"..
|
||||
baa..": "..cba,0)end
|
||||
local function bba()local cba=_aa
|
||||
if daa()=='['then local dba=0;while
|
||||
daa(dba+1)=='='do dba=dba+1 end
|
||||
if daa(dba+1)=='['then for _=0,dba+1 do caa()end
|
||||
local _ca=_aa
|
||||
while true do if daa()==''then
|
||||
aba("Expected `]"..string.rep('=',dba).."]` near <eof>.",3)end;local cca=true;if daa()==']'then for i=1,dba do if daa(i)~='='then
|
||||
cca=false end end
|
||||
if daa(dba+1)~=']'then cca=false end else cca=false end;if cca then break else
|
||||
caa()end end;local aca=cd:sub(_ca,_aa-1)for i=0,dba+1 do caa()end
|
||||
local bca=cd:sub(cba,_aa-1)return aca,bca else return nil end else return nil end end
|
||||
while true do local cba=''
|
||||
while true do local dca=daa()
|
||||
if bb[dca]then cba=cba..caa()elseif
|
||||
dca=='-'and daa(1)=='-'then caa()caa()cba=cba..'--'local _da,ada=bba()
|
||||
if ada then cba=cba..ada else while daa()~='\n'and
|
||||
daa()~=''do cba=cba..caa()end end else break end end;local dba=aaa;local _ca=baa
|
||||
local aca=":"..aaa..":"..baa..":> "local bca=daa()local cca=nil
|
||||
if bca==''then cca={Type='Eof'}elseif
|
||||
_c[bca]or db[bca]or bca=='_'then local dca=_aa;repeat caa()bca=daa()until not
|
||||
(_c[bca]or db[bca]or ac[bca]or bca=='_')
|
||||
local _da=cd:sub(dca,_aa-1)
|
||||
if dc[_da]then cca={Type='Keyword',Data=_da}else cca={Type='Ident',Data=_da}end elseif ac[bca]or(daa()=='.'and ac[daa(1)])then local dca=_aa
|
||||
if bca=='0'and
|
||||
daa(1)=='x'then caa()caa()while bc[daa()]do caa()end;if _ba('Pp')then
|
||||
_ba('+-')while ac[daa()]do caa()end end else
|
||||
while ac[daa()]do caa()end;if _ba('.')then while ac[daa()]do caa()end end;if _ba('Ee')then
|
||||
_ba('+-')while ac[daa()]do caa()end end end;cca={Type='Number',Data=cd:sub(dca,_aa-1)}elseif bca=='\''or bca==
|
||||
'\"'then local dca=_aa;local _da=caa()local ada=_aa;while true do local dda=caa()
|
||||
if dda=='\\'then caa()elseif
|
||||
dda==_da then break elseif dda==''then aba("Unfinished string near <eof>")end end;local bda=cd:sub(ada,
|
||||
_aa-2)local cda=cd:sub(dca,_aa-1)
|
||||
cca={Type='String',Data=cda,Constant=bda}elseif bca=='['then local dca,_da=bba()
|
||||
if _da then cca={Type='String',Data=_da,Constant=dca}else
|
||||
caa()cca={Type='Symbol',Data='['}end elseif _ba('>=<')then if _ba('=')then cca={Type='Symbol',Data=bca..'='}else
|
||||
cca={Type='Symbol',Data=bca}end elseif _ba('~')then
|
||||
if _ba('=')then
|
||||
cca={Type='Symbol',Data='~='}else aba("Unexpected symbol `~` in source.",2)end elseif _ba('.')then
|
||||
if _ba('.')then if _ba('.')then cca={Type='Symbol',Data='...'}else
|
||||
cca={Type='Symbol',Data='..'}end else cca={Type='Symbol',Data='.'}end elseif _ba(':')then if _ba(':')then cca={Type='Symbol',Data='::'}else
|
||||
cca={Type='Symbol',Data=':'}end elseif cc[bca]then caa()
|
||||
cca={Type='Symbol',Data=bca}else local dca,_da=bba()if dca then cca={Type='String',Data=_da,Constant=dca}else
|
||||
aba("Unexpected Symbol `"..
|
||||
bca.."` in source.",2)end end;cca.LeadingWhite=cba;cca.Line=dba;cca.Char=_ca
|
||||
cca.Print=function()
|
||||
return"<".. (cca.Type..string.rep(' ',7 -#
|
||||
cca.Type))..
|
||||
" ".. (cca.Data or'').." >"end;dd[#dd+1]=cca;if cca.Type=='Eof'then break end end end)if not __a then return false,a_a end;local b_a={}local c_a={}local d_a=1
|
||||
function b_a:Peek(_aa)_aa=_aa or 0;return dd[math.min(
|
||||
#dd,d_a+_aa)]end
|
||||
function b_a:Get()local _aa=dd[d_a]d_a=math.min(d_a+1,#dd)return _aa end;function b_a:Is(_aa)return b_a:Peek().Type==_aa end;function b_a:Save()c_a[
|
||||
#c_a+1]=d_a end
|
||||
function b_a:Commit()c_a[#c_a]=nil end;function b_a:Restore()d_a=c_a[#c_a]c_a[#c_a]=nil end
|
||||
function b_a:ConsumeSymbol(_aa)
|
||||
local aaa=self:Peek()
|
||||
if aaa.Type=='Symbol'then if _aa then
|
||||
if aaa.Data==_aa then self:Get()return true else return nil end else self:Get()return aaa end else return
|
||||
nil end end
|
||||
function b_a:ConsumeKeyword(_aa)local aaa=self:Peek()if
|
||||
aaa.Type=='Keyword'and aaa.Data==_aa then self:Get()return true else return nil end end;function b_a:IsKeyword(_aa)local aaa=b_a:Peek()return
|
||||
aaa.Type=='Keyword'and aaa.Data==_aa end
|
||||
function b_a:IsSymbol(_aa)
|
||||
local aaa=b_a:Peek()return aaa.Type=='Symbol'and aaa.Data==_aa end
|
||||
function b_a:IsEof()return b_a:Peek().Type=='Eof'end;return true,b_a end
|
||||
function ParseLua(cd)local dd,__a=LexLua(cd)if not dd then return false,__a end
|
||||
local function a_a(ada)local bda=">> :"..
|
||||
|
||||
__a:Peek().Line..":"..__a:Peek().Char..": "..ada.."\n"local cda=0
|
||||
for dda in
|
||||
cd:gmatch("[^\n]*\n?")do if dda:sub(-1,-1)=='\n'then dda=dda:sub(1,-2)end;cda=
|
||||
cda+1
|
||||
if cda==__a:Peek().Line then bda=bda..">> `"..
|
||||
dda:gsub('\t',' ').."`\n"for i=1,__a:Peek().Char
|
||||
do local __b=dda:sub(i,i)
|
||||
if __b=='\t'then bda=bda..' 'else bda=bda..' 'end end
|
||||
bda=bda.." ^---"break end end;return bda end;local b_a=0;local c_a={}local d_a={'_','a','b','c','d'}
|
||||
local function _aa(ada)local bda={}bda.Parent=ada
|
||||
bda.LocalList={}bda.LocalMap={}
|
||||
function bda:RenameVars()
|
||||
for cda,dda in pairs(bda.LocalList)do local __b;b_a=0
|
||||
repeat b_a=b_a+1;local a_b=b_a
|
||||
__b=''while a_b>0 do local b_b=a_b%#d_a;a_b=(a_b-b_b)/#d_a
|
||||
__b=__b..d_a[b_b+1]end until
|
||||
not c_a[__b]and
|
||||
not ada:GetLocal(__b)and not bda.LocalMap[__b]dda.Name=__b;bda.LocalMap[__b]=dda end end
|
||||
function bda:GetLocal(cda)local dda=bda.LocalMap[cda]if dda then return dda end;if bda.Parent then
|
||||
local __b=bda.Parent:GetLocal(cda)if __b then return __b end end;return nil end
|
||||
function bda:CreateLocal(cda)local dda={}dda.Scope=bda;dda.Name=cda;dda.CanRename=true;bda.LocalList[#
|
||||
bda.LocalList+1]=dda
|
||||
bda.LocalMap[cda]=dda;return dda end;bda.Print=function()return"<Scope>"end;return bda end;local aaa;local baa
|
||||
local function caa(ada)local bda=_aa(ada)if not __a:ConsumeSymbol('(')then return false,
|
||||
a_a("`(` expected.")end;local cda={}local dda=false
|
||||
while not
|
||||
__a:ConsumeSymbol(')')do
|
||||
if __a:Is('Ident')then
|
||||
local c_b=bda:CreateLocal(__a:Get().Data)cda[#cda+1]=c_b;if not __a:ConsumeSymbol(',')then
|
||||
if
|
||||
__a:ConsumeSymbol(')')then break else return false,a_a("`)` expected.")end end elseif
|
||||
__a:ConsumeSymbol('...')then dda=true
|
||||
if not __a:ConsumeSymbol(')')then return false,
|
||||
a_a("`...` must be the last argument of a function.")end;break else return false,a_a("Argument name or `...` expected")end end;local __b,a_b=baa(bda)if not __b then return false,a_b end;if not
|
||||
__a:ConsumeKeyword('end')then
|
||||
return false,a_a("`end` expected after function body")end;local b_b={}
|
||||
b_b.AstType='Function'b_b.Scope=bda;b_b.Arguments=cda;b_b.Body=a_b;b_b.VarArg=dda;return true,b_b end
|
||||
local function daa(ada)
|
||||
if __a:ConsumeSymbol('(')then local bda,cda=aaa(ada)
|
||||
if not bda then return false,cda end
|
||||
if not __a:ConsumeSymbol(')')then return false,a_a("`)` Expected.")end;cda.ParenCount=(cda.ParenCount or 0)+1;return true,cda elseif
|
||||
__a:Is('Ident')then local bda=__a:Get()local cda=ada:GetLocal(bda.Data)if not cda then
|
||||
c_a[bda.Data]=true end;local dda={}dda.AstType='VarExpr'dda.Name=bda.Data
|
||||
dda.Local=cda;return true,dda else return false,a_a("primary expression expected")end end
|
||||
local function _ba(ada,bda)local cda,dda=daa(ada)if not cda then return false,dda end
|
||||
while true do
|
||||
if __a:IsSymbol('.')or
|
||||
__a:IsSymbol(':')then local __b=__a:Get().Data;if not __a:Is('Ident')then return false,
|
||||
a_a("<Ident> expected.")end;local a_b=__a:Get()
|
||||
local b_b={}b_b.AstType='MemberExpr'b_b.Base=dda;b_b.Indexer=__b;b_b.Ident=a_b;dda=b_b elseif not
|
||||
bda and __a:ConsumeSymbol('[')then local __b,a_b=aaa(ada)if not __b then
|
||||
return false,a_b end;if not __a:ConsumeSymbol(']')then
|
||||
return false,a_a("`]` expected.")end;local b_b={}b_b.AstType='IndexExpr'
|
||||
b_b.Base=dda;b_b.Index=a_b;dda=b_b elseif not bda and __a:ConsumeSymbol('(')then local __b={}
|
||||
while not
|
||||
__a:ConsumeSymbol(')')do local b_b,c_b=aaa(ada)if not b_b then return false,c_b end
|
||||
__b[#__b+1]=c_b
|
||||
if not __a:ConsumeSymbol(',')then if __a:ConsumeSymbol(')')then break else return false,
|
||||
a_a("`)` Expected.")end end end;local a_b={}a_b.AstType='CallExpr'a_b.Base=dda;a_b.Arguments=__b;dda=a_b elseif not bda and
|
||||
__a:Is('String')then local __b={}__b.AstType='StringCallExpr'__b.Base=dda
|
||||
__b.Arguments={__a:Get()}dda=__b elseif not bda and __a:IsSymbol('{')then local __b,a_b=aaa(ada)if not __b then
|
||||
return false,a_b end;local b_b={}b_b.AstType='TableCallExpr'b_b.Base=dda
|
||||
b_b.Arguments={a_b}dda=b_b else break end end;return true,dda end
|
||||
local function aba(ada)
|
||||
if __a:Is('Number')then local bda={}bda.AstType='NumberExpr'bda.Value=__a:Get()return
|
||||
true,bda elseif __a:Is('String')then local bda={}bda.AstType='StringExpr'
|
||||
bda.Value=__a:Get()return true,bda elseif __a:ConsumeKeyword('nil')then local bda={}bda.AstType='NilExpr'
|
||||
return true,bda elseif __a:IsKeyword('false')or __a:IsKeyword('true')then local bda={}
|
||||
bda.AstType='BooleanExpr'bda.Value=(__a:Get().Data=='true')return true,bda elseif
|
||||
__a:ConsumeSymbol('...')then local bda={}bda.AstType='DotsExpr'return true,bda elseif __a:ConsumeSymbol('{')then local bda={}
|
||||
bda.AstType='ConstructorExpr'bda.EntryList={}
|
||||
while true do
|
||||
if __a:IsSymbol('[')then __a:Get()local cda,dda=aaa(ada)
|
||||
if not cda then return
|
||||
false,a_a("Key Expression Expected")end
|
||||
if not __a:ConsumeSymbol(']')then return false,a_a("`]` Expected")end
|
||||
if not __a:ConsumeSymbol('=')then return false,a_a("`=` Expected")end;local __b,a_b=aaa(ada)if not __b then
|
||||
return false,a_a("Value Expression Expected")end
|
||||
bda.EntryList[#bda.EntryList+1]={Type='Key',Key=dda,Value=a_b}elseif __a:Is('Ident')then local cda=__a:Peek(1)
|
||||
if
|
||||
cda.Type=='Symbol'and cda.Data=='='then local dda=__a:Get()if not __a:ConsumeSymbol('=')then
|
||||
return false,a_a("`=` Expected")end;local __b,a_b=aaa(ada)if not __b then return false,
|
||||
a_a("Value Expression Expected")end
|
||||
bda.EntryList[
|
||||
#bda.EntryList+1]={Type='KeyString',Key=dda.Data,Value=a_b}else local dda,__b=aaa(ada)
|
||||
if not dda then return false,a_a("Value Exected")end
|
||||
bda.EntryList[#bda.EntryList+1]={Type='Value',Value=__b}end elseif __a:ConsumeSymbol('}')then break else local cda,dda=aaa(ada)
|
||||
bda.EntryList[#bda.EntryList+1]={Type='Value',Value=dda}if not cda then return false,a_a("Value Expected")end end
|
||||
if __a:ConsumeSymbol(';')or __a:ConsumeSymbol(',')then elseif
|
||||
__a:ConsumeSymbol('}')then break else return false,a_a("`}` or table entry Expected")end end;return true,bda elseif __a:ConsumeKeyword('function')then local bda,cda=caa(ada)if not bda then
|
||||
return false,cda end;cda.IsLocal=true;return true,cda else return _ba(ada)end end;local bba=lookupify{'-','not','#'}local cba=8
|
||||
local dba={['+']={6,6},['-']={6,6},['%']={7,7},['/']={7,7},['*']={7,7},['^']={10,9},['..']={5,4},['==']={3,3},['<']={3,3},['<=']={3,3},['~=']={3,3},['>']={3,3},['>=']={3,3},['and']={2,2},['or']={1,1}}
|
||||
local function _ca(ada,bda)local cda,dda
|
||||
if bba[__a:Peek().Data]then local __b=__a:Get().Data
|
||||
cda,dda=_ca(ada,cba)if not cda then return false,dda end;local a_b={}a_b.AstType='UnopExpr'
|
||||
a_b.Rhs=dda;a_b.Op=__b;dda=a_b else cda,dda=aba(ada)if not cda then return false,dda end end
|
||||
while true do local __b=dba[__a:Peek().Data]
|
||||
if __b and __b[1]>bda then
|
||||
local a_b=__a:Get().Data;local b_b,c_b=_ca(ada,__b[2])if not b_b then return false,c_b end;local d_b={}
|
||||
d_b.AstType='BinopExpr'd_b.Lhs=dda;d_b.Op=a_b;d_b.Rhs=c_b;dda=d_b else break end end;return true,dda end;aaa=function(ada)return _ca(ada,0)end
|
||||
local function aca(ada)local bda=nil
|
||||
if
|
||||
__a:ConsumeKeyword('if')then local cda={}cda.AstType='IfStatement'cda.Clauses={}
|
||||
repeat local dda,__b=aaa(ada)if not dda then
|
||||
return false,__b end;if not __a:ConsumeKeyword('then')then return false,
|
||||
a_a("`then` expected.")end
|
||||
local a_b,b_b=baa(ada)if not a_b then return false,b_b end
|
||||
cda.Clauses[#cda.Clauses+1]={Condition=__b,Body=b_b}until not __a:ConsumeKeyword('elseif')
|
||||
if __a:ConsumeKeyword('else')then local dda,__b=baa(ada)
|
||||
if not dda then return false,__b end;cda.Clauses[#cda.Clauses+1]={Body=__b}end;if not __a:ConsumeKeyword('end')then
|
||||
return false,a_a("`end` expected.")end;bda=cda elseif __a:ConsumeKeyword('while')then
|
||||
local cda={}cda.AstType='WhileStatement'local dda,__b=aaa(ada)
|
||||
if not dda then return false,__b end;if not __a:ConsumeKeyword('do')then
|
||||
return false,a_a("`do` expected.")end;local a_b,b_b=baa(ada)
|
||||
if not a_b then return false,b_b end;if not __a:ConsumeKeyword('end')then
|
||||
return false,a_a("`end` expected.")end;cda.Condition=__b;cda.Body=b_b;bda=cda elseif
|
||||
__a:ConsumeKeyword('do')then local cda,dda=baa(ada)if not cda then return false,dda end
|
||||
if not
|
||||
__a:ConsumeKeyword('end')then return false,a_a("`end` expected.")end;local __b={}__b.AstType='DoStatement'__b.Body=dda;bda=__b elseif
|
||||
__a:ConsumeKeyword('for')then
|
||||
if not __a:Is('Ident')then return false,a_a("<ident> expected.")end;local cda=__a:Get()
|
||||
if __a:ConsumeSymbol('=')then local dda=_aa(ada)
|
||||
local __b=dda:CreateLocal(cda.Data)local a_b,b_b=aaa(ada)if not a_b then return false,b_b end
|
||||
if not
|
||||
__a:ConsumeSymbol(',')then return false,a_a("`,` Expected")end;local c_b,d_b=aaa(ada)if not c_b then return false,d_b end;local _ab,aab
|
||||
if
|
||||
__a:ConsumeSymbol(',')then _ab,aab=aaa(ada)if not _ab then return false,aab end end;if not __a:ConsumeKeyword('do')then
|
||||
return false,a_a("`do` expected")end;local bab,cab=baa(dda)
|
||||
if not bab then return false,cab end;if not __a:ConsumeKeyword('end')then
|
||||
return false,a_a("`end` expected")end;local dab={}
|
||||
dab.AstType='NumericForStatement'dab.Scope=dda;dab.Variable=__b;dab.Start=b_b;dab.End=d_b;dab.Step=aab
|
||||
dab.Body=cab;bda=dab else local dda=_aa(ada)
|
||||
local __b={dda:CreateLocal(cda.Data)}
|
||||
while __a:ConsumeSymbol(',')do if not __a:Is('Ident')then return false,
|
||||
a_a("for variable expected.")end
|
||||
__b[#__b+1]=dda:CreateLocal(__a:Get().Data)end;if not __a:ConsumeKeyword('in')then
|
||||
return false,a_a("`in` expected.")end;local a_b={}local b_b,c_b=aaa(ada)if not b_b then
|
||||
return false,c_b end;a_b[#a_b+1]=c_b
|
||||
while __a:ConsumeSymbol(',')do
|
||||
local bab,cab=aaa(ada)if not bab then return false,cab end;a_b[#a_b+1]=cab end;if not __a:ConsumeKeyword('do')then
|
||||
return false,a_a("`do` expected.")end;local d_b,_ab=baa(dda)
|
||||
if not d_b then return false,_ab end;if not __a:ConsumeKeyword('end')then
|
||||
return false,a_a("`end` expected.")end;local aab={}
|
||||
aab.AstType='GenericForStatement'aab.Scope=dda;aab.VariableList=__b;aab.Generators=a_b;aab.Body=_ab;bda=aab end elseif __a:ConsumeKeyword('repeat')then local cda,dda=baa(ada)
|
||||
if not cda then return false,dda end;if not __a:ConsumeKeyword('until')then
|
||||
return false,a_a("`until` expected.")end;local __b,a_b=aaa(ada)
|
||||
if not __b then return false,a_b end;local b_b={}b_b.AstType='RepeatStatement'b_b.Condition=a_b;b_b.Body=dda;bda=b_b elseif
|
||||
__a:ConsumeKeyword('function')then if not __a:Is('Ident')then
|
||||
return false,a_a("Function name expected")end;local cda,dda=_ba(ada,true)if not cda then
|
||||
return false,dda end;local __b,a_b=caa(ada)if not __b then return false,a_b end
|
||||
a_b.IsLocal=false;a_b.Name=dda;bda=a_b elseif __a:ConsumeKeyword('local')then
|
||||
if __a:Is('Ident')then
|
||||
local cda={__a:Get().Data}while __a:ConsumeSymbol(',')do if not __a:Is('Ident')then return false,
|
||||
a_a("local var name expected")end
|
||||
cda[#cda+1]=__a:Get().Data end;local dda={}if
|
||||
__a:ConsumeSymbol('=')then
|
||||
repeat local a_b,b_b=aaa(ada)if not a_b then return false,b_b end
|
||||
dda[#dda+1]=b_b until not __a:ConsumeSymbol(',')end;for a_b,b_b in
|
||||
pairs(cda)do cda[a_b]=ada:CreateLocal(b_b)end
|
||||
local __b={}__b.AstType='LocalStatement'__b.LocalList=cda;__b.InitList=dda;bda=__b elseif
|
||||
__a:ConsumeKeyword('function')then if not __a:Is('Ident')then
|
||||
return false,a_a("Function name expected")end;local cda=__a:Get().Data
|
||||
local dda=ada:CreateLocal(cda)local __b,a_b=caa(ada)if not __b then return false,a_b end;a_b.Name=dda
|
||||
a_b.IsLocal=true;bda=a_b else
|
||||
return false,a_a("local var or function def expected")end elseif __a:ConsumeSymbol('::')then if not __a:Is('Ident')then return false,
|
||||
a_a('Label name expected')end
|
||||
local cda=__a:Get().Data
|
||||
if not __a:ConsumeSymbol('::')then return false,a_a("`::` expected")end;local dda={}dda.AstType='LabelStatement'dda.Label=cda;bda=dda elseif
|
||||
__a:ConsumeKeyword('return')then local cda={}
|
||||
if not __a:IsKeyword('end')then local __b,a_b=aaa(ada)
|
||||
if __b then cda[1]=a_b;while
|
||||
__a:ConsumeSymbol(',')do local b_b,c_b=aaa(ada)if not b_b then return false,c_b end
|
||||
cda[#cda+1]=c_b end end end;local dda={}dda.AstType='ReturnStatement'dda.Arguments=cda;bda=dda elseif
|
||||
__a:ConsumeKeyword('break')then local cda={}cda.AstType='BreakStatement'bda=cda elseif __a:IsKeyword('goto')then
|
||||
if not
|
||||
__a:Is('Ident')then return false,a_a("Label expected")end;local cda=__a:Get().Data;local dda={}dda.AstType='GotoStatement'
|
||||
dda.Label=cda;bda=dda else local cda,dda=_ba(ada)if not cda then return false,dda end
|
||||
if
|
||||
__a:IsSymbol(',')or __a:IsSymbol('=')then
|
||||
if(dda.ParenCount or 0)>0 then return false,
|
||||
a_a("Can not assign to parenthesized expression, is not an lvalue")end;local __b={dda}
|
||||
while __a:ConsumeSymbol(',')do local _ab,aab=_ba(ada)
|
||||
if not _ab then return false,aab end;__b[#__b+1]=aab end
|
||||
if not __a:ConsumeSymbol('=')then return false,a_a("`=` Expected.")end;local a_b={}local b_b,c_b=aaa(ada)if not b_b then return false,c_b end;a_b[1]=c_b;while
|
||||
__a:ConsumeSymbol(',')do local _ab,aab=aaa(ada)if not _ab then return false,aab end
|
||||
a_b[#a_b+1]=aab end;local d_b={}
|
||||
d_b.AstType='AssignmentStatement'd_b.Lhs=__b;d_b.Rhs=a_b;bda=d_b elseif
|
||||
dda.AstType=='CallExpr'or
|
||||
dda.AstType=='TableCallExpr'or dda.AstType=='StringCallExpr'then local __b={}__b.AstType='CallStatement'__b.Expression=dda;bda=__b else return false,
|
||||
a_a("Assignment Statement Expected")end end;bda.HasSemicolon=__a:ConsumeSymbol(';')return true,bda end
|
||||
local bca=lookupify{'end','else','elseif','until'}
|
||||
baa=function(ada)local bda={}bda.Scope=_aa(ada)bda.AstType='Statlist'local cda={}
|
||||
while not
|
||||
bca[__a:Peek().Data]and not __a:IsEof()do
|
||||
local dda,__b=aca(bda.Scope)if not dda then return false,__b end;cda[#cda+1]=__b end;bda.Body=cda;return true,bda end;local function cca()local ada=_aa()return baa(ada)end;local dca,_da=cca()
|
||||
return dca,_da end
|
||||
local _d=lookupify{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}
|
||||
local ad=lookupify{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}
|
||||
local bd=lookupify{'0','1','2','3','4','5','6','7','8','9'}
|
||||
function Format_Mini(cd)local dd,__a;local a_a=0
|
||||
local function b_a(d_a,_aa,aaa)
|
||||
if a_a>150 then a_a=0;return d_a.."\n".._aa end;aaa=aaa or' 'local baa,caa=d_a:sub(-1,-1),_aa:sub(1,1)
|
||||
if
|
||||
ad[baa]or _d[baa]or baa=='_'then
|
||||
if not
|
||||
(ad[caa]or _d[caa]or caa=='_'or bd[caa])then return d_a.._aa elseif caa=='('then
|
||||
return d_a..aaa.._aa else return d_a..aaa.._aa end elseif bd[baa]then
|
||||
if caa=='('then return d_a.._aa else return d_a..aaa.._aa end elseif baa==''then return d_a.._aa else
|
||||
if caa=='('then return d_a..aaa.._aa else return d_a.._aa end end end
|
||||
__a=function(d_a)local _aa=string.rep('(',d_a.ParenCount or 0)
|
||||
if
|
||||
d_a.AstType=='VarExpr'then if d_a.Local then _aa=_aa..d_a.Local.Name else
|
||||
_aa=_aa..d_a.Name end elseif d_a.AstType=='NumberExpr'then _aa=_aa..
|
||||
d_a.Value.Data elseif d_a.AstType=='StringExpr'then
|
||||
_aa=_aa..d_a.Value.Data elseif d_a.AstType=='BooleanExpr'then _aa=_aa..tostring(d_a.Value)elseif
|
||||
d_a.AstType=='NilExpr'then _aa=b_a(_aa,"nil")elseif d_a.AstType=='BinopExpr'then
|
||||
_aa=b_a(_aa,__a(d_a.Lhs))_aa=b_a(_aa,d_a.Op)_aa=b_a(_aa,__a(d_a.Rhs))elseif d_a.AstType==
|
||||
'UnopExpr'then _aa=b_a(_aa,d_a.Op)
|
||||
_aa=b_a(_aa,__a(d_a.Rhs))elseif d_a.AstType=='DotsExpr'then _aa=_aa.."..."elseif d_a.AstType=='CallExpr'then _aa=_aa..
|
||||
__a(d_a.Base)_aa=_aa.."("for i=1,#d_a.Arguments do _aa=_aa..
|
||||
__a(d_a.Arguments[i])
|
||||
if i~=#d_a.Arguments then _aa=_aa..","end end;_aa=_aa..")"elseif d_a.AstType==
|
||||
'TableCallExpr'then _aa=_aa..__a(d_a.Base)_aa=_aa..
|
||||
__a(d_a.Arguments[1])elseif d_a.AstType=='StringCallExpr'then
|
||||
_aa=_aa..__a(d_a.Base)_aa=_aa..d_a.Arguments[1].Data elseif
|
||||
d_a.AstType=='IndexExpr'then
|
||||
_aa=_aa..__a(d_a.Base).."["..__a(d_a.Index).."]"elseif d_a.AstType=='MemberExpr'then _aa=_aa..__a(d_a.Base)..
|
||||
d_a.Indexer..d_a.Ident.Data elseif
|
||||
d_a.AstType=='Function'then d_a.Scope:RenameVars()
|
||||
_aa=_aa.."function("
|
||||
if#d_a.Arguments>0 then for i=1,#d_a.Arguments do
|
||||
_aa=_aa..d_a.Arguments[i].Name
|
||||
if i~=#d_a.Arguments then _aa=_aa..","elseif d_a.VarArg then _aa=_aa..",..."end end elseif
|
||||
d_a.VarArg then _aa=_aa.."..."end;_aa=_aa..")"_aa=b_a(_aa,dd(d_a.Body))
|
||||
_aa=b_a(_aa,"end")elseif d_a.AstType=='ConstructorExpr'then _aa=_aa.."{"
|
||||
for i=1,#d_a.EntryList do
|
||||
local aaa=d_a.EntryList[i]
|
||||
if aaa.Type=='Key'then _aa=_aa.."["..
|
||||
__a(aaa.Key).."]="..__a(aaa.Value)elseif aaa.Type==
|
||||
'Value'then _aa=_aa..__a(aaa.Value)elseif aaa.Type=='KeyString'then
|
||||
_aa=_aa..
|
||||
aaa.Key.."="..__a(aaa.Value)end;if i~=#d_a.EntryList then _aa=_aa..","end end;_aa=_aa.."}"end
|
||||
_aa=_aa..string.rep(')',d_a.ParenCount or 0)a_a=a_a+#_aa;return _aa end
|
||||
local c_a=function(d_a)local _aa=''
|
||||
if d_a.AstType=='AssignmentStatement'then
|
||||
for i=1,#d_a.Lhs do
|
||||
_aa=_aa..__a(d_a.Lhs[i])if i~=#d_a.Lhs then _aa=_aa..","end end;if#d_a.Rhs>0 then _aa=_aa.."="
|
||||
for i=1,#d_a.Rhs do
|
||||
_aa=_aa..__a(d_a.Rhs[i])if i~=#d_a.Rhs then _aa=_aa..","end end end elseif
|
||||
d_a.AstType=='CallStatement'then _aa=__a(d_a.Expression)elseif d_a.AstType=='LocalStatement'then
|
||||
_aa=_aa.."local "
|
||||
for i=1,#d_a.LocalList do _aa=_aa..d_a.LocalList[i].Name;if i~=#
|
||||
d_a.LocalList then _aa=_aa..","end end
|
||||
if#d_a.InitList>0 then _aa=_aa.."="for i=1,#d_a.InitList do _aa=_aa..
|
||||
__a(d_a.InitList[i])
|
||||
if i~=#d_a.InitList then _aa=_aa..","end end end elseif d_a.AstType=='IfStatement'then
|
||||
_aa=b_a("if",__a(d_a.Clauses[1].Condition))_aa=b_a(_aa,"then")
|
||||
_aa=b_a(_aa,dd(d_a.Clauses[1].Body))
|
||||
for i=2,#d_a.Clauses do local aaa=d_a.Clauses[i]
|
||||
if aaa.Condition then
|
||||
_aa=b_a(_aa,"elseif")_aa=b_a(_aa,__a(aaa.Condition))
|
||||
_aa=b_a(_aa,"then")else _aa=b_a(_aa,"else")end;_aa=b_a(_aa,dd(aaa.Body))end;_aa=b_a(_aa,"end")elseif d_a.AstType=='WhileStatement'then
|
||||
_aa=b_a("while",__a(d_a.Condition))_aa=b_a(_aa,"do")_aa=b_a(_aa,dd(d_a.Body))
|
||||
_aa=b_a(_aa,"end")elseif d_a.AstType=='DoStatement'then _aa=b_a(_aa,"do")
|
||||
_aa=b_a(_aa,dd(d_a.Body))_aa=b_a(_aa,"end")elseif d_a.AstType=='ReturnStatement'then _aa="return"
|
||||
for i=1,#d_a.Arguments
|
||||
do _aa=b_a(_aa,__a(d_a.Arguments[i]))if i~=
|
||||
#d_a.Arguments then _aa=_aa..","end end elseif d_a.AstType=='BreakStatement'then _aa="break"elseif d_a.AstType=='RepeatStatement'then
|
||||
_aa="repeat"_aa=b_a(_aa,dd(d_a.Body))_aa=b_a(_aa,"until")
|
||||
_aa=b_a(_aa,__a(d_a.Condition))elseif d_a.AstType=='Function'then d_a.Scope:RenameVars()if d_a.IsLocal then
|
||||
_aa="local"end;_aa=b_a(_aa,"function ")if d_a.IsLocal then
|
||||
_aa=_aa..d_a.Name.Name else _aa=_aa..__a(d_a.Name)end;_aa=
|
||||
_aa.."("
|
||||
if#d_a.Arguments>0 then
|
||||
for i=1,#d_a.Arguments do _aa=_aa..
|
||||
d_a.Arguments[i].Name;if i~=#d_a.Arguments then _aa=_aa..","elseif d_a.VarArg then
|
||||
_aa=_aa..",..."end end elseif d_a.VarArg then _aa=_aa.."..."end;_aa=_aa..")"_aa=b_a(_aa,dd(d_a.Body))
|
||||
_aa=b_a(_aa,"end")elseif d_a.AstType=='GenericForStatement'then d_a.Scope:RenameVars()
|
||||
_aa="for "
|
||||
for i=1,#d_a.VariableList do
|
||||
_aa=_aa..d_a.VariableList[i].Name;if i~=#d_a.VariableList then _aa=_aa..","end end;_aa=_aa.." in"
|
||||
for i=1,#d_a.Generators do
|
||||
_aa=b_a(_aa,__a(d_a.Generators[i]))if i~=#d_a.Generators then _aa=b_a(_aa,',')end end;_aa=b_a(_aa,"do")_aa=b_a(_aa,dd(d_a.Body))
|
||||
_aa=b_a(_aa,"end")elseif d_a.AstType=='NumericForStatement'then _aa="for "_aa=_aa..
|
||||
d_a.Variable.Name.."="_aa=_aa..
|
||||
__a(d_a.Start)..","..__a(d_a.End)if d_a.Step then
|
||||
_aa=_aa..","..__a(d_a.Step)end;_aa=b_a(_aa,"do")
|
||||
_aa=b_a(_aa,dd(d_a.Body))_aa=b_a(_aa,"end")end;a_a=a_a+#_aa;return _aa end
|
||||
dd=function(d_a)local _aa=''d_a.Scope:RenameVars()for aaa,baa in pairs(d_a.Body)do
|
||||
_aa=b_a(_aa,c_a(baa),';')end;return _aa end;cd.Scope:RenameVars()return dd(cd)end
|
||||
local minify = function(cd)local dd,__a=ParseLua(cd)if not dd then return false,__a end
|
||||
return true,Format_Mini(__a)end
|
||||
|
||||
-- Packaging part:
|
||||
local args = table.pack(...)
|
||||
local projectPath = args[1] or "Basalt"
|
||||
local minifyProject = args[2]=="true" and true or false
|
||||
local outputFileName = args[3] or "basalt.lua"
|
||||
|
||||
assert(fs.isDir(projectPath), "Unable to find directory: "..projectPath)
|
||||
|
||||
local projectFiles = {
|
||||
"objects",
|
||||
"libraries",
|
||||
"Frame.lua",
|
||||
"loadObjects.lua",
|
||||
"Object.lua",
|
||||
"theme.lua",
|
||||
}
|
||||
local subDirs = {
|
||||
"objects", "libraries"
|
||||
}
|
||||
local mainFile = "main.lua"
|
||||
|
||||
local outputFile = io.open(outputFileName, "w")
|
||||
|
||||
if(outputFile==nil)then
|
||||
error("Can't open file "..outputFileName)
|
||||
end
|
||||
|
||||
outputFile:write([[
|
||||
local project = {}
|
||||
local packaged = true
|
||||
local baseRequire = require
|
||||
local require = function(path)
|
||||
for _,v in pairs(project)do
|
||||
for name,b in pairs(v)do
|
||||
if(name==path)then
|
||||
return b()
|
||||
end
|
||||
end
|
||||
end
|
||||
return baseRequire(path);
|
||||
end
|
||||
local getProject = function(subDir)
|
||||
if(subDir~=nil)then
|
||||
return project[subDir]
|
||||
end
|
||||
return project
|
||||
end
|
||||
]])
|
||||
|
||||
for k,v in pairs(subDirs)do
|
||||
outputFile:write("project['"..v.."'] = {}")
|
||||
end
|
||||
outputFile:write("project['default'] = {}")
|
||||
|
||||
local function writeNewPackage(subdir, name, path)
|
||||
if not(fs.isDir(path))then
|
||||
outputFile:write("project['"..subdir.."']['"..name.."'] = ".."function(...)")
|
||||
local file = io.open(path, "r")
|
||||
local fileData = file:read("*all")
|
||||
if(minifyProject)then
|
||||
local success, data = minify(fileData)
|
||||
if(success)then
|
||||
outputFile:write(data:gsub("]]", "] ]"):gsub("]]", "] ]").."\n")
|
||||
else
|
||||
print("Error: Can't minify "..path)
|
||||
end
|
||||
else
|
||||
outputFile:write(fileData:gsub("]]", "] ]"):gsub("]]", "] ]").."\n")
|
||||
end
|
||||
file:close()
|
||||
outputFile:write("end; \n")
|
||||
end
|
||||
end
|
||||
|
||||
for _,v in pairs(projectFiles)do
|
||||
if(fs.isDir(fs.combine(projectPath, v)))then
|
||||
for _,b in pairs(fs.list(fs.combine(projectPath, v)))do
|
||||
writeNewPackage(v, b:gsub(".lua", ""), fs.combine(fs.combine(projectPath, v), b))
|
||||
end
|
||||
else
|
||||
writeNewPackage("default", v:gsub(".lua", ""), fs.combine(projectPath, v))
|
||||
end
|
||||
end
|
||||
|
||||
local main = io.open(fs.combine(projectPath, mainFile), "r")
|
||||
local mainData = main:read("*all")
|
||||
if(minifyProject)then
|
||||
local success,data = minify(mainData)
|
||||
if(success)then
|
||||
outputFile:write(data)
|
||||
else
|
||||
print("Error: Can't minify "..fs.combine(projectPath, mainFile).." "..data)
|
||||
end
|
||||
else
|
||||
outputFile:write(mainData)
|
||||
end
|
||||
main:close()
|
||||
outputFile:close()
|
||||
14
docs/Home.md
@@ -1,12 +1,20 @@
|
||||
# Welcome to The Basalt Wiki!
|
||||
# Welcome to The Basalt Wiki
|
||||
|
||||
*Note: The Basalt Wiki is a work in progress. Please treat wiki errors the same as bugs and report them accordingly.*
|
||||
|
||||
Here you can find information about how to use Basalt as well as examples of functional Basalt code. The aim of Basalt is to improve user interaction through visual display.
|
||||
Basalt is a user-friendly UI framework for CC:Tweaked (also known as "ComputerCraft: Tweaked") - a popular Minecraft mod. It was developed to enhance user interaction through visual displays. In this wiki, you'll find information on how to use Basalt as well as examples of functional Basalt code.
|
||||
|
||||
This Website is made with the help of ChatGPT.
|
||||
|
||||
## About Basalt
|
||||
|
||||
Basalt is intended to be an easy-to-understand UI Framework designed for CC:Tweaked (Also know as "ComputerCraft: Tweaked") - a popular minecraft mod. For more information about CC:Tweaked, checkout the project's [wiki](https://tweaked.cc/) or [download](https://modrinth.com/mod/cc-tweaked).
|
||||
Basalt is an easy-to-understand UI framework designed to improve user interaction with CC:Tweaked. Some of its key features include:
|
||||
|
||||
- A set of pre-built UI components for creating interfaces quickly and easily.
|
||||
- A flexible layout system that allows users to create custom designs.
|
||||
- A powerful event handling system for managing user input and interaction.
|
||||
- Support for multiple screen resolutions and aspect ratios.
|
||||
- Extensive documentation and examples to help users get started quickly.
|
||||
|
||||
## Quick Demo
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
---
|
||||
|
||||
Thanks for checking out our wiki, join our discord for more help: [discord.gg/yM7kndJdJJ](discord.gg/yNNnmBVBpE)
|
||||
Thanks for checking out our wiki, join our discord for more help: [https://discord.gg/yM7kndJdJJ](discord.gg/yNNnmBVBpE)
|
||||
|
||||
BIN
docs/_media/dynamic-frames.mp4
Normal file
BIN
docs/_media/flexbox-example.png
Normal file
|
After Width: | Height: | Size: 4.2 KiB |
BIN
docs/_media/flexgrow-property.mp4
Normal file
BIN
docs/_media/frame-flexgrow-property.mp4
Normal file
BIN
docs/_media/frames-with-menubars.mp4
Normal file
BIN
docs/_media/frames-with-sidebar.mp4
Normal file
BIN
docs/_media/pane-example-1.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
docs/_media/pane-example-2.png
Normal file
|
After Width: | Height: | Size: 717 B |
BIN
docs/_media/resizable-frames.mp4
Normal file
BIN
docs/_media/scrollable-frames.mp4
Normal file
@@ -1,4 +1,4 @@
|
||||
- Getting Started
|
||||
- [Home](Home)
|
||||
- [Quick Start](home/Quick-Start)
|
||||
- [Installer](home/installer)
|
||||
- [How To](home/How-To)
|
||||
- [Download](home/download)
|
||||
|
||||
@@ -1,32 +1,40 @@
|
||||
[1.6 Docs](https://basalt.madefor.cc/docs1_6)
|
||||
|
||||
- About
|
||||
- [Home](Home.md)
|
||||
- [Quick Start](home/Quick-Start.md)
|
||||
- [Installer](home/installer)
|
||||
- [Home](/)
|
||||
- [How To](home/How-To)
|
||||
- [Download](home/download)
|
||||
- Objects
|
||||
- [Basalt](objects/Basalt.md)
|
||||
- [Object](objects/Object.md)
|
||||
- [VisualObject](objects/VisualObject.md)
|
||||
- [ChangeableObject](objects/ChangeableObject.md)
|
||||
- [Container](objects/Container.md)
|
||||
- [BaseFrame](objects/BaseFrame.md)
|
||||
- [MonitorFrame](objects/MonitorFrame.md)
|
||||
- [Frame](objects/Frame.md)
|
||||
- [Flexbox](objects/Flexbox.md)
|
||||
- [MovableFrame](objects/MovableFrame.md)
|
||||
- [ScrollableFrame](objects/ScrollableFrame.md)
|
||||
- [Button](objects/Button.md)
|
||||
- [Checkbox](objects/Checkbox.md)
|
||||
- [Dropdown](objects/Dropdown.md)
|
||||
- [Frame](objects/Frame.md)
|
||||
- [Graph](objects/Graph.md)
|
||||
- [Image](objects/Image.md)
|
||||
- [Input](objects/Input.md)
|
||||
- [Label](objects/Label.md)
|
||||
- [List](objects/List.md)
|
||||
- [Menubar](objects/Menubar.md)
|
||||
- [Dropdown](objects/Dropdown.md)
|
||||
- [Menubar](objects/Menubar.md)
|
||||
- [Radio](objects/Radio.md)
|
||||
- [Pane](objects/Pane.md)
|
||||
- [Program](objects/Program.md)
|
||||
- [Progressbar](objects/Progressbar.md)
|
||||
- [Radio](objects/Radio.md)
|
||||
- [Scrollbar](objects/Scrollbar.md)
|
||||
- [Slider](objects/Slider.md)
|
||||
- [Textfield](objects/Textfield.md)
|
||||
- [Animation](objects/Animation.md)
|
||||
- [Thread](objects/Thread.md)
|
||||
- [Treeview](objects/Treeview.md)
|
||||
- [Timer](objects/Timer.md)
|
||||
- Tips & Tricks
|
||||
- [Your Logic](tips/logic.md)
|
||||
- [Button coloring](tips/buttonColoring.md)
|
||||
- [Designing/Animating](tips/design.md)
|
||||
- [Dynamic Values](tips/dynamicvalues.md)
|
||||
- [XML](tips/xml.md)
|
||||
- Guides
|
||||
- [Introduction to Basalt](guides/introduction.md)
|
||||
|
||||
|
||||
3173
docs/bTemp.lua
Normal file
0
docs/docs1_6/.nojekyll
Normal file
19
docs/docs1_6/Home.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Welcome to The Basalt Wiki
|
||||
|
||||
*Note: The Basalt Wiki is a work in progress. Please treat wiki errors the same as bugs and report them accordingly.*
|
||||
|
||||
Here you can find information about how to use Basalt as well as examples of functional Basalt code. The aim of Basalt is to improve user interaction through visual display.
|
||||
|
||||
## About Basalt
|
||||
|
||||
Basalt is intended to be an easy-to-understand UI Framework designed for CC:Tweaked (Also known as "ComputerCraft: Tweaked") - a popular minecraft mod. For more information about CC:Tweaked, checkout the project's [wiki](https://tweaked.cc/) or [download](https://modrinth.com/mod/cc-tweaked).
|
||||
|
||||
## Quick Demo
|
||||
|
||||

|
||||
|
||||
## Questions & Bugs
|
||||
|
||||
Obviously NyoriE has implemented some easter eggs, *some people* call them "bugs". If you happen to discover one of these just make a new [Github Issue](https://github.com/Pyroxenium/Basalt/issues)
|
||||
|
||||
Additionally, if you have questions about Basalt or how to make use of it, feel free to create a new discussion on [Basalt's Discussion Board (Github)](https://github.com/Pyroxenium/Basalt/discussions), or ask in our [discord](https://discord.gg/yNNnmBVBpE).
|
||||
3
docs/docs1_6/_footer.md
Normal file
@@ -0,0 +1,3 @@
|
||||
---
|
||||
|
||||
Thanks for checking out our wiki, join our discord for more help: [discord.gg/yM7kndJdJJ](discord.gg/yNNnmBVBpE)
|
||||
BIN
docs/docs1_6/_media/basaltPreview2.gif
Normal file
|
After Width: | Height: | Size: 3.1 MiB |
BIN
docs/docs1_6/_media/dynamic-frames.mp4
Normal file
BIN
docs/docs1_6/_media/frames-with-menubars.mp4
Normal file
BIN
docs/docs1_6/_media/frames-with-sidebar.mp4
Normal file
BIN
docs/docs1_6/_media/installer.png
Normal file
|
After Width: | Height: | Size: 4.6 KiB |
BIN
docs/docs1_6/_media/logo.png
Normal file
|
After Width: | Height: | Size: 252 KiB |
BIN
docs/docs1_6/_media/pane-example-1.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
docs/docs1_6/_media/pane-example-2.png
Normal file
|
After Width: | Height: | Size: 717 B |
BIN
docs/docs1_6/_media/resizable-frames.mp4
Normal file
BIN
docs/docs1_6/_media/scrollable-frames.mp4
Normal file
4
docs/docs1_6/_navbar.md
Normal file
@@ -0,0 +1,4 @@
|
||||
- Getting Started
|
||||
- [Home](Home)
|
||||
- [How To](home/How-To)
|
||||
- [Download](home/download)
|
||||
32
docs/docs1_6/_sidebar.md
Normal file
@@ -0,0 +1,32 @@
|
||||
- About
|
||||
- [Home](home)
|
||||
- [How To](home/How-To)
|
||||
- [Download](home/download)
|
||||
- Objects
|
||||
- [Basalt](objects/Basalt.md)
|
||||
- [Object](objects/Object.md)
|
||||
- [Button](objects/Button.md)
|
||||
- [Checkbox](objects/Checkbox.md)
|
||||
- [Dropdown](objects/Dropdown.md)
|
||||
- [Frame](objects/Frame.md)
|
||||
- [Image](objects/Image.md)
|
||||
- [Input](objects/Input.md)
|
||||
- [Label](objects/Label.md)
|
||||
- [List](objects/List.md)
|
||||
- [Menubar](objects/Menubar.md)
|
||||
- [Pane](objects/Pane.md)
|
||||
- [Program](objects/Program.md)
|
||||
- [Progressbar](objects/Progressbar.md)
|
||||
- [Radio](objects/Radio.md)
|
||||
- [Scrollbar](objects/Scrollbar.md)
|
||||
- [Slider](objects/Slider.md)
|
||||
- [Textfield](objects/Textfield.md)
|
||||
- [Animation](objects/Animation.md)
|
||||
- [Thread](objects/Thread.md)
|
||||
- [Timer](objects/Timer.md)
|
||||
- Tips & Tricks
|
||||
- [Your Logic](tips/logic.md)
|
||||
- [Button coloring](tips/buttonColoring.md)
|
||||
- [Designing/Animating](tips/design.md)
|
||||
- [Dynamic Values](tips/dynamicvalues.md)
|
||||
- [XML](tips/xml.md)
|
||||
BIN
docs/docs1_6/favicon-16x16.png
Normal file
|
After Width: | Height: | Size: 1.0 KiB |
BIN
docs/docs1_6/favicon-32x32.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
docs/docs1_6/favicon.ico
Normal file
|
After Width: | Height: | Size: 15 KiB |
61
docs/docs1_6/home/How-To.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# How-To
|
||||
|
||||
After downloading the project you can finally start creating your own program and use basalt. The first thing you want to use in your program is always:
|
||||
|
||||
```lua
|
||||
local basalt = require("basalt")
|
||||
```
|
||||
|
||||
It doesn't matter if you're using the source folder or the minified/packed version of basalt. Both can be found by using require("basalt") without .lua.
|
||||
|
||||
Also to really run basalt you should use
|
||||
|
||||
```lua
|
||||
basalt.autoUpdate()
|
||||
```
|
||||
|
||||
somewhere on the bottom of your program. basalt.autoUpdate() starts the event listener and the draw handler.
|
||||
|
||||
## Example
|
||||
|
||||
Here is a fully working example of how a program could look like:
|
||||
|
||||
```lua
|
||||
local basalt = require("basalt") --> Load the basalt framework into the variable called "basalt"
|
||||
|
||||
--> Now we want to create a base frame, we call the variable "main" - by default everything you create is visible. (you don't need to use :show())
|
||||
local main = basalt.createFrame()
|
||||
|
||||
local button = main:addButton() --> Here we add our first button
|
||||
button:setPosition(4, 4) -- of course we want to change the default position of our button
|
||||
button:setSize(16, 3) -- and the default size.
|
||||
button:setText("Click me!") --> This method displays what the text of our button should look like
|
||||
|
||||
local function buttonClick() --> Let us create a function we want to call when the button gets clicked
|
||||
basalt.debug("I got clicked!")
|
||||
end
|
||||
|
||||
-- Now we just need to register the function to the buttons onClick event handlers, this is how we can achieve that:
|
||||
button:onClick(buttonClick)
|
||||
|
||||
|
||||
basalt.autoUpdate() -- As soon as we call basalt.autoUpdate, the event and draw handlers will listen to any incomming events (and draw if necessary)
|
||||
```
|
||||
|
||||
If you're like us and strive for succinct and beautiful code, here is a cleaner implementation of the code above:
|
||||
|
||||
```lua
|
||||
local basalt = require("basalt")
|
||||
|
||||
local main = basalt.createFrame()
|
||||
local button = main --> Basalt returns an instance of the object on most methods, to make use of "call-chaining"
|
||||
:addButton() --> This is an example of call chaining
|
||||
:setPosition(4,4)
|
||||
:setText("Click me!")
|
||||
:onClick(
|
||||
function()
|
||||
basalt.debug("I got clicked!")
|
||||
end)
|
||||
|
||||
basalt.autoUpdate()
|
||||
```
|
||||