Improved event system
Updated the event system to the current dev branch
This commit is contained in:
129
Basalt/main.lua
129
Basalt/main.lua
@@ -10,7 +10,7 @@ local moveThrottle = 300
|
|||||||
local dragThrottle = 50
|
local dragThrottle = 50
|
||||||
|
|
||||||
local baseTerm = term.current()
|
local baseTerm = term.current()
|
||||||
local version = "1.6.3"
|
local version = "1.6.4"
|
||||||
|
|
||||||
local projectDirectory = fs.getDir(table.pack(...)[2] or "")
|
local projectDirectory = fs.getDir(table.pack(...)[2] or "")
|
||||||
|
|
||||||
@@ -23,13 +23,26 @@ if not term.isColor or not term.isColor() then
|
|||||||
error('Basalt requires an advanced (golden) computer to run.', 0)
|
error('Basalt requires an advanced (golden) computer to run.', 0)
|
||||||
end
|
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()
|
local function stop()
|
||||||
updaterActive = false
|
updaterActive = false
|
||||||
baseTerm.clear()
|
baseTerm.clear()
|
||||||
baseTerm.setCursorPos(1, 1)
|
baseTerm.setCursorPos(1, 1)
|
||||||
|
for k,v in pairs(colors)do
|
||||||
|
if(type(v)=="number")then
|
||||||
|
baseTerm.setPaletteColor(v, colors.packRGB(table.unpack(defaultColors[k])))
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local basaltError = function(errMsg)
|
local function basaltError(errMsg)
|
||||||
baseTerm.clear()
|
baseTerm.clear()
|
||||||
baseTerm.setBackgroundColor(colors.black)
|
baseTerm.setBackgroundColor(colors.black)
|
||||||
baseTerm.setTextColor(colors.red)
|
baseTerm.setTextColor(colors.red)
|
||||||
@@ -79,6 +92,10 @@ local getTheme = function(name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local bInstance = {
|
local bInstance = {
|
||||||
|
getDynamicValueEventSetting = function()
|
||||||
|
return basalt.dynamicValueEvents
|
||||||
|
end,
|
||||||
|
|
||||||
getMainFrame = function()
|
getMainFrame = function()
|
||||||
return mainFrame
|
return mainFrame
|
||||||
end,
|
end,
|
||||||
@@ -176,8 +193,8 @@ end
|
|||||||
|
|
||||||
local stopped, moveX, moveY = nil, nil, nil
|
local stopped, moveX, moveY = nil, nil, nil
|
||||||
local moveTimer = nil
|
local moveTimer = nil
|
||||||
local function mouseMoveEvent(stp, x, y)
|
local function mouseMoveEvent(_, stp, x, y)
|
||||||
stopped, moveX, moveY = stopped, x, y
|
stopped, moveX, moveY = stp, x, y
|
||||||
if(moveTimer==nil)then
|
if(moveTimer==nil)then
|
||||||
moveTimer = os.startTimer(moveThrottle/1000)
|
moveTimer = os.startTimer(moveThrottle/1000)
|
||||||
end
|
end
|
||||||
@@ -197,7 +214,7 @@ local function dragHandlerTimer()
|
|||||||
activeFrame = mainFrame
|
activeFrame = mainFrame
|
||||||
end
|
end
|
||||||
|
|
||||||
local function mouseDragEvent(b, x, y)
|
local function mouseDragEvent(_, b, x, y)
|
||||||
btn, dragX, dragY = b, x, y
|
btn, dragX, dragY = b, x, y
|
||||||
if(dragThrottle<50)then
|
if(dragThrottle<50)then
|
||||||
dragHandlerTimer()
|
dragHandlerTimer()
|
||||||
@@ -208,79 +225,75 @@ local function mouseDragEvent(b, x, y)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function basaltUpdateEvent(event, p1, p2, p3, p4)
|
local function basaltUpdateEvent(event, ...)
|
||||||
if(basaltEvent:sendEvent("basaltEventCycle", event, p1, p2, p3, p4)==false)then return end
|
local a = {...}
|
||||||
|
if(basaltEvent:sendEvent("basaltEventCycle", event, ...)==false)then return end
|
||||||
|
if(event=="terminate")then basalt.stop() end
|
||||||
if(mainFrame~=nil)then
|
if(mainFrame~=nil)then
|
||||||
if (event == "mouse_click") then
|
local mouseEvents = {
|
||||||
mainFrame:mouseHandler(p1, p2, p3, false)
|
mouse_click = mainFrame.mouseHandler,
|
||||||
activeFrame = mainFrame
|
mouse_up = mainFrame.mouseUpHandler,
|
||||||
elseif (event == "mouse_drag") then
|
mouse_scroll = mainFrame.scrollHandler,
|
||||||
mouseDragEvent(p1, p2, p3)
|
mouse_drag = mouseDragEvent,
|
||||||
elseif (event == "mouse_up") then
|
mouse_move = mouseMoveEvent,
|
||||||
mainFrame:mouseUpHandler(p1, p2, p3, p4)
|
}
|
||||||
activeFrame = mainFrame
|
local mouseEvent = mouseEvents[event]
|
||||||
elseif (event == "mouse_scroll") then
|
if(mouseEvent~=nil)then
|
||||||
mainFrame:scrollHandler(p1, p2, p3, p4)
|
mouseEvent(mainFrame, ...)
|
||||||
activeFrame = mainFrame
|
drawFrames()
|
||||||
elseif (event == "mouse_move") then
|
return
|
||||||
mouseMoveEvent(p1, p2, p3)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if(event == "monitor_touch") then
|
if(event == "monitor_touch") then
|
||||||
if(monFrames[p1]~=nil)then
|
if(monFrames[p1]~=nil)then
|
||||||
monFrames[p1]:mouseHandler(1, p2, p3, true)
|
monFrames[p1]:mouseHandler(1, a[2], a[3], true)
|
||||||
activeFrame = monFrames[p1]
|
activeFrame = monFrames[p1]
|
||||||
end
|
end
|
||||||
if(count(monGroups)>0)then
|
if(count(monGroups)>0)then
|
||||||
for k,v in pairs(monGroups)do
|
for k,v in pairs(monGroups)do
|
||||||
v[1]:mouseHandler(1, p2, p3, true, p1)
|
v[1]:mouseHandler(1, a[2], a[3], true, a[1])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
drawFrames()
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if(activeFrame~=nil)then
|
||||||
|
local keyEvents = {
|
||||||
if(event == "char")then
|
char = activeFrame.charHandler,
|
||||||
if(activeFrame~=nil)then
|
key = activeFrame.keyHandler,
|
||||||
activeFrame:charHandler(p1)
|
key_up = activeFrame.keyUpHandler,
|
||||||
end
|
}
|
||||||
end
|
local keyEvent = keyEvents[event]
|
||||||
if(event == "key_up")then
|
if(keyEvent~=nil)then
|
||||||
if(activeFrame~=nil)then
|
if(event == "key")then
|
||||||
activeFrame:keyUpHandler(p1)
|
activeKey[a[1]] = true
|
||||||
end
|
elseif(event == "key_up")then
|
||||||
activeKey[p1] = false
|
activeKey[a[1]] = 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)
|
|
||||||
end
|
end
|
||||||
|
keyEvent(activeFrame, ...)
|
||||||
|
drawFrames()
|
||||||
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
handleSchedules(event, p1, p2, p3, p4)
|
|
||||||
|
if(event=="timer")and(a[1]==moveTimer)then
|
||||||
|
moveHandlerTimer()
|
||||||
|
elseif(event=="timer")and(a[1]==dragTimer)then
|
||||||
|
dragHandlerTimer()
|
||||||
|
else
|
||||||
|
for k, v in pairs(frames) do
|
||||||
|
v:eventHandler(event, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
handleSchedules(event, ...)
|
||||||
drawFrames()
|
drawFrames()
|
||||||
end
|
end
|
||||||
|
|
||||||
basalt = {
|
basalt = {
|
||||||
logging = false,
|
logging = false,
|
||||||
|
dynamicValueEvents = false,
|
||||||
setTheme = setTheme,
|
setTheme = setTheme,
|
||||||
getTheme = getTheme,
|
getTheme = getTheme,
|
||||||
drawFrames = drawFrames,
|
drawFrames = drawFrames,
|
||||||
@@ -337,9 +350,9 @@ basalt = {
|
|||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
update = function(event, p1, p2, p3, p4)
|
update = function(event, ...)
|
||||||
if (event ~= nil) then
|
if (event ~= nil) then
|
||||||
local ok, err = xpcall(basaltUpdateEvent, debug.traceback, event, p1, p2, p3, p4)
|
local ok, err = xpcall(basaltUpdateEvent, debug.traceback, event, ...)
|
||||||
if not(ok)then
|
if not(ok)then
|
||||||
basaltError(err)
|
basaltError(err)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user