Atlas.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes Software License
  3. -- Version 2.1
  4. --
  5. -- Copyright (c) 2013, Esoteric Software
  6. -- All rights reserved.
  7. --
  8. -- You are granted a perpetual, non-exclusive, non-sublicensable and
  9. -- non-transferable license to install, execute and perform the Spine Runtimes
  10. -- Software (the "Software") solely for internal use. Without the written
  11. -- permission of Esoteric Software (typically granted by licensing Spine), you
  12. -- may not (a) modify, translate, adapt or otherwise create derivative works,
  13. -- improvements of the Software or develop new applications using the Software
  14. -- or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. -- trademark, patent or other intellectual property or proprietary rights
  16. -- notices on or in the Software, including any copy thereof. Redistributions
  17. -- in binary or source form must include this license and terms.
  18. --
  19. -- THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. -- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. -- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. -- EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. -- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. -- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. -- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. -- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. -- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. -- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. -------------------------------------------------------------------------------
  30. local AtlasPage = require 'lib/spine-lua/AtlasPage'
  31. local AtlasRegion = require 'lib/spine-lua/AtlasRegion'
  32. local Atlas = {}
  33. function Atlas.new(atlas, textureLoader)
  34. local self = {
  35. textureLoader = textureLoader,
  36. pages = {},
  37. regions = {}
  38. }
  39. local page = nil
  40. local function getLine(str)
  41. local index = str:find('\n')
  42. if not index then return str, nil end
  43. local line = str:sub(1, index):gsub('^%s+', ''):gsub('%s+$', '')
  44. return line, str:sub(index + 1)
  45. end
  46. local function getValues(str)
  47. local line, str = getLine(str)
  48. local colon = line:find(':')
  49. if not colon then return end
  50. local chunk = line:sub(colon + 1)
  51. local t = {}
  52. chunk:gsub('%s*([^,]+),?', function(val) table.insert(t, val) end)
  53. return str, unpack(t)
  54. end
  55. local line, str = '', atlas
  56. if not str then return nil end
  57. repeat
  58. line, str = getLine(str)
  59. if #line == 0 then
  60. page = nil
  61. elseif not page then
  62. page = AtlasPage.new()
  63. page.name = line
  64. str, page.width, page.height = getValues(str)
  65. str, page.format = getValues(str)
  66. str, page.minFilter, page.magFilter = getValues(str)
  67. local direction
  68. str, direction = getValues(str)
  69. page.uWrap, page.vWrap = 'none', 'none'
  70. if direction == 'xy' then page.uWrap, page.vWrap = 'repeat', 'repeat'
  71. elseif direction == 'x' then page.uWrap = 'repeat'
  72. elseif direction == 'y' then page.vWrap = 'repeat' end
  73. table.insert(self.pages, page)
  74. else
  75. local region = AtlasRegion.new()
  76. region.name = line
  77. region.page = page
  78. local rotate
  79. str, rotate = getValues(str)
  80. region.rotate = rotate == 'true'
  81. local x, y
  82. str, x, y = getValues(str)
  83. local width, height
  84. str, width, height = getValues(str)
  85. region.u, region.v = x / page.width, y / page.height
  86. if region.rotate then
  87. region.u2 = (x + height) / page.width
  88. region.v2 = (y + width) / page.height
  89. else
  90. region.u2 = (x + width) / page.width
  91. region.v2 = (y + height) / page.height
  92. end
  93. region.x, region.y, region.width, region.height = x, y, width, height
  94. str, region.originalWidth, region.originalHeight = getValues(str)
  95. str, region.offsetX, region.offsetY = getValues(str)
  96. str, region.index = getValues(str)
  97. for _, key in pairs({'x', 'y', 'width', 'height', 'originalWidth', 'originalHeight', 'offsetX', 'offsetY', 'index'}) do
  98. region[key] = tonumber(region[key])
  99. end
  100. table.insert(self.regions, region)
  101. end
  102. until not str:find('\n')
  103. function self:findRegion(name)
  104. for i, region in ipairs(self.regions) do
  105. if region.name == name then return region end
  106. end
  107. return nil
  108. end
  109. return self
  110. end
  111. return Atlas