MangaReader/GuiManager/Core/EventDefinitions.int
2020-02-18 21:01:55 -05:00

202 lines
5.2 KiB
Plaintext

local multi, thread = require("multi").init()
local buttonConv = {"l","r","m","x1","x2"} -- For the old stuff
function gui:addThread(t)
table.insert(self.threads,t)
end
function gui:addConn(t)
table.insert(self.conns,t)
end
function gui:OnClicked(func)
if not self.clickEvnt then
self.clickEvnt = true
self._connClicked = multi:newConnection()
self:addConn(self._connClicked(func))
self:addThread(multi:newThread(self.Name.."_Updater",function()
while true do
thread.hold(function() return self.Active or not self.Destroyed end)
if love.mouse.isDown(1) and self:canPress() then
self._connClicked:Fire("1",self,love.mouse.getX()-self.x,love.mouse.getY()-self.y)
end
if love.mouse.isDown(2) and self:canPress() then
self._connClicked:Fire("r",self,love.mouse.getX()-self.x,love.mouse.getY()-self.y)
end
if love.mouse.isDown(3) and self:canPress() then
self._connClicked:Fire("m",self,love.mouse.getX()-self.x,love.mouse.getY()-self.y)
end
if love.mouse.isDown(4) and self:canPress() then
self._connClicked:Fire("x1",self,love.mouse.getX()-self.x,love.mouse.getY()-self.y)
end
if love.mouse.isDown(5) and self:canPress() then
self._connClicked:Fire("x2",self,love.mouse.getX()-self.x,love.mouse.getY()-self.y)
end
if self.Destroyed then
thread.kill()
end
end
end))
else
self:addConn(self._connClicked(func))
end
end
function gui:OnPressed(func)
self:addConn(multi.OnMousePressed(function(x,y,b)
if self:canPress() then
func(buttonConv[b],self,x,y)
end
end))
end
function gui:OnPressedOuter(func)
self:addConn(multi.OnMousePressed(function(x,y,b)
if not(self:canPress()) then
func(buttonConv[b],self)
end
end,nil,1))
end
function gui:OnReleased(func)
self:addConn(multi.OnMouseReleased(function(x,y,b)
if self:canPress() then
func(buttonConv[b],self,x,y)
end
end))
end
function gui:OnReleasedOuter(func)
self:addConn(multi.OnMouseReleased(function(x,y,b)
if not(self:canPress()) then
func(buttonConv[b],self)
end
end,nil,1))
end
function gui:OnUpdate(func)
if not self.updateEvnt then
self._connUpdate = multi:newConnection()
self:addConn(self._connUpdate(func))
self.updateEvnt = true
self:addThread(multi:newThread(self.Name.."_Updater",function()
while true do
thread.hold(function() return self.Active end)
self._connUpdate:Fire(self)
end
end))
else
self:addConn(self._connUpdate(func))
end
end
function gui:OnMouseMoved(func)
self:addConn(multi.OnMouseMoved(function(x,y,dx,dy)
if self:canPress() then
func(self,x-self.x,y-self.y,dx,dy)
end
end,nil,1))
end
gui.WhileHovering=gui.OnMouseMoved -- To keep older features working
local mbenter = multi:newConnection()
function gui:OnMouseEnter(func)
self.HE=false
self:addConn(mbenter(func))
self:OnMouseMoved(function()
if self.HE == false then
self.HE=true
self._HE = true
mbenter:Fire(self)
end
end)
end
function gui:OnMouseExit(func)
if not self.exitEvnt then
self._connExit = multi:newConnection()
self:addConn(self._connExit(func))
self.exitEvnt = true
self.HE=false
self:addThread(multi:newThread(self.Name.."_OnExit",function()
while true do
thread.hold(function() return self.HE or self.Destroyed end)
if not(self:canPress()) then
self.HE=false
self._connExit:Fire(self)
end
if self.Destroyed then
thread.kill()
end
end
end))
else
self:addConn(self._connExit(func))
end
end
function gui:OnMouseWheelMoved(func)
self:addConn(multi.OnMouseWheelMoved(function(...)
if self:canPress() then
func(self,...)
end
end))
end
function gui:enableDragging(bool)
self.draggable = bool
if self.dragEvnt then
return
end
self.dragEvnt = true
self._connDragStart = multi:newConnection()
self._connDragging = multi:newConnection()
self._connDragEnd = multi:newConnection()
self.hasDrag = false
local startX
local startY
self:OnPressed(function(b,self,x,y)
if b~=self.dragbut or not(self.draggable) then return end
self._connDragStart:Fire(self)
self.hasDrag = true
startX = x
startY = y
end)
multi.OnMouseMoved(function(x,y,dx,dy)
if self.hasDrag and self.draggable then
self:Move(dx,dy)
self._connDragging:Fire(self)
end
end)
self:addConn(multi.OnMouseReleased(function(x,y,b)
if buttonConv[b]~=self.dragbut or not(self.draggable) or not(self.hasDrag) then return end
self.hasDrag = false
startX = nil
startY = nil
self._connDragEnd:Fire(self)
end))
end
function gui:OnDragStart(func)
if not self.dragEvnt then
self:enableDragging(true)
self:addConn(self._connDragStart(func))
else
self:addConn(self._connDragStart(func))
end
end
function gui:OnDragging(func)
if not self.dragEvnt then
self:enableDragging(true)
self:addConn(self._connDragging(func))
else
self:addConn(self._connDragging(func))
end
end
function gui:OnDragEnd(func)
if not self.dragEvnt then
self:enableDragging(true)
self:addConn(self._connDragEnd(func))
else
self:addConn(self._connDragEnd(func))
end
end
function gui:OnHotKey(key,func)
local tab=key:split("+")
self:addConn(multi.OnKeyPressed(function()
for i=1,#tab do
if not(love.keyboard.isDown(tab[i])) then
return
end
end
func(self)
end))
end
gui.addHotKey=gui.OnHotKey
gui.setHotKey=gui.OnHotKey