Atlas.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes License Agreement
  3. -- Last updated May 1, 2019. Replaces all prior versions.
  4. --
  5. -- Copyright (c) 2013-2019, Esoteric Software LLC
  6. --
  7. -- Integration of the Spine Runtimes into software or otherwise creating
  8. -- derivative works of the Spine Runtimes is permitted under the terms and
  9. -- conditions of Section 2 of the Spine Editor License Agreement:
  10. -- http://esotericsoftware.com/spine-editor-license
  11. --
  12. -- Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. -- or otherwise create derivative works of the Spine Runtimes (collectively,
  14. -- "Products"), provided that each user of the Products must obtain their own
  15. -- Spine Editor license and redistribution of the Products in any form must
  16. -- include this license and copyright notice.
  17. --
  18. -- THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. -- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. -- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. -- NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. -- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. -- INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. -- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. -- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. -------------------------------------------------------------------------------
  29. local Atlas = {}
  30. function Atlas.parse(atlasPath, atlasBase)
  31. local function parseIntTuple4( l )
  32. local a,b,c,d = string.match( l , " ? ?%a+: ([+-]?%d+), ?([+-]?%d+), ?([+-]?%d+), ?([+-]?%d+)" )
  33. a,b,c,d = tonumber( a ), tonumber( b ), tonumber( c ), tonumber( d )
  34. return a and b and c and d and {a, b, c ,d}
  35. end
  36. local function parseIntTuple2( l )
  37. local a,b = string.match( l , " ? ?%a+: ([+-]?%d+), ?([+-]?%d+)" )
  38. a,b = tonumber( a ), tonumber( b )
  39. return a and b and {a, b}
  40. end
  41. if not atlasPath then
  42. error("Error: " .. atlasPath .. ".atlas" .. " doesn't exist!")
  43. return nil
  44. end
  45. local atlasLines = spine.utils.readFile( atlasPath, atlasBase )
  46. if not atlasLines then
  47. error("Error: " .. atlasPath .. ".atlas" .. " unable to read!")
  48. return nil
  49. end
  50. local pages = {}
  51. local it = string.gmatch(atlasLines, "(.-)\r?\n") -- iterate over lines
  52. for l in it do
  53. if #l == 0 then
  54. l = it()
  55. if l then
  56. local page = { name = l }
  57. l = it()
  58. page.size = parseIntTuple2( l )
  59. if page.size then
  60. l = it()
  61. end
  62. page.format = string.match( l, "%a+: (.+)" )
  63. page.filter = {string.match( it(), "%a+: (.+),(.+)" )}
  64. page.wrap = string.match( it(), "%a+: (.+)" )
  65. page.regions = {}
  66. table.insert( pages, page )
  67. else
  68. break
  69. end
  70. else
  71. local region = {name = l}
  72. region.rotate = string.match( it(), "%a+: (.+)" ) == "true"
  73. region.xy = parseIntTuple2( it() )
  74. region.size = parseIntTuple2( it() )
  75. l = it()
  76. region.splits = parseIntTuple4(l)
  77. if region.splits then
  78. l = it()
  79. region.pad = parseIntTuple4(l)
  80. if region.pad then
  81. l = it()
  82. end
  83. end
  84. region.orig = parseIntTuple2( l )
  85. region.offset = parseIntTuple2( it() )
  86. region.index = tonumber( string.match( it() , "%a+: ([+-]?%d+)" ) )
  87. table.insert( pages[#pages].regions, region )
  88. end
  89. end
  90. return pages
  91. end
  92. return Atlas