Advertisement
Kijan

Rulebook UI LUA

Jul 16th, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.07 KB | None | 0 0
  1. ruleBook = {
  2.   currentPage = 1,
  3.   previousCurrentPage = 1,
  4.   selectedBook = "Rulebook",
  5.   Rulebook = {title = "First Rulebook"},
  6.   Secondbook = {title = "Second Rulebook"},
  7. }
  8.  
  9. function onLoad(save_state)
  10.   for i = 1, 3 do
  11.     table.insert(ruleBook["Rulebook"], i)
  12.   end
  13.   for i = 1, 7 do
  14.     table.insert(ruleBook["Secondbook"], "Second" .. i)
  15.   end
  16. end
  17.  
  18. ---------------------Rules Book
  19. local zoomFactor = 1.2
  20.  
  21. function showRules(player, value, id)
  22.   if ruleBook.selectedBook ~= value then
  23.     ruleBook.selectedBook = value
  24.     ruleBook.currentPage = 1
  25.     Global.UI.setAttribute("CurrentPageCount", "text", 1)
  26.     Global.UI.setAttribute("MaxPageCount", "text", "/ " .. #ruleBook[ruleBook.selectedBook])
  27.     Global.UI.setAttribute("nextPage", "active", "true")
  28.     Global.UI.setAttribute("previousPage", "active", "false")
  29.   end
  30.   Global.UI.setAttribute("RulesImage", "image", ruleBook[ruleBook.selectedBook][ruleBook.currentPage])
  31.   Global.UI.setAttribute("RulesText", "text", ruleBook[ruleBook.selectedBook].title)
  32.   Global.UI.show("RulesPanel")
  33.   Global.UI.setAttribute("RulesPanel", "active", "true")
  34. end
  35.  
  36. function RulesClicked(player, value, id)
  37.   if id:find("closeButton") then
  38.     Global.UI.hide("RulesPanel")
  39.   elseif id:find("nextPage") then
  40.     if ruleBook.currentPage < #ruleBook[ruleBook.selectedBook] then
  41.       ruleBook.currentPage = ruleBook.currentPage + 1
  42.       if ruleBook.currentPage == #ruleBook[ruleBook.selectedBook] then --if on the last page
  43.         Global.UI.setAttribute("nextPage", "active", "false")
  44.       else
  45.         --turn on next button if it's off
  46.         if Global.UI.getAttribute("nextPage", "active") == "false" then
  47.           Global.UI.setAttribute("nextPage", "active", "true")
  48.         end
  49.       end
  50.       --Turn on previous button if it's off
  51.       if Global.UI.getAttribute("previousPage", "active") == "false" then
  52.         Global.UI.setAttribute("previousPage", "active", "true")
  53.       end
  54.       Global.UI.setAttribute("CurrentPageCount", "text", ruleBook.currentPage)
  55.       Global.UI.setAttribute("RulesImage", "image", ruleBook[ruleBook.selectedBook][ruleBook.currentPage])
  56.     end
  57.   elseif id:find("previousPage") then
  58.     if ruleBook.currentPage > 1 then
  59.       ruleBook.currentPage = ruleBook.currentPage - 1
  60.       if ruleBook.currentPage == 1 then --if on the first page
  61.         Global.UI.setAttribute("previousPage", "active", "false")
  62.       else
  63.         --turn on previous button if it's off
  64.         if Global.UI.getAttribute("previousPage", "active") == "false" then
  65.           Global.UI.setAttribute("previousPage", "active", "true")
  66.         end
  67.       end
  68.       --Turn on next button if it's off
  69.       if Global.UI.getAttribute("nextPage", "active") == "false" then
  70.         Global.UI.setAttribute("nextPage", "active", "true")
  71.       end
  72.       Global.UI.setAttribute("CurrentPageCount", "text", ruleBook.currentPage)
  73.       Global.UI.setAttribute("RulesImage", "image", ruleBook[ruleBook.selectedBook][ruleBook.currentPage])
  74.     end
  75.   elseif id:find("zoomInButton") then
  76.     local newWidth = tonumber(Global.UI.getAttribute("RulesImage", "width")) * zoomFactor
  77.     local newHeight = tonumber(Global.UI.getAttribute("RulesImage", "height")) * zoomFactor
  78.     Global.UI.setAttribute("RulesImage", "width", tostring(newWidth))
  79.     Global.UI.setAttribute("RulesImage", "height", tostring(newHeight))
  80.   elseif id:find("zoomOutButton") then
  81.     local newWidth = tonumber(Global.UI.getAttribute("RulesImage", "width")) / zoomFactor
  82.     local newHeight = tonumber(Global.UI.getAttribute("RulesImage", "height")) / zoomFactor
  83.     Global.UI.setAttribute("RulesImage", "width", tostring(newWidth))
  84.     Global.UI.setAttribute("RulesImage", "height", tostring(newHeight))
  85.   elseif id:find("widenButton") then
  86.     local newWidth = Global.UI.getAttribute("imageScroll", "width") * zoomFactor
  87.     Global.UI.setAttribute("imageScroll", "width", tostring(newWidth))
  88.     newWidth = Global.UI.getAttribute("RulesPanel", "width") * zoomFactor
  89.     Global.UI.setAttribute("RulesPanel", "width", tostring(newWidth))
  90.   elseif id:find("narrowButton") then
  91.     local newWidth = Global.UI.getAttribute("imageScroll", "width") / zoomFactor
  92.     Global.UI.setAttribute("imageScroll", "width", tostring(newWidth))
  93.     newWidth = Global.UI.getAttribute("RulesPanel", "width") / zoomFactor
  94.     Global.UI.setAttribute("RulesPanel", "width", tostring(newWidth))
  95.   end
  96. end
  97.  
  98. function onEndEditZoomPercentage(player, value, id)
  99.   if value == "" then
  100.     Global.UI.setAttribute("InputZoomMultiplier", "text", zoomFactor * 100)
  101.     return
  102.   end
  103.  
  104.   if value + 0 <= 100 then
  105.     broadcastToColor("Zoom value must be above 100", player.color, stringColorToRGB("Red"))
  106.   else
  107.     zoomFactor = value / 100
  108.   end
  109. end
  110.  
  111. function onEndEditCurrentPage(player, value, id)
  112.   ruleBook.previousCurrentPage = ruleBook.currentPage
  113.  
  114.   if value == "" then
  115.     Global.UI.setAttribute("CurrentPageCount", "text", ruleBook.previousCurrentPage)
  116.     return
  117.   end
  118.  
  119.   ruleBook.currentPage = value + 0
  120.   if ruleBook.currentPage <= #ruleBook[ruleBook.selectedBook] and ruleBook.currentPage > 0 then
  121.     if ruleBook.currentPage == #ruleBook[ruleBook.selectedBook] then --if on the last page
  122.       Global.UI.setAttribute("nextPage", "active", "false")
  123.       Global.UI.setAttribute("previousPage", "active", "true")
  124.     elseif ruleBook.currentPage == 1 then
  125.       Global.UI.setAttribute("previousPage", "active", "false")
  126.       Global.UI.setAttribute("nextPage", "active", "true")
  127.     else -- if in between first and last page
  128.       Global.UI.setAttribute("nextPage", "active", "true")
  129.       Global.UI.setAttribute("previousPage", "active", "true")
  130.     end
  131.     Global.UI.setAttribute("CurrentPageCount", "text", ruleBook.currentPage)
  132.     Global.UI.setAttribute("RulesImage", "image", ruleBook[ruleBook.selectedBook][ruleBook.currentPage])
  133.   else
  134.     Global.UI.setAttribute("CurrentPageCount", "text", ruleBook.previousCurrentPage)
  135.     broadcastToColor("You must type a number between 1 and "..#ruleBook[ruleBook.selectedBook], player.color, stringColorToRGB("Red"))
  136.   end
  137. end
  138. -----------------Rules Book
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement