Advertisement
riskyshot69

Timer UI

Jul 3rd, 2024
1,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | None | 0 0
  1. local TimerUI = {}
  2.  
  3. local function formatTime(seconds) -- 01:00 format
  4.     local minutes = math.floor(seconds / 60)
  5.     seconds = seconds - minutes * 60
  6.     return string.format("%02d:%02d",minutes,seconds)
  7. end
  8.  
  9.  
  10. function TimerUI:init()
  11.     TimerUI.data = {}
  12.     TimerUI.data.uiFlowblock = Citizen.InvokeNative(0xC0081B34E395CE48, -119209833)
  13.  
  14.     local temp = 0
  15.     while not UiflowblockIsLoaded(TimerUI.data.uiFlowblock) do
  16.         temp = temp + 1
  17.         if temp > 10000 then
  18.             print('Failed To Load Flowblock')
  19.             return
  20.         end
  21.         Citizen.Wait(1)
  22.     end
  23.  
  24.     if not Citizen.InvokeNative(0x10A93C057B6BD944, TimerUI.data.uiFlowblock) --[[ UIFLOWBLOCK_IS_LOADED ]] then
  25.         print("uiflowblock failed to load")
  26.         return
  27.     end
  28.  
  29.     TimerUI.data.container =  DatabindingAddDataContainerFromPath("", "centralInfoDatastore")
  30.  
  31.     -- Must be set empty else a default "Message" text will overlap the time
  32.     DatabindingAddDataString(TimerUI.data.container, "timerMessageString", "")
  33.  
  34.     TimerUI.data.timer = DatabindingAddDataString(TimerUI.data.container, "timerString", "")
  35.     TimerUI.data.show = DatabindingAddDataBool(TimerUI.data.container, "isVisible", false)
  36.  
  37.     UiflowblockEnter(TimerUI.data.uiFlowblock, `cTimer`)
  38.  
  39.     -- maybe add in while loop to make sure machine is created
  40.     if UiStateMachineExists(1546991729) == 0 then
  41.         TimerUI.data.stateMachine = UiStateMachineCreate(1546991729, TimerUI.data.uiFlowblock)
  42.     end
  43.  
  44.     -- Time Variable
  45.     TimerUI.data.time = 0
  46.  
  47.     return TimerUI.data.stateMachine
  48. end
  49.  
  50. -- To stop earlier
  51. function TimerUI:stopTimer()
  52.     self.data.time = 0
  53.     DatabindingWriteDataBool(self.data.show, false)
  54. end
  55.  
  56. ---@param time integer -- in seconds
  57. ---@param low? integer -- seconds at which timer color will turn to red
  58. function TimerUI:startTimer(time, low) -- in seconds
  59.     if self.data == nil or UiStateMachineExists(1546991729) == 0 then return end
  60.     DatabindingWriteDataBool(self.data.show, true)
  61.     self.data.time = time or 60
  62.  
  63.     while self.data.time >=1 do
  64.         DatabindingWriteDataString(self.data.timer ,formatTime(self.data.time))
  65.         self.data.time = self.data.time - 1
  66.         Wait(1000)
  67.         if low and self.data.time <= low then
  68.             DatabindingAddDataBool(self.data.container, "isTimerLow", true) -- Makes Timer Color as Red
  69.         end
  70.         if self.data.time <= 0 then
  71.             TimerUI:finishTimer()
  72.         end
  73.     end
  74. end
  75.  
  76. function TimerUI:finishTimer()
  77.     UiStateMachineDestroy(1546991729)
  78.     if DatabindingIsEntryValid(self.data.container) then
  79.         print('Removed Container')
  80.         DatabindingRemoveDataEntry(self.data.container)
  81.     end
  82.     if DatabindingIsEntryValid(self.data.timer) then
  83.         print('Removed Timer String')
  84.         DatabindingRemoveDataEntry(self.data.timer)
  85.     end
  86.     if DatabindingIsEntryValid(self.data.show) then
  87.         print('Removed Show Bool')
  88.         DatabindingRemoveDataEntry(self.data.show)
  89.     end
  90. end
  91.  
  92. -- Start Timer
  93. RegisterCommand("startTime", function()
  94.     TimerUI:init()
  95.     TimerUI:startTimer(20, 5)
  96. end)
  97.  
  98. -- Stop Timer in between
  99. RegisterCommand("stopTime", function()
  100.     TimerUI:stopTimer()
  101. end)
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement