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,6 +217,7 @@ 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
buildBackBetter = function()
c:newThread(function() c:newThread(function()
local dragging = false local dragging = false
local waiting = {} local waiting = {}
@ -225,11 +227,51 @@ function gui:newBase(typ,x, y, w, h, sx, sy, sw, sh)
local entered = false local entered = false
local moved = false local moved = false
local mx, my local mx, my
local movedFunc = thread:newFunction(function(self,ref,mx,my)
self:Pause()
thread.hold(function()
return ref:canPress(mx,my)
end)
ref.OnExit(ref,mx,my)
entered = false
self:Resume()
end)
local dragFunc = thread:newFunction(function(self, ref, mx, my)
self:Pause()
thread.hold(function() return not(love.mouse.isDown(dragbutton)) end)
if dragging then
global_drag = nil
dragging = false
ref.OnDragEnd:Fire(ref,mx,my)
end
self:Resume()
end)
local releaseFunc = thread:newFunction(function(self, ref, mx, my, i)
self:Pause()
thread.hold(function() return not(love.mouse.isDown(i)) end)
if pressed[i] then
pressed[i] = false
waiting[i] = false
ref.OnReleased:Fire(ref, i, mx, my)
end
self:Resume()
end)
while true do while true do
thread.hold(function() -- Only Progress thread if events are subscribed to! thread.hold(function() -- Only Progress thread if events are subscribed to!
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()) 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())
end) end)
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 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
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() local x, y, w, h = c:getAbsolutes()
mx, my = love.mouse.getPosition() mx, my = love.mouse.getPosition()
-- mouse moved -- mouse moved
@ -242,13 +284,7 @@ function gui:newBase(typ,x, y, w, h, sx, sy, sw, sh)
if moved and not entered and c:canPress(mx,my) then if moved and not entered and c:canPress(mx,my) then
entered = true entered = true
c.OnEnter:Fire(c,mx,my) c.OnEnter:Fire(c,mx,my)
c:newThread(function() movedFunc(movedFunc, c, mx, my)
thread.hold(function()
return not c:canPress(mx,my)
end)
c.OnExit:Fire(c,mx,my)
entered = false
end)
end end
-- pressed/released/drag events -- pressed/released/drag events
@ -260,48 +296,32 @@ function gui:newBase(typ,x, y, w, h, sx, sy, sw, sh)
end end
if love.mouse.isDown(i) and c:canPress(mx,my) then if love.mouse.isDown(i) and c:canPress(mx,my) then
if not pressed[i] then if not pressed[i] then
if draggable and love.mouse.isDown(dragbutton) and (not global_drag or global_drag==c) then if draggable and love.mouse.isDown(dragbutton) and (not global_drag or global_drag == c) then
if not dragging then if not dragging then
global_drag = c global_drag = c
c.OnDragStart:Fire(c, mx, my) c.OnDragStart:Fire(c, mx, my)
ox, oy = mx, my ox, oy = mx, my
c:newThread(function() dragFunc(dragFunc, c, mx, my)
thread.hold(function() return not(love.mouse.isDown(dragbutton)) end)
if dragging then
global_drag = nil
dragging = false
c.OnDragEnd:Fire(c,mx,my)
end
end).OnError(function()
global_drag = nil
dragging = false
end)
end end
dragging = true dragging = true
end 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
c:newThread(function() releaseFunc(releaseFunc, c, mx, my, i)
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).OnError(function()
buildBackBetter()
end) end)
end end
end buildBackBetter()
end
end
end).OnError(print)
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