This commit is contained in:
Ryan Ward 2022-09-29 12:40:10 -04:00
parent dce78866ca
commit 7976727dc9
2 changed files with 100 additions and 81 deletions

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
*multi
main.lua main.lua
conf.lua conf.lua
*.ttf *.ttf

View File

@ -180,6 +180,7 @@ end
-- Base Library -- Base Library
function gui:newBase(typ,x, y, w, h, sx, sy, sw, sh) function gui:newBase(typ,x, y, w, h, sx, sy, sw, sh)
local c = {} local c = {}
local buildBackBetter
local centerX = false local centerX = false
local centerY = false local centerY = false
local centering = false local centering = false
@ -216,92 +217,111 @@ function gui:newBase(typ,x, y, w, h, sx, sy, sw, sh)
c.OnMoved = multi:newConnection() c.OnMoved = multi:newConnection()
-- Mouse event thread -- Mouse event thread
c:newThread(function() buildBackBetter = function()
local dragging = false c:newThread(function()
local waiting = {} local dragging = false
local pressed = {} local waiting = {}
local ox, oy = 0, 0 local pressed = {}
local mox, moy = 0, 0 local ox, oy = 0, 0
local entered = false local mox, moy = 0, 0
local moved = false local entered = false
local mx, my local moved = false
while true do local mx, my
thread.hold(function() -- Only Progress thread if events are subscribed to! local movedFunc = thread:newFunction(function(self,ref,mx,my)
return c.active and (draggable or c.OnEnter:hasConnections() or c.OnExit:hasConnections() or c.WhilePressing:hasConnections() or c.OnPressed:hasConnections() or c.OnReleased:hasConnections() or c.OnDragStart:hasConnections() or c.OnDragging:hasConnections() or c.OnDragEnd:hasConnections()) self:Pause()
end) thread.hold(function()
thread.sleep(.001) -- Limits the potiential speed for events to 1/200. So 200 fps max, to be fair pressing mouse click 200 times by hand in a second is probably not possible return ref:canPress(mx,my)
local x, y, w, h = c:getAbsolutes()
mx, my = love.mouse.getPosition()
-- mouse moved
if mx~=mox or my~=moy then
moved = true
c.OnMoved:Fire(c, mx - mox, my - moy)
end
-- enter/exit
if moved and not entered and c:canPress(mx,my) then
entered = true
c.OnEnter:Fire(c,mx,my)
c:newThread(function()
thread.hold(function()
return not c:canPress(mx,my)
end)
c.OnExit:Fire(c,mx,my)
entered = false
end) end)
end ref.OnExit(ref,mx,my)
entered = false
-- pressed/released/drag events self:Resume()
for i=1,c.maxMouseButtons do end)
if dragging and i == dragbutton then local dragFunc = thread:newFunction(function(self, ref, mx, my)
c.OnDragging:Fire(c, mx - ox, my - oy) self:Pause()
ox = mx thread.hold(function() return not(love.mouse.isDown(dragbutton)) end)
oy = my if dragging then
global_drag = nil
dragging = false
ref.OnDragEnd:Fire(ref,mx,my)
end end
if love.mouse.isDown(i) and c:canPress(mx,my) then self:Resume()
if not pressed[i] then end)
if draggable and love.mouse.isDown(dragbutton) and (not global_drag or global_drag==c) then local releaseFunc = thread:newFunction(function(self, ref, mx, my, i)
if not dragging then self:Pause()
global_drag = c thread.hold(function() return not(love.mouse.isDown(i)) end)
c.OnDragStart:Fire(c, mx, my) if pressed[i] then
ox, oy = mx, my pressed[i] = false
c:newThread(function() waiting[i] = false
thread.hold(function() return not(love.mouse.isDown(dragbutton)) end) ref.OnReleased:Fire(ref, i, mx, my)
if dragging then end
global_drag = nil self:Resume()
dragging = false end)
c.OnDragEnd:Fire(c,mx,my) while true do
end thread.hold(function() -- Only Progress thread if events are subscribed to!
end).OnError(function() return c.active and (draggable or c.OnEnter:hasConnections() or c.OnExit:hasConnections() or c.WhilePressing:hasConnections() or c.OnPressed:hasConnections() or c.OnReleased:hasConnections() or c.OnDragStart:hasConnections() or c.OnDragging:hasConnections() or c.OnDragEnd:hasConnections())
global_drag = nil end)
dragging = false thread.sleep(.01) -- Limits the potiential speed for events to 1/200. So 200 fps max, to be fair pressing mouse click 200 times by hand in a second is probably not possible
end) if not love.mouse.isDown(1,2,3,4,5) then
dragging = false
waiting = {}
pressed = {}
ox, oy = 0, 0
mox, moy = 0, 0
entered = false
moved = false
mx, my = nil, nil
global_drag = nil
end
local x, y, w, h = c:getAbsolutes()
mx, my = love.mouse.getPosition()
-- mouse moved
if mx~=mox or my~=moy then
moved = true
c.OnMoved:Fire(c, mx - mox, my - moy)
end
-- enter/exit
if moved and not entered and c:canPress(mx,my) then
entered = true
c.OnEnter:Fire(c,mx,my)
movedFunc(movedFunc, c, mx, my)
end
-- pressed/released/drag events
for i=1,c.maxMouseButtons do
if dragging and i == dragbutton then
c.OnDragging:Fire(c, mx - ox, my - oy)
ox = mx
oy = my
end
if love.mouse.isDown(i) and c:canPress(mx,my) then
if not pressed[i] then
if draggable and love.mouse.isDown(dragbutton) and (not global_drag or global_drag == c) then
if not dragging then
global_drag = c
c.OnDragStart:Fire(c, mx, my)
ox, oy = mx, my
dragFunc(dragFunc, c, mx, my)
end
dragging = true
end end
dragging = true
end
c:newThread(function()
c.OnPressed:Fire(c, i, mx, my) c.OnPressed:Fire(c, i, mx, my)
end) end
end pressed[i] = true
pressed[i] = true -- Only process when the drag button turn is active
-- Only process when the drag button turn is active c.WhilePressing:Fire(c, i, mx, my)
c.WhilePressing:Fire(c, i, mx, my) if not waiting[i] then
if not waiting[i] then waiting[i] = true
waiting[i] = true releaseFunc(releaseFunc, c, mx, my, i)
c:newThread(function() end
thread.hold(function() return not(love.mouse.isDown(i)) end)
if pressed[i] then
pressed[i] = false
waiting[i] = false
c.OnReleased:Fire(c, i, mx, my)
end
end)
end end
end end
end end
end end).OnError(function()
end).OnError(print) buildBackBetter()
end)
end
buildBackBetter()
function c:OnUpdate(func) -- Not crazy about this approach, will probably rework this function c:OnUpdate(func) -- Not crazy about this approach, will probably rework this
if type(self)=="function" then func = self end if type(self)=="function" then func = self end
mainupdater(function() mainupdater(function()
@ -310,7 +330,7 @@ function gui:newBase(typ,x, y, w, h, sx, sy, sw, sh)
end end
local function centerthread() local function centerthread()
c:newThread("Object_Centering",function() c:newThread(function()
while true do while true do
thread.hold(function() thread.hold(function()
return centerX or centerY -- If the condition is true it acts like a yield return centerX or centerY -- If the condition is true it acts like a yield