Skip to content

Commit

Permalink
Merge pull request #107 from megagrump/clickEvents
Browse files Browse the repository at this point in the history
no more dropped click events at low framerate; reduce number of temp. tables
  • Loading branch information
flamendless committed Dec 21, 2021
2 parents 969c86e + d379cc0 commit db0a9de
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 18 deletions.
60 changes: 60 additions & 0 deletions Internal/Core/TablePool.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--[[
MIT License
Copyright (c) 2019-2021 Love2D Community <love2d.org>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]
local insert = table.insert
local TablePool = {}
TablePool.__index = TablePool
function TablePool:pull()
local count = #self
if count == 0 then return {} end
local result = self[count]
self[count] = nil
return result
end
function TablePool:pullClean()
local count = #self
if count == 0 then return {} end
local result = self[count]
self[count] = nil
for k in pairs(result) do
result[k] = nil
end
return result
end
function TablePool:push(t)
insert(self, t)
end
return function()
return setmetatable({}, TablePool)
end
1 change: 1 addition & 0 deletions Internal/Input/Common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ local Common = {}
Common.Event =
{
None = 0,
Pressed = 1,
Released = 2
}
Expand Down
48 changes: 30 additions & 18 deletions Internal/Input/Mouse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ local insert = table.insert
local Common = require(SLAB_PATH .. '.Internal.Input.Common')
local DrawCommands = require(SLAB_PATH .. '.Internal.Core.DrawCommands')
local TablePool = require(SLAB_PATH .. '.Internal.Core.TablePool')
local Mouse = {}
Expand All @@ -49,6 +50,7 @@ local MouseMovedFn = nil
local MousePressedFn = nil
local MouseReleasedFn = nil
local Events = {}
local eventPool = TablePool()
-- Custom cursors allow the developer to override any specific system cursor used. This system will also
-- allow developers to set an empty image to hide the cursor for specific states, such as mouse resize.
Expand All @@ -72,14 +74,14 @@ local function OnMouseMoved(X, Y, DX, DY, IsTouch)
end
local function PushEvent(Type, X, Y, Button, IsTouch, Presses)
insert(Events, {
Type = Type,
X = X,
Y = Y,
Button = Button,
IsTouch = IsTouch,
Presses = Presses
})
local ev = eventPool:pull()
ev.Type = Type
ev.X = X
ev.Y = Y
ev.Button = Button
ev.IsTouch = IsTouch
ev.Presses = Presses
insert(Events, 1, ev)
end
local function OnMousePressed(X, Y, Button, IsTouch, Presses)
Expand All @@ -101,20 +103,30 @@ local function OnMouseReleased(X, Y, Button, IsTouch, Presses)
end
local function ProcessEvents()
State.Buttons = {}
for k, v in pairs(State.Buttons) do
v.Type = Common.Event.None
end
local wasPressed = false
for i = #Events, 1, -1 do
local ev = Events[i]
-- delay release events until next frame
if ev.Type == Common.Event.Released and wasPressed then break end
wasPressed = ev.Type == Common.Event.Pressed
for I, V in ipairs(Events) do
if State.Buttons[V.Button] == nil then
State.Buttons[V.Button] = {}
if State.Buttons[ev.Button] == nil then
State.Buttons[ev.Button] = {}
end
local Button = State.Buttons[V.Button]
Button.Type = V.Type
Button.IsTouch = V.IsTouch
Button.Presses = V.Presses
end
local button = State.Buttons[ev.Button]
button.Type = ev.Type
button.IsTouch = ev.IsTouch
button.Presses = ev.Presses
Events = {}
eventPool:push(Events[i])
Events[i] = nil
end
end
function Mouse.Initialize(Args)
Expand Down

0 comments on commit db0a9de

Please sign in to comment.