feat(announcement)!: redesign announcement UI with responsive layout and timer styles
- Add responsive scaling based on screen resolution (1920x1080 baseline) - Implement two announcement styles: "loader" (fixed border) and "simple" (border timer) - Add text wrapping utility for responsive message rendering - Add custom border timer drawing (DrawBorderTimer) for animated border consumption - Create responsive fonts that rebuild on screen size changes - Add intro/outro animations with easing (EaseOutCubic) - Support configurable duration and style per announcement - Support announcement sound playback on display - Refactor Show() function to accept style and duration parameters - Update net message to transmit style and duration from server - Improve visual hierarchy with proper spacing, padding, and layout - Change from fixed pixel dimensions to scaled responsive values BREAKING CHANGE: The announcement API now requires style and duration parameters. The Show() function signature changed from Show(title, text) to Show(title, text, style, duration). Net message format changed to include style and duration fields.
This commit is contained in:
1 file changed
+205
-32
@@ -5,38 +5,155 @@ local logo = Material("nocode/announcement/logo.png")
|
|||||||
BladW = BladW or {}
|
BladW = BladW or {}
|
||||||
BladW.Announcement = BladW.Announcement or {}
|
BladW.Announcement = BladW.Announcement or {}
|
||||||
|
|
||||||
-- Largeur du fond de la barre "loader" (doit rester synchro avec le rndx.Draw plus bas)
|
local COL_ACCENT = Color(76, 119, 200)
|
||||||
local BAR_WIDTH = 200
|
local COL_WHITE = Color(255, 255, 255)
|
||||||
|
local COL_BG = Color(0, 0, 0, 200)
|
||||||
|
local COL_TRACK = Color(54, 54, 54, 200)
|
||||||
|
|
||||||
-- État de l'annonce en cours (persiste au hot-reload)
|
-- ============================================================
|
||||||
|
-- Fonts responsives (recréées quand la résolution change)
|
||||||
|
-- ============================================================
|
||||||
|
local function AnnScale()
|
||||||
|
return math.min(ScrW() / 1920, ScrH() / 1080)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Durées et easing de l'animation d'apparition / disparition
|
||||||
|
local INTRO_TIME = 0.45
|
||||||
|
local OUTRO_TIME = 0.35
|
||||||
|
local function EaseOutCubic(t) return 1 - (1 - t) ^ 3 end
|
||||||
|
|
||||||
|
local function BuildFonts()
|
||||||
|
local s = AnnScale()
|
||||||
|
surface.CreateFont("bladw_ann_title", {
|
||||||
|
font = "Poppins-SemiBold", size = math.max(1, math.Round(25 * s)), weight = 600, antialias = true,
|
||||||
|
})
|
||||||
|
surface.CreateFont("bladw_ann_text", {
|
||||||
|
font = "Poppins-SemiBold", size = math.max(1, math.Round(20 * s)), weight = 800, antialias = true,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
BuildFonts()
|
||||||
|
hook.Add("OnScreenSizeChanged", "BladeWin:AnnouncementFonts", BuildFonts)
|
||||||
|
|
||||||
|
-- ============================================================
|
||||||
|
-- Utilitaires
|
||||||
|
-- ============================================================
|
||||||
|
|
||||||
|
-- Découpe `text` en lignes ne dépassant pas `maxW` (px) pour la police `font`.
|
||||||
|
local function WrapText(text, font, maxW)
|
||||||
|
surface.SetFont(font)
|
||||||
|
local lines, line = {}, ""
|
||||||
|
|
||||||
|
for _, word in ipairs(string.Explode(" ", text or "")) do
|
||||||
|
local test = (line == "") and word or (line .. " " .. word)
|
||||||
|
if surface.GetTextSize(test) > maxW and line ~= "" then
|
||||||
|
lines[#lines + 1] = line
|
||||||
|
line = word
|
||||||
|
else
|
||||||
|
line = test
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if line ~= "" then lines[#lines + 1] = line end
|
||||||
|
|
||||||
|
return lines
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Contour du cadre en jauge de temps : portion restante en `colRemain`, consommée en `colDone`.
|
||||||
|
-- `remaining` : 1 = plein, 0 = écoulé.
|
||||||
|
local function DrawBorderTimer(x, y, w, h, r, remaining, colRemain, colDone, thickness)
|
||||||
|
thickness = math.max(1, thickness or 3)
|
||||||
|
|
||||||
|
local pts = {}
|
||||||
|
local function line(x1, y1, x2, y2)
|
||||||
|
local steps = math.max(1, math.floor(math.Distance(x1, y1, x2, y2) / 4))
|
||||||
|
for i = 0, steps do
|
||||||
|
pts[#pts + 1] = { Lerp(i / steps, x1, x2), Lerp(i / steps, y1, y2) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local function arc(cx, cy, a0, a1)
|
||||||
|
for i = 0, 6 do
|
||||||
|
local a = math.rad(Lerp(i / 6, a0, a1))
|
||||||
|
pts[#pts + 1] = { cx + math.cos(a) * r, cy + math.sin(a) * r }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Sens horaire depuis le début du bord haut
|
||||||
|
line(x + r, y, x + w - r, y)
|
||||||
|
arc (x + w - r, y + r, 270, 360)
|
||||||
|
line(x + w, y + r, x + w, y + h - r)
|
||||||
|
arc (x + w - r, y + h - r, 0, 90)
|
||||||
|
line(x + w - r, y + h, x + r, y + h)
|
||||||
|
arc (x + r, y + h - r, 90, 180)
|
||||||
|
line(x, y + h - r, x, y + r)
|
||||||
|
arc (x + r, y + r, 180, 270)
|
||||||
|
|
||||||
|
local total = 0
|
||||||
|
for i = 1, #pts - 1 do
|
||||||
|
total = total + math.Distance(pts[i][1], pts[i][2], pts[i + 1][1], pts[i + 1][2])
|
||||||
|
end
|
||||||
|
if total == 0 then return end
|
||||||
|
|
||||||
|
local consumedLen = (1 - remaining) * total
|
||||||
|
draw.NoTexture()
|
||||||
|
|
||||||
|
local acc = 0
|
||||||
|
for i = 1, #pts - 1 do
|
||||||
|
local x1, y1 = pts[i][1], pts[i][2]
|
||||||
|
local x2, y2 = pts[i + 1][1], pts[i + 1][2]
|
||||||
|
local segLen = math.Distance(x1, y1, x2, y2)
|
||||||
|
|
||||||
|
surface.SetDrawColor(acc < consumedLen and colDone or colRemain)
|
||||||
|
surface.DrawTexturedRectRotated((x1 + x2) * 0.5, (y1 + y2) * 0.5,
|
||||||
|
segLen + thickness, thickness, -math.deg(math.atan2(y2 - y1, x2 - x1)))
|
||||||
|
|
||||||
|
acc = acc + segLen
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- ============================================================
|
||||||
|
-- État de l'annonce (persiste au hot-reload)
|
||||||
|
-- ============================================================
|
||||||
BladW.Announcement.Active = BladW.Announcement.Active or false
|
BladW.Announcement.Active = BladW.Announcement.Active or false
|
||||||
BladW.Announcement.Title = BladW.Announcement.Title or "Annonce"
|
BladW.Announcement.Title = BladW.Announcement.Title or "Annonce"
|
||||||
BladW.Announcement.Text = BladW.Announcement.Text or ""
|
BladW.Announcement.Text = BladW.Announcement.Text or ""
|
||||||
|
BladW.Announcement.Style = BladW.Announcement.Style or "loader"
|
||||||
|
BladW.Announcement.Duration = BladW.Announcement.Duration or 10
|
||||||
BladW.Announcement.StartTime = BladW.Announcement.StartTime or 0
|
BladW.Announcement.StartTime = BladW.Announcement.StartTime or 0
|
||||||
|
|
||||||
-- Déclenche l'affichage d'une annonce pour la durée configurée
|
-- Déclenche l'affichage d'une annonce. style / duration vides -> valeurs de la config.
|
||||||
function BladW.Announcement.Show(title, text)
|
function BladW.Announcement.Show(title, text, style, duration)
|
||||||
BladW.Announcement.Title = title ~= "" and title or "Annonce"
|
local Conf = BladW.Announcement.Conf or {}
|
||||||
|
|
||||||
|
BladW.Announcement.Title = (title and title ~= "") and title or "Annonce"
|
||||||
BladW.Announcement.Text = text or ""
|
BladW.Announcement.Text = text or ""
|
||||||
|
BladW.Announcement.Style = (style and style ~= "") and style or (Conf.announcement_style or "loader")
|
||||||
|
BladW.Announcement.Duration = (duration and duration > 0) and duration or (Conf.announcement_duration or 10)
|
||||||
BladW.Announcement.StartTime = CurTime()
|
BladW.Announcement.StartTime = CurTime()
|
||||||
BladW.Announcement.Active = true
|
BladW.Announcement.Active = true
|
||||||
|
|
||||||
|
if Conf.announcement_sound and Conf.announcement_sound ~= "" then
|
||||||
|
surface.PlaySound(Conf.announcement_sound)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Serveur -> client : une annonce a été diffusée
|
-- Serveur -> client : une annonce a été diffusée
|
||||||
net.Receive("bladWShowAnnouncement", function()
|
net.Receive("bladWShowAnnouncement", function()
|
||||||
local title = net.ReadString()
|
local title = net.ReadString()
|
||||||
local text = net.ReadString()
|
local text = net.ReadString()
|
||||||
BladW.Announcement.Show(title, text)
|
local style = net.ReadString()
|
||||||
|
local duration = net.ReadUInt(16)
|
||||||
|
BladW.Announcement.Show(title, text, style, duration)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
-- ============================================================
|
||||||
|
-- Rendu (responsive + adaptatif au message)
|
||||||
|
-- ============================================================
|
||||||
hook.Add("HUDPaint", "BladeWin:Announcement", function()
|
hook.Add("HUDPaint", "BladeWin:Announcement", function()
|
||||||
if not BladW.Announcement.Active then return end
|
if not BladW.Announcement.Active then return end
|
||||||
|
|
||||||
local ply = LocalPlayer()
|
local ply = LocalPlayer()
|
||||||
if not IsValid(ply) then return end
|
if not IsValid(ply) then return end
|
||||||
|
|
||||||
local Conf = BladW.Announcement.Conf
|
local duration = BladW.Announcement.Duration or 10
|
||||||
local duration = Conf.announcement_duration or 10
|
|
||||||
local elapsed = CurTime() - BladW.Announcement.StartTime
|
local elapsed = CurTime() - BladW.Announcement.StartTime
|
||||||
|
|
||||||
-- Temps d'affichage écoulé -> on masque l'annonce
|
-- Temps d'affichage écoulé -> on masque l'annonce
|
||||||
@@ -45,30 +162,86 @@ hook.Add("HUDPaint", "BladeWin:Announcement", function()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local title = BladW.Announcement.Title
|
local style = BladW.Announcement.Style or "loader"
|
||||||
local text = BladW.Announcement.Text
|
local title = BladW.Announcement.Title
|
||||||
|
local text = BladW.Announcement.Text
|
||||||
|
local remaining = math.Clamp(1 - (elapsed / duration), 0, 1)
|
||||||
|
|
||||||
if Conf.announcement_style == "simple" then
|
-- Échelle responsive
|
||||||
rndx.Draw(14, 788, 38, 343, 133, nil, rndx.SHAPE_FIGMA + rndx.BLUR)
|
local s = AnnScale()
|
||||||
rndx.Draw(14, 788, 38, 343, 133, Color(0,0,0, 200), rndx.SHAPE_FIGMA)
|
local padX = 26 * s
|
||||||
rndx.DrawOutlined(14, 788, 38, 343, 133, Color(255, 255, 255), 3, rndx.SHAPE_FIGMA)
|
local padY = 18 * s
|
||||||
rndx.DrawMaterial(0, 910, 20, 100, 100, color_white, logo)
|
local gap = 8 * s
|
||||||
draw.SimpleText(title, "bladw_text", 960, 113, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
|
local logoS = 62 * s
|
||||||
draw.SimpleText(text, "bladw_text_Medium", 960, 140, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
|
local barW = 200 * s
|
||||||
|
local barH = 18 * s
|
||||||
|
local radius = 14 * s
|
||||||
|
|
||||||
|
-- Wrap du message (largeur interne responsive ; la vraie limite est le nb de caractères)
|
||||||
|
local wrapW = ScrW() * 0.8
|
||||||
|
local lines = WrapText(text, "bladw_ann_text", wrapW)
|
||||||
|
|
||||||
|
surface.SetFont("bladw_ann_title")
|
||||||
|
local titleW, titleH = surface.GetTextSize(title)
|
||||||
|
|
||||||
|
surface.SetFont("bladw_ann_text")
|
||||||
|
local _, lineH = surface.GetTextSize("Ay")
|
||||||
|
local msgW = 0
|
||||||
|
for _, l in ipairs(lines) do
|
||||||
|
msgW = math.max(msgW, (surface.GetTextSize(l)))
|
||||||
end
|
end
|
||||||
|
|
||||||
if Conf.announcement_style == "loader" then
|
-- Dimensions de la box (adaptées au contenu)
|
||||||
-- La barre représente le temps d'affichage restant : elle se vide sur la durée
|
local contentW = math.max(titleW, msgW, style == "loader" and barW or 0)
|
||||||
local remaining = math.Clamp(1 - (elapsed / duration), 0, 1)
|
local boxW = math.Clamp(contentW + padX * 2, 260 * s, ScrW() * 0.92)
|
||||||
local loaderBar = remaining * BAR_WIDTH
|
local msgH = #lines * lineH
|
||||||
|
local boxH = padY + logoS + gap + titleH + gap + msgH
|
||||||
|
+ (style == "loader" and (gap + barH) or 0) + padY
|
||||||
|
|
||||||
rndx.Draw(14, 788, 38, 343, 133, nil, rndx.SHAPE_FIGMA + rndx.BLUR)
|
-- Animation : fondu + glisse depuis le haut (et inverse à la disparition)
|
||||||
rndx.Draw(14, 788, 38, 343, 133, Color(0,0,0, 200), rndx.SHAPE_FIGMA)
|
local appear = math.Clamp(elapsed / INTRO_TIME, 0, 1)
|
||||||
rndx.DrawOutlined(14, 788, 38, 343, 133, Color(255, 255, 255), 3, rndx.SHAPE_FIGMA)
|
local disappear = math.Clamp((duration - elapsed) / OUTRO_TIME, 0, 1)
|
||||||
rndx.DrawMaterial(0, 788, 20, 100, 100, color_white, logo)
|
local anim = math.min(EaseOutCubic(appear), EaseOutCubic(disappear)) -- 0 -> 1 -> 0
|
||||||
draw.SimpleText(title, "bladw_text", 960, 113, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
|
local slide = (1 - anim) * 26 * s
|
||||||
draw.SimpleText(text, "bladw_text_Medium", 960, 140, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
|
|
||||||
rndx.Draw(14, 880, 60, BAR_WIDTH, 20, Color(54,54,54, 200), rndx.SHAPE_FIGMA)
|
local boxX = (ScrW() - boxW) * 0.5
|
||||||
rndx.Draw(14, 880, 60, loaderBar, 20, Color(76, 119, 200), rndx.SHAPE_FIGMA)
|
local boxY = ScrH() * 0.035 - slide
|
||||||
|
local cx = boxX + boxW * 0.5
|
||||||
|
|
||||||
|
surface.SetAlphaMultiplier(anim)
|
||||||
|
|
||||||
|
-- Fond
|
||||||
|
rndx.Draw(radius, boxX, boxY, boxW, boxH, nil, rndx.SHAPE_FIGMA + rndx.BLUR)
|
||||||
|
rndx.Draw(radius, boxX, boxY, boxW, boxH, COL_BG, rndx.SHAPE_FIGMA)
|
||||||
|
|
||||||
|
-- Le style "loader" garde un contour blanc fixe (le "simple" utilise sa jauge-bordure)
|
||||||
|
if style == "loader" then
|
||||||
|
rndx.DrawOutlined(radius, boxX, boxY, boxW, boxH, COL_WHITE, 3 * s, rndx.SHAPE_FIGMA)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Contenu empilé et centré
|
||||||
|
local y = boxY + padY
|
||||||
|
rndx.DrawMaterial(0, cx - logoS * 0.5, y, logoS, logoS, COL_WHITE, logo)
|
||||||
|
y = y + logoS + gap
|
||||||
|
|
||||||
|
draw.SimpleText(title, "bladw_ann_title", cx, y, COL_WHITE, TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
|
||||||
|
y = y + titleH + gap
|
||||||
|
|
||||||
|
for _, l in ipairs(lines) do
|
||||||
|
draw.SimpleText(l, "bladw_ann_text", cx, y, COL_WHITE, TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
|
||||||
|
y = y + lineH
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Jauge de temps
|
||||||
|
if style == "loader" then
|
||||||
|
y = y + gap
|
||||||
|
local bx = cx - barW * 0.5
|
||||||
|
rndx.Draw(barH * 0.5, bx, y, barW, barH, COL_TRACK, rndx.SHAPE_FIGMA)
|
||||||
|
rndx.Draw(barH * 0.5, bx, y, barW * remaining, barH, COL_ACCENT, rndx.SHAPE_FIGMA)
|
||||||
|
else
|
||||||
|
-- Le bord bleu se consume en blanc
|
||||||
|
DrawBorderTimer(boxX, boxY, boxW, boxH, radius, remaining, COL_ACCENT, COL_WHITE, 3 * s)
|
||||||
|
end
|
||||||
|
|
||||||
|
surface.SetAlphaMultiplier(1)
|
||||||
end)
|
end)
|
||||||
Reference in new issue
Block a user