View Single Post
03-19-24, 09:46 PM   #12
Hubb777
A Flamescale Wyrmkin
 
Hubb777's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2024
Posts: 113
Originally Posted by Xrystal View Post
This has some examples and info on using it for sound
https://warcraft.wiki.gg/wiki/API_PlaySound

As to a colour alert, it depends on what you mean.

You could do something as simple as colourizing the text.
This can be done using hex code colouring when you set the text

eg |cAARRGGBBTextToDisplay|r

or for clarity and flexibility where ColorOn can be set to a different colour before setting the text.
Lua Code:
  1. local ColorOn = "|cAARRGGBB"
  2. local ColorOff = "|r"
  3. f.Text:SetText(ColorOn .. "Text to Display" .. ColorOff)

Or you could add a texture to the frame holding the text and colourise that according to your needs
More Info : https://warcraft.wiki.gg/wiki/API_Te...etColorTexture

Set the texture up at frame Creation time
Lua Code:
  1. f.Color = f:CreateTexture(nil,"BACKGROUND") - Set to Background so that the text appears above it
  2. f.Color:SetAllPoints()

Then, when you need to change it's color use this line
Lua Code:
  1. f.Color:SetColorTexture(r, g, b, a) - Where r,g,b and a are values between 0 and 1.


Hello. I have the code, but I don't understand how to use it.


Lua Code:
  1. ------------------------------------------------------------------------------------------------------
  2. -- These might be converted to Saved Variables so each character can determine
  3. -- wether or not to play a sound, the alert times and colors and sound to play.
  4. -- If so then most of the code below will have to move into an event handler for
  5. -- the PLAYER_LOGIN or PLAYER_ENTERING_WORLD event.
  6. local useColor = true
  7. local useSound = true
  8. local alert1 = 600 -- Alarm 1 set to 5 minutes before event
  9. local alert1Color = "|cffffff00" -- Yellow
  10. local alert2 = 30 -- Alarm 2 set to 30 seconds before event
  11. local alert2Color = "|cffff0000" -- Red
  12. local soundKit = 32585 -- Alarm sound
  13. ------------------------------------------------------------------------------------------------------
  14.  
  15. local function printTime(timetotrun, inevent)
  16.     local hideSeconds = timetotrun >= 120
  17.     local msg = L.Waiting
  18.     local msgColor = "|cffffffff"
  19.     if inevent then
  20.         msg = L.Running
  21.     else
  22.         if useColor and timetotrun <= alert2 then
  23.             msgColor = alert2Color
  24.         elseif timetotrun <= alert1 then
  25.             if useSound and not ZAMTimer_4_Events.Alerted then
  26.                 ZAMTimer_4_Events.Alerted = true
  27.                 PlaySound(soundKit, "Master")
  28.             end
  29.             if useColor then
  30.                 msgColor = alert1Color
  31.             end
  32.         end
  33.     end
  34.     f.text:SetText(format(msg, msgColor, SecondsToTime(timetotrun, hideSeconds)))
  35. end
  36.  
  37. ZAMTimer_4_Events.Alerted = false



The result should be something like this.


Lua Code:
  1. local addonName, addon = ...
  2. local L, MyRegion
  3. local RegionTimes = {
  4.     [1] = {
  5.         startTime = 1679572800,
  6.         totalDuration = 14400, -- complete session time 4 hours repeating
  7.         sub_sessionDuration = 3600, -- 1 hour
  8.         waitTime = 3300, -- 55 minutes
  9.         eventtime = 300, -- 5 minutes implied but..
  10.         [1] = { -- sub-sessions
  11.             name = "A",
  12.         },
  13.         [2] = {
  14.             name = "B",
  15.         },
  16.         [3] = {
  17.             name = "C",
  18.         },
  19.         [4] = {
  20.             name = "D",
  21.         },
  22.     },
  23. }
  24.  
  25. --[[ TEST TIMES ONLY: over 4 minutes instead of 4 hours ]]--
  26.  
  27. --[[
  28. RegionTimes[1].totalDuration = 240 -- 4 minutes
  29. RegionTimes[1].sub_sessionDuration = 60 -- 1 minute
  30. RegionTimes[1].waitTime = 55 -- seconds
  31. RegionTimes[1].eventtime = 5 -- seconds
  32. ]]--
  33.  
  34. --[[ END TEST TIMES ]]--
  35.  
  36. local Localizations = {
  37.     enUS = {
  38.         Waiting = "%s before event %s starts",
  39.         Running = "Event: |cFF35BE21%s|r\n%s remaining",
  40.         A = "Name of event A",
  41.         B = "Name of event B",
  42.         C = "Name of event B",
  43.         D = "Name of event B",
  44.     },
  45.      deDE = {
  46.         Waiting = "%s verbleibende Zeit bis zur Veranstaltung %s",
  47.         Running = "Ereignis: |cFF35BE21%s|r\n%s Kommen",
  48.         A = "Freiflächen",
  49.         B = "Grüne",
  50.         C = "Rote",
  51.         D = "Weiß",
  52.     },
  53. }
  54.  
  55. ------------------------------------------------------------------------------------------------------
  56. -- These might be converted to Saved Variables so each character can determine
  57. -- wether or not to play a sound, the alert times and colors and sound to play.
  58. -- If so then most of the code below will have to move into an event handler for
  59. -- the PLAYER_LOGIN or PLAYER_ENTERING_WORLD event.
  60. local useColor = true
  61. local useSound = true
  62. local alert1 = 600 -- Alarm 1 set to 5 minutes before event
  63. local alert1Color = "|cffffff00" -- Yellow
  64. local alert2 = 30 -- Alarm 2 set to 30 seconds before event
  65. local alert2Color = "|cffff0000" -- Red
  66. local soundKit = 32585 -- Alarm sound
  67. ------------------------------------------------------------------------------------------------------
  68.  
  69. local function printTime(timetotrun, inevent)
  70.     local hideSeconds = timetotrun >= 120
  71.     local msg = L.Waiting
  72.     local msgColor = "|cffffffff"
  73.     if inevent then
  74.         msg = L.Running
  75.     else
  76.         if useColor and timetotrun <= alert2 then
  77.             msgColor = alert2Color
  78.         elseif timetotrun <= alert1 then
  79.             if useSound and not ZAMTimer_4_Events.Alerted then
  80.                 ZAMTimer_4_Events.Alerted = true
  81.                 PlaySound(soundKit, "Master")
  82.             end
  83.             if useColor then
  84.                 msgColor = alert1Color
  85.             end
  86.         end
  87.     end
  88.     f.text:SetText(format(msg, msgColor, SecondsToTime(timetotrun, hideSeconds)))
  89. end
  90.  
  91. ZAMTimer_4_Events.Alerted = false
  92.  
  93. local function OnUpdate(self, elapsed)
  94.     self.Elapsed = self.Elapsed - elapsed
  95.     if self.Elapsed > 0 then -- Only check once per second
  96.         return
  97.     end
  98.     self.Elapsed = 1 -- reset the timeout (we've counted down 1 second)
  99.     local serverTime = GetServerTime()
  100.     local remainingTime = (MyRegion.startTime - serverTime) % MyRegion.totalDuration
  101.     local base = math.ceil(remainingTime / MyRegion.sub_sessionDuration)
  102.     local hourRemaining = MyRegion.sub_sessionDuration - ((base * MyRegion.sub_sessionDuration) - remainingTime)
  103.     local id = 4 - (base - 1)
  104.     if id == 5 then
  105.         id = 1
  106.     end
  107.     local msg
  108.     if hourRemaining > MyRegion.waitTime then
  109.         msg = format(L.Running, L[MyRegion[id].name], SecondsToTime(hourRemaining - MyRegion.waitTime, false))
  110.     else
  111.         id = id == 4 and 1 or id + 1
  112.         msg = format(L.Waiting, SecondsToTime(hourRemaining, false), L[MyRegion[id].name])
  113.     end
  114.     self.Text:SetText(msg)
  115.     self:SetSize(self.Text:GetWidth() + 10, self.Text:GetHeight() + 10)
  116. end
  117.  
  118. local Backdrop = {
  119.     bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
  120. }
  121.  
  122. local f = CreateFrame("Button", "ZAMTimer_4_Events", UIParent, "BackdropTemplate")
  123. f:SetWidth(255)                                          
  124. f:SetHeight(30)
  125. f:SetPoint("CENTER")
  126. f:SetBackdrop(Backdrop)
  127. f:SetClampedToScreen(true)
  128. f:EnableMouse(true)
  129. f:SetMovable(true)
  130. f:SetUserPlaced(true)
  131. f:RegisterForDrag("LeftButton")
  132. f:RegisterForClicks("AnyUp")
  133. f.Text = f:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  134. f.Text:SetPoint("CENTER")
  135. f.Elapsed = 0 -- Set starting timeout (0 second)
  136. f:SetScript("OnDragStart",function(self)
  137.     self:StartMoving()
  138. end)
  139. f:SetScript("OnDragStop",function(self)  
  140.     self:StopMovingOrSizing()
  141. end)
  142.  
  143. f:RegisterEvent("PLAYER_LOGIN")
  144. f:SetScript("OnEvent", function(self)
  145.     local locale = GetLocale()
  146.     L = Localizations[locale] or Localizations.enUS -- Default to enUS if locale doesn't exist in the table
  147.     MyRegion = RegionTimes[GetCurrentRegion()] or RegionTimes[1] -- Default to region 1 (US) if it doesn't exist in the table
  148.     f:SetScript("OnUpdate", OnUpdate)
  149. end)
  150.  
  151. SLASH_ZAM4TIMER1 = "/z4" -- toggle hiding/showing the ZAMTimer_4_Events frame using just /z4
  152. SlashCmdList.ZAM4TIMER = function(msg)
  153.     ZAMTimer_4_Events.Elapsed = 0 -- set the "clock" to re-calculate when shown.
  154.     ZAMTimer_4_Events:SetShown(not ZAMTimer_4_Events:IsShown()) -- hide/show the frame
  155. end

Last edited by Hubb777 : 03-19-24 at 09:51 PM.
  Reply With Quote