Advertisement
kensuaga

Counter - Movable and scaleable.

Sep 3rd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.15 KB | None | 0 0
  1. -- Universal Counter Tokens      original coded by: MrStump
  2. -- Scale and position adapted by kensuaga
  3.  
  4. --Center positions for the buttons
  5.     posX = 0
  6.     posY = 0.2
  7.     posZ = 0.5
  8.  
  9. --Scale of the buttons
  10.     scale = 0.5
  11.  
  12.  
  13. -- Do not change anything below these line unless you know what you are doing.
  14.  
  15. --Saves the count value into a table (data_to_save) then encodes it into the Tabletop save
  16. function onSave()
  17.     local data_to_save = {saved_count = count}
  18.     saved_data = JSON.encode(data_to_save)
  19.     return saved_data
  20. end
  21.  
  22. --Loads the saved data then creates the buttons
  23. function onload(saved_data)
  24.     generateButtonParamiters()
  25.     --Checks if there is a saved data. If there is, it gets the saved value for 'count'
  26.     if saved_data != '' then
  27.         local loaded_data = JSON.decode(saved_data)
  28.         count = loaded_data.saved_count
  29.     else
  30.         --If there wasn't saved data, the default value is set to 10.
  31.         count = 0
  32.     end
  33.  
  34.     --Generates the buttons after putting the count value onto the 'display' button
  35.     b_display.label = tostring(count)
  36.     if count >= 100 then
  37.         b_display.font_size = 360
  38.     else
  39.         b_display.font_size = 500
  40.     end
  41.     self.createButton(b_display)
  42.     self.createButton(b_plus)
  43.     self.createButton(b_minus)
  44.     self.createButton(b_plus5)
  45.     self.createButton(b_minus5)
  46. end
  47.  
  48. --Activates when + is hit. Adds 1 to 'count' then updates the display button.
  49. function increase()
  50.     count = count + 1
  51.     updateDisplay()
  52. end
  53.  
  54. --Activates when - is hit. Subtracts 1 from 'count' then updates the display button.
  55. function decrease()
  56.     --Prevents count from going below 0
  57.     if count > 0 then
  58.         count = count - 1
  59.         updateDisplay()
  60.     end
  61. end
  62.  
  63. --Activates when + is hit. Adds 5 to 'count' then updates the display button.
  64. function increase5()
  65.     count = count + 5
  66.     updateDisplay()
  67. end
  68.  
  69. --Activates when - is hit. Subtracts 5 from 'count' then updates the display button.
  70. function decrease5()
  71.     --Prevents count from going below 0
  72.     if count > 4 then
  73.         count = count - 5
  74.     else
  75.         count = 0
  76.     end
  77.     updateDisplay()
  78. end
  79.  
  80. function customSet()
  81.     local description = self.getDescription()
  82.     if description != '' and type(tonumber(description)) == 'number' then
  83.         self.setDescription('')
  84.         count = tonumber(description)
  85.         updateDisplay()
  86.     end
  87. end
  88.  
  89. --function that updates the display. I trigger it whenever I change 'count'
  90. function updateDisplay()
  91.     --If statement to resize font size if it gets too long
  92.     if count >= 100 then
  93.         b_display.font_size = 360
  94.     else
  95.         b_display.font_size = 500
  96.     end
  97.     b_display.label = tostring(count)
  98.     self.editButton(b_display)
  99. end
  100.  
  101.  
  102.  
  103. --This is activated when onload runs. This sets all paramiters for our buttons.
  104. --I do not have to put this all into a function, but I prefer to do it this way.
  105. function generateButtonParamiters()
  106.     b_display = {
  107.         index = 0, click_function = 'customSet', function_owner = self, label = '',
  108.         position = {posX, posY, posZ}, width = 600, height = 600, font_size = 500,
  109.         scale = {scale,scale,scale}
  110.     }
  111.     b_plus = {
  112.         click_function = 'increase', function_owner = self, label =  '+1',
  113.         position = {posX + 0.75*scale, posY, posZ + 0.26*scale},
  114.         width = 150, height = 300, font_size = 100, scale = {scale,scale,scale}
  115.     }
  116.     b_minus = {
  117.         click_function = 'decrease', function_owner = self, label =  '-1',
  118.         position = {posX + -0.75*scale, posY, posZ + 0.26*scale},
  119.         width = 150, height = 300, font_size = 100, scale = {scale,scale,scale}
  120.     }
  121.     b_plus5 = {
  122.         click_function = 'increase5', function_owner = self, label =  '+5',
  123.         position = {posX + 0.75*scale, posY, posZ + -0.29*scale},
  124.         width = 150, height = 230, font_size = 100, scale = {scale,scale,scale}
  125.     }
  126.     b_minus5 = {
  127.         click_function = 'decrease5', function_owner = self, label =  '-5',
  128.         position = {posX + -0.75*scale, posY, posZ + -0.29*scale},
  129.         width = 150, height = 230, font_size = 100, scale = {scale,scale,scale}
  130.     }
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement