Advertisement
Kijan

Counter on Card

Jun 8th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.74 KB | None | 0 0
  1. MIN_VALUE = 0
  2. MAX_VALUE = 50
  3. value = 0
  4. backValue = 0
  5.  
  6. f_size = 600
  7.  
  8. function updateSave()
  9.    local data_to_save = {value, backValue}
  10.    saved_data = JSON.encode(data_to_save)
  11.    self.script_state = saved_data
  12. end
  13.  
  14. function onload(saved_data)
  15.    if saved_data ~= "" then
  16.       local loaded_data = JSON.decode(saved_data)
  17.       --Set up information off of loaded_data
  18.       if loaded_data[1] ~= nil then value = loaded_data[1] else value = MIN_VALUE end
  19.       if loaded_data[2] ~= nil then backValue = loaded_data[2] else backValue = MIN_VALUE end
  20.    else
  21.       --Set up information for if there is no saved saved data
  22.       value = MIN_VALUE
  23.       backValue = MIN_VALUE
  24.    end
  25.  
  26.    createBtns()
  27. end
  28.  
  29.  
  30. --Beginning Setup
  31.  
  32.  
  33. --Make setup button
  34. function createBtns()
  35.  
  36.    local pos = { - 0.9, 0.5, - 1.1}
  37.    local backPos = {0.9, - 0.5, - 1.1}
  38.    local rot = {0, 0, 0}
  39.    local backRot = {0, 0, 180}
  40.    local h = 500
  41.    local w = 500
  42.    local f_color = {1, 1, 1, 255}
  43.    local bg_color = {0, 0, 0, 0}
  44.    local diff = 0.8
  45.  
  46.    --center display
  47.    self.createButton({
  48.       label = tostring(value),
  49.       click_function = "add_subtract",
  50.       function_owner = self,
  51.       position = pos,
  52.       rotation = rot,
  53.       height = h,
  54.       width = w,
  55.       scale = {x = 0.25, y = 0.25, z = 0.25},
  56.       font_size = get_size(value),
  57.       color = f_size,
  58.       font_color = f_color,
  59.       color = bg_color
  60.    })
  61.    --Back Side
  62.    self.createButton({
  63.       label = tostring(backValue),
  64.       click_function = "add_subtract_back",
  65.       function_owner = self,
  66.       position = backPos,
  67.       rotation = backRot,
  68.       height = h,
  69.       width = w,
  70.       scale = {x = 0.25, y = 0.25, z = 0.25},
  71.       font_size = get_size(value),
  72.       color = f_size,
  73.       font_color = f_color,
  74.       color = bg_color
  75.    })
  76.  
  77. end
  78.  
  79. function get_size(value)
  80.    local char_count = string.len(tostring(value))
  81.    return f_size * 2 / (char_count + 1)
  82. end
  83.  
  84. function add_subtract(_obj, _color, alt_click)
  85.    mod = alt_click and - 1 or 1
  86.    new_value = math.min(math.max(value + mod, MIN_VALUE), MAX_VALUE)
  87.    if value ~= new_value then
  88.       value = new_value
  89.       updateDisplay()
  90.       updateSave()
  91.    end
  92. end
  93.  
  94. function add_subtract_back(_obj, _color, alt_click)
  95.    mod = alt_click and - 1 or 1
  96.    new_value = math.min(math.max(backValue + mod, MIN_VALUE), MAX_VALUE)
  97.    if backValue ~= new_value then
  98.       backValue = new_value
  99.       updateDisplay()
  100.       updateSave()
  101.    end
  102. end
  103.  
  104. function updateDisplay()
  105.    self.editButton({
  106.       index = 0,
  107.       label = tostring(value),
  108.       font_size = get_size(value)
  109.    })
  110.    self.editButton({
  111.       index = 1,
  112.       label = tostring(backValue),
  113.       font_size = get_size(backValue)
  114.    })
  115. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement