utils.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 utils = {}
  30. local math_sqrt = math.sqrt
  31. local math_random = math.random
  32. utils.degRad = math.pi / 180
  33. function tablePrint (tt, indent, done)
  34. done = done or {}
  35. for key, value in pairs(tt) do
  36. local spaces = string.rep (" ", indent)
  37. if type(value) == "table" and not done [value] then
  38. done [value] = true
  39. print(spaces .. "{")
  40. utils.print(value, indent + 2, done)
  41. print(spaces .. "}")
  42. else
  43. io.write(spaces .. tostring(key) .. " = ")
  44. utils.print(value, indent + 2, done)
  45. end
  46. end
  47. end
  48. function utils.print (value, indent, done)
  49. indent = indent or 0
  50. if "nil" == type(value) then
  51. print(tostring(nil))
  52. elseif "table" == type(value) then
  53. local spaces = string.rep (" ", indent)
  54. print(spaces .. "{")
  55. tablePrint(value, indent + 2)
  56. print(spaces .. "}")
  57. elseif "string" == type(value) then
  58. print("\"" .. value .. "\"")
  59. else
  60. print(tostring(value))
  61. end
  62. end
  63. function utils.indexOf (haystack, needle)
  64. for i,value in ipairs(haystack) do
  65. if value == needle then return i end
  66. end
  67. return nil
  68. end
  69. function utils.copy (from, to)
  70. if not to then to = {} end
  71. for k,v in pairs(from) do
  72. to[k] = v
  73. end
  74. return to
  75. end
  76. function utils.newNumberArray (size)
  77. local a = {}
  78. local i = 1
  79. while i <= size do
  80. a[i] = 0
  81. i = i + 1
  82. end
  83. return a
  84. end
  85. function utils.newNumberArrayZero (size)
  86. local a = {}
  87. local i = 0
  88. while i < size do
  89. a[i] = 0
  90. i = i + 1
  91. end
  92. return a
  93. end
  94. function utils.setArraySize (array, size)
  95. if #array == size then return array end
  96. if #array < size then
  97. local i = #array + 1
  98. while i <= size do
  99. array[i] = 0
  100. i = i + 1
  101. end
  102. else
  103. local originalSize = #array
  104. local i = originalSize
  105. while i > size do
  106. array[i] = nil -- dirty trick to appease # without realloc
  107. i = i - 1
  108. end
  109. end
  110. return array
  111. end
  112. function utils.arrayCopy (src, srcOffset, dst, dstOffset, size)
  113. local n = srcOffset + size
  114. while srcOffset < n do
  115. dst[dstOffset] = src[srcOffset]
  116. dstOffset = dstOffset + 1
  117. srcOffset = srcOffset + 1
  118. end
  119. end
  120. function utils.clamp (value, min, max)
  121. if value < min then return min end
  122. if value > max then return max end
  123. return value
  124. end
  125. function utils.signum (value)
  126. if value < 0 then
  127. return -1
  128. elseif value > 0 then
  129. return 1
  130. else
  131. return 0
  132. end
  133. end
  134. -- Implements Java float modulo
  135. function utils.mod(a, b)
  136. if b < 0 then b = -b end
  137. if a < 0 then
  138. return -(-a % b)
  139. else
  140. return a % b
  141. end
  142. end
  143. function utils.randomTriangular(min, max)
  144. return utils.randomTriangularWith(min, max, (min + max) * 0.5)
  145. end
  146. function utils.randomTriangularWith(min, max, mode)
  147. local u = math.random()
  148. local d = max - min
  149. if (u <= (mode - min) / d) then return min + math_sqrt(u * d * (mode - min)) end
  150. return max - math_sqrt((1 - u) * d * (max - mode))
  151. end
  152. return utils