Mouse.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. --[[
  2. MIT License
  3. Copyright (c) 2019 Mitchell Davis <coding.jackalope@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. --]]
  20. local Mouse =
  21. {
  22. }
  23. local State =
  24. {
  25. Button = {},
  26. WasButton = {},
  27. ClickTime = {},
  28. LastClickTime = {},
  29. X = 0.0,
  30. Y = 0.0,
  31. DeltaX = 0.0,
  32. DeltaY = 0.0
  33. }
  34. local Cursors = nil
  35. local DoubleClickTime = 0.25
  36. local CurrentCursor = "arrow"
  37. local PendingCursor = ""
  38. local function UpdateClickTime(Button)
  39. if Mouse.IsClicked(Button) then
  40. State.LastClickTime[Button] = State.ClickTime[Button]
  41. State.ClickTime[Button] = love.timer.getTime()
  42. end
  43. end
  44. function Mouse.Update()
  45. local LastX, LastY = State.X, State.Y
  46. State.X, State.Y = love.mouse.getPosition()
  47. State.DeltaX, State.DeltaY = State.X - LastX, State.Y - LastY
  48. local Button1 = love.mouse.isDown(1)
  49. local Button2 = love.mouse.isDown(2)
  50. local Button3 = love.mouse.isDown(3)
  51. State.WasButton[1] = State.Button[1]
  52. State.WasButton[2] = State.Button[2]
  53. State.WasButton[3] = State.Button[3]
  54. State.Button[1] = Button1
  55. State.Button[2] = Button2
  56. State.Button[3] = Button3
  57. UpdateClickTime(1)
  58. UpdateClickTime(2)
  59. UpdateClickTime(3)
  60. if Cursors == nil then
  61. Cursors = {}
  62. Cursors.Arrow = love.mouse.getSystemCursor('arrow')
  63. Cursors.SizeWE = love.mouse.getSystemCursor('sizewe')
  64. Cursors.SizeNS = love.mouse.getSystemCursor('sizens')
  65. Cursors.SizeNESW = love.mouse.getSystemCursor('sizenesw')
  66. Cursors.SizeNWSE = love.mouse.getSystemCursor('sizenwse')
  67. Cursors.IBeam = love.mouse.getSystemCursor('ibeam')
  68. end
  69. Mouse.SetCursor('arrow')
  70. end
  71. function Mouse.IsPressed(Button)
  72. return State.Button[Button]
  73. end
  74. function Mouse.IsClicked(Button)
  75. return State.Button[Button] and not State.WasButton[Button]
  76. end
  77. function Mouse.IsDoubleClicked(Button)
  78. if Mouse.IsClicked(Button) and State.LastClickTime[Button] ~= nil then
  79. return love.timer.getTime() - State.LastClickTime[Button] <= DoubleClickTime
  80. end
  81. return false
  82. end
  83. function Mouse.IsReleased(Button)
  84. return not State.Button[Button] and State.WasButton[Button]
  85. end
  86. function Mouse.Position()
  87. return State.X, State.Y
  88. end
  89. function Mouse.HasDelta()
  90. return State.DeltaX ~= 0.0 or State.DeltaY ~= 0.0
  91. end
  92. function Mouse.GetDelta()
  93. return State.DeltaX, State.DeltaY
  94. end
  95. function Mouse.IsDragging(Button)
  96. return Mouse.IsPressed(Button) and Mouse.HasDelta()
  97. end
  98. function Mouse.SetCursor(Type)
  99. if Cursors == nil then
  100. return
  101. end
  102. PendingCursor = Type
  103. end
  104. function Mouse.UpdateCursor()
  105. if PendingCursor ~= "" and PendingCursor ~= CurrentCursor then
  106. CurrentCursor = PendingCursor
  107. PendingCursor = ""
  108. local Type = CurrentCursor
  109. if Type == 'arrow' then
  110. love.mouse.setCursor(Cursors.Arrow)
  111. elseif Type == 'sizewe' then
  112. love.mouse.setCursor(Cursors.SizeWE)
  113. elseif Type == 'sizens' then
  114. love.mouse.setCursor(Cursors.SizeNS)
  115. elseif Type == 'sizenesw' then
  116. love.mouse.setCursor(Cursors.SizeNESW)
  117. elseif Type == 'sizenwse' then
  118. love.mouse.setCursor(Cursors.SizeNWSE)
  119. elseif Type == 'ibeam' then
  120. love.mouse.setCursor(Cursors.IBeam)
  121. end
  122. end
  123. end
  124. return Mouse