--include "glib.lua" require ('physics') local vec=physics.Vect local mouse=vec(0,0) local millis=timer.getMilliSecCounter process = class() process.inputEnabled=true process.priority=0--1 is always. 2 is run at least once every two runs. 3 can't skip 4 runs in a row. 4 only runs if fps is above target. Also set 0 if disabling process. This only increments unsuc/sets unsuc to 0 upon timer run process.paintPriority=0--0 if a background process process.targetFPS=30--if fps dips below target, then if priority is not 1 it will skip running the process unless it meets the minimum run amount process.lastRun=0--set to millis() upon starting process process.lastPaint=0 process.numUnsucRuns=0--resets after a successful run process.numPaintUnsuc=0 function process:setDefaults() self.inputEnabled=true self.priority=1 self.paintPriority=1 self.targetFPS=30 self.lastRun=millis() self.lastPaint=millis() end function process:deactivate() self.priority=0 self.paintPriority=0 self.inputEnabled=false end function process:deactivatePrior() self.priority=0 self.paintPriority=0 end function process:setBackground(priority) if priority~=nil then self.priority=priority self.lastRun=millis() end self.paintPriority=0 end function process:setPaintable(priority) self.paintPriority=priority self.lastPaint=millis() end function process:setPriorities(prior, paintPrior, input) self.priority=prior self.paintPriority=paintPrior self.lastRun=millis() self.lastPaint=millis() if input~=nil then self.inputEnabled=input else self.inputEnabled=true end end function process:init() end function process:paint(gc, x, y, width, height) end function process:timer() end function process:charIn(ch) end function process:arrowKey(key) end function process:escapeKey() end function process:enterKey() end function process:tabKey() end function process:contextMenu() end function process:backtabKey() end function process:backspaceKey() end function process:clearKey() end function process:mouseMove(x, y) end function process:mouseDown(x, y) end function process:mouseUp() end function process:rightMouseDown(x, y) end function process:help() end function process:varChange(varList) end function process:Open() end function process:Close() end function process:activate() end local processes = {} function PushProcess(proc) proc:setDefaults() proc:Open() table.insert(processes, proc) disp:invalidate() end function PullProcess() if #processes > 0 then processes[#processes]:Close() table.remove(processes) if #processes>0 then processes[#processes]:activate() end disp:invalidate() end end -- Link events to processManager function on.paint(gc, x, y, width, height) if x~=nil then print("x: "..x) end for _, proc in pairs(processes) do if proc.paintPriority==1 then proc:paint(gc, x, y, width, height) elseif proc.paintPriority==2 then if millis()-proc.lastPaint>=1000/proc.targetFPS then proc:paint(gc) proc.lastPaint=millis() else if proc.numPaintUnsuc>=1 then proc:paint(gc, x, y, width, height) proc.numPaintUnsuc=0 proc.lastPaint=millis() else proc.numPaintUnsuc=proc.numPaintUnsuc+1 proc.lastPaint=millis() end end elseif proc.paintPriority==3 then if millis()-proc.lastPaint>=1000/proc.targetFPS then proc:paint(gc, x, y, width, height) proc.lastPaint=millis() else if proc.numPaintUnsuc>=3 then proc:paint(gc, x, y, width, height) proc.numPaintUnsuc=0 proc.lastPaint=millis() else proc.numPaintUnsuc=proc.numPaintUnsuc+1 proc.lastPaint=millis() end end elseif proc.paintPriority==4 then if millis()-proc.lastPaint>=1000/proc.targetFPS then proc:paint(gc, x, y, width, height) proc.lastPaint=millis() end end end end function on.timer() for _, proc in pairs(processes) do if proc.priority==1 then proc:timer() proc.lastRun=millis() elseif proc.priority==2 then if millis()-proc.lastRun<=1000/proc.targetFPS then proc:timer() proc.lastRun=millis() else if proc.numPaintUnsuc>=1 then proc:timer() proc.numPaintUnsuc=0 proc.lastRun=millis() else proc.numPaintUnsuc=proc.numPaintUnsuc+1 proc.lastRun=millis() end end elseif proc.priority==3 then if millis()-proc.lastRun<=1000/proc.targetFPS then proc:timer() proc.lastRun=millis() else if proc.numPaintUnsuc>=3 then proc:timer() proc.numPaintUnsuc=0 proc.lastRun=millis() else proc.numPaintUnsuc=proc.numPaintUnsuc+1 proc.lastRun=millis() end end elseif proc.priority==4 then if millis()-proc.lastRun<=1000/proc.targetFPS then proc:timer() proc.lastRun=millis() end end end end function on.charIn(ch) for _, proc in pairs(processes) do if proc.inputEnabled then proc:charIn(ch) end end --disp:invalidate() end function on.arrowKey(key) for _, proc in pairs(processes) do if proc.inputEnabled then proc:arrowKey(key) end end --disp:invalidate() end function on.escapeKey() for _, proc in pairs(processes) do if proc.inputEnabled then proc:escapeKey() end end --disp:invalidate() end function on.enterKey() for _, proc in pairs(processes) do if proc.inputEnabled then proc:enterKey() end end --disp:invalidate() end function on.tabKey() for _, proc in pairs(processes) do if proc.inputEnabled then proc:tabKey() end end --disp:invalidate() end function on.contextMenu() for _, proc in pairs(processes) do if proc.inputEnabled then proc:contextMenu() end end --disp:invalidate() end function on.backtabKey() for _, proc in pairs(processes) do if proc.inputEnabled then proc:backtabKey() end end --disp:invalidate() end function on.backspaceKey() for _, proc in pairs(processes) do if proc.inputEnabled then proc:backspaceKey() end end --disp:invalidate() end function on.clearKey() for _, proc in pairs(processes) do if proc.inputEnabled then proc:clearKey() end end --disp:invalidate() end function on.mouseDown(x, y) for _, proc in pairs(processes) do if proc.inputEnabled then proc:mouseDown(x, y) end end --disp:invalidate() end function on.mouseUp() for _, proc in pairs(processes) do if proc.inputEnabled then proc:mouseUp() end end end function on.mouseMove(x, y) mouse=vec(x, y) for _, proc in pairs(processes) do if proc.inputEnabled then proc:mouseMove(x, y) end end end function on.rightMouseDown(x, y) for _, proc in pairs(processes) do if proc.inputEnabled then proc:rightMouseDown(x, y) end end --disp:invalidate() end function on.help() for _, proc in pairs(processes) do if proc.inputEnabled then proc:help() end end --disp:invalidate() end function on.varChange(varList) for _, proc in pairs(processes) do if proc.inputEnabled then proc:varChange(varList) end end --disp:invalidate() end