CursorMgr.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ---@class CursorMgr
  2. ---@field LeftMouseDown function
  3. ---@field LeftMouseUp function
  4. ---@field LeftMouseHold function
  5. ---@field RightMouseDown function
  6. ---@field RightMouseUp function
  7. ---@field RightMouseHold function
  8. ---@field PointIn function
  9. ---@field PointExit function
  10. CursorMgr = class()
  11. local this = CursorMgr
  12. this. mSetCursor = CS.TCFramework.CursorUtil
  13. ---this. sceneTouch = SceneTouch
  14. this. PointIn = false
  15. this. interval = 5
  16. this. time = 0
  17. this.CursorChanged = false
  18. ---@type Vector2
  19. this.cursorOffset = nil
  20. this.CursorIcon = {
  21. Normal = { atlas = "", src = "Cursor/Cursor_Normal.png" },
  22. Select = { atlas = "", src = "Cursor/Cursor_Select.png" },
  23. Catch = { atlas = "", src = "Cursor/Cursor_Catch.png" }
  24. }
  25. function this.SetCursor(tex, offset)
  26. this.mSetCursor.SetCursor(tex, offset)
  27. end
  28. function this.SetSprite(atlasName, iconName)
  29. ---@type UnityEngine.Texture2D
  30. local t = SL:LoadAsset(iconName, typeof(CS.UnityEngine.Texture2D)).res
  31. if not this.cursorOffset then
  32. this.cursorOffset = Vector2(t.width / 4, t.height / 4)
  33. end
  34. this.SetCursor(t, this.cursorOffset)
  35. end
  36. function this.OnGoing()
  37. this.time = this.time + 1
  38. if this.time < this.interval then
  39. return
  40. end
  41. this.PointIn = false
  42. this.time = 0
  43. local hitX, hitY, hitZ = CSPreScene.LinearCastGround(SL.MainCamera:GetCamera()) --MainCamera.camera)
  44. local coord = Scene.WorldToTile(hitX, hitY, hitZ)
  45. ---@param cell Cell
  46. local cell = CellManager.GetCell(coord.x, coord.z)
  47. if cell then
  48. if cell.ids and table.count(cell.ids) >= 1 and DebugFlag.LogEnable then
  49. local id = table.getKey(cell.ids, 1)
  50. local item = DropItemManager.GetItem(id)
  51. if item and DebugFlag.LogEnable then
  52. this.SetSprite(this.CursorIcon.Catch.atlas, this.CursorIcon.Catch.src)
  53. this.PointIn = true
  54. this.CursorChanged = true
  55. logError("鼠标光标:悬浮状态" .. "#" .. this.CursorIcon.Catch.atlas .. "#" .. this.CursorIcon.Catch.src)
  56. end
  57. end
  58. end
  59. end
  60. function this.SetDefault()
  61. this.SetSprite(this.CursorIcon.Normal.atlas, this.CursorIcon.Normal.src)
  62. this.CursorChanged = false
  63. end
  64. function this.Init()
  65. this.LeftMouseDown = function()
  66. this.SetSprite(this.CursorIcon.Select.atlas, this.CursorIcon.Select.src)
  67. this.CursorChanged = true
  68. end
  69. this.LeftMouseHold = function()
  70. this.SetSprite(this.CursorIcon.Select.atlas, this.CursorIcon.Select.src)
  71. this.CursorChanged = true
  72. end
  73. this.SetDefault()
  74. end
  75. function this.Update()
  76. if Misc.IsOnUI() or (not SL:GetIsInGame()) then
  77. return
  78. end
  79. if (Input.GetMouseButtonDown(0) and this.LeftMouseDown) then
  80. this.LeftMouseDown()
  81. elseif (Input.GetMouseButtonUp(0) and this.LeftMouseUp) then
  82. this.LeftMouseUp()
  83. elseif (Input.GetMouseButton(0) and this.LeftMouseHold) then
  84. this.LeftMouseHold()
  85. elseif (Input.GetMouseButtonDown(1) and this.RightMouseDown) then
  86. this. RightMouseDown()
  87. elseif (Input.GetMouseButtonUp(1) and this.RightMouseUp) then
  88. this. RightMouseUp()
  89. elseif (Input.GetMouseButton(1) and this.RightMouseHold) then
  90. this.RightMouseHold()
  91. else
  92. this.OnGoing()
  93. if not this.PointIn and this.CursorChanged then
  94. this.SetDefault()
  95. end
  96. end
  97. end