Image.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 Cursor = require(SLAB_PATH .. '.Internal.Core.Cursor')
  21. local DrawCommands = require(SLAB_PATH .. '.Internal.Core.DrawCommands')
  22. local LayoutManager = require(SLAB_PATH .. '.Internal.UI.LayoutManager')
  23. local Mouse = require(SLAB_PATH .. '.Internal.Input.Mouse')
  24. local Stats = require(SLAB_PATH .. '.Internal.Core.Stats')
  25. local Style = require(SLAB_PATH .. '.Style')
  26. local Tooltip = require(SLAB_PATH .. '.Internal.UI.Tooltip')
  27. local Window = require(SLAB_PATH .. '.Internal.UI.Window')
  28. local Image = {}
  29. local Instances = {}
  30. local ImageCache = {}
  31. local function GetImage(Path)
  32. if ImageCache[Path] == nil then
  33. ImageCache[Path] = love.graphics.newImage(Path)
  34. local WrapH, WrapV = ImageCache[Path]:getWrap()
  35. end
  36. return ImageCache[Path]
  37. end
  38. local function GetInstance(Id)
  39. local Key = Window.GetId() .. '.' .. Id
  40. if Instances[Key] == nil then
  41. local Instance = {}
  42. Instance.Id = Id
  43. Instance.Image = nil
  44. Instances[Key] = Instance
  45. end
  46. return Instances[Key]
  47. end
  48. function Image.Begin(Id, Options)
  49. local StatHandle = Stats.Begin('Image', 'Slab')
  50. Options = Options == nil and {} or Options
  51. Options.Tooltip = Options.Tooltip == nil and "" or Options.Tooltip
  52. Options.Rotation = Options.Rotation == nil and 0 or Options.Rotation
  53. Options.Scale = Options.Scale == nil and 1 or Options.Scale
  54. Options.ScaleX = Options.ScaleX == nil and Options.Scale or Options.ScaleX
  55. Options.ScaleY = Options.ScaleY == nil and Options.Scale or Options.ScaleY
  56. Options.Color = Options.Color == nil and {1.0, 1.0, 1.0, 1.0} or Options.Color
  57. Options.SubX = Options.SubX == nil and 0.0 or Options.SubX
  58. Options.SubY = Options.SubY == nil and 0.0 or Options.SubY
  59. Options.SubW = Options.SubW == nil and 0.0 or Options.SubW
  60. Options.SubH = Options.SubH == nil and 0.0 or Options.SubH
  61. Options.WrapH = Options.WrapH == nil and "clamp" or Options.WrapH
  62. Options.WrapV = Options.WrapV == nil and "clamp" or Options.WrapV
  63. local Instance = GetInstance(Id)
  64. local WinItemId = Window.GetItemId(Id)
  65. if Instance.Image == nil then
  66. if Options.Image == nil then
  67. assert(Options.Path ~= nil, "Path to an image is required if no image is set!")
  68. Instance.Image = GetImage(Options.Path)
  69. else
  70. Instance.Image = Options.Image
  71. end
  72. end
  73. Instance.Image:setWrap(Options.WrapH, Options.WrapV)
  74. local W = Instance.Image:getWidth() * Options.ScaleX
  75. local H = Instance.Image:getHeight() * Options.ScaleY
  76. local UseSubImage = false
  77. if Options.SubW > 0.0 and Options.SubH > 0.0 then
  78. W = Options.SubW
  79. H = Options.SubH
  80. UseSubImage = true
  81. end
  82. LayoutManager.AddControl(W, H)
  83. local X, Y = Cursor.GetPosition()
  84. local MouseX, MouseY = Window.GetMousePosition()
  85. if not Window.IsObstructedAtMouse() and X <= MouseX and MouseX <= X + W and Y <= MouseY and MouseY <= Y + H then
  86. Tooltip.Begin(Options.Tooltip)
  87. Window.SetHotItem(WinItemId)
  88. end
  89. if UseSubImage then
  90. DrawCommands.SubImage(
  91. X,
  92. Y,
  93. Instance.Image,
  94. Options.SubX,
  95. Options.SubY,
  96. Options.SubW,
  97. Options.SubH,
  98. Options.Rotation,
  99. Options.ScaleX,
  100. Options.ScaleY,
  101. Options.Color)
  102. else
  103. DrawCommands.Image(X, Y, Instance.Image, Options.Rotation, Options.ScaleX, Options.ScaleY, Options.Color)
  104. end
  105. Cursor.SetItemBounds(X, Y, W, H)
  106. Cursor.AdvanceY(H)
  107. Window.AddItem(X, Y, W, H, WinItemId)
  108. Stats.End(StatHandle)
  109. end
  110. function Image.GetSize(Image)
  111. if Image ~= nil then
  112. local Data = Image
  113. if type(Image) == 'string' then
  114. Data = GetImage(Image)
  115. end
  116. if Data ~= nil then
  117. return Data:getWidth(), Data:getHeight()
  118. end
  119. end
  120. return 0, 0
  121. end
  122. return Image