Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.87 KB | None | 0 0
  1. -- Universal Counter Tokens coded by: MrStump
  2.  
  3. --Saves the count value into a table (data_to_save) then encodes it into the Tabletop save
  4. function onSave()
  5. local data_to_save = {}
  6. data_to_save.saved_counts = {}
  7. for i,v in pairs(buttons.counts) do
  8. data_to_save.saved_counts[tostring(i)] = v or 0
  9. end
  10.  
  11. saved_data = JSON.encode(data_to_save)
  12.  
  13. --Uncomment this line to reset the save data, necessary when messing with variable names
  14. --saved_data = ''
  15. return saved_data
  16. end
  17.  
  18. --Loads the saved data then creates the buttons
  19. function onload(saved_data)
  20. buttons = {}
  21. buttons.index = 0
  22. buttons.counts = {}
  23.  
  24. buttons.params = {}
  25.  
  26. buttons.params.positions = {}
  27. buttons.params.positions.offsets = {}
  28. buttons.params.sizes = {}
  29. buttons.targetFunc = {}
  30.  
  31. ------------------------------
  32. --Edit below this line
  33. ------------------------------
  34.  
  35. --to add a location, simply add a new button.whatever table with a type
  36. buttons.centralTorso = {type = 'armor'}
  37. buttons.head = {type = 'armor'}
  38. buttons.rightTorso = {type = 'armor'}
  39. buttons.leftTorso = {type = 'armor'}
  40. buttons.rightArm = {type = 'armor'}
  41. buttons.leftArm = {type = 'armor'}
  42. buttons.rightLeg = {type = 'armor'}
  43. buttons.leftLeg = {type = 'armor'}
  44.  
  45. --if you added a location, you need to give it a position, coordinates are relative to the center of the model
  46. buttons.params.positions.centralTorso = {x = 0, y = 0.1, z = -0.75}
  47. buttons.params.positions.head = {x = 0, y = 0.1, z = -1.4}
  48. buttons.params.positions.rightTorso = {x = 0.47, y = 0.1, z = -1.1}
  49. buttons.params.positions.leftTorso = {x = -0.47, y = 0.1, z = -1.1}
  50. buttons.params.positions.rightArm = {x = 1, y = 0.1, z = -1}
  51. buttons.params.positions.leftArm = {x = -1, y = 0.1, z = -1}
  52. buttons.params.positions.rightLeg = {x = 0.67, y = 0.1, z = 1.1}
  53. buttons.params.positions.leftLeg = {x = -0.67, y = 0.1, z = 1.1}
  54.  
  55. --make a style of button by adding a new size
  56. buttons.params.sizes.armor = {}
  57. buttons.params.sizes.armor.display = {width = 200, height = 100, font = 100}
  58. buttons.params.sizes.armor.button = {width = 50, height = 50, font = 50}
  59. buttons.params.sizes.armor.offsets = {
  60. topButtons = {x = 0.1, y = 0, z = -0.17},
  61. bottomButtons = {x = 0.1, y = 0, z = 0.17}
  62. }
  63.  
  64. ------------------------------
  65. --Don't touch below this line
  66. ------------------------------
  67.  
  68. for i,v in pairs(buttons) do
  69. buttons.counts[tostring(i)] = 0
  70.  
  71. if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
  72. self.setVar(tostring(i) .. 'PlusOne', function () plus(tostring(i), 1) end)
  73. self.setVar(tostring(i) .. 'PlusFive', function () plus(tostring(i), 5) end)
  74. self.setVar(tostring(i) .. 'MinusOne', function () minus(tostring(i), 1) end)
  75. self.setVar(tostring(i) .. 'MinusFive', function () minus(tostring(i), 5) end)
  76. end
  77. end
  78.  
  79. generateButtonParamiters()
  80.  
  81. --Checks if there is a saved data. If there is, it gets the saved value for 'buttons.counts.ct'
  82.  
  83. if saved_data != '' then
  84. local loaded_data = JSON.decode(saved_data)
  85. buttons.counts = loaded_data.saved_counts
  86. for i,v in pairs(buttons) do
  87. if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
  88. if buttons.counts[tostring(i)] == nil then
  89. buttons.counts[tostring(i)] = 0
  90. end
  91. end
  92. end
  93. else
  94. for i,v in pairs(buttons) do
  95. if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
  96. buttons.counts[tostring(i)] = 0
  97. end
  98. end
  99. end
  100.  
  101. for i,v in pairs(buttons) do
  102.  
  103. if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
  104. buttons[tostring(i)].display.label = tostring(buttons.counts[tostring(i)])
  105.  
  106. self.createButton(buttons[tostring(i)].display)
  107. self.createButton(buttons[tostring(i)].plusOne)
  108. self.createButton(buttons[tostring(i)].plusFive)
  109. self.createButton(buttons[tostring(i)].minusOne)
  110. self.createButton(buttons[tostring(i)].minusFive)
  111. end
  112. end
  113. end
  114.  
  115. function dud()
  116.  
  117. end
  118.  
  119. function plus(location, amount)
  120. buttons.counts[location] = buttons.counts[location] + amount
  121. updateDisplay()
  122. end
  123.  
  124. function minus(location, amount)
  125. --Prevents count from going below 0
  126. if buttons.counts[location] > amount - 1 then
  127. buttons.counts[location] = buttons.counts[location] - amount
  128. else
  129. buttons.counts[location] = 0
  130. end
  131. updateDisplay()
  132. end
  133.  
  134.  
  135. --This is activated when onload runs. This sets all paramiters for our buttons.
  136. --I do not have to put this all into a function, but I prefer to do it this way.
  137.  
  138. function setupButton(targetFunc, positions, sizes, label, offsets, isLeft)
  139. local buttonInfo = {}
  140. buttonInfo.function_owner = self
  141. buttonInfo.index = buttons.index
  142. buttonInfo.click_function = targetFunc
  143.  
  144. if isLeft then
  145. orientationModifier = -1
  146. else
  147. orientationModifier = 1
  148. end
  149.  
  150. if offsets == nil then
  151. buttonInfo.position = {positions.x, positions.y, positions.z}
  152. else
  153. buttonInfo.position = {positions.x + (offsets.x) * orientationModifier, positions.y + offsets.y, positions.z + offsets.z}
  154. end
  155.  
  156. buttonInfo.width = sizes.width
  157. buttonInfo.height = sizes.height
  158. buttonInfo.font_size = sizes.font
  159. buttonInfo.label = label
  160.  
  161. buttons.index = buttons.index + 1
  162.  
  163. return buttonInfo
  164. end
  165.  
  166. function generateButtonParamiters()
  167. for i,v in pairs(buttons) do
  168. if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
  169. buttons[tostring(i)].display = setupButton('dud', buttons.params.positions[tostring(i)], buttons.params.sizes[buttons[tostring(i)].type].display, '', nil, false)
  170. buttons[tostring(i)].plusOne = setupButton(tostring(i) .. 'PlusOne', buttons.params.positions[tostring(i)], buttons.params.sizes[buttons[tostring(i)].type].button, '+1', buttons.params.sizes[buttons[tostring(i)].type].offsets.bottomButtons, false)
  171. buttons[tostring(i)].plusFive = setupButton(tostring(i) .. 'PlusFive', buttons.params.positions[tostring(i)], buttons.params.sizes[buttons[tostring(i)].type].button, '+5', buttons.params.sizes[buttons[tostring(i)].type].offsets.topButtons, false)
  172. buttons[tostring(i)].minusOne = setupButton(tostring(i) .. 'MinusOne', buttons.params.positions[tostring(i)], buttons.params.sizes[buttons[tostring(i)].type].button, '-1', buttons.params.sizes[buttons[tostring(i)].type].offsets.bottomButtons, true)
  173. buttons[tostring(i)].minusFive = setupButton(tostring(i) .. 'MinusFive', buttons.params.positions[tostring(i)], buttons.params.sizes[buttons[tostring(i)].type].button, '-5',buttons.params.sizes[buttons[tostring(i)].type].offsets.topButtons, true)
  174. end
  175. end
  176.  
  177. end
  178.  
  179.  
  180. function updateDisplay()
  181. for i,v in pairs(buttons) do
  182. if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
  183. buttons[tostring(i)].display.label = tostring(buttons.counts[tostring(i)])
  184. self.editButton(buttons[tostring(i)].display)
  185. end
  186. end
  187. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement