Advertisement
Guest User

af

a guest
Jul 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. -- This module displays a radial (circular) progress indicator based on images and config information
  2. -- that is generated at https://eryn.io/RadialSpriteSheetGenerator/
  3. -- @readme https://github.com/evaera/RadialSpriteSheetGenerator/blob/master/README.md
  4. -- @author evaera
  5.  
  6. local HttpService = game:GetService("HttpService")
  7.  
  8. local RadialImage = { _version = 1 }
  9. RadialImage.__index = RadialImage
  10.  
  11. local ConfigurationProperties = {
  12. version = "number";
  13. size = "number";
  14. count = "number";
  15. columns = "number";
  16. rows = "number";
  17. images = "table";
  18. }
  19.  
  20. function RadialImage.new(config, label)
  21. if type(config) == "string" then
  22. config = HttpService:JSONDecode(config)
  23. elseif type(config) ~= "table" then
  24. error("Argument #1 (configuration) must be a JSON string or table.", 2)
  25. end
  26.  
  27. for k, v in pairs(config) do
  28. if ConfigurationProperties[k] == nil then
  29. error(("Invalid property name in Radial Image configuration: %s"):format(k), 2)
  30. end
  31.  
  32. if type(v) ~= ConfigurationProperties[k] then
  33. error(("Invalid property type %q in Radial Image configuration: must be a %s."):format(k, ConfigurationProperties[k]), 2)
  34. end
  35. end
  36.  
  37. if config.version ~= RadialImage._version then
  38. error(("Passed configuration version does not match this module's version (which is %d)"):format(RadialImage._version), 2)
  39. end
  40.  
  41. local self = { config = config; label = label }
  42. setmetatable(self, RadialImage)
  43.  
  44. return self
  45. end
  46.  
  47. function RadialImage:GetFromAlpha(alpha)
  48. if type(alpha) ~= "number" then
  49. error("Argument #1 (alpha) to GetFromAlpha must be a number.", 2)
  50. end
  51.  
  52. local count, size, columns, rows = self.config.count, self.config.size, self.config.columns, self.config.rows
  53. local index = alpha >= 1 and count - 1 or math.floor(alpha * count)
  54. local page = math.floor(index / (columns * rows)) + 1
  55. local pageIndex = index - (columns * rows * (page - 1))
  56. local x = (pageIndex % columns) * size
  57. local y = math.floor(pageIndex / rows) * size
  58.  
  59. return x, y, page
  60. end
  61.  
  62. function RadialImage:UpdateLabel(alpha, label)
  63. label = label or self.label
  64.  
  65. if type(alpha) ~= "number" then
  66. error("Argument #1 (alpha) to UpdateLabel must be a number.", 2)
  67. end
  68.  
  69. if typeof(label) ~= "Instance" or not (label:IsA("ImageLabel") or label:IsA("ImageButton")) then
  70. error("Attempt to update label but no label has been given. Either pass the label as argument #2 to \"new\", or as argument #2 to \"UpdateLabel\".", 2)
  71. end
  72.  
  73. local x, y, page = self:GetFromAlpha(alpha)
  74.  
  75. label.ImageRectSize = Vector2.new(self.config.size, self.config.size)
  76. label.ImageRectOffset = Vector2.new(x, y)
  77. label.Image = alpha <= 0 and "" or self.config.images[page]
  78. end
  79.  
  80. return RadialImage
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement