utils.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes Software License v2.5
  3. --
  4. -- Copyright (c) 2013-2016, Esoteric Software
  5. -- All rights reserved.
  6. --
  7. -- You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. -- non-transferable license to use, install, execute, and perform the Spine
  9. -- Runtimes software and derivative works solely for personal or internal
  10. -- use. Without the written permission of Esoteric Software (see Section 2 of
  11. -- the Spine Software License Agreement), you may not (a) modify, translate,
  12. -- adapt, or develop new applications using the Spine Runtimes or otherwise
  13. -- create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. -- delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. -- or other intellectual property or proprietary rights notices on or in the
  16. -- Software, including any copy thereof. Redistributions in binary or source
  17. -- 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 SOFTWARE 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, BUSINESS INTERRUPTION, OR LOSS OF
  25. -- USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. -- IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. -- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. -- POSSIBILITY OF SUCH DAMAGE.
  29. -------------------------------------------------------------------------------
  30. local utils = {}
  31. utils.degRad = math.pi / 180
  32. function tablePrint (tt, indent, done)
  33. done = done or {}
  34. for key, value in pairs(tt) do
  35. local spaces = string.rep (" ", indent)
  36. if type(value) == "table" and not done [value] then
  37. done [value] = true
  38. print(spaces .. "{")
  39. utils.print(value, indent + 2, done)
  40. print(spaces .. "}")
  41. else
  42. io.write(spaces .. tostring(key) .. " = ")
  43. utils.print(value, indent + 2, done)
  44. end
  45. end
  46. end
  47. function utils.print (value, indent, done)
  48. indent = indent or 0
  49. if "nil" == type(value) then
  50. print(tostring(nil))
  51. elseif "table" == type(value) then
  52. local spaces = string.rep (" ", indent)
  53. print(spaces .. "{")
  54. tablePrint(value, indent + 2)
  55. print(spaces .. "}")
  56. elseif "string" == type(value) then
  57. print("\"" .. value .. "\"")
  58. else
  59. print(tostring(value))
  60. end
  61. end
  62. function utils.indexOf (haystack, needle)
  63. for i,value in ipairs(haystack) do
  64. if value == needle then return i end
  65. end
  66. return nil
  67. end
  68. function utils.copy (from, to)
  69. if not to then to = {} end
  70. for k,v in pairs(from) do
  71. to[k] = v
  72. end
  73. return to
  74. end
  75. function utils.newNumberArray (size)
  76. local a = {}
  77. local i = 1
  78. while i <= size do
  79. a[i] = 0
  80. i = i + 1
  81. end
  82. return a
  83. end
  84. function utils.newNumberArrayZero (size)
  85. local a = {}
  86. local i = 0
  87. while i < size do
  88. a[i] = 0
  89. i = i + 1
  90. end
  91. return a
  92. end
  93. function utils.setArraySize (array, size)
  94. if #array == size then return array end
  95. if #array < size then
  96. local i = #array + 1
  97. while i <= size do
  98. array[i] = 0
  99. i = i + 1
  100. end
  101. else
  102. local originalSize = #array
  103. local i = originalSize
  104. while i > size do
  105. array[i] = nil -- dirty trick to appease # without realloc
  106. i = i - 1
  107. end
  108. end
  109. return array
  110. end
  111. function utils.arrayCopy (src, srcOffset, dst, dstOffset, size)
  112. local n = srcOffset + size
  113. while srcOffset < n do
  114. dst[dstOffset] = src[srcOffset]
  115. dstOffset = dstOffset + 1
  116. srcOffset = srcOffset + 1
  117. end
  118. end
  119. function utils.clamp (value, min, max)
  120. if value < min then return min end
  121. if value > max then return max end
  122. return value
  123. end
  124. function utils.signum (value)
  125. if value < 0 then
  126. return -1
  127. elseif value > 0 then
  128. return 1
  129. else
  130. return 0
  131. end
  132. end
  133. -- Implements Java float modulo
  134. function utils.mod(a, b)
  135. if b < 0 then b = -b end
  136. if a < 0 then
  137. return -(-a % b)
  138. else
  139. return a % b
  140. end
  141. end
  142. return utils